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

Does indicators calculates for each new bar?



  • I'm learning Python in finance with backtrader. Now, I've implemented a simple stragedy for buy/sell assests, but I have no a good understanding of the platform. The documentation is good but still is not clear for me. So, for now there are 2 main questions:

    1. Does indicators calculate the result each time when next method is running? For example, in the init method I'm calculating SMA as follows: self.sma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.maperiod)
      As I can see from the log output the SMA is different each time next is calling.
    2. If the inidcators re-calculates each time next is called may I declare a variable in the __init__ to store the date of the last purchase date?

    I've already implemented this and seems like all is working, but I'm not sure.


  • administrators

    Indicators offer you always the latest value, automatically recalculated. It's exactly the point, no interaction from the end user for things which can be automatically done.

    You may declare anything inside __init__ and store anything in your declared variables. It's your code and your way of doing it.



  • @backtrader great, appreciate your help.