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

Observer overwriting issue

  • I made many custom codes on backtrader, so it's hard for me to explain the exact situation.
    But I'll try, and I'd be very thankful if you can save me from this.

    Situation :

    • I use multiple strategies in a portfolio
    • I use an observer on each of the strategy, added with cerebro.addobserver, as usual
    • I have currently ~ 20 strategies running in my portfolio
    • My observer has two lines, "date" and "pnl"

    The issue :
    When I go into the "next" method, I print :

    • bt.num2date(self._owner.lines.datetime[0]), to see the current date "in the owner strategy"
    •[0] (before writing anything inside) to see if[0] is nan or not
      In rare cases,[0] already has something written inside, which is actually what I wrote in the previous iteration of next for the "previous" bt.num2date(self._owner.lines.datetime[0])

    The question :
    How is it possible to overwrite something using only[0] ? Why does it not "advance" in time?

    My hypothesis :
    My datas in different strategies have different datetime / trading days, especially for some special dates like Christmas or Easter Monday, and this could cause some trouble in "advancing" in the lines.

    Sorry for my english, and my "not-quite-clear" issue.

    Stay healthy everyone :)

Log in to reply