2

我使用 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 有关。20121031 与 quantmod 通常期望的正常 (%Y-%m-%d) ex.2012-10-31 格式相反。文档说这可以通过 setDefaults(getSymbols.MySQL,...) 更改,但没有说明如何。有什么想法吗?

4

1 回答 1

0

抱歉,我不允许发表评论,但是您的返回列名称包含“ticker”,并且 db.fields 没有指定这一点,导致两者之间没有对齐,这不是问题吗?

从我所包含的示例中可以看出,实际表的名称应该与股票代码相同。所以不应该有一个“股票代码”列。

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)
} 

于 2016-12-09T15:56:00.217 回答