Algorithm with Futures and Custom Indicator

Hi, I am fairly new to QuantConnect. 

I have been trying to read the documentation and look at the examples and discussions - a lot of helpful material here, but I still have some questions (and an error message) I hope the community can help me with. Any help will be greatly appreciated :-)

Basic premise of my questions: (see attached code)

  • I want to trade Futures (ES pr now)
  • I want to create and use a custom indicator
    • The indicator is a daily indicator that uses intraday data to calculate the value
    • The exact implementation can be many things; for instance number of 5 minute bars up-close for the day, market profile value area etc etc.
    • For "yesterdays" indicator value, all the intraday data for yesterday is used. 
    • For "todays" indicator value, intraday data up to current trade time is used.
  • The way the current future-contract is retrieved (in OnData) is a little verbose - is it possible to do this in the Initialize (maybe by specifying a continuous contract)?
  • Is there a better way of filling an indicator with a whole days worth of intraday data (I was not able to use the History-method with a consolidator)?
    • Right now this is done very manually
  • ERROR MESSAGE: If I uncomment line 46 ( UpdateIndicators(halfHourBar) ) - I get error when running backtest... does anybody know what causes this (I am using a Future, but it is asking me to add a Security).  The only thing done there is to generate the indicators that I on purpose do not want to be auto-filled with data (subscription).
    • Runtime Error: Please register to receive data for symbol ' ' using the AddSecurity() function.
    • System.Exception: Please register to receive data for symbol ' ' using the AddSecurity() function.
      at QuantConnect.Algorithm.QCAlgorithm.GetSubscription (QuantConnect.Symbol symbol, System.Nullable`1[T] tickType) [0x00098] in :0 
      at QuantConnect.Algorithm.QCAlgorithm.GetSubscription (QuantConnect.Symbol symbol) [0x00001] in :0 
      at QuantConnect.Algorithm.QCAlgorithm.CreateIndicatorName (QuantConnect.Symbol symbol, System.String type, System.Nullable`1[T] resolution) [0x00010] in :0 
      at QuantConnect.Algorithm.CSharp.FuturesWithCustomIndicator.CustomIndicator (System.String symbol, System.Int32 length, System.Int32 dayOfYear, System.Nullable`1[T] resolution) [0x0001e] in <4728ce776caa419a89c752e14aaca01c>:0 
      at QuantConnect.Algorithm.CSharp.FuturesWithCustomIndicator.UpdateIndicators (QuantConnect.Data.Market.TradeBar halfHourBar) [0x0001e] in 
Update Backtest

Hi GrindTrader, 

1) The updated futures data are in the future chains. OnData() will be called when there is new data available based on the resolution while Initialize() is called only once at the beginning of the algorithm. It is required to retrieve the futures data in futures chains in OnData().

2) For the custom indicator with the consolidator, 

you can use "RegisterIndicator(contract_symbol, indicator, Consolidator);" to update the indicator with the consolidated data. With RegisterIndicator, you don't need to manually update the indicator each time there is a new consolidated bar.  

(Update method is mandatory in the custom indicator, with Update(), LEAN can automatically update the indicator with RegisterIndicator ). You should only use RegisterIndicator () once for each futures contract.  Please refer to the following algorithm


        public void OnHalfHour(object sender, QuoteBar halfHourBar)

This is the helper method which will be called when there is a new consolidated bar, you can use this method to update the indicator manually with customIndicator.Update(time, value) in this method. 

3) when you create the custom indicator constructor CustomIndicator(contract, *arguments), the symbol here should be the futures contract symbol object instead of the "ES". In this way, when you use RegisterIndicator(symbol, indicator, consolidator) to update the indicator, LEAN can know you need the data of the specified contract symbol otherwise, there is no data for "ES", it is a general string ticker.

Futures should use QuoteBarConsolidator instead of TradeBarConsolidator. Please refer to the algorithm

Thank you very much for your reply Jing Wu - yes, using the specified contract symbol (in stead of the generic "ES)) in the Indicator name solved the problem. Great :-)


Update Backtest


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.


This discussion is closed