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 pass on a list variable containing trading holidays to MyCalendar(bt.TradingCalendar) to overcome resampling problem?

    Indicators/Strategies/Analyzers
    1
    1
    59
    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.
    • M
      mahaaveer last edited by

      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,
          }
      
      1 Reply Last reply Reply Quote 0
      • 1 / 1
      • First post
        Last post
      Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors