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

Problem plotting with data from IBstore and RSI



  • Hello,
    I'm logged into a paper account in Interactive Broker Workstation with user edemo.

    The following code works:

    import backtrader as bt
    from datetime import datetime
    
    class firstStrategy(bt.Strategy):
    
    	lb_ = 30
    	ub_ = 70
    	period_ = 21
    
    	count = 0
    	max_count = 50
    
    	def __init__(self):
    		count = 0
    
    	def next(self):
    		self.count = self.count + 1
    		if (self.count >= self.max_count):
    			cerebro.runstop()
    
    cerebro = bt.Cerebro()
    
    cerebro.addstrategy(firstStrategy)
    
    store = bt.stores.IBStore(port=7497)
    
    data = store.getdata(dataname='LABU',
    					 timeframe=bt.TimeFrame.Ticks)
    					 
    cerebro.resampledata(data, timeframe=bt.TimeFrame.Seconds,
    					 compression=5)
    cerebro.adddata(data)
    
    cerebro.broker.setcash(10000)
    
    cerebro.run()
    
    cerebro.plot(style='candlestick')
    

    and presents the following graph (notice that for some reason in shows the graph twice..):
    0_1542733682508_run_1.png

    when I add RSI, the plot stops working. I simple change class firstStrategy into the following:

    import backtrader as bt
    from datetime import datetime
    
    class firstStrategy(bt.Strategy):
    
    	lb_ = 30
    	ub_ = 70
    	period_ = 21
    
    	count = 0
    	max_count = 50
    
    	def __init__(self):
    		self.rsi = bt.indicators.RSI_SMA(self.data.close, period=self.period_, lowerband=self.lb_, upperband=self.ub_)
    		self.count = 0
    
    	def next(self):
    		if not self.position:
    			if self.rsi < 30:
    				self.buy()
    		else:
    			if self.rsi > 70:
    				self.sell()
    
    		self.count = self.count + 1
    		if (self.count >= self.max_count):
    			cerebro.runstop()
    
    cerebro = bt.Cerebro()
    cerebro.addstrategy(firstStrategy)
    store = bt.stores.IBStore(port=7497)
    data = store.getdata(dataname='LABU',
    					 timeframe=bt.TimeFrame.Ticks)
    cerebro.resampledata(data, timeframe=bt.TimeFrame.Seconds,
    					 compression=5)
    cerebro.adddata(data)
    cerebro.broker.setcash(10000)
    cerebro.run()
    cerebro.plot(style='candlestick')
    

    The plot fails, with the following message:

    Traceback (most recent call last):
      File "C:/Users/ilans/PycharmProjects/BAckTestRookies_getting_started/worked.py", line 39, in <module>
    	cerebro.plot(style='candlestick')
      File "C:\Python\lib\site-packages\backtrader\cerebro.py", line 991, in plot
    	start=start, end=end, use=use)
      File "C:\Python\lib\site-packages\backtrader\plot\plot.py", line 228, in plot
    	downinds=self.dplotsdown[ind])
      File "C:\Python\lib\site-packages\backtrader\plot\plot.py", line 392, in plotind
    	ax = masterax or self.newaxis(ind, rowspan=self.pinf.sch.rowsminor)
      File "C:\Python\lib\site-packages\backtrader\plot\plot.py", line 358, in newaxis
    	rowspan=rowspan, sharex=self.pinf.sharex)
      File "C:\Python\lib\site-packages\matplotlib\pyplot.py", line 1270, in subplot2grid
    	colspan=colspan)
      File "C:\Python\lib\site-packages\matplotlib\gridspec.py", line 70, in new_subplotspec
    	subplotspec = self[loc1:loc1+rowspan, loc2:loc2+colspan]
      File "C:\Python\lib\site-packages\matplotlib\gridspec.py", line 168, in __getitem__
    	[_normalize(k1, nrows), _normalize(k2, ncols)], (nrows, ncols))
      File "C:\Python\lib\site-packages\matplotlib\gridspec.py", line 160, in _normalize
    	raise IndexError("invalid index")
    IndexError: invalid index
    

    However, everything is fine and dandy when I'm running the same code, only instead of IBstore, I use Quandl:

    data = bt.feeds.Quandl(
    dataname='TWTR',
    fromdate=datetime(2016, 1, 1),
    todate=datetime(2016, 12, 31),
    reverse=False)
    

    Can you please assist me with this?
    Thanks!


  • administrators

    @ilan-seeder said in Problem plotting with data from IBstore and RSI:

    Can you please assist me with this?

    Quandl is not a live data feed. You are downloading live data and stopping the run, thus unbalancing buffers for the indicator.

    You probably want to do a historical download for IB to plot.