How to indicate that data feed is live data
-
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
-
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!
-
@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!