For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Question on live data update



  • I'm going to add live data fetching to my code. I read the example "sample\ibtest\ibtest.py" and have a question:

    In the class: TestStrategy of the sample code, it contain a SMA calculation in the init of the TestStrategy class:

    self.sma = bt.indicators.MovAv.SMA(self.data, period=self.p.smaperiod)

    Because the init run once only (during the TestStrategy class instance creation). However, the real time data (i.e. self.data) updates continuously, how can the "self.sma" be reflected, as init run once only?



  • Once you have your data in live mode runonce and preloading is disabled by default.



  • @adrianyuen said in Question on live data update:

    Because the init run once only (during the TestStrategy class instance creation). However, the real time data (i.e. self.data) updates continuously, how can the "self.sma" be reflected, as init run once only?

    The thing is that the SMA indicator is not actually calculated in __init__ method, it just creates an additional data feed object which happens to be an indicators calculating SMA for the given data that it was initialized with.

    The indicator values will be calculated once the underlying data feed will be loaded (either iteratively using next method or all in once if runonce and preloading options are specified)

    I think it was explained in the docs (Platform Concept section):

    https://www.backtrader.com/docu/concepts/#almost-everything-is-a-data-feed



  • Thanks rajanprabu and vladisld. That means, if live data is involved, and I want my backtesting strategy to be reusable in live data environment, I can't put my indicator calculations (usually in the form of array operations) in init. I must rewrite those indicators as methods, which accepts data feeds in each iteration of "next"



  • @adrianyuen said in Question on live data update:

    I can't put my indicator calculations (usually in the form of array operations) in init.

    Yes you can. If you have a look at the ibtest.py which also has SMA indicator as one uses as in backtesting. Indicator values are calculated using next method. This is also why I live backtrader as one can use same framework to backtest and take it live. If you are using interactive brokers, you just have to add data, broker and some notifications to your strategy class.



  • @rajanprabu Can I understand the mechanism as: the SMA indicator can be put in init section of the ibtest.py example because the build-in SMA indicator implemented the "next" method. When live data fits in, Cerebro calls the next() method of the SMA indicator to update the indicator value. Is it correct?


Log in to reply
 

});