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

Strategy state persistence

  • Hello,

    I'm a newbie in backtrader, but platform looks very powerful and I'd like to use it for backtesting as well as live trading with IB.
    I want to run a strategy which opens trades based on external signals daily and holds it up to 5 days depending on further signals. Trades on the same instrument can be opened daily and should be managed separately.
    Essentially for live trading I'd like to persist my trades and orders into some database and be able load them back into backtrader on system start in case of restarts or failures or just after a weekend. It seems persistence is not a feature of the platform yet.
    It seems one solution could be is to push trades and orders updates to database during notify,notify_trade and make strategy logic to work with database trades/orders data. This might be easier to implement but it would be difficult to make it work out of the box for both live trading and backtesting.
    Are there any other ideas on how to implement trades/orders persistence?


  • administrators

  • Thanks for the links.
    Having own trade and order management looks like an overkill since this is implemented in backtrader already but without persistence which essentially not required for a backtesting but required for proper live trading.
    Would it work if I will be saving trades and orders into database and inject them back somehow into _trades on backtrader restart?


  • administrators

    The platform makes no assumptions as to what it is in the _trades attribute when it starts. If something is there because you have pickled it and later unpickled it back into the attribute it will be used.

    Orders is probably a different beast, because they travel between the broker and the strategys and are also in trades.

    Saving and restoring the position (although it is attempted to read it from the broker) would be needed, because the actual position from the algorithm doesn't have to match what you have in the broker. You may have opened/closed positions manually.

Log in to reply