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 pass on a list variable containing trading holidays to MyCalendar(bt.TradingCalendar) to overcome resampling problem?
-
I am resampling daily data to weekly using cerebro.resampledata()
Since some of the fridays are holidays, it needs to be added to the calendar for resampling to work as intended.In main function, i calculate those missing fridays and have a list ready:
stockdf_daily = pd.read_csv(stock_datapath, dayfirst=True, parse_dates=True, index_col=0) stock_data_daily = PandasData(dataname=stockdf_daily) stockdf_daily.index = pd.to_datetime(stockdf_daily.index) miss_dates = pd.date_range( start=stockdf_daily.index.min(), end=stockdf_daily.index.max()).difference(stockdf_daily.index) miss_dates = miss_dates.tolist() missed_fridays = [] for date in miss_dates: day = date.weekday() if day == 4: date = date.date() missed_fridays.append(date) cerebro.adddata(stock_data_daily) cerebro.resampledata(stock_data_daily, timeframe=bt.TimeFrame.Weeks, compression=1) cerebro.addcalendar(MyCalendar)
Manually adding some 50+ days seems counter productive. How do i pass on this list variable to params of MyCalendar ?
class MyCalendar(bt.TradingCalendar): global missed_fridays params = { 'open': time(9, 15), 'close': time(15, 30), 'holidays': missed_fridays, }