quantmod - getSymbols.MySQL格式

时间:2012-11-04 06:28:36

标签: r quantmod

我使用quantmod getSymbols查询MySQL数据库,其中包含以下内容:

pot<-getSymbols(Symbols="pot",src='MySQL',user='root',password='root',dbname='data',
   adjust=TRUE,db.fields=c("date","open","high","low","close","volume","time"),
   auto.assign=FALSE,
   field.names=c("ticker","date","time","open","high","low","close","volume"),
   format="%Y%m%d")

返回: 弄乱日期转换。

      pot.Open  pot.High  pot.Low   pot.Close pot.Volume pot.Adjusted
     

7058-02-20“40.1700”“40.1900”“40.1000”“40.1500”“104900”“09:30:59”
  7058-02-20“40.0900”“40.1725”“40.0900”“40.1725”“13200”“09:31:53”
  7058-02-20“40.1900”“40.3394”“40.1800”“40.2900”“16500”“09:32:57”
  7058-02-20“40.3000”“40.3700”“40.2400”“40.2400”“36500”“09:33:58”

  7058-02-20“40.2600”“40.3000”“40.2000”“40.2500”“6700”“09:34:59”
  7058-02-20“40.2600”“40.3100”“40.2000”“40.2000”“13900”“09:35:59”

我希望它看起来像这样:

               pot.Open  pot.High  pot.Low   pot.Close pot.Volume pot.Adjusted
     

2012-10-31 09:30:59“40.1700”“40.1900”“40.1000”“40.1500”“104900”“09:30:59”
  2012-10-31 09:31:53“40.0900”“40.1725”“40.0900”“40.1725”“13200”“09:31:53”
  2012-10-31 09:32:57“40.1900”“40.3394”“40.1800”“40.2900”“16500”“09:32:57”
  2012-10-31 09:33:58“40.3000”“40.3700”“40.2400”“40.2400”“36500”“09:33:58”
  2012-10-31 09:34:59“40.2600”“40.3000”“40.2000”“40.2500”“6700”“09:34:59”
  2012-10-31 09:35:59“40.2600”“40.3100”“40.2000”“40.2000”“13900”“09:35:59”

我认为它与表格中的日期格式有关(%Y%m%d)ex。 2012-1031而不是正常(%Y-%m-%d)ex.2012-10-31格式quantmod通常预期。文档说这可以通过setDefaults(getSymbols.MySQL,...)来改变,但没有说明如何。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

很抱歉,我不允许发表评论,但问题不在于您的返回列名称包含&#34; ticker&#34;,而db.fields并未指定此项,造成两者之间的不一致?

从示例中我可以看到,实际的表格名称应该与自动收报机相同。所以不应该是一个&#34;自动收报机&#34;列。

&#13;
&#13;
library(quantmod)
library(RMySQL)

MySymbols = c("MRK","GOOG","AAPL")

getSymbols(MySymbols, src="yahoo")
dbc = dbConnect(MySQL(), user="youruser", pass="yourpass", host="yourhost", dbname="yourdb")
for(I in 1:length(MySymbols) {
        dbWriteTable(dbc, name=MySymbols[i],value=as.data.frame(get(MySymbols[i])), overwrite=TRUE)
        res = dbSendQuery(dbc, paste("ALTER TABLE `yourtable`.`",MySymbols[i],
    "` CHANGE COLUMN `row_names` `Date` DATE NULL DEFAULT NULL,
                CHANGE COLUMN `", MySymbols [i],"_Open` `o` DOUBLE NULL DEFAULT NULL,
                CHANGE COLUMN `", MySymbols [i],"_High` `h` DOUBLE NULL DEFAULT NULL,
                CHANGE COLUMN `", MySymbols [i], "_Low` `l` DOUBLE NULL DEFAULT NULL,
                CHANGE COLUMN `", MySymbols [i], "_Close` `c` DOUBLE NULL DEFAULT NULL,
                CHANGE COLUMN `", MySymbols [i], "_Volume` `v` DOUBLE NULL DEFAULT NULL,
                CHANGE COLUMN `",MySymbols[i],"_Adjusted` `a` DOUBLE NULL DEFAULT;", sep=""))
        dbClearResult(res)
} 
&#13;
&#13;
&#13;