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

Looping over date periods of data in optstrategy

  • Hi All,

    relatively new to Backtrader, still learning the ropes, I wanted to ask if there is an easy way to "loop over date periods" of data in optstrategy in Backtrader. What do I mean by that is:

    -I have say 12 months of hourly data
    -I would like for optstrategy to run 12 times - once for each month - and read analyzer output for each of those 12 separate months
    -The issue is that "fromdate" and "todate" are arguments of datafeed that is added to cerebro - so even if I add them also to optstrategy as arguments - I am not sure if there is a way to pass them on to datafeed in every optstrategy iteration in a way that data feed is restricted only to these dates (in fact ideally I would be passing just fromdate and period duration and todate would be calculatd based on these two)

    I probably could figure out a way for my analyzer to somehow "reset" every month and log results but I feel like this actually would be much harder in my case (there are few complications - including the fact that if I want to get results for full month of February , I in fact have to additionally include extra 100 hours of data upfront (last 100 hours of January) if I am using indicators with 100 eriod length) .

    So I was wondering if there is easier way to do that - by passing fromdate and todate to optstrategy (or fromdate and period length to optstrategy) and then having optstrategy loop over multiple periods of time within the data

    Thank you in advance for any help!


  • Not sure it could be easily done - but if possible will be glad to take a look :-)

    Personally I'm iterating the date periods outside the Cerebro loop (just running multiple Cerebro engines - one per date period - in my case even serial ).

    Given that iterating over other parameters permutations occupies all the machine resources - serially iterating the date period doesn't introduce any considerable inefficiency.

    On the positive side - one may pass the analysis data from one date period to another - something that is not trivial to do in parallel execution.

Log in to reply