Prevent live trading during "warm up" period
-
Hey all!
I've setup a strategy which seem to work alright and I've run simulations using backtrader and would now like to use it for live trading too.
The problem is that if I let it run for several candles to "warm up" the strategy, it will get buy/sell signals and try to execute trades on these historical candles. The reason being is that for instance for a 1 day candle and a 21 EMA I don't have to wait a few weeks to get a signal.
Is there a way to run it's "warm up" period and only make trades when it makes it to the ***** DATA NOTIF: LIVE period ?I've run it using these two CCXT implementations, one by Dave-Vallance
and the other by Ed Bartosh':from_date = datetime.utcnow() - timedelta(days=1) data_ticks = bt.feeds.CCXT( exchange='binance', symbol='BTC/USDT', timeframe=bt.TimeFrame.Minutes, config={'rateLimit': 1000, 'enableRateLimit': True}, fromdate=from_date, compression=5 )
store = CCXTStore(exchange='binance', currency='USDT', config={ 'rateLimit': 1000, 'enableRateLimit': True}, retries=5, debug=False) hist_start_date = datetime.utcnow() - timedelta(days=3) data_ticks = store.getdata(dataname='BTC/USDT', name="BTCUSDT", timeframe=bt.TimeFrame.Minutes, fromdate=hist_start_date, compression=60, ohlcv_limit=500, drop_newest=True)
thanks in advance!
-
hi @joeyy
I haven't used CCXT feed but IBKR one, but logic should be the same. You need to have a condition for your code to test if your data is live or not. And take trades only when live data coming through and not during backfilling.
if self.datastatus and awesome_secret_code : self.buy()
-
thanks for the quick reply. I'm indeed looking for a flag that will tell me if it's a live, delayed or simulated trade.
Unfortunately self.datastatus doesn't seem to workdef next(self): print(self.datastatus)
I'm getting a
AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'datastatus'
-
i think I got it, I can do something like this
def notify_data(self, data, status, *args, **kwargs): print('*' * 5, 'DATA NOTIF:', data._getstatusname(status)) if status == data.LIVE: self.datastatus = 1 else: self.datastatus = 0
and then use it in your next example.
awesome, thanks!