Live trader off back of Backtrader
ttamg last edited by
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 ....
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.
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.
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?
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
I believe it's entirely up to you to code this thing up, but there is a fundamental problem in the description
- 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
runis only run once ... if you are trading live you don't when the last iteration of
nextis going to happen, because you are trading live and there is no expected end.
ttamg last edited by
@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
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
stopmethod of your strategy and you can decide whether what the last
nexttriggered/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
cerebrosetup (i.e.: create feed, add feed, add strategy, run cerebro)
Shiva last edited by
have you been able to do so, if possible please share your work.