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 indicate that data feed is live data

    General Code/Help
    live data live trading custom data fee
    3
    3
    1030
    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.
    • S
      Shiva last edited by

      Hello, I am new to python and also to Backtrader, I am able to develop python program to fetch live feeds from alphavantage and inserting the feed to MySQL database. I can fetch the data from MySQL also and use it in backtrader for backtesting. But I need cerebro to consider the same feed as live. My code below shows demo implementation.

      class MySQLdata(DataBase):
          params = (
              ('dbHost', None),
              ('dbUser', None),
              ('dbPwd', None),
              ('dbPort', '3306'),
              ('dbName', None),
              ('ticker', "SBIN.NS"),
              ('historical', False),
              ('sessionend', None),
              ('fromdate', datetime.datetime.min),
              ('todate', datetime.datetime.max),
              ('name', ""),
              )
      
          def __init__(self):
              self.engine = create_engine('mysql+mysqlconnector://'+ self.p.dbUser + ':' + self.p.dbPwd + '@' + self.p.dbHost +
                                          ':' + self.p.dbPort + '/'+self.p.dbName
                                          + '?charset=utf8mb4', echo=False)
      
          def start(self):
              self.conn = self.engine.connect()
              self.result = self.conn.execute(
              
                         "SELECT `price_date`,`open_price`,`high_price`,`low_price`,`close_price`,`volume` FROM `daily_price_in` WHERE `symbol_id` = 'SBIN.NS' " + " AND `price_date` between '" + self.p.fromdate.strftime(
                      "%Y-%m-%d") + "' and '" + self.p.todate.strftime("%Y-%m-%d") + "' ORDER BY `price_date` ASC")
      
      
          def stop(self):
              self.engine.dispose()
      
          def _load(self):
              one_row = self.result.fetchone()
              if one_row is None:
                  return False
      
              self.lines.datetime[0] = date2num(one_row[0]) # for intraday data, time also need to be combined here.
              self.lines.open[0] = float(one_row[1])
              self.lines.high[0] = float(one_row[2])
              self.lines.low[0] = float(one_row[3])
              self.lines.close[0] = float(one_row[4])
              self.lines.volume[0] = int(one_row[5])
              self.lines.openinterest[0] = -1
              return True
      
          def islive(self):
              return True
      
      
      if __name__ == "__main__":
      
      
          data = MySQLdata(dbHost = '127.0.0.1',
                           dbUser = 'root',
                           dbPwd = 'bala',
                           dbName = 'securities_master',
                           sessionstart=datetime.time(9, 0),
                           sessionend=datetime.time(16, 0)
                           )
      
          # --- use case for Alphavantage data feed ---
      
          cerebro = bt.Cerebro()
          cerebro.addstrategy(TestStrategy)
          cerebro.adddata(data)
      
          cerebro.broker.setcash(100000.00)
      
          # Add a FixedSize sizer according to the stake
          cerebro.addsizer(bt.sizers.FixedSize, stake=100)
      
          cerebro.run(live=True)
          # cerebro.run()
      
          # cerebro.plot(style='candle', numfigs=1, volup='green', voldown='red',
          #              voltrans=75.0, voloverlay=False)
      
      def start(self):
          self.conn = self.engine.connect()
          self.result = self.conn.execute(
              "SELECT `price_date`,`open_price`,`high_price`,`low_price`,`close_price`,`volume` FROM `daily_price_in` WHERE `symbol_id` = 'SBIN.NS' " + " AND `price_date` between '" + self.p.fromdate.strftime(
                  "%Y-%m-%d") + "' and '" + self.p.todate.strftime("%Y-%m-%d") + "' ORDER BY `price_date` ASC")
      
      
      def stop(self):
          self.engine.dispose()
      
      def _load(self):
          one_row = self.result.fetchone()
          if one_row is None:
              return False
      
          self.lines.datetime[0] = date2num(one_row[0]) # for intraday data, time also need to be combined here.
          self.lines.open[0] = float(one_row[1])
          self.lines.high[0] = float(one_row[2])
          self.lines.low[0] = float(one_row[3])
          self.lines.close[0] = float(one_row[4])
          self.lines.volume[0] = int(one_row[5])
          self.lines.openinterest[0] = -1
          return True
      
      def islive(self):
          return True
      

      if name == "main":

      data = MySQLdata(dbHost = '127.0.0.1',
                       dbUser = 'xxxx',
                       dbPwd = '****',
                       dbName = 'securities_master',
                       sessionstart=datetime.time(9, 0),
                       sessionend=datetime.time(16, 0)
                       )
      
      
      
      cerebro = bt.Cerebro()
      cerebro.addstrategy(TestStrategy)
      cerebro.adddata(data)
      
      cerebro.run(live=True)
      ```
      

      I need to modify _load method to return None so as to indicate cerebro that feed is live and wait till the MySql DB is filled with data and it is once again called in cerebro.

      Thanks

      1 Reply Last reply Reply Quote 0
      • S
        sp last edited by

        Hi Shiva,

        Any progress on this? I'm trying to do the exact same thing, I've managed to set up everything else and the datafeed is the last thing missing.

        Please let me know, it would be greatly appreciated and I don't mind collaborating for anything else

        Thanks!

        R 1 Reply Last reply Reply Quote 0
        • R
          ronnyBravo @sp last edited by

          @sp I'm looking into same thing right now and would like to see if you guys figured it out.

          Can you drop me a line if you had any progress with this.

          Thanks!

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors