Back

Futures HELP: Accessing UnderlyingSymbol for contracts held in your Portfolio?

Hi, 

I am building on the Bootcamp Futures strategy by tryng to create an algo that simply holds and rolls into the contract with the highest OpenInterst, for four Futures assets: Gold, SP500, 30Y TBonds & the USD Index.

The problem is the code buys the newest OpenInterest contract without selling the old one.

I need to check if the underlying asset of a contract is held in my portfolio , and if it is, then liquidate it. As it stands, the code is currently buying multiple contracts of the same underlying asset but with different expiries, as they return different Futures.Symbols. 

 

I can check if the exact Contract is in there with; self.Portfolio[self.liquidContract.Symbol].Invested... But this doesn't tell me if I am holding a contract with a different expiry for the same underlyingSymbol.

Q1: How do I check if the "UnderlyingSymbol" is currently in my Portfolio - without specifying a contract expiry? For example, I want to check if "GC"(Gold) is in my portfolio. If it is then I would like to liquidate that contract and roll it into a future contract.
Using the "self.UnderlyingSymbol" seems to return "None", instead of the underlying symbol "GC"
Q2: The" USD Index Futures" don't seem to exist anymore/have any data?

Kind Regards,
Mark 

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.


Hi Mark,

As most contracts don't have an underlying symbol, we can roll between contacts by simply keeping a reference to the last contract we purchased.

if not self.Portfolio[self.liquidContract.Symbol].Invested:

# Sell the old contract if there was one and it hasn't expired
if self.prevLiquidContract is not None and \
self.liquidContract != self.prevLiquidContract and \
self.prevLiquidContract.Expiry > self.Time:

self.SetHoldings(self.prevLiquidContract.Symbol, 0) # Liquidate old contract

# Buy the most liquid contract
self.SetHoldings(self.liquidContract.Symbol, 0.08)
self.prevLiquidContract = self.liquidContract

With this, each time a new most-liquid contract is found, we:
1) Liquidate the old contract (if there was one and it hasn't expired yet)
2) Buy the new contract
3) Set the previous liquid contract to the new one

I've attached a backtest which demonstrates this procedure using the SP500 EMini index futures. This backtest also demonstrates that we do have data for this US index future.

Best,
Derek Melchin

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.


First of all thank you very much for the help Derek! 

However, in testing your response I have created more questions for myself!? ;)

1) Is there any reason you use setHoldings(0) instead of self.Liquidate?

2) It seems your assignemnt of self.usd has a bug in it as you have assigned it to the emini S&P500, instead of USD Index. I am still not sure the FX USD Index is available?

3) When I ran your code it didnt seem to be selling anything - only buying SP emini contracts. Where do you see the contracts that it is rolling out of, And how do you know this is happening? 

4) Further, when I added three assets to the universe it crashed. I think the problem self.prevLiquidContract was tied to whatever asset we happened to have bought last... and in a multiple asset universe we should have three assets in self.prevLiquidContract (Gold, Bonds and S&P), not one.

5) So, to solve this problem I created a dict self.currentHoldings of contracts keyed by the first two letters of each contract, which seems to do the job intended....

Backtests attached.

0


6) UNTIL, I added 2-lines (36-37) to simply return if we were already invested in the most liquid contract. This shouldn't have changed the results at all but it does? With that line added it buys S&P & Gold, but doesnt buy Bonds on the first day of trading. Can you tell me why?

0


Is there an easier way to roll into the front month contract? I am struggling to make the code above work 100% of the time to select just a single contract with the highest open interest. Quite often it will buy an asset I already have in the portfolio.

0

Soory I should have added, with this error multiple times (symbol changes).

Backtest Handled Error: The security with symbol 'ZN XCZJLC9NO9WL' is marked as non-tradable.

0

Hi Mark,

(1) There is no reason. Either one will work.

(2) Sorry, The US Dollar Index Futures is provided by ICE, which we do not have data for. We're updating the documentation now to hide this product-code.

(3) The order logs of the attached backtest show the liquidation trades with the tag "Liquidate from delisting..."

(4) See #5.

(5) This is an appropriate solution in this situation.

(6) We just need to change `return` to `continue`

The "non-tradable" error is because the algorithm is trying to liquidate an expired contract. We just need to ensure we are deleting expired contracts from self.currentHoldings as LEAN will automatically sell them when they are delisted. Refer to the attached backtest.

As of right now, the code works with the changes listed above. An alternative to rolling based on open interest would be would to roll based on which contract has the most volume.

Best,
Derek Melchin

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.


Thanks Derek, hmmm Thats given me a few more questions if I may...

3) The first version of 8049-Futures Rolling (with one asset) had no sales listed or order tags "Liquidate from delisting..." in the orders. Only Buy orders. The new version is liquidating AFTER we have already purchased a new contract... sometimes days later, which means we are holding DOUBLE the desired amount of that asset during that interim period. It should liquidate, before re-purchasing.

0

Hi Mark,

The issues are a result of an unknown bug in LEAN. After discussing with the team, a GitHub issue has been created. You can track its progress here.

Best,
Derek Melchin

1

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.


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