- ago
Happy Thanksgiving to everyone! On that note, I was looking for a way to test for seasonal performance around holidays. Could not find anything in this forum or in the help. I'm sure someone has tested this and has some ideas.
Thank you!
0
33
Solved
2 Replies

Reply

Bookmark

Sort
Cone8
 ( 6.36% )
- ago
#1
Here's something that I just whipped up that you can play with (of course it took longer than expected).

Buy X days before a holiday and sell Y days after - optimize away!

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using System.Collections.Generic; using System.Linq; namespace WealthScript8 {       public class HolidaySeasonality : UserStrategyBase    {       Parameter _daysBefore;       Parameter _daysAfter;       DateTime _holidate;              public HolidaySeasonality()       {          _daysBefore = AddParameter("Enter Days Before", ParameterType.Int32, 1, 1, 10, 1);          _daysAfter = AddParameter("Exit Days After", ParameterType.Int32, 1, 0, 10, 1);       }       public override void Initialize(BarHistory bars)       {          StartIndex = 1;       }       public override void Execute(BarHistory bars, int idx)       {             if (!HasOpenPosition(bars, PositionType.Long))          {             int days = HolidayCalculator.SessionsToHoliday(bars, bars.DateTimes[idx]);             if (days > -1 && days <= _daysBefore.AsInt)             {                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);                _holidate = bars.AddTradingDays(idx, days + 1);                            }          }          else          {             if (bars.DateTimes[idx] >= _holidate)             {                int days = HolidayCalculator.SessionSinceHoliday(bars, bars.DateTimes[idx]);                if (days > -1 && days > _daysAfter.AsInt)                   ClosePosition(LastPosition, OrderType.Market);             }          }       }    }    public static class HolidayCalculator    {       //returns the number of days to the next holiday or -1 if none       public static int SessionsToHoliday(BarHistory bars, DateTime specifiedDate)       {          specifiedDate = specifiedDate.Date;          if (bars.Market.HolidayDates == null || bars.Market.HolidayDates.Count == 0)             throw new ArgumentException("The list of holiday dates cannot be null or empty.", nameof(bars.Market.HolidayDates));          List<DateTime> holidayDates = bars.Market.HolidayDates.OrderBy(date => date).ToList();          var nextHoliday = holidayDates.FirstOrDefault(date => date > specifiedDate);          return nextHoliday != default ? bars.TradingDaysBetweenDates(specifiedDate, nextHoliday) : -1;       }       //returns the number of days from the previous holiday or -1 if none       public static int SessionSinceHoliday(BarHistory bars, DateTime specifiedDate)       {          specifiedDate = specifiedDate.Date;          if (bars.Market.HolidayDates == null || bars.Market.HolidayDates.Count == 0)             throw new ArgumentException("The list of holiday dates cannot be null or empty.", nameof(bars.Market.HolidayDates));          List<DateTime> holidayDates = bars.Market.HolidayDates.OrderBy(date => date).ToList();          var lastHoliday = holidayDates.LastOrDefault(date => date < specifiedDate);          return lastHoliday != default ? bars.TradingDaysBetweenDates(lastHoliday, specifiedDate) : -1;       }    } }
2
Best Answer
- ago
#2
Thank you, Cone! That was more than i expected! I will probably have more questions as i get into this but it's a great start.
0

Reply

Bookmark

Sort