0

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

  • Questions that occur to me: Do you realize that assigning to `x` twice will overwrite the first value? Is there some difficulty getting `Quandl` tp process a value constructed from `paste0`? What is the structure of the object that `Quandl` delivers? What does it mean to say "compute the third contract with the second"? What are (xy1,xy2)? – IRTFM Dec 03 '16 at 03:31
  • Thanks @42- for your questions, I have edited the question and answered yours, I don't know what to do with `paste0` even I don't know if it's necessary. Hope you understand now my question – Ramón Tarí Agulló Dec 03 '16 at 10:41
  • I thought you wanted to prepend ""CME/GC" to all combinations of GJMQV crossed with your years. Uising expand.grid and then `apply` along rows using `paste0` would seem the most economical method to generate your requests. I don't generally find code without a natural language explanation very useful in setting up a problem. I also would have guessed that you wanted to assign the result of `Quandl("CME/GCG1975")` to an R name. Then if you wanted to loop through the names you might be in a position to grab two at a time in sequence for whatever your code does. – IRTFM Dec 03 '16 at 22:27
  • Yes,now the code is clearer.Thanks for your sincerity about -natural language explanation-. I understand about you say me, to do `qt = expand.grid.df(data.frame(Year=1975:2016), data.frame(Month="G","J","M","Q","V"))`Now I've got a `data.frame` with "integer" in column 1 and the next 5 columns as "factor".Now I must do what your last sentence says -be in a position to grab two at a time in sequence- but I don't understand it. I have divided this question in other post [http://stackoverflow.com/questions/40953151/loop-r-with-quandl-as-optional] so there is more information. Thanks for your time – Ramón Tarí Agulló Dec 03 '16 at 23:05

0 Answers0