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

Starting strategy later while using get(ago,size)



  • In my Strategy class, dataclose.get(0, 288) gets an empty list the first run. How do I start the strategy later after 288 data points?


  • administrators

    That's really a broad question.

    • What is "first run"?
    • What indicators have you defined?

    Assuming you mean next, it could suffice with adding a Simple Moving Average of at least 288 periods.

    Some sample code lines do always help.

    But in general the idea is that you don't need to access a period backwards longer than the period defined by the indicators you define .



  • @backtrader said in Starting strategy later while using get(ago,size):

    Assuming you mean next, it could suffice with adding a Simple Moving Average of at least 288 periods.

    Or instead in next() you insert a line prior to all code responsible for managing trades, if len(self.data0) > 288:, but as @backtrader said, it's hard to know what's going on without a code sample.



  • Yes, I am simply calling dataclose.get(0, 288) in next() of my Strategy class. I have 5-minute candlestick data samples, so 288 samples is 24 hours of data. The "first run" is just the first time next() is called.

    Using the moving average indicator with 288 period does the trick of delaying the next() until 288th cycle. Thanks.

    It seems the right way to do is to get calculation done in an Indicator class, so I can set period to the delay period I want. So I am looking at sma.py which calls Average(self.data, period=self.p.period). This seems to affect how the Strategy class operate. But I couldn't find the Average class anywhere in the backtrader package folder. Where can I find relevant information about it?


  • administrators

    @Andre said in Starting strategy later while using get(ago,size):

    which calls Average(self.data, period=self.p.period). This seems to affect how the Strategy class operate

    No, it's not affecting how the Strategy operates. It simply informs the strategy about the minimum period contribution it has. See the following:

    The Minimum Period concept is explained in both and what will be called when.

    The Average indicator is documented here:

    If you want to read the code look in indicators/basicops.py