Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Prevent live trading during "warm up" period

    General Code/Help
    2
    4
    178
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      joeyy last edited by

      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!

      1 Reply Last reply Reply Quote 0
      • R
        rajanprabu last edited by

        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()
        
        1 Reply Last reply Reply Quote 0
        • J
          joeyy last edited by

          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 work

           def next(self):
                  print(self.datastatus)
          

          I'm getting a

          AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'datastatus'
          
          1 Reply Last reply Reply Quote 0
          • J
            joeyy last edited by

            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!

            1 Reply Last reply Reply Quote 0
            • 1 / 1
            • First post
              Last post
            Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors