Greetings to the backtrader author and admins, thank you for making such great product open source!
I am trying to connect my script with IB for live trading, but I have trouble to fetch live data (except forex data, which is used in the official tutorial). When I browsed around it seems other people had the similar issue, but I didn't see any good solution posted. So I am trying to open this discussion again. I checked that I do have the right data subscription from IB, because I also manually trade stock/futures.
My scrip is
HOST = '127.0.0.1'
LIVE_PORT = 7496
PAPER_PORT = 7497
def record_data_verbose(data: bt.feeds.DataBase, name: str) -> str:
txt = []
txt.append('name: {}'.format(name))
txt.append('local time: {}'.format(datetime.now().strftime("%H:%M:%S")))
txt.append('feed len: {}'.format(len(data)))
txt.append('feed time: {}'.format(
data.datetime.datetime(0).isoformat())
)
txt.append('open: {}'.format(data.open[0]))
txt.append('high: {}'.format(data.high[0]))
txt.append('low: {}'.format(data.low[0]))
txt.append('close: {}'.format(data.close[0]))
txt.append('vol: {}'.format(data.volume[0]))
return ','.join(txt)
class DataRecorder(bt.Strategy):
def notify_data(self, data, status, *args, **kwargs):
print(get_data_status(data=data, status=status))
def next(self):
print(record_data_verbose(self.getdatabyname('es'), name='es'))
# import pdb; pdb.set_trace()
def run(args=None):
cerebro = bt.Cerebro(stdstats=False)
store = bt.stores.IBStore(
host=HOST,
port=LIVE_PORT,
# clientId=36,
notifyall=True,
_debug=True, # print all debugging message
)
data1 = store.getdata(dataname='ES-202209',
sectype='FUT',
exchange='GLOBEX',
timeframe=bt.TimeFrame.Seconds)
cerebro.adddata(data1, name='es')
cerebro.addstrategy(DataRecorder)
cerebro.run()
if __name__ == '__main__':
run()
The ouput I got from IB is:
python exp/broker_connection/fut_conn.py
Server Version: 76
TWS Time at connection:20220809 19:31:14 EST
<managedAccounts accountsList=XXXXXX>
<nextValidId orderId=1>
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:uscrypto>
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:hfarm>
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm.nj>
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:jfarm>
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfuture>
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:cashfarm>
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:eufarmnj>
<error id=-1, errorCode=2104, errorMsg=Market data farm connection is OK:usfarm>
<error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:euhmds>
<error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:fundfarm>
<error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:ushmds>
<error id=-1, errorCode=2158, errorMsg=Sec-def data farm connection is OK:secdefil>
<currentTime time=1660087874>
<contractDetails reqId=16777216, contractDetails=<ib.ext.ContractDetails.ContractDetails object at 0x7f8418296220>>
<contractDetailsEnd reqId=16777216>
data delayed
<marketDataType reqId=16777217, marketDataType=1>
<tickPrice tickerId=16777217, field=1, price=4127.0, canAutoExecute=1>
<tickSize tickerId=16777217, field=0, size=51>
<tickPrice tickerId=16777217, field=2, price=4127.5, canAutoExecute=1>
<tickSize tickerId=16777217, field=3, size=20>
<tickPrice tickerId=16777217, field=4, price=4127.0, canAutoExecute=0>
<tickSize tickerId=16777217, field=5, size=3>
<tickSize tickerId=16777217, field=0, size=51>
<tickSize tickerId=16777217, field=3, size=20>
<tickSize tickerId=16777217, field=5, size=3>
<tickSize tickerId=16777217, field=8, size=4749>
<tickPrice tickerId=16777217, field=6, price=4129.75, canAutoExecute=0>
<tickPrice tickerId=16777217, field=7, price=4126.25, canAutoExecute=0>
<tickPrice tickerId=16777217, field=9, price=4124.5, canAutoExecute=0>
<tickPrice tickerId=16777217, field=14, price=4128.75, canAutoExecute=0>
<tickString tickerId=16777217, tickType=45, value=1660087875>
<tickGeneric tickerId=16777217, tickType=49, value=0.0>
<tickString tickerId=16777217, tickType=48, value=4127.25;19.0000000000000000;1660087874912;4749.0000000000000000;4127.79600969;false>
<tickGeneric tickerId=16777217, tickType=49, value=0.0>
<tickPrice tickerId=16777217, field=1, price=4127.25, canAutoExecute=1>
<tickSize tickerId=16777217, field=0, size=2>
<tickSize tickerId=16777217, field=0, size=2>
<tickSize tickerId=16777217, field=0, size=3>
<tickSize tickerId=16777217, field=0, size=9>
<tickSize tickerId=16777217, field=3, size=14>
<error id=-1, errorCode=2105, errorMsg=HMDS data farm connection is broken:euhmds>
<error id=-1, errorCode=2106, errorMsg=HMDS data farm connection is OK:euhmds>
<tickSize tickerId=16777217, field=0, size=7>
<tickSize tickerId=16777217, field=0, size=9>
<tickSize tickerId=16777217, field=3, size=13>
<tickString tickerId=16777217, tickType=48, value=4127.50;3.0000000000000000;1660087885528;4752.0000000000000000;4127.79582281;false>
<tickString tickerId=16777217, tickType=45, value=1660087885>
<tickPrice tickerId=16777217, field=4, price=4127.5, canAutoExecute=0>
<tickSize tickerId=16777217, field=5, size=1>
<tickSize tickerId=16777217, field=5, size=1>
Clearly IB was sending back some useful info, but backtrader didn't print out anything from its next() method. And if I change the prodcut to forex, then the next() method will receive the correct data. Can anyone help take a look? Much Thanks!