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

Live trader off back of Backtrader



  • Hi All -
    I have a number of bots that live trade on various exchanges via the REST APIs (fetch the market data / candles regularly, make some decisions, post trades automatically). I'm in the process of building in Backtrader into the core so as to improve the strategies. It's a great backtesting tool and I think it could also be used to fire off real trade signals too.

    I don't see a standard way people are doing this on the forums yet, so wanted to share my plans for feedback first ....

    1. Fetch and process the latest candles via the API. This will be the same as the last fetch but with the extra last candle added to the dataset. Will then parse this so it can be used by Backtrader as a bt.feeds object. This is pretty simple to do and has been done before.

    2. Creating a SQLite database to hold the trades and positions when running 'live' - need a database in case the bot needs a reboot! Simple to do.

    3. The database should be updated during the next or notify_order methods in the strategy when running live only - what I really want is this only to fire on the last iteration of the run() method, as the previous ones are all just part of the set-up. It's really the new orders due to the last candle that matters. Probably simple to do but feels like a bit of a 'hack'. Am I missing a better way?

    4. Separately to do the real live trades, a new trade in the database will fire off a job to post a trade to the exchange via the API and ensure it executes. I have a separate python job to do this.

    Has this already been done? Or is there something already built into Backtrader that does this?

    Thanks a lot


  • administrators

    I believe it's entirely up to you to code this thing up, but there is a fundamental problem in the description

    @ttamg said in Live trader off back of Backtrader:

    1. The database should be updated during the next or notify_order methods in the strategy when running live only - what I really want is this only to fire on the last iteration of the run() method, as the previous ones are all just part of the set-up

    Aside from the fact that run is only run once ... if you are trading live you don't when the last iteration of next is going to happen, because you are trading live and there is no expected end.



  • @backtrader Thanks for the note. Yes that only works when triggering trading decisions after the next candle has closed. Was thinking perhaps need to subclass the Cerebro class and adjust the run method. Anyway thanks for the reply. Good to know I am not reinventing the wheel


  • administrators

    You then don't need a live behavior from cerebro. You need a data feed which declares its end (like any regular CSV feed does, for example). When the data is exhausted you will be then taken to the stop method of your strategy and you can decide whether what the last next triggered/calculated is of your interest or not to issue a trade.

    See Docs - Strategy - the lifecycle is explained right at the beginning.

    When new candles come in, you rerun the cerebro setup (i.e.: create feed, add feed, add strategy, run cerebro)


Log in to reply
 

});