Skip to content

Instantly share code, notes, and snippets.

@eduedix eduedix/bt1.py Secret
Created Aug 23, 2018

Embed
What would you like to do?
bt IB reconnect
import datetime
import backtrader as bt
import pytz
import pandas as pd
import backtrader as bt
import datetime
class ForwarderStrat(bt.Strategy):
def notify_data(self, data, status, *args, **kwargs):
print('*' * 5, datetime.datetime.utcnow(), 'DATA NOTIF:', data._getstatusname(status), data._name, *args)
def notify_store(self, msg, *args, **kwargs):
print('*' * 5, datetime.datetime.utcnow(), 'STORE NOTIF:', msg)
def printAll(self):
for i, x in enumerate(self.datas):
print(self.datas[i]._name, self.datas[i].datetime.datetime(), self.datas[i].close[0])
print()
def next(self):
self.printAll()
def main():
cerebro = bt.Cerebro(stdstats=False)
ibstore = bt.stores.IBStore(
host='127.0.0.1', port=4002, clientId=42, reconnect=-1)
broker = ibstore.getbroker()
cerebro.setbroker(broker)
tickNameEndswith = '_tick'
instrumentNames = ['AUD.CAD-CASH-IDEALPRO',
'AUD.NZD-CASH-IDEALPRO',
'AUD.USD-CASH-IDEALPRO',
'CAD.CHF-CASH-IDEALPRO',
'CAD.JPY-CASH-IDEALPRO',
'CHF.JPY-CASH-IDEALPRO',
'EUR.AUD-CASH-IDEALPRO',
'EUR.CAD-CASH-IDEALPRO',
'EUR.CHF-CASH-IDEALPRO',
'EUR.GBP-CASH-IDEALPRO',
'EUR.JPY-CASH-IDEALPRO',
'EUR.USD-CASH-IDEALPRO',
'GBP.AUD-CASH-IDEALPRO',
'GBP.CAD-CASH-IDEALPRO',
'GBP.CHF-CASH-IDEALPRO',
'GBP.JPY-CASH-IDEALPRO',
'GBP.NZD-CASH-IDEALPRO',
'GBP.USD-CASH-IDEALPRO',
'NZD.CAD-CASH-IDEALPRO',
'NZD.USD-CASH-IDEALPRO',
'USD.CAD-CASH-IDEALPRO',
'USD.CHF-CASH-IDEALPRO',
'USD.JPY-CASH-IDEALPRO']
for instrumentName in instrumentNames:
data = ibstore.getdata(dataname=instrumentName, tz=pytz.timezone('UTC'))
cerebro.resampledata(data, name='{}{}'.format(instrumentName, tickNameEndswith), timeframe=bt.TimeFrame.Seconds, compression=30)
cerebro.addstrategy(ForwarderStrat)
cerebro.run(tradehistory=True)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.