编辑:大家好,我想从 Quandl 下载所有黄金个人合约。合同从 1975 年到 2016 年,月份代码:G(二月)J(四月)M(六月)Q(八月)V(十月)Z(十二月)。链接底部显示所有CME 黄金期货。因此,如果您单击一个单独的合同,假设最旧的合同是 1975 年 2 月,那么在 R 中下载它的语法是:
require(Quandl)
Quandl("CME/GCG1975")
一般语法:
Quandl(":database_code/:dataset_code", type = ":return_format")
"database_code" : CME (Chicago Mercantile Exchange)
"dataset_code" : INSTRUMENT:MONTH:YEAR (example: GC:G:1975 without colon)
参数:
The parameter order can take the following values:
"asc" : return data in ascending order
"desc" : return data in descending order
The parameter type can take the following values:
"raw" : returns a data frame
"zoo" : returns a zoo object
"ts" : returns a time-series object
"xts" : returns an extensible time-series object
"timeSeries" : returns a financial time-series object
When type is omitted, a data frame is returned.
现在,我为 1975 年 2 月和 4 月的最后 2 份合同做我想做的事情:
require(Quandl)
pre_contract <- Quandl("CME/GCG1975",order = "asc", type="raw") #download previous or February 1975 and post contract starting from February 1975, then April 1975, June 1975...
post_contract <- Quandl("CME/GCJ1975",order = "asc", type="raw") #download post contract or April 1975
pre_contract$Last = pre_contract$Change = NULL #delete column Last and Change
post_contract$Last = post_contract$Change = NULL #delete column Last and Change
colnames(pre_contract) # output: [1] "Date" [2] "Open" [3] "High" [4] "Low" [5] "Settle" [6] "Volume" [7] "Prev. Day Open Interest"
row_pre = nrow(pre_contract)-5 # this is the 6th last row
pre_contract[row_pre,"Date"] #output: "1975-02-18" this is the 6th last date, col 33 from the total columns 38, not necessary
row_post = which(post_contract$Date == pre_contract[nrow(pre_contract)-5,"Date"]) #output:33 says which row in post_contract is "Date" 1975-02-18
post_contract[row_post,"Date"] #output:"1975-02-18" my visual confirmation, not necessary
row_pre <- row_pre - 1 # I don't want to add 2 times the same date (1975-02-18), and I want the row of the April 1975
contract_union <- rbind(pre_contract[1:row_pre,],post_contract[row_post:nrow(post_contract),]) #I paste both contracts in same data.frame, the division-date is 1975-02-18 where I use the row of the April 1975
下一步是做同样的事情,但将 1975 年 4 月作为 pre_contract,将 1975 年 6 月作为 post_contract,直到 2016 年 12 月才是实际合同,最后contract_union
所有合同都按升序排列。对于这个项目,我不知道要使用哪些工具/命令。我认为在Quandl
CME/GC
可以固定和放置的语法中monthcode
,year
作为带有限制的变量,月份代码的限制是月份的字母,G J M Q V Z
而年份的限制是1975:2016
。
require(xlsx)
write.xlsx(contract_union,"/gold-data.xlsx")
我不需要最终解决方案,如果您能在任何方面帮助我/指导我就可以了。我愿意接受任何建议/问题。我愿意学习任何对这个项目有用的东西。
谢谢, RTA