Skip to content

Instantly share code, notes, and snippets.

@backtrader
Last active May 9, 2024 03:06
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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()
@ch01ca
Copy link

ch01ca commented Mar 26, 2020

Hi I got an AttributeError: 'module' object has no attribute 'IBStore'

update:
I found it is caused by anaconda envs. after reinstalled envs and bagcktrader, it works

@fly2fire
Copy link

pipenv install IbPy2
befroe run

@ch01ca
Copy link

ch01ca commented May 25, 2020

pipenv install IbPy2
befroe run

thanks

@ch01ca
Copy link

ch01ca commented Jun 9, 2020

hi guys, when I set cerebro.addsizer(bt.sizers.PercentSizer, percents=90), I got below error:
TypeError: error() takes 2 positional arguments but 4 were given

`Traceback (most recent call last):
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 970, in placeOrder
self.send(order.m_displaySize)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/overloading.py", line 82, in call
return func(*args)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 1765, in send_3
self.send(str(val))
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/overloading.py", line 82, in call
return func(*args)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 1754, in send_1
self.m_dos.write(val)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/init.py", line 152, in write
send(pack('!c', char))
BrokenPipeError: [Errno 32] Broken pipe

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/Users/me/quan/backtrader/ibtutorial.py", line 86, in
run()
File "/Users/me/quan/backtrader/ibtutorial.py", line 82, in run
cerebro.run()
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/cerebro.py", line 1298, in runstrategies
self._runnext(runstrats)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/cerebro.py", line 1630, in _runnext
strat._next()
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/strategy.py", line 347, in _next
super(Strategy, self)._next()
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/lineiterator.py", line 271, in _next
self.next()
File "/Users/me/quan/backtrader/ibtutorial.py", line 64, in next
self.buy()
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/strategy.py", line 939, in buy
**kwargs)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/brokers/ibbroker.py", line 379, in buy
return self.submit(order)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/brokers/ibbroker.py", line 337, in submit
self.ib.placeOrder(order.m_orderId, order.data.tradecontract, order)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/backtrader/stores/ibstore.py", line 1286, in placeOrder
self.conn.placeOrder(orderid, contract, order)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/init.py", line 60, in inner
return func(*args, **kwds)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 1155, in placeOrder
self.error_0(id, EClientErrors.FAIL_SEND_ORDER, str(e))
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/init.py", line 60, in inner
return func(*args, **kwds)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/ext/EClientSocket.py", line 1709, in error_0
self.m_anyWrapper.error(id, errorCode, errorMsg)
File "/Users/me/anaconda3/envs/backtr/lib/python3.5/site-packages/ib/lib/overloading.py", line 82, in call
return func(*args)
TypeError: error() takes 2 positional arguments but 4 were given`

@Shu882
Copy link

Shu882 commented Jan 15, 2024

Hi I got an AttributeError: 'module' object has no attribute 'IBStore'

update: I found it is caused by anaconda envs. after reinstalled envs and bagcktrader, it works

How did you solve this? I have the same problem no matter what environment I use, Pycharm, VS code, jupyterlab, or Python IDE...

@Jamesfryer1998
Copy link

Hi I got an AttributeError: 'module' object has no attribute 'IBStore'
update: I found it is caused by anaconda envs. after reinstalled envs and bagcktrader, it works

How did you solve this? I have the same problem no matter what environment I use, Pycharm, VS code, jupyterlab, or Python IDE...

I am the same. I am using Conda, I have backtrader and IBPY2 successfully installed and I still get this error. Any other solutions?

@AndelFeng
Copy link

The same error here. anyone got the solutions?

@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