Skip to content

Instantly share code, notes, and snippets.

@backtrader

backtrader/ibtutorial.py

Last active Jun 9, 2020
Embed
What would you like to do?
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()
@cero1010

This comment has been minimized.

Copy link

@cero1010 cero1010 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

This comment has been minimized.

Copy link

@fly2fire fly2fire commented Apr 22, 2020

pipenv install IbPy2
befroe run

@cero1010

This comment has been minimized.

Copy link

@cero1010 cero1010 commented May 25, 2020

pipenv install IbPy2
befroe run

thanks

@cero1010

This comment has been minimized.

Copy link

@cero1010 cero1010 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`

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.