Problem Connecting to IB platform with Backtrader
-
I am having a problem when trying to send trade orders with backtrader. I looked for a similar problem here but could not find anything. I get a notification of "NOTSUBSCRIBED" and according to the documentation (https://www.backtrader.com/docu/live/ib/ib.html) it means that the program does not have the right permissions.
I am running the TWS platform with a paper money account and following the Global settings setup from this tutorial: https://medium.com/@danjrod/interactive-brokers-in-python-with-backtrader-23dea376b2fc
Where can I give permission to backtrader to interact with the TWS platform?I am hoping that someone can point me in the right direction, and this could possibly have a simple solution. Thank you. The problem and code are pasted below:
When I simply run the tutorial script I get this output from the terminal:
TWS Time at connection:20190222 11:10:01 EST ***** DATA NOTIF: DELAYED ***** DATA NOTIF: NOTSUBSCRIBED
The code I am currently running is below, which may have been modified from the tutorial in minor ways (but both scripts behave the same way).
#!/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-STK-SMART-USD', 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()
-
@andresberejnoi said in Problem Connecting to IB platform with Backtrader:
according to the documentation (https://www.backtrader.com/docu/live/ib/ib.html) it means that the program does not have the right permissions.
No. It means YOU don't have the right permissions.
@andresberejnoi said in Problem Connecting to IB platform with Backtrader:
Where can I give permission to backtrader to interact with the TWS platform?
It has nothing to do with giving permission to a program. You need a data subscription. Read this (amongst many other):
@andresberejnoi said in Problem Connecting to IB platform with Backtrader:
The code I am currently running is below, which may have been modified from the tutorial in minor ways (but both scripts behave the same way).
Don't modify the tutorial (except for the data ticker) and see what happens. Or run the sample
ibtest.py
from the sources and see what happens. I don't know if you broke it or not, but more often than not, people claim to have modified nothing or something unimportant or are running some code which is not posted ... and the non-modified code ends up being the culprit. -
@backtrader Thank you for your help. Yes, it was a data subscription issue. I only have a paper money account and did not want to open a real IB account yet. I also was not sure how to request delayed market data instead of live market data.
I found out that I am allowed to stream forex data as is, so for now I am using that.Thanks again for your help.