Compression using daily and minute timeframes unsuccessful during replaydata()
-
Tried running the illustration given in the docs.
It works well for destination timeframe 'weekly', compression 1
But fails for timeframes 'daily' and 'minute' using either compression.Here's the code that was executed.
Note:
-The handy dictionary for timeframe conversion is updated for 'minute'from __future__ import (absolute_import, division, print_function, unicode_literals) import argparse import backtrader as bt import backtrader.feeds as btfeeds import backtrader.indicators as btind from datetime import datetime class SMAStrategy(bt.Strategy): params = ( ('period', 10), ('onlydaily', False), ) def __init__(self): self.sma = btind.SMA(self.data, period=self.p.period) def start(self): self.counter = 0 def prenext(self): self.counter += 1 print('prenext len %d - counter %d' % (len(self), self.counter)) def next(self): self.counter += 1 print('---next len %d - counter %d' % (len(self), self.counter)) def runstrat(): args = parse_args() # Create a cerebro entity cerebro = bt.Cerebro(stdstats=False) cerebro.addstrategy( SMAStrategy, # args for the strategy period=args.period, ) # Load the Data datapath = args.dataname or "nifty19-20.csv" data = btfeeds.BacktraderCSVData(dataname=datapath, fromdate=datetime.now().replace(year=2020, month=8, day=17, hour=0, minute=0, second=0), timeframe=bt.TimeFrame.Minutes) # Handy dictionary for the argument timeframe conversion tframes = dict( minute=bt.TimeFrame.Minutes, daily=bt.TimeFrame.Days, weekly=bt.TimeFrame.Weeks, monthly=bt.TimeFrame.Months) # First add the original data - smaller timeframe cerebro.replaydata(data, timeframe=tframes['minute'], compression=5) # Run over everything cerebro.run() # Plot the result cerebro.plot(style='bar') def parse_args(): parser = argparse.ArgumentParser( description='Pandas test script') parser.add_argument('--dataname', default='', required=False, help='File Data to Load') parser.add_argument('--timeframe', default='minute', required=False, choices=['minute', 'daily', 'weekly', 'monhtly'], help='Timeframe to resample to') parser.add_argument('--compression', default=10, required=False, type=int, help='Compress n bars into 1') parser.add_argument('--period', default=10, required=False, type=int, help='Period to apply to indicator') return parser.parse_args() if __name__ == '__main__': runstrat()
Output: ---next len 1476 - counter 1476 ---next len 1477 - counter 1477 ---next len 1478 - counter 1478 ---next len 1479 - counter 1479 ---next len 1480 - counter 1480 ---next len 1481 - counter 1481 ---next len 1482 - counter 1482 ---next len 1483 - counter 1483 ---next len 1484 - counter 1484 ---next len 1485 - counter 1485 ---next len 1486 - counter 1486 ---next len 1487 - counter 1487 ---next len 1488 - counter 1488 ---next len 1489 - counter 1489 ---next len 1490 - counter 1490 ---next len 1491 - counter 1491 ---next len 1492 - counter 1492 ---next len 1493 - counter 1493 ---next len 1494 - counter 1494 ---next len 1495 - counter 1495 ---next len 1496 - counter 1496 ---next len 1497 - counter 1497 ---next len 1498 - counter 1498 ---next len 1499 - counter 1499 ---next len 1500 - counter 1500
Am I missing something?
-
I faced a similar problem:(
I guess the issue is how backtrader comprehends time frame of the smaller data feed provided. Unless explicitly mentioned, it defaults todaily
and hence it can not build a bar with a tf smaller than daily. -
Already done that. Still no Luck.
-
@Soham-Jain said in Compression using daily and minute timeframes unsuccessful during replaydata():
data = btfeeds.BacktraderCSVData(dataname=datapath, fromdate=datetime.now().replace(year=2020, month=8, day=17, hour=0, minute=0, second=0), timeframe=bt.TimeFrame.Minutes)
@Soham-Jain I think you need to give compression along with timeframe. say compression=1 to indicate its 1 minute timeframe. BT by default takes it as 1 day but not sure if it takes 1 minutes for bt.TimeFrame.Minutes.
-
@rajanprabu said in Compression using daily and minute timeframes unsuccessful during replaydata():
te timefr
I tried it with the additional param
compression=1
but the result is still the same.from __future__ import (absolute_import, division, print_function, unicode_literals) import argparse import backtrader as bt import backtrader.feeds as btfeeds import backtrader.indicators as btind from datetime import datetime class SMAStrategy(bt.Strategy): params = ( ('period', 10), ('onlydaily', False), ) def __init__(self): pass def start(self): self.counter = 0 def prenext(self): self.counter += 1 print(f'{self.datas[0].datetime.datetime(0)} prenext len %d - counter %d' % (len(self), self.counter)) def next(self): self.counter += 1 print(f'{self.datas[0].datetime.datetime(0)} ---next len %d - counter %d' % (len(self), self.counter)) def runstrat(): args = parse_args() # Create a cerebro entity cerebro = bt.Cerebro(stdstats=False) cerebro.addstrategy( SMAStrategy, # args for the strategy period=args.period, ) # Load the Data datapath = args.dataname or "nifty19-20.csv" data = btfeeds.BacktraderCSVData(dataname=datapath, fromdate=datetime.now().replace(year=2020, month=8, day=17, hour=0, minute=0, second=0), timeframe=bt.TimeFrame.Minutes, compression=1) # Handy dictionary for the argument timeframe conversion tframes = dict( minute=bt.TimeFrame.Minutes, daily=bt.TimeFrame.Days, weekly=bt.TimeFrame.Weeks, monthly=bt.TimeFrame.Months) # First add the original data - smaller timeframe cerebro.replaydata(data, timeframe=tframes['daily'], compression=1) # Run over everything cerebro.run() # Plot the result cerebro.plot(style='bar') def parse_args(): parser = argparse.ArgumentParser( description='Pandas test script') parser.add_argument('--dataname', default='', required=False, help='File Data to Load') parser.add_argument('--timeframe', default='minute', required=False, choices=['minute', 'daily', 'weekly', 'monhtly'], help='Timeframe to resample to') parser.add_argument('--compression', default=10, required=False, type=int, help='Compress n bars into 1') parser.add_argument('--period', default=10, required=False, type=int, help='Period to apply to indicator') return parser.parse_args() if __name__ == '__main__': runstrat()
2020-08-20 23:59:59.999989 ---next len 1482 - counter 1482 2020-08-20 23:59:59.999989 ---next len 1483 - counter 1483 2020-08-20 23:59:59.999989 ---next len 1484 - counter 1484 2020-08-20 23:59:59.999989 ---next len 1485 - counter 1485 2020-08-20 23:59:59.999989 ---next len 1486 - counter 1486 2020-08-20 23:59:59.999989 ---next len 1487 - counter 1487 2020-08-20 23:59:59.999989 ---next len 1488 - counter 1488 2020-08-20 23:59:59.999989 ---next len 1489 - counter 1489 2020-08-20 23:59:59.999989 ---next len 1490 - counter 1490 2020-08-20 23:59:59.999989 ---next len 1491 - counter 1491 2020-08-20 23:59:59.999989 ---next len 1492 - counter 1492 2020-08-20 23:59:59.999989 ---next len 1493 - counter 1493 2020-08-20 23:59:59.999989 ---next len 1494 - counter 1494 2020-08-20 23:59:59.999989 ---next len 1495 - counter 1495 2020-08-20 23:59:59.999989 ---next len 1496 - counter 1496 2020-08-20 23:59:59.999989 ---next len 1497 - counter 1497 2020-08-20 23:59:59.999989 ---next len 1498 - counter 1498 2020-08-20 23:59:59.999989 ---next len 1499 - counter 1499 2020-08-20 23:59:59.999989 ---next len 1500 - counter 1500
:(
I still can;t figure out where exactly am I going wrong!
Btw, Thanks Rajan for the help. -
might be just easier to compress it yourself with Pandas
-
- But will it simulate replay data?
pandas
will just help resample the minute data to a higher timeframe. Am I right?
-
@Soham-Jain I haven't worked on resampling for a while but I'm quite sure pandas should support downsampling. That is a very common data science task.
-
try to add
sessionstart
andsessionend
parameters. seems logical to have them. -
try to add sessionstart and sessionend parameters. seems logical to have them.
It worked. Can't thank you enough @ab_trader