R: Continuous futures working backward

2019-08-04 05:56发布

问题:

I want to create a continuous futures series, that is to eliminate a gap between two series.

First thing I want is to download all individual contracts from the beginning to the now, the syntax is always the same:

Quandl("CME/INSTRUMENT_MONTHCODE_YEAR")

1.INSTRUMENT is GC (gold) in this case

2.MONTHCODE is G J M Q V Z

3.YEAR is from 1975 to 2017 (the actual contract)

With the data, I start working from the last contract, in this case "CME/GCG1975" and with the next contract "CME/GCJ1975". Then I see the last 6 values (are the more recent because date is descending) of the first contract GCG1975

require(Quandl)
GCG1975 = Quandl("CME/GCG1975",order="asc", type="raw") 
tail(GCG1975,6)

order can be asc desc (ascending or descending), type can be : raw (data frame) ts xts zoo

And it outputs:

Image: quandl-1.png = Last values of GCG1975

Then I just want the 6th row starting from the final, and I want to eliminate the columns "Last" "Change" (this could be before starting processing each individual contract):

Image: quandl-2.png = Last 6th value GCG1975

Then I want to find the row with date 1975-02-18 (last 6th value GCG1975) in the next contract (GCJ1975):

Image: quandl-3.png = 1975-02-18 on GCJ1975

Then I compute the difference between the "Settle" of the G contract and the "Settle" of the J contract.

Difference_contract = 183.6 - 185.4
Difference_contract = -1.8

So that means that the next or J contract is 1.5 points up respect the before contract so we have to sum -1.8 to all the following numbers of the J contract (Open, High, Low, Settle), including the row 1975-02-18. This:

Image: quandl-4.png = Differences between contracts

And then we have a continuous series like this:

Image: quandl-5.png = Continuous series

All this differences and sums to make a continuous series is done since the last contract until the actual contract.

I think I can't post this because I don't have 10 points of reputation and I can just post 2 image-links. Any guidance would help me, any question you have ask me.

Thanks and hope everything is well.

RTA

Edit: I have uploaded the photos and its links on post to my dropbox so you must look into it because Stackoverflow don't allow to post more than 2 links without 10 points of reputation.

Dropbox file