Back

Futures Backtests: getting data and specifying contracts

I'm looking to run backtests (and actually do research in Jupyter) using futures contracts. I'm starting from the Basic Futures template (here)

I can't seem to find the details of the futures/futures chain data structure. Clearly there is data in the chain (the code uses x.expiry). I am unable to find what else is there; I've tried via here, but I believe due to my shortcomings, am unable to translate into Python code. 

With that said, I'm looking to work on using the current active future for a given contract. Right now, for instance, the active gold future is GC Dec17 (GCZ7). I'd like to be able determine the active futures via comparing volumes for the first N contracts or directly requesting GCZ7. 

Please help by 1) providing a way showing how to get the ticker and other information for a given futurs contract, 2) show some example code to grab a specific future, eg GCZ7, and 3) how to get the active contract based on prior day and current day volumes (and possibly OI). 

Update Backtest








 

Hi Ari Pine,  all the properities of futures contract can be found here

https://www.quantconnect.com/lean/docs#topic14.html

Specific contract can be selected according to Symbol

contracts = filter(lambda x: x.Symbol.Value == 'GCZ17', chain.Value)
# access properities of contracts
for chain in slice.FutureChains:
for i in chain.Value:
askprice = i.AskPrice
openinterest = i.OpenInterest
expiry = i.Expiry

Volume is not a property here, AskSize, BidSize and OpenInterest are available to describe the liquidity of contracts.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Is it possible to set it to filter only active contracts? I see this: setfilter-func but I don't see an example of how to use it.

I was thinking that I could provide a list of active contract months. For instance, ['G', 'J', 'M', 'Q', 'Z'] and the SetFilter method could only get those. Those are the active months for gold. 

0

Hi Ari, Setfilter() method can only filter the contracts by using the specified expiration range values.  More precise contract filter can be realized in OnData() by specify the property of the contract.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Got. So I would filter out using some lambda function that would search for those contract codes (G, Z, etc). 

It does look to me like SetFilter has a SetFilter(func) method. I'm not an expert in API documentation (obviously) so please explain what that means and provide an example.

0

SetFilter(func) means that you can define a funciton to filter the contracts. It is useful when you want to include the non-weekly option contracts. Here is an example for the options

https://github.com/QuantConnect/Lean/blob/master/Algorithm.Python/OptionExerciseAssignRegressionAlgorithm.py

For options the function can filter contacts by the range of expiration date, the strike price and include the weekly contracts. But for futures, SetFilter can only filter contracts by the expiration date. 

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Feature suggestion: allow futures to be filtered by contract code. Many contracts are only of interest for certain expiries. For instance, gold actives are (as above) G, J, M, Q, Z. However, there are still non-active months disseminated, even if they are rarely traded. Other contracts, like crude oil trade active for every month.

In the case of gold or any contract that has both active and serial months, this filtering will help both with data and also to avoid improperly using an illiquid contract that might just happen to fall in. 

I realize that I can do this via filtering in OnData. However, this may bring it to the attention of traders new to futures who may not realize some of these complexities to futures. 

0

We have merged a feature that enables us to use complex filtering. Here is how we can use it:

# lambda expression:
futureES = self.AddFuture(Futures.Indices.SP500EMini)
futureES.SetFilter(lambda x: x.FrontMonth())

# implicit method
futureGC = self.AddFuture(Futures.Metals.Gold)
futureGC.SetFilter(self.gold_filter_function)

def gold_filter_function(self, universe):
return universe.BackMonth()

x in the lambda expression and universe in the gold_filter_function are FutureFilterUniverse objects.
I think you can use the ExpirationCycle method:

futureGC.SetFilter(lambda x: x.ExpirationCycle([1, 3]))
# where 1 represents January

to get only the active months for gold.

0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Thank you. That looks very interesting. I'll try it out.

0

Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed