@vladisld said in IB - Switch between Multiple (Near) Online Datafeeds:
print(f'{self.datas[1].datetime.datetime(0).isoformat}:{self.datas[1].open[0] ....')
Re: "what do you mean..?" I had the impression that cerebro (ES: brain) did not collect the data for the 2nd feed.
Your answer and your short example-hint helped a lot. I was wrong.
from __future__ import (absolute_import, division, print_function, unicode_literals) import backtrader as bt import datetime #import matplotlib import time import pytz class St(bt.Strategy): #pr_St__class = St() #NameError: name 'St' is not defined #print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ pr_St__class', pr_St__class) #here he defines the structure of one record D-OHLC-V def logdata(self): txt = [] txt.append('{}'.format(len(self))) #current object lenght of data, number of records for pricing txt.append(' data_s') #1st datafeed txt.append('{}'.format(self.data.datetime.datetime(0).isoformat())) txt.append('{:.2f}'.format(self.data.open[0])) txt.append('{:.2f}'.format(self.data.high[0])) txt.append('{:.2f}'.format(self.data.low[0])) txt.append('{:.2f}'.format(self.data.close[0])) txt.append('{:.2f}'.format(self.data.volume[0])) txt.append(' data_c') #2nd datafeed txt.append('{}'.format(self.datas[1].datetime.datetime(0).isoformat())) txt.append('{:.2f}'.format(self.datas[1].open[0])) txt.append('{:.2f}'.format(self.datas[1].high[0])) txt.append('{:.2f}'.format(self.datas[1].low[0])) txt.append('{:.2f}'.format(self.datas[1].close[0])) txt.append('{:.2f}'.format(self.datas[1].volume[0])) print(','.join(txt)) def next(self): self.logdata() def run(args=None): cerebro = bt.Cerebro(stdstats=False) #with this i get clean data, no history. A clean structure store = bt.stores.IBStore(port=7496, clientId=1001) #20200701 with TWS THIS finally worked well data_s = store.getdata(dataname='EUR.USD-CASH-IDEALPRO', timeframe=bt.TimeFrame.Ticks, stopafter=10, #stopafter backfill_start=False, backfill=False, latethrough=True, tz='CET' ) cerebro.resampledata(data_s, timeframe=bt.TimeFrame.Seconds, compression=1) data_c = store.getdata(dataname='CHF.USD-CASH-IDEALPRO', timeframe=bt.TimeFrame.Ticks, stopafter=10, #stopafter backfill_start=False, backfill=False, latethrough=True, tz='CET' ) cerebro.resampledata(data_c, timeframe=bt.TimeFrame.Seconds, compression=1) cerebro.addstrategy(St) cerebro.run() # cerebro.plot(plotter=None, iplot=True) #Unfortunately I always get a lot of unneeded nformatio like platform Darwin, V 3.1.3, loaded modules, fonts if __name__ == '__main__': run()Having changed the code above (based on your recommendation) now I see what data is there:
Server Version: 76 TWS Time at connection:20200702 22:27:13 CET 1, data_s,2020-07-02T22:27:14,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:27:14,1.06,1.06,1.06,1.06,0.00 2, data_s,2020-07-02T22:27:14,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:27:22,1.06,1.06,1.06,1.06,0.00 3, data_s,2020-07-02T22:27:31,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:27:22,1.06,1.06,1.06,1.06,0.00 4, data_s,2020-07-02T22:27:31,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:28:57,1.06,1.06,1.06,1.06,0.00 5, data_s,2020-07-02T22:28:58,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:28:57,1.06,1.06,1.06,1.06,0.00 6, data_s,2020-07-02T22:29:01,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:29:01,1.06,1.06,1.06,1.06,0.00 7, data_s,2020-07-02T22:29:01,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:29:08,1.06,1.06,1.06,1.06,0.00 8, data_s,2020-07-02T22:29:01,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:29:10,1.06,1.06,1.06,1.06,0.00 9, data_s,2020-07-02T22:29:01,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:29:14,1.06,1.06,1.06,1.06,0.00 10, data_s,2020-07-02T22:29:21,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:29:14,1.06,1.06,1.06,1.06,0.00 11, data_s,2020-07-02T22:29:21,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:29:25,1.06,1.06,1.06,1.06,0.00 12, data_s,2020-07-02T22:29:29,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:29:25,1.06,1.06,1.06,1.06,0.00 13, data_s,2020-07-02T22:29:37,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:29:25,1.06,1.06,1.06,1.06,0.00 14, data_s,2020-07-02T22:30:02,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:30:02,1.06,1.06,1.06,1.06,0.00 15, data_s,2020-07-02T22:30:02,1.12,1.12,1.12,1.12,0.00, data_c,2020-07-02T22:30:07,1.06,1.06,1.06,1.06,0.00data_s is the 1st feed EUR.USD
data_c is the 2nd feed CHF.USD
As an outlook this is getting more interesting when displaying a stock and the corresponding option(s).
It needs to be improved as I am using for the resampling the compression=1. Not always there is data. Increasing the compression to 10 could be a workaround. Even better when i leav it empty For now I can go ahead.
My question is answered and solved so that i can go ahead. Thank you very much vladisld.