For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Not able to fetch live data from IB but works with Yahoofinance



  • Hello!
    Im trying to fetch data from Interactive Brokers, however it simply throws me the following error when I try to plot the data

    (base) D:\backtrader projects>D:\Python\python.exe "d:\backtrader projects\strategies\testytest.py"
    Server Version: 76
    TWS Time at connection:20190421 18:33:13 CET
    21-Apr-19 18:33:19 DEBUG     CACHEDIR=C:\Users\Tomas_000\.matplotlib
    21-Apr-19 18:33:19 DEBUG     Using fontManager instance from C:\Users\Tomas_000\.matplotlib\fontlist-v300.json
    Traceback (most recent call last):
     File "d:\backtrader projects\strategies\testytest.py", line 64, in <module>
       cerebro.plot()
     File "D:\Python\lib\site-packages\backtrader\cerebro.py", line 996, in plot
       plotter.show()
     File "D:\Python\lib\site-packages\backtrader\plot\plot.py", line 795, in show
       self.mpyplot.show()
    AttributeError: 'Plot_OldSync' object has no attribute 'mpyplot' 
    

    However if I try to run the exact same code but instead comment out the IBData feed and insert a yahoo datafeed it will work and plot the graph as wanted.

    My code is

    from __future__ import (absolute_import, division, print_function, unicode_literals)
    
    import backtrader as bt
    import datetime  # For datetime objects
    import os.path  # To manage paths
    import sys  # To find out the script name (in argv[0])
    import math
    from backtrader.indicators import Indicator, MovAv, RelativeStrengthIndex, Highest, Lowest
    
    class TestStrategy(bt.Strategy):
      
       def __init__(self):
           # Keep a reference to the "close" line in the data[0] dataseries
           self.dataclose = self.datas[0].close
    
       def next(self):
           print(self.dataclose)
    
    if __name__ == '__main__':
       #Variable for our starting cash
       startcash = 10000
    
       #Create an instance of cerebro
       cerebro = bt.Cerebro()
    
       #Add the strategy
       cerebro.addstrategy(TestStrategy)
    
       # Add Sizer
       #cerebro.addsizer(LongOnly)
    
       # Activate optimization
       StrategyOptimizationEnabled = False
    
       #Add our strategy
       if StrategyOptimizationEnabled == True:
           cerebro.optstrategy(TestStrategy, maperiod=range(14,21)) 
    
       #Get Apple data from Yahoo Finance. THIS ACTUALLY WORKS BUT IBDATA DOESNT WORK
    #    data = bt.feeds.YahooFinanceData(
    #        dataname="AAPL",
    #        fromdate = datetime.datetime(2019,1,1),
    #        todate = datetime.datetime(2020,1,1)
    #        )
    
       #Get Twitter data from Yahoo Finance.
       store = bt.stores.IBStore(port=7497)
       data = store.getdata(dataname='TWTR', timeframe=bt.TimeFrame.Ticks, fromdate=datetime.datetime(2019,1,1), todate=datetime.datetime(2020,1,1))    
       cerebro.resampledata(data, timeframe=bt.TimeFrame.Seconds, compression=10)
    
       #Add the data to Cerebro
       #cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=15)
    
       # Set our desired cash start
       cerebro.broker.setcash(startcash)
    
       # Run over everything
       opt_runs = cerebro.run()
    
       # Set the commission
       cerebro.broker.setcommission(commission=0.005)
    
       # Plot the result
       cerebro.plot()
    

  • administrators

    Incredible feat. Just let us know how you get Yahoo to give you ticks from 2019-01-01 to 2020-01-01.


  • administrators

    You may of course consider:

    • Am I producing debugging information?
    • Have I activated the debug mode of the Interactive Brokers data?
    • Is 10-seconds asking for too many when looking backwards to 2019-01-01?
    • Am I attempting a historical download or do I want to trade real-time?
    • Am I talking to the Fake-Data Demo, Paper Trading Account or Live Trading Account?
    • Do I have the right data permissions?

    That's the minimum information you should have at hand. Algotrading involves engineering and being thorough. And sorry, comparing a Yahoo download with connecting to a live broker (even if in Fake Data mode) is not really a comparison.


Log in to reply
 

});