what @ab_trader said is what you should follow to prevent later issues. You could overwrite the close method of the strategy and check for your broker. If it is your broker, use a different way to close the order, if not, just call super(self).close(order)
@songcl said in How to Get Live Data from IB:
I've run your code almost unchanged (up to some syntax errors) and it seems to be working ok:
Data added to cerebro
Server Version: 76
TWS Time at connection:20200613 12:40:05
Strategy initialization finished.
***** DATA NOTIF: DELAYED
2020-04-01 13:30:00 : 35.62
2020-04-01 13:32:00 : 35.6
2020-04-01 13:34:00 : 35.92
2020-04-01 13:36:00 : 36.16
Please check if there is a problem with your TWS account or client is not running. You may also examine the TWS API logs for some errors.
It seems the connection to IB broker was terminated from the server side. You may want to take a look at the TWS API Logs to see what was the cause (it could be a lot of different reasons for connection drop)
@marketlab3 MarketLab is a service to help developper and trader to backtest their own crypto trading strategies.
We offer data recorded in realtime, open source libraries and documentation to backtest easly your algorithms.
Have a look on our website and get started for free: https://www.market-lab.app/
I discovered your good tool backtrader_addons & start using it. I found a 'ZeroDivisionError: float division by zero' bug when there is no trade in a test.
Below are the stacktrace for your reference.
Traceback (most recent call last):
File "C:/Users/john/PycharmProjects/Backtrader/venv/Include/IBPaperTrade.py", line 83, in <module>
strats = cerebro.run(tradehistory=True) # for trade_list
File "C:\Users\john\PycharmProjects\Backtrader\venv\lib\site-packages\backtrader\cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "C:\Users\john\PycharmProjects\Backtrader\venv\lib\site-packages\backtrader\cerebro.py", line 1301, in runstrategies
TestStrategy, Bracket, 2020-06-04-030233, Strategies execution completed 0 trade with self.broker.cash=7788592.0392 and self.broker.getvalue()=7788591.99
File "C:\Users\john\PycharmProjects\Backtrader\venv\lib\site-packages\backtrader\strategy.py", line 486, in _stop
File "C:\Users\john\PycharmProjects\Backtrader\venv\lib\site-packages\backtrader\analyzer.py", line 200, in _stop
File "C:\Users\john\PycharmProjects\Backtrader\venv\lib\site-packages\backtrader_addons-0.18.10-py3.6.egg\backtrader_addons\analyzers\limited_test_report.py", line 80, in stop
self.rpt['win_percent'] = 100.0 * win_percent / len(trades)
ZeroDivisionError: float division by zero
BT has built in 'Highest' and 'Lowest' indicators that will do this for you.
For a long time I used it wrong a received inconsistent results. This structure works for me now:
import backtrader as bt
import backtrader.indicators as bt.ind
params = (
self.highest = highest = bt.ind.Highest(self.data, period=self.p.highperiod_0, plot=True, subplot=False)
self.lowest = lowest = bt.ind.Lowest(self.data, period=self.p.lowperiod_0, plot=True, subplot=False)
#LONG POSITION: Breakout high when
self.data.close > self.highest
#SHORT POSITION: Breakout low when
self.data.close() < self.lowest
You need to set the period for the number of bars you want to find the highest/lowest data for. Once you have this static high/low working, try using a dynamic indicator for something a little more complex: