Skip to content

Instantly share code, notes, and snippets.

@backtrader
Last active May 9, 2024 03:06
Show Gist options
  • Save backtrader/456cf0891a7a62ceb2bbb5e4234c01e7 to your computer and use it in GitHub Desktop.
Save backtrader/456cf0891a7a62ceb2bbb5e4234c01e7 to your computer and use it in GitHub Desktop.
Interactive Brokers in Python with backtrader
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2018 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import backtrader as bt
class St(bt.Strategy):
def logdata(self):
txt = []
txt.append('{}'.format(len(self)))
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]))
print(','.join(txt))
data_live = False
def notify_data(self, data, status, *args, **kwargs):
print('*' * 5, 'DATA NOTIF:', data._getstatusname(status), *args)
if status == data.LIVE:
self.data_live = True
def notify_order(self, order):
if order.status == order.Completed:
buysell = 'BUY ' if order.isbuy() else 'SELL'
txt = '{} {}@{}'.format(buysell, order.executed.size,
order.executed.price)
print(txt)
bought = 0
sold = 0
def next(self):
self.logdata()
if not self.data_live:
return
if not self.bought:
self.bought = len(self) # keep entry bar
self.buy()
elif not self.sold:
if len(self) == (self.bought + 3):
self.sell()
def run(args=None):
cerebro = bt.Cerebro(stdstats=False)
store = bt.stores.IBStore(port=7497)
data = store.getdata(dataname='TWTR', timeframe=bt.TimeFrame.Ticks)
cerebro.resampledata(data, timeframe=bt.TimeFrame.Seconds, compression=10)
cerebro.broker = store.getbroker()
cerebro.addstrategy(St)
cerebro.run()
if __name__ == '__main__':
run()
@Jamesfryer1998
Copy link

@AndelFeng @Shu882 @ch01ca @fly2fire I found a fix.

Problem
Assuming we are using backtrader and ibpy2, there is already a clue...

So in our current state we cannot find IBStores which we can confirm it doesn't exist by using dir in python to print the properties of bt.stores:

['VCStore', 'VChartFile', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', 'absolute_import', 'division', 'ibstore', 'print_function', 'unicode_literals', 'vchartfile', 'vcstore']

Since we know ibpy2 is python2 (trailing 2 in package name being a giveaway) it's reasonable to guess that the reason this doesn't work is some mismatch between this python2 thing (and its assumptions about how things work) and the python3 backtrader.

Solution
So the way to test that guess is to port the whole of ibpy2 to python3:

find x/lib/python3.9/site-packages/ib/ -name '*.py' -exec 2to3 -w {} \;

(x in this is the name of the virtual env I installed it under)

If we check the properties of bt.stores again IBStores shows up:

['IBStore', 'VCStore', 'VChartFile', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', 'absolute_import', 'division', 'ibstore', 'print_function', 'unicode_literals', 'vchartfile', 'vcstore']

@AndelFeng
Copy link

@Jamesfryer1998
Does it work now? I wonder how to fix it in my Windows PC. Thanks a lot:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment