Changes for MGSHBacktest model.

Code cleanup.
This commit is contained in:
2025-02-10 07:57:00 -05:00
parent 36b10b4824
commit 0db4473632
16 changed files with 378 additions and 78 deletions

View File

@@ -1,8 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using MarketData.Utils;
using MarketData.DataAccess;
// Filename: DateGenerator.cs
// Author:Sean Kessler
@@ -10,6 +7,7 @@ using MarketData.DataAccess;
namespace MarketData.Utils
{
/// <summary>DateGenerator - Generate Historical Dates</summary>
/// "BusinessDay" is analagous to "MarketDay". To qualify as a business day the date must not be a weekend or a market holiday
[Serializable]
public class DateGenerator
{
@@ -19,6 +17,8 @@ namespace MarketData.Utils
public DateGenerator()
{
}
/// <summary>GetCurrMonthStart - Finds the first business day of the previous month</summary>
/// <returns>DateTime</returns>
public DateTime GetPrevMonthStart(DateTime dateTime)
{
DateTime startDate = new DateTime(dateTime.Year, dateTime.Month, 1);
@@ -26,6 +26,8 @@ namespace MarketData.Utils
startDate = GetNextBusinessDay(startDate);
return startDate;
}
/// <summary>GetCurrMonthStart - Finds the first business day of the current month</summary>
/// <returns>DateTime</returns>
public DateTime GetCurrMonthStart(DateTime dateTime)
{
DateTime startDate = new DateTime(dateTime.Year, dateTime.Month, 1);
@@ -46,7 +48,7 @@ namespace MarketData.Utils
while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Subtract(oneDay);
return asOf.Date;
}
/// <summary>FindNextBusinessDays - Finds following business day</summary>
/// <summary>FindForwardBusinessDay - Finds following business day going daysForward days</summary>
/// <returns>DateTime</returns>
public DateTime FindForwardBusinessDay(DateTime asOf,int daysForward)
{
@@ -56,6 +58,17 @@ namespace MarketData.Utils
}
return asOf.Date;
}
/// <summary>FindPastBusinessDay - Finds previous business day going daysPast days</summary>
/// <returns>DateTime</returns>
public DateTime FindPastBusinessDay(DateTime asOf,int daysPast)
{
for (int index = 0; index < daysPast; index++)
{
asOf = FindPrevBusinessDay(asOf);
}
return asOf.Date;
}
/// <summary>FindNextBusinessDay - Finds following nth business day</summary>
/// <returns>DateTime</returns>
public DateTime FindNextBusinessDay(DateTime asOf,int days)
@@ -77,6 +90,7 @@ namespace MarketData.Utils
while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Add(oneDay);
return asOf.Date;
}
/// <summary>GetPrevBusinessDay - Gets previous business day</summary>
/// <note>If the given date is a business day then this method will return the given date</note>
/// <returns>None</returns>
@@ -86,6 +100,7 @@ namespace MarketData.Utils
while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Subtract(oneDay);
return asOf.Date;
}
/// <summary>GetNextBusinessDay - Gets next business day</summary>
/// <note>If the given date is a business day then this method will return the given date</note>
/// <returns>None</returns>
@@ -95,6 +110,7 @@ namespace MarketData.Utils
while(IsWeekend(asOf)||IsHoliday(asOf))asOf=asOf.Add(oneDay);
return asOf.Date;
}
/// <summary>GetNextDay - Gets next business day</summary>
/// <note>If the given date is a business day then this method will return the given date</note>
/// <returns>None</returns>
@@ -102,6 +118,7 @@ namespace MarketData.Utils
{
return (asOf.Add(Utility.OneDay)).Date;
}
/// <summary>GetPrevFriday - Gets date of prior friday</summary>
/// <note>Get the date of the previous friday - if previous Friday is holiday will seek previous business day</note>
/// <returns>None</returns>
@@ -124,6 +141,7 @@ namespace MarketData.Utils
}
throw new Exception("The week no longer contains Friday?");
}
public DateTime GetPrevQuarterStartDate(DateTime asOf)
{
int month=asOf.Month;
@@ -136,6 +154,7 @@ namespace MarketData.Utils
quarterStartDate=GetNextBusinessDay(quarterStartDate);
return quarterStartDate;
}
public DateTime GetNextQuarterStartDate(DateTime asOf)
{
DateTime prevQuarterStartDate=GetPrevQuarterStartDate(asOf);
@@ -148,6 +167,7 @@ namespace MarketData.Utils
nextQuarterStartDate=GetNextBusinessDay(nextQuarterStartDate);
return nextQuarterStartDate;
}
public DateTime GetCurrentMonthEnd(DateTime asOf)
{
TimeSpan oneDay=new TimeSpan(-1,0,0,0);
@@ -156,6 +176,7 @@ namespace MarketData.Utils
while(IsWeekend(date)||IsHoliday(date))date=date.Add(oneDay);
return date;
}
public DateTime GetNextMonthEnd(DateTime asOf)
{
TimeSpan oneDay=new TimeSpan(-1,0,0,0);
@@ -165,6 +186,7 @@ namespace MarketData.Utils
while(IsWeekend(date)||IsHoliday(date))date=date.Add(oneDay);
return date;
}
public DateTime GetPrevMonthEnd(DateTime asOf)
{
TimeSpan oneDay=new TimeSpan(-1,0,0,0);
@@ -174,6 +196,7 @@ namespace MarketData.Utils
while(IsWeekend(date)||IsHoliday(date))date=date.Add(oneDay);
return date;
}
public DateTime GetPrevMonthEnd(DateTime asOf,int count)
{
for(int index=0;index<count;index++)
@@ -182,6 +205,7 @@ namespace MarketData.Utils
}
return asOf;
}
public DateTime EnsureWeekday(DateTime asOf)
{
TimeSpan oneDay=new TimeSpan(1,0,0,0);
@@ -189,6 +213,7 @@ namespace MarketData.Utils
while(IsWeekend(asOf=asOf.Add(oneDay)));
return asOf;
}
public DateTime GenerateHistoricalDate(DateTime startDate,int dayCount)
{
DateTime histDate;
@@ -210,6 +235,7 @@ namespace MarketData.Utils
}
return histDate.Add(singleDay);
}
// Does not account for weekends etc.,
public DateTime GenerateFutureDate(DateTime startDate,int dayCount)
{
@@ -227,6 +253,7 @@ namespace MarketData.Utils
}
return futureDate.Add(singleDay);
}
// Accounts for weekends
public DateTime GenerateFutureBusinessDate(DateTime startDate,int dayCount)
{
@@ -244,12 +271,14 @@ namespace MarketData.Utils
}
return futureDate.Add(singleDay);
}
public int DaysBetweenActual(DateTime historicalDate)
{
DateTime today = DateTime.Now;
TimeSpan timeSpan = historicalDate.Date - today;
return (int)timeSpan.TotalDays;
}
public int DaysBetweenActual(DateTime historicalDate,DateTime startingDate)
{
TimeSpan timeSpan = historicalDate.Date-startingDate;
@@ -258,11 +287,13 @@ namespace MarketData.Utils
else totalDays = Math.Abs(totalDays);
return totalDays;
}
public DateTime DaysAddActual(DateTime date, int daysActual)
{
TimeSpan days = new TimeSpan(daysActual, 0, 0, 0);
return date + days;
}
public int DaysBetween(DateTime startDate, DateTime endDate)
{
if (startDate > endDate)
@@ -276,10 +307,12 @@ namespace MarketData.Utils
return (int)timeSpan.TotalDays;
}
}
public int MonthsBetween(DateTime startDate,DateTime endDate)
{
return DaysBetween(startDate,endDate)/30;
}
public static List<int> GenerateHistoricalYear(int startYear,int years)
{
List<int> yearsList = new List<int>();
@@ -290,6 +323,7 @@ namespace MarketData.Utils
}
return yearsList;
}
public List<DateTime> GenerateHistoricalDates(DateTime startDate, int dayCount)
{
List<DateTime> histDates=new List<DateTime>();
@@ -308,11 +342,13 @@ namespace MarketData.Utils
}
return histDates;
}
public int TradingDaysBetween(DateTime startDate,DateTime endDate)
{
List<DateTime> historicalDates=GenerateHistoricalDates(startDate,endDate);
return historicalDates.Count;
}
// The function will figure out which date is the most recent and which one is the historical date.
// Generally, make startDate the most recent and endDate the historical date.
// The returned series will contain an ascending date series with the earliest (most distant) date in the lowest numbered index.
@@ -343,6 +379,7 @@ namespace MarketData.Utils
if (reverse) histDates.Reverse();
return histDates;
}
// The function will figure out which date is the most recent and which one is the historical date.
// Generally, make startDate the most recent and endDate the historical date.
public List<DateTime> GenerateHistoricalDatesActual(DateTime startDate, DateTime endDate)
@@ -372,17 +409,20 @@ namespace MarketData.Utils
if (reverse) histDates.Reverse();
return histDates;
}
public bool IsMarketOpen(DateTime dateTime)
{
if(IsWeekend(dateTime)||IsHoliday(dateTime))return false;
return true;
}
public bool IsWeekend(DateTime dateTime)
{
if(DayOfWeek.Sunday==dateTime.DayOfWeek || DayOfWeek.Saturday==dateTime.DayOfWeek)return true;
return false;
}
// make this access a singleton cache.
// This uses a singleton cache of holidays.
public bool IsHoliday(DateTime dateTime)
{
return HolidayCache.GetInstance().IsHoliday(dateTime);