Hi fellow backtraders!
I have just started familiarizing myself with Backtrader. I have a newbie question for you:
Using the examples in the Quickstart guide I got to the section on plotting.
https://www.backtrader.com/docu/quickstart/quickstart/#visual-inspection-plotting
I copied the code exactly and got an identical plot except the x axis (date) is not showing at all. The cursor gives the right dates but there is no text on the axis. What have I forgotten?!
Thanks
(PS: Also, is there a place in Backtrader Community for newbies like me to look at first to find the usual gotchas.)
Best posts made by rluc99
-
Basic Plotting - No X Axis
-
RE: Starting with Interactive Brokers (IB) - no data feed
I have discovered the problem. It is not related to Backtrader.
I did some digging around on the IB website. It seems that the IB demo account does not allow historical bars through the API for anything other than FOREX. The same applies to paper trading or live accounts unless there is a subscription to the relevant market data feed. If there is a subscription in place then the historical bars are available at any time, including week-ends.
(As an aside, historical bars are constructed by TWS regardless of whether there is a subscription in place or not.)
Delayed data is available during market hours for accounts without market data subscription, but to obtain this data a flag has to be set in the API. I don't think Backtrader allows the user to set this flag but I have not investigated this in detail.Most of the above information comes from the following two links:
http://interactivebrokers.github.io/tws-api/market_data.html
http://interactivebrokers.github.io/tws-api/historical_data.html -
Plotting time tick labels
Is it possible to plot time tick labels (for intra-day data). I have loaded a file containing a single day of minute data, but no labels appear on the x-axis. The cursor is also not picking up any minute data (only the day).
I have checked that the file is being read in correctly and the hour/minute/second parameters are being updated on each bar.
Latest posts made by rluc99
-
RE: Starting with Interactive Brokers (IB) - no data feed
I have discovered the problem. It is not related to Backtrader.
I did some digging around on the IB website. It seems that the IB demo account does not allow historical bars through the API for anything other than FOREX. The same applies to paper trading or live accounts unless there is a subscription to the relevant market data feed. If there is a subscription in place then the historical bars are available at any time, including week-ends.
(As an aside, historical bars are constructed by TWS regardless of whether there is a subscription in place or not.)
Delayed data is available during market hours for accounts without market data subscription, but to obtain this data a flag has to be set in the API. I don't think Backtrader allows the user to set this flag but I have not investigated this in detail.Most of the above information comes from the following two links:
http://interactivebrokers.github.io/tws-api/market_data.html
http://interactivebrokers.github.io/tws-api/historical_data.html -
RE: Starting with Interactive Brokers (IB) - no data feed
@run-out Thanks - I'll give it a try
-
RE: Starting with Interactive Brokers (IB) - no data feed
@run-out . Thanks so much for taking the time to put together your comprehensive response!
I copied your code exactly, apart from the
clientId=
parameter which I did not include since I am using the IB demo account without a client ID. See below:
Unfortunately, I get the same result as I was getting before. IE I can receive forex bars but not stocks.
In other words this line:data0 = store.getdata(dataname="AAPL-STK-SMART-USD", **stockkwargs)
produces:
C:\Users\Owner\Miniconda3\envs\projectv36_2\python.exe C:/Users/Owner/TestTA-LIB/test_IB_connection.py Server Version: 76 TWS Time at connection:20190927 16:41:10 British Summer Time Process finished with exit code 0
Whereas replacing it with this line:
data0 = store.getdata(dataname="GBP.USD-CASH-IDEALPRO", **stockkwargs)
produces:
C:\Users\Owner\Miniconda3\envs\projectv36_2\python.exe C:/Users/Owner/TestTA-LIB/test_IB_connection.py Server Version: 76 TWS Time at connection:20190927 16:56:40 British Summer Time GBP.USD-CASH-IDEALPRO, 1, 2019-09-24, 1.25, 1.25, 1.25, 1.25, -1.00 GBP.USD-CASH-IDEALPRO, 2, 2019-09-24, 1.25, 1.25, 1.25, 1.25, -1.00 . . . GBP.USD-CASH-IDEALPRO, 32, 2019-09-24, 1.25, 1.25, 1.25, 1.25, -1.00 GBP.USD-CASH-IDEALPRO, 33, 2019-09-24, 1.25, 1.25, 1.25, 1.25, -1.00 Process finished with exit code 0
TWS is open and connected and the market is open. AAPL bars are showing up on the chart (after a refresh). See below:
I am using the latest TWS version:
I feel it should be something very obvious but I have no idea what!
-
RE: Starting with Interactive Brokers (IB) - no data feed
Still no luck accessing data. Is there some way of debugging the communication between IB and backtrader?
Code:
data = store.getdata(dataname='AAPL', secType='STK', exchange='SMART', currency='USD', historical=True, timeframe=bt.TimeFrame.Minutes, compression=5)
output:
C:\Users\Owner\Miniconda3\envs\projectv36_2\python.exe C:/Users/Owner/TestTA-LIB/test_IB.py Server Version: 76 TWS Time at connection:20190926 19:15:55 British Summer Time Process finished with exit code 0
code
data = store.getdata(dataname='AAPL', secType='STK', exchange='SMART', currency='USD', historical=False, timeframe=bt.TimeFrame.Minutes, compression=5)
output:
C:\Users\Owner\Miniconda3\envs\projectv36_2\python.exe C:/Users/Owner/TestTA-LIB/test_IB.py Server Version: 76 TWS Time at connection:20190926 19:17:03 British Summer Time ***** DATA NOTIF: DELAYED
-
RE: Starting with Interactive Brokers (IB) - no data feed
@backtrader I see. So in that case I presume I will only get historical stock data from IB when the market is open (and with
historical=True
) even though it is available from TWS at any time. -
RE: Starting with Interactive Brokers (IB) - no data feed
@backtrader Thanks. I will give
historical=True
a try.
I thought it wasn't needed if atodate
was specified (otherwise why does the first case work?) -
RE: Starting with Interactive Brokers (IB) - no data feed
I am at a loss! I have managed to get data from IB for forex pairs but I have failed with stocks, and futures. This applies for both backfill and realtime data
I have used the example contracts in
https://interactivebrokers.github.io/tws-api/basic_contracts.html
as a guide
I am using the demo account on IB
I have TWS running with no errors
Any suggestions would be welcome as I am running out of ideas!Here is my code for GBP.USD which works:
def run(args=None): cerebro = bt.Cerebro(stdstats=False) cerebro.addstrategy(St) store = bt.stores.IBStore(port=7497) data = store.getdata(dataname='GBP.USD', sectype='CASH', exchange='IDEALPRO', fromdate=datetime.datetime(2019, 9, 11), todate=datetime.datetime(2019, 9, 12), timeframe=bt.TimeFrame.Minutes, compression=5) cerebro.adddata(data) cerebro.run() if __name__ == '__main__': run()
C:\Users\Owner\Miniconda3\envs\projectv36_2\python.exe C:/Users/Owner/TestTA-LIB/test_IB.py Server Version: 76 TWS Time at connection:20190924 21:18:34 BST ***** DATA NOTIF: DELAYED 1,2019-09-11T21:15:00,1.23210,1.23269,1.23210,1.23269,-1.00000 2,2019-09-11T21:20:00,1.23269,1.23315,1.23260,1.23273,-1.00000 . . . . 33,2019-09-11T23:55:00,1.23341,1.23345,1.23330,1.23342,-1.00000 34,2019-09-12T00:00:00,1.23342,1.23342,1.23286,1.23307,-1.00000 Process finished with exit code 0
and here is my code for AAPL which doesn't work
data = store.getdata(dataname='AAPL', sectype='STK', exchange='SMART', currency='USD', fromdate=datetime.datetime(2019, 9, 11), todate=datetime.datetime(2019, 9, 12), timeframe=bt.TimeFrame.Minutes, compression=5)
C:\Users\Owner\Miniconda3\envs\projectv36_2\python.exe C:/Users/Owner/TestTA-LIB/test_IB.py Server Version: 76 TWS Time at connection:20190924 20:41:11 BST ***** DATA NOTIF: DELAYED
nothing appears even after waiting 30 mins with the program running!
-
RE: Starting with Interactive Brokers (IB) - no data feed
@backtrader thanks for you reply. I will do as you suggest, and do some more investigation using TWS directly to find out what the problem is.
-
RE: Starting with Interactive Brokers (IB) - no data feed
I think I might have found the problem
The relevant code in the blog (which was written in May of last year) isdata = store.getdata(dataname='TWTR', timeframe=bt.TimeFrame.Ticks)
However I think that IB may now require more info in the dataname string than just the ticker (eg also exchange information)
-
Starting with Interactive Brokers (IB) - no data feed
Hi
I am following the blog in Medium:
https://medium.com/@danjrod/interactive-brokers-in-python-with-backtrader-23dea376b2fc
So far I can get connection notification from TWS (ie the message: "TWS Time at connection...." but there is no data received. There are no run time errors or other messages.
I am using the code (without modification) in the blog section "Getting a data stream".
I am using a demo (paper trading) account on IB
Thanks