Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Empty plot in PyCharm

    General Code/Help
    plot pycharm
    1
    1
    14
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R
      rajah9 last edited by

      I am able to resample hourly bars, but my plot remains empty. Using PyCharm, I have tried plot(iplot=True) and plot(iplot=False).

      Here's the code:

      from __future__ import absolute_import, division, print_function, unicode_literals
      import backtrader as bt
      import backtrader.stores.ibstore as ibstore
      import datetime
      import logging
      logging.basicConfig(level=logging.DEBUG)
      logger = logging.getLogger(__name__)
      
      _CLIENTID = 100
      
      class St(bt.Strategy):
          def __init__(self):
              self.sma = bt.indicators.SMA(self.data)
      
          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]))
              logger.debug(','.join(txt))
      
          data_live = False
      
          def notify_data(self, data, status, *args, **kwargs):
              print('*' * 5, 'DATA Notification:', data._getstatusname(status), *args)
              if status == data.LIVE:
                  self.data_live = True
      
          def next(self):
              self.logdata()
              if not self.data_live:
                  return
      
      _TICKER = "TSLA-STK-SMART-USD"
      _FROMDATE = datetime.datetime(2021,1,4)
      _TODATE = datetime.datetime(2021,1,8)
      _HAS_STATS = False
      
      def run(args=None):
          cerebro = bt.Cerebro(stdstats=_HAS_STATS)
      
          store = ibstore.IBStore(host="127.0.0.1", port=7497, clientId= _CLIENTID )
          cerebro.broker = store.getbroker()
          stockkwargs = dict(
              timeframe=bt.TimeFrame.Minutes,
              compression=5,
              rtbar=False,  # use RealTime 5 seconds bars
              historical=True,  # only historical download
              qcheck=0.5,  # timeout in seconds (float) to check for events
              fromdate=_FROMDATE,  # get data from..
              todate=_TODATE,  # get data to..
              latethrough=False,  # let late samples through
              tradename=None  # use a different asset as order target
          )
      
          data0 = store.getdata(dataname=_TICKER, **stockkwargs)
          cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=60)
      
          cerebro.addstrategy(St)
          cerebro.run()
          cerebro.plot(iplot=True, stdstats=_HAS_STATS) # Same behavior whether iplot=True or =False
      
      if __name__ == "__main__":
          run()
      
      

      Here's the console output:

      Server Version: 76
      TWS Time at connection:20210220 08:41:53 EST
      ***** DATA Notification: DELAYED
      DEBUG:__main__:30,2021-01-05T17:00:00,749.65,754.40,735.11,753.21,6011.00
      DEBUG:__main__:31,2021-01-05T18:00:00,753.39,754.18,751.60,753.34,1126.00
      DEBUG:__main__:32,2021-01-05T19:00:00,753.32,753.32,750.49,752.90,1179.00
      (... deleted ...)
      DEBUG:__main__:62,2021-01-07T17:00:00,829.40,834.72,816.04,833.00,5338.00
      DEBUG:__main__:63,2021-01-07T18:00:00,832.40,833.01,829.02,830.36,1797.00
      DEBUG:__main__:64,2021-01-07T19:00:00,830.59,831.95,828.81,829.50,1086.00
      ***** DATA Notification: DISCONNECTED
      DEBUG:matplotlib:CACHEDIR=C:\Users\Owner\.matplotlib
      DEBUG:matplotlib.font_manager:Using fontManager instance from C:\Users\Owner\.matplotlib\fontlist-v330.json
      DEBUG:matplotlib.pyplot:Loaded backend TkAgg version unknown.
      

      It displays a Figure popup, but it is always an empty figure, never plots, and has to be closed. I was expecting to show an hourly plot with an SMA line.

      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
      $(document).ready(function () { app.coldLoad(); }); }