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

Dynamic feeds issue



  • Hi! I am not able to figure out whether this issue is with the ccxt branch that ed made or backtrader itself. But the issue arrises when trying to add more than one data feed. Here is the code:

    class Object(object):
        def __init__(self):
            self.exchange = None
            self.config = None
            self.value = None
    
    
    class TestStrategy(bt.Strategy):
    
        def start(self):
            self.counter = 0
    
            print('START')
    
        def next(self):
            try:
                self.counter +=1
                print(self.data0.open[0],self.data1.open[0])
                print('got here')
            except:
                traceback.print_exc()
                pass
    
    
    
    def check_oppor(sid, DATA):
        global addD, datas
        global broker, makers, takers, withdrawls, configs, exchanges, keys, secrets
        exchanges = DATA['EXCHANGES']
        keys = DATA['API_KEY']
        secrets = DATA['API_SECRET']
        exchanges = [item for items in exchanges for item in items.split(",")]
        keys = [item for items in keys for item in items.split(",")]
        secrets = [item for items in secrets for item in items.split(",")]
    
    
    
        for key, secret in zip(keys, secrets):
            config = {
                'apiKey': key,
                'secret': secret,
                'nonce': lambda: str(int(time.time() * 1000))
            }
            configs.append(config)
    
        datas = []
        cerebro = bt.Cerebro()
    
        hist_start_date = datetime.utcnow() - timedelta(minutes=3)
    
        indicators = []
        print(len(configs))
        d = exchanges
    
        for index, c in zip(range(len(configs)), configs):
            try:
                print(index, "this is the index")
                Obj = getattr(ccxt, exchanges[index])
                Obj = Obj(c)
    
                indicators.append(Obj)
    
                data = bt.feeds.CCXT(exchange=exchanges[index], config=c,
                                 symbol='ETH/BTC',
                                 timeframe=bt.TimeFrame.Minutes,
                                 fromdate=hist_start_date, compression=1, ohlcv_limit=999,retries=100)
    
                data1 = bt.feeds.CCXT(exchange=exchanges[index], config=c,
                                  symbol='BCH/BTC',
                                  timeframe=bt.TimeFrame.Minutes,
                                  fromdate=hist_start_date, compression=1, ohlcv_limit=999,retries=100)
    
                data2 = bt.feeds.CCXT(exchange=exchanges[index], config=c,
                                  symbol='LTC/BTC',
                                  timeframe=bt.TimeFrame.Minutes,
                                  fromdate=hist_start_date, compression=1, ohlcv_limit=999,retries=100)
    
    
                datas.append([data,data1,data2])
                print(datas)
            except:
                traceback.print_exc()
                pass
    
        allDatas = [elem for sublist in datas for elem in sublist]
        for i in indicators:
            fees = i.describe()['fees']
            maker = fees['trading']['maker']
            taker = fees['trading']['taker']
            makers.append(maker)
            takers.append(taker)
    
            withdrawal = fees['funding']['withdraw']
            withdrawls.append(withdrawal)
            print(withdrawal)
    
        broker = bt.brokers.CCXTBroker(exchange='kucoin',
                                       currency='BTC', config=configs[1])
    
        addD = cerebro.adddata
        print (allDatas)
        for ind, d in enumerate(allDatas, start=0):
            try:
                addD(d, name="data" + str(ind))
                print(ind)
            except:
                traceback.print_exc()
                pass
    
        print(allDatas)
    
        cerebro.addstrategy(TestStrategy)
        cerebro.run()
    
    

    However, if we add one feed per iteration, there are no issues

    class Object(object):
        def __init__(self):
            self.exchange = None
            self.config = None
            self.value = None
    
    
    class TestStrategy(bt.Strategy):
    
        def start(self):
            self.counter = 0
    
            print('START')
    
        def next(self):
            try:
                self.counter +=1
                print(self.data0.open[0],self.data1.open[0])
                print('got here')
            except:
                traceback.print_exc()
                pass
    
    
    
    def check_oppor(sid, DATA):
        global addD, datas
        global broker, makers, takers, withdrawls, configs, exchanges, keys, secrets
        exchanges = DATA['EXCHANGES']
        keys = DATA['API_KEY']
        secrets = DATA['API_SECRET']
        exchanges = [item for items in exchanges for item in items.split(",")]
        keys = [item for items in keys for item in items.split(",")]
        secrets = [item for items in secrets for item in items.split(",")]
    
    
    
        for key, secret in zip(keys, secrets):
            config = {
                'apiKey': key,
                'secret': secret,
                'nonce': lambda: str(int(time.time() * 1000))
            }
            configs.append(config)
    
        datas = []
        cerebro = bt.Cerebro()
    
        hist_start_date = datetime.utcnow() - timedelta(minutes=3)
    
        indicators = []
        print(len(configs))
        d = exchanges
    
        for index, c in zip(range(len(configs)), configs):
            try:
                print(index, "this is the index")
                Obj = getattr(ccxt, exchanges[index])
                Obj = Obj(c)
    
                indicators.append(Obj)
    
                data = bt.feeds.CCXT(exchange=exchanges[index], config=c,
                                 symbol='ETH/BTC',
                                 timeframe=bt.TimeFrame.Minutes,
                                 fromdate=hist_start_date, compression=1, ohlcv_limit=999,retries=100)
    
                data1 = bt.feeds.CCXT(exchange=exchanges[index], config=c,
                                  symbol='BCH/BTC',
                                  timeframe=bt.TimeFrame.Minutes,
                                  fromdate=hist_start_date, compression=1, ohlcv_limit=999,retries=100)
    
                data2 = bt.feeds.CCXT(exchange=exchanges[index], config=c,
                                  symbol='LTC/BTC',
                                  timeframe=bt.TimeFrame.Minutes,
                                  fromdate=hist_start_date, compression=1, ohlcv_limit=999,retries=100)
    
    #adding only one feed
                datas.append([data])
                print(datas)
            except:
                traceback.print_exc()
                pass
    
        allDatas = [elem for sublist in datas for elem in sublist]
        for i in indicators:
            fees = i.describe()['fees']
            maker = fees['trading']['maker']
            taker = fees['trading']['taker']
            makers.append(maker)
            takers.append(taker)
    
            withdrawal = fees['funding']['withdraw']
            withdrawls.append(withdrawal)
            print(withdrawal)
    
        broker = bt.brokers.CCXTBroker(exchange='kucoin',
                                       currency='BTC', config=configs[1])
    
        addD = cerebro.adddata
        print (allDatas)
        for ind, d in enumerate(allDatas, start=0):
            try:
                addD(d, name="data" + str(ind))
                print(ind)
            except:
                traceback.print_exc()
                pass
    
        print(allDatas)
    
        cerebro.addstrategy(TestStrategy)
        cerebro.run()
    
    


  • If you use another data feed, bt built-in data feed, do you have the same issue?