Thank you so much! That solved my problem. To thoes who has the same problem, remember also to improt timeframe from backtrader!

Latest posts made by Quanliang Xie
-
RE: Datetime.time won't give me the correct time.
-
Passing data into data feed one by one
Hi, I want to test a single strategy using a series of csv files from a folder. I tried to pass them one by one into the data feed using cerebro. adddata(), but the data just keep adding together, which at a single time, it will be multiple data point.
All I can find online is passing into multiple data feed at the same time, but that is not what I want. I want to test my strategy in one data feed, then move on to next and so on.
Is there a way to put the datas into a queue and pass them into the cerebro one by one?
Here is my code:
if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() path = "/mnt/c/users/liang/desktop/backtesting/small_data/*.csv" for datapath in glob.glob(path): data = bt.feeds.GenericCSVData( dataname= datapath, dtformat= ('%Y-%m-%d %H:%M:%S UTC'), nullvalue=0.0, timeframe = TimeFrame.Minutes, datetime=0, time=-1, high=-1, low=-1, open=-1, close=1, #this is actully popularity volume=-1, openinterest=-1) # Add the Data Feed to Cerebro cerebro.adddata(data) cerebro.addstrategy(TestStrategy, Name=(datapath[50:-15])) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
What I did here is not efficient at all. I restarted cerebro multiple times. Is there a way to avioding this?
Thank you so much for the help!
-
Datetime.time won't give me the correct time.
Hi, I am new to the backtrader comminity. I am currently having trouble getting the time using datetime.time or datetime.datetime. Below is my code and result.
class TestStrategy(bt.Strategy): params = (('myparam', 27), ('exitbars', 5), ('period',24), ('pop_change_1h', 50), ('pop_change_4h', 50), ('pop_change_24h', 50), ('pop_lim_1h', 50), ('pop_lim_4h', 50), ('pop_lim_24h', 50), ) def log(self, txt, dt=None): dt = dt or self.datas[0].datetime.datetime(0) print('%s, %s' %(dt.isoformat(), txt)) def __init__(self,Name): # Keep a reference to the popularity line in the data[0] dataseries self.popularity = self.datas[0].close self.name = Name self.s = yprice(self.name,'') def next(self): self.log(self.datas[0].datetime.datetime(0)) if __name__ == '__main__': # Create a cerebro entity cerebro = bt.Cerebro() path = "/mnt/c/users/liang/desktop/backtesting/small_data/*.csv" for datapath in glob.glob(path): data = bt.feeds.GenericCSVData( dataname= datapath, dtformat= ('%Y-%m-%d %H:%M:%S UTC'), nullvalue=0.0, datetime=0, time=-1, high=-1, low=-1, open=-1, close=1, #this is actully popularity volume=-1, openinterest=-1) #print(datapath[50:-15]) # Add the Data Feed to Cerebro cerebro.adddata(data) cerebro.addstrategy(TestStrategy, Name=(datapath[50:-15])) print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue()) cerebro.run() print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
Result:
My csv format is
I always get 23:59:59 as my time. Can someone show me how to get the correct time that I can use in my strategy? I am wondering if I need to use a custom Data feed Class. Please correct me if I am worng. Thanks!