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/

    How to use historical and live data together

    General Code/Help
    1
    2
    175
    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.
    • James Liu
      James Liu last edited by

      Hello everyone,

      I want to use historical data and live data in my strategy like this.

      1. add historical data
      2. add live data feeder (I wrote a simulator feeder for LIVE)
      3. I hope MyInd calculates historical data firstly(warmup data), and then MyInd append LIVE data to historical data when it receives LIVE data
      4. MyInd calculates merged historical and LIVE data.
      5. MyStrategy can use MyInd results to do something.

      The following is my code and my questions:
      Questions:

      • In MyInd, I can't get LIVE data

      Code:

      import _thread
      from datetime import datetime
      import time
      import backtrader as bt
      import pandas as pd
      from backtrader import date2num
      from backtrader.feeds import DataBase
      
      
      class LiveDataStream(DataBase):
          def __init__(self):
              self.isOpen = True
              self.i = 0
              _thread.start_new_thread(self._timerstart, (1,))
      
          def start(self):
              print('in live data start')
              super(LiveDataStream, self).start()
      
          def _timerstart(self, n):
              print('in live data timer')
              while True:
                  if self.isOpen == False:
                      self.isOpen = True
                  time.sleep(n)
      
          def _load(self):
              if self.isOpen == True:
                  print('in live data load')
                  self.i += 1
                  self.lines.datetime[0] = date2num(datetime.now())
                  # Get the rest of the unpacked data
                  self.lines.open[0] = 100 + self.i
                  self.lines.high[0] = 120 + self.i
                  self.lines.low[0] = 80 + self.i
                  self.lines.close[0] = 90 + self.i
                  self.lines.volume[0] = 2342 + self.i
                  self.lines.openinterest[0] = 443 + self.i
                  self.isOpen = False
                  return True
              else:
                  return None
      
          def islive(self):
              '''Returns ``True`` to notify ``Cerebro`` that preloading and runonce
              should be deactivated'''
              print('in live data islive')
              return True
      
          def stop(self):
              '''Stops and tells the store to stop'''
              print('in live data stop')
              super().stop()
      
      
      class MyInd(bt.Indicator):
          lines = ('ma',)
          params = (('period', 5),)
          plotinfo = dict(subplot=False)
      
          def __init__(self):
              self.l.ma = bt.ind.SMA(self.data, period=self.p.period)
              super(MyInd, self).__init__()
      
          def prenext(self):
              print("in MyInd prenext")
      
          def next(self):
              self.l.ma = bt.ind.SMA(self.data, period=self.p.period)
              print("in MyInd data0 next: %s, %.2f" % (self.data.datetime.date(0), self.data.close[0]))
              # print("in MyInd data1 next: %s, %.2f" % (self.data1.datetime.date(0), self.data1.close[0]))
      
      
      class MyStrategy(bt.Strategy):
          def __init__(self):
              self.my_ind = MyInd()
      
          def next(self):
              print('in MyStrategy data0 next: date: %s, close: %.2f' % (self.data.datetime.date(0),
                                                                         self.data.close[0]))
              print('in MyStrategy data1 next: date: %s, close: %.2f' % (self.data1.datetime.date(0),
                                                                         self.data1.close[0]))
      
          def notify_data(self, data, status, *args, **kwargs):
              if status == data.LIVE:
                  print("live data")
              elif status == data.DELAYED:
                  print("DELAYED data")
      
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(MyStrategy)
      
      history_price = pd.read_csv('2006-day-001.txt', parse_dates=['Date']).set_index('Date')
      data0 = bt.feeds.PandasData(dataname=history_price)
      cerebro.adddata(data0)
      
      cerebro.adddata(LiveDataStream())
      
      cerebro.run()
      
      
      1 Reply Last reply Reply Quote 0
      • James Liu
        James Liu last edited by

        Thank you.

        And, this is the output:

        in live data islive
        in live data start
        in live data load
        in MyInd prenext
        in MyInd prenext
        in MyInd prenext
        in MyInd prenext
        in live data timer
        in MyInd data0 next: 2006-12-07, 4018.69
        in MyInd data0 next: 2006-12-08, 4019.89
        in MyInd data0 next: 2006-12-11, 4052.89
        in MyInd data0 next: 2006-12-12, 4059.74
        in MyInd data0 next: 2006-12-13, 4094.33
        in MyInd data0 next: 2006-12-14, 4118.84
        in MyInd data0 next: 2006-12-15, 4140.66
        in MyInd data0 next: 2006-12-18, 4130.06
        in MyInd data0 next: 2006-12-19, 4100.48
        in MyInd data0 next: 2006-12-20, 4118.54
        in MyInd data0 next: 2006-12-21, 4112.10
        in MyInd data0 next: 2006-12-22, 4073.50
        in MyInd data0 next: 2006-12-27, 4134.86
        in MyInd data0 next: 2006-12-28, 4130.66
        in MyInd data0 next: 2006-12-29, 4119.94
        in MyInd data0 next: 2006-12-29, 4119.94
        in MyStrategy data0 next: date: 2006-12-29, close: 4119.94, ma: 4114.21
        in MyStrategy data1 next: date: 2021-11-23, close: 91.00, ma: 4114.21
        in live data load
        in MyInd data0 next: 2006-12-29, 4119.94
        in MyStrategy data0 next: date: 2006-12-29, close: 4119.94, ma: 4114.21
        in MyStrategy data1 next: date: 2021-11-23, close: 92.00, ma: 4114.21
        
        1 Reply Last reply Reply Quote 0
        • 1 / 1
        • First post
          Last post
        Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors