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/

    How replay data with live binance ticks?

    General Discussion
    crypto binance
    3
    3
    281
    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.
    • B
      banian last edited by

      I want to receive updates inside OHLC data. I get live binance ticks using https://github.com/Dave-Vallance/bt-ccxt-store And when try to replay it to every 10 seconds get this error:

      ValueError: backtrader CCXT module doesn't support fetching OHLCV data for time frame Seconds, comression 10
      

      When i try to use minutes timeframe it work like resample and get data every minute, not every ticks.

      How i can replay live ticks data?

      This is my code:

      Strategy

      class TestSt(bt.Strategy):
          data_live = False
      
          def __init__(self):
              self.open = self.data.open
              self.high = self.data.high
              self.low = self.data.low
              self.close = self.data.close
              self.volume = self.data.volume
              self.date = self.data.datetime
              self.order = None
      
          def log(self, txt, dt=None):
              ''' Logging function fot this strategy'''
              try:
                  dt = dt or self.date[0]
              except Exception as e:
                  dt = datetime.datetime.utcnow()
              if isinstance(dt, float):
                  dt = bt.num2date(dt)
              logger.debug(f'{dt.isoformat()} {txt}')
          
          def notify_data(self, data, status, *args, **kwargs):
              self.log(f'data notif: {data._getstatusname(status)}')
              if status == data.LIVE:
                  self.data_live = True
      
          def next(self):
              if self.data_live:
                  self.log(
                      f'{len(self)} '
                      f'{self.open[0]:.2f} ' 
                      f'{self.high[0]:.2f} '
                      f'{self.low[0]:.2f} '
                      f'{self.close[0]:.2f} '
                      f'{self.volume[0]:.2f} '
                      # f'{self.range[0]:.2f} '
                  )
      
          base = 'ETH'
          quote = 'USDT'
      
          logger.add('trading.logs')
      
          cerebro = bt.Cerebro(quicknotify=True)
      
          broker_config = {
                  'apiKey': api_key,
                  'secret': api_secret,
                  'nonce': lambda: str(int(time.time() * 1000)),
                  'enableRateLimit': True,
              }
      
          store = CCXTStore(exchange='binance', currency=quote, config=broker_config, retries=5, debug=False)
      
          broker_mapping = {
              'order_types': {
                  bt.Order.Market: 'market',
                  bt.Order.Limit: 'limit',
                  bt.Order.Stop: 'stop-loss',
                  bt.Order.StopLimit: 'stop limit'
              },
              'mappings': {
                  'closed_order': {
                      'key': 'status',
                      'value': 'closed'
                  },
                  'canceled_order': {
                      'key': 'status',
                      'value': 'canceled'
                  }
              }
          }
      
          broker = store.getbroker(broker_mapping=broker_mapping)
          cerebro.setbroker(broker)
      
          # hist_start_date = dt.datetime.utcnow() - dt.timedelta(minutes=1)
          data = store.getdata(
              dataname=base+'/'+quote,
              name=base+quote,
              timeframe=bt.TimeFrame.Ticks,
              # fromdate=dt.datetime.utcnow(),
              # compression=1,
              # ohlcv_limit=99999
          )
      
          # cerebro.adddata(data)
          cerebro.replaydata(data, timeframe=bt.TimeFrame.Seconds, compression=10)
          # cerebro.resampledata(datafeed, timeframe=bt.TimeFrame.Seconds, compression=3)
      
          cerebro.addsizer(bt.sizers.PercentSizer, percents=1)
          # cerebro.addstrategy(Donald, prev_day_pct=300, stop_days=2, logs=True)
          cerebro.addstrategy(TestSt)
      
          logger.info(f'{quote} balance {cerebro.broker.getvalue()}')
      
          cerebro.run()
      

      Output when use cerebro.adddata(data):

      2021-08-11 14:20:32.633 | INFO     | __main__:<module>:76 - USDT balance 111.71465204
      2021-08-11 14:20:32.636 | DEBUG    | testst:log:28 - 2021-08-11T11:20:32.636576 data notif: LIVE
      2021-08-11 14:20:34.105 | DEBUG    | testst:log:28 - 2021-08-11T11:19:14.066998 1 3233.47 3233.47 3233.47 3233.47 0.50 
      2021-08-11 14:20:35.442 | DEBUG    | testst:log:28 - 2021-08-11T11:19:14.071997 2 3233.47 3233.47 3233.47 3233.47 0.50 
      2021-08-11 14:20:36.350 | DEBUG    | testst:log:28 - 2021-08-11T11:19:14.275003 3 3233.72 3233.72 3233.72 3233.72 0.08 
      2021-08-11 14:20:37.277 | DEBUG    | testst:log:28 - 2021-08-11T11:19:14.321996 4 3233.73 3233.73 3233.73 3233.73 0.06 
      

      Looks like ticks but not for now. Why?

      output when use resample data
      cerebro.replaydata(data, timeframe=bt.TimeFrame.Seconds, compression=10):

      Traceback (most recent call last):
        File "02-binance-ccxt.py", line 78, in <module>
          cerebro.run()
        File "/Users/dm/opt/anaconda3/lib/python3.7/site-packages/backtrader/cerebro.py", line 1127, in run
          runstrat = self.runstrategies(iterstrat)
        File "/Users/dm/opt/anaconda3/lib/python3.7/site-packages/backtrader/cerebro.py", line 1298, in runstrategies
          self._runnext(runstrats)
        File "/Users/dm/opt/anaconda3/lib/python3.7/site-packages/backtrader/cerebro.py", line 1542, in _runnext
          drets.append(d.next(ticks=False))
        File "/Users/dm/opt/anaconda3/lib/python3.7/site-packages/backtrader/feed.py", line 407, in next
          ret = self.load()
        File "/Users/dm/opt/anaconda3/lib/python3.7/site-packages/backtrader/feed.py", line 479, in load
          _loadret = self._load()
        File "/Users/dm/opt/anaconda3/lib/python3.7/site-packages/ccxtbt/ccxtfeed.py", line 111, in _load
          self._fetch_ohlcv()
        File "/Users/dm/opt/anaconda3/lib/python3.7/site-packages/ccxtbt/ccxtfeed.py", line 135, in _fetch_ohlcv
          granularity = self.store.get_granularity(self._timeframe, self._compression)
        File "/Users/dm/opt/anaconda3/lib/python3.7/site-packages/ccxtbt/ccxtstore.py", line 129, in get_granularity
          (bt.TimeFrame.getname(timeframe, compression), compression))
      ValueError: backtrader CCXT module doesn't support fetching OHLCV data for time frame Seconds, comression 10
      

      why it try use ccxt for fetching ohlc?

      output when use replaydata with minutes timeframe
      cerebro.replaydata(data, timeframe=bt.TimeFrame.Minutes, compression=1):

      2021-08-11 14:27:22.954 | INFO     | __main__:<module>:76 - USDT balance 111.71465204
      2021-08-11 14:27:22.956 | DEBUG    | testst:log:28 - 2021-08-11T11:27:22.956767 data notif: LIVE
      2021-08-11 14:27:24.685 | DEBUG    | testst:log:28 - 2021-08-11T11:08:00 1 3237.39 3240.70 3235.10 3235.51 675.82 
      2021-08-11 14:27:25.499 | DEBUG    | testst:log:28 - 2021-08-11T11:09:00 2 3235.51 3236.12 3231.81 3232.79 222.11 
      2021-08-11 14:27:26.323 | DEBUG    | testst:log:28 - 2021-08-11T11:10:00 3 3232.78 3234.36 3231.82 3233.22 208.32 
      2021-08-11 14:27:27.150 | DEBUG    | testst:log:28 - 2021-08-11T11:11:00 4 3233.22 3234.35 3230.75 3230.93 155.17 
      2021-08-11 14:27:28.064 | DEBUG    | testst:log:28 - 2021-08-11T11:12:00 5 3230.94 3233.26 3229.30 3232.47 385.39 
      2021-08-11 14:27:28.869 | DEBUG    | testst:log:28 - 2021-08-11T11:13:00 6 3232.46 3234.41 3231.45 3233.47 207.92 
      2021-08-11 14:27:29.686 | DEBUG    | testst:log:28 - 2021-08-11T11:14:00 7 3233.47 3233.60 3231.22 3233.03 145.63 
      2021-08-11 14:27:30.521 | DEBUG    | testst:log:28 - 2021-08-11T11:15:00 8 3233.03 3233.04 3229.10 3230.45 342.59 
      2021-08-11 14:27:31.443 | DEBUG    | testst:log:28 - 2021-08-11T11:16:00 9 3230.46 3234.64 3227.51 3234.39 386.75 
      2021-08-11 14:27:32.250 | DEBUG    | testst:log:28 - 2021-08-11T11:17:00 10 3234.44 3240.78 3234.43 3238.38 572.22 
      2021-08-11 14:27:33.081 | DEBUG    | testst:log:28 - 2021-08-11T11:18:00 11 3238.38 3238.38 3235.33 3236.61 265.52 
      2021-08-11 14:27:33.902 | DEBUG    | testst:log:28 - 2021-08-11T11:19:00 12 3236.62 3236.62 3231.91 3234.48 185.72 
      2021-08-11 14:27:34.719 | DEBUG    | testst:log:28 - 2021-08-11T11:20:00 13 3234.48 3237.69 3234.08 3235.98 528.40 
      2021-08-11 14:27:36.051 | DEBUG    | testst:log:28 - 2021-08-11T11:21:00 14 3235.96 3240.57 3235.95 3238.66 395.24 
      2021-08-11 14:27:36.857 | DEBUG    | testst:log:28 - 2021-08-11T11:22:00 15 3238.66 3240.00 3236.03 3236.10 417.97 
      2021-08-11 14:27:37.664 | DEBUG    | testst:log:28 - 2021-08-11T11:23:00 16 3236.10 3238.66 3235.00 3235.00 485.24 
      2021-08-11 14:27:38.508 | DEBUG    | testst:log:28 - 2021-08-11T11:24:00 17 3235.00 3236.69 3232.47 3235.02 322.88 
      2021-08-11 14:27:39.431 | DEBUG    | testst:log:28 - 2021-08-11T11:25:00 18 3235.02 3236.66 3229.61 3229.79 345.73 
      2021-08-11 14:27:40.244 | DEBUG    | testst:log:28 - 2021-08-11T11:26:00 19 3229.70 3233.24 3229.55 3231.52 327.90 
      2021-08-11 14:27:41.057 | DEBUG    | testst:log:28 - 2021-08-11T11:27:00 20 3231.52 3232.18 3230.15 3230.94 143.55
      

      why data received not from now?

      vladisld 1 Reply Last reply Reply Quote 0
      • V
        Vincent 0 last edited by

        @banian Hi, Have you resolve this probelm?

        1 Reply Last reply Reply Quote 0
        • vladisld
          vladisld @banian last edited by

          @banian It seems to be a limitation of bt-ccxt-store store. Did you try to open an issue in Dave's repository (https://github.com/Dave-Vallance/bt-ccxt-store/issues) ?

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