问题
EDIT: Hi everyone, I want to download all Gold individual contracts from Quandl. The contracts are from 1975 to 2016 with the month codes: G(February) J(April) M(June) Q(August) V(October) Z(December). At the bottom of the link appears all of them CME Gold Futures. So if you click on one individual contract, let's say the oldest one, is February 1975 and the syntax to download it in R is:
require(Quandl)
Quandl("CME/GCG1975")
General Syntax:
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)
Parameters:
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.
Now, I do what I want for the last 2 contracts, February and April 1975:
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
Next step, is to do the same but with April 1975 as pre_contract and June 1975 as post_contract, until December 2016 that is the actual contract, and finally contract_union
have all contracts in ascending order.
For this project I don't know which instruments/commands to use. I think that in the syntax of Quandl
CME/GC
could be fixed and put monthcode
and year
as variable with limits, limits of monthcode are the letters of months G J M Q V Z
and limits of years are 1975:2016
.
require(xlsx)
write.xlsx(contract_union,"/gold-data.xlsx")
I don't need a final-solution, if you can help me/guidance me in any part is fine. I'm open to any suggestions/questions. And I'm open to learn anything useful for this project.
Thanks, RTA
来源:https://stackoverflow.com/questions/40942717/r-quandl-loop