Table of Contents

Class DateHelper

Namespace
Balsam
Assembly
Balsam.Backtester.dll

A collection of utilities for manipulating dates.

public static class DateHelper
Inheritance
DateHelper
Inherited Members

Methods

AddBusinessDays(IEnumerable<DateTime>, int)

Adds the specified number of business days to each date in the list. Useful for lagging data.

public static IEnumerable<DateTime> AddBusinessDays(IEnumerable<DateTime> dates, int value)

Parameters

dates IEnumerable<DateTime>
value int

Returns

IEnumerable<DateTime>

AddDays(IEnumerable<DateTime>, double)

Adds the specified number of days to each date in the list. Useful for lagging data.

public static IEnumerable<DateTime> AddDays(IEnumerable<DateTime> dates, double value)

Parameters

dates IEnumerable<DateTime>
value double

Returns

IEnumerable<DateTime>

AddMonths(IEnumerable<DateTime>, int)

Adds the specified number of months to each date in the list. Useful for lagging data.

public static IEnumerable<DateTime> AddMonths(IEnumerable<DateTime> dates, int months)

Parameters

dates IEnumerable<DateTime>
months int

Returns

IEnumerable<DateTime>

AreDatesIdentical(params ISeries[])

Returns true if the dates in the specified series are identical.

public static bool AreDatesIdentical(params ISeries[] series)

Parameters

series ISeries[]

Returns

bool

ContainsNonBusinessDays(ISeries)

Returns true if any of the dates in the specified series are not weekdays.

public static bool ContainsNonBusinessDays(ISeries series)

Parameters

series ISeries

Returns

bool

ContainsNonBusinessDays(IEnumerable<DateTime>)

Returns true if any of the dates are not weekdays.

public static bool ContainsNonBusinessDays(IEnumerable<DateTime> dates)

Parameters

dates IEnumerable<DateTime>

Returns

bool

ContainsNonBusinessDays(IEnumerable<DateTime>, IEnumerable<DateTime>)

Returns true if any of the dates are not weekdays or otherwise are found in the specified list of holidays.

public static bool ContainsNonBusinessDays(IEnumerable<DateTime> dates, IEnumerable<DateTime> holidays)

Parameters

dates IEnumerable<DateTime>
holidays IEnumerable<DateTime>

Returns

bool

ConvertDatesToFirstOfMonth(ISeries)

Converts monthly dates to use 1st day of month.

public static IEnumerable<DateTime> ConvertDatesToFirstOfMonth(ISeries series)

Parameters

series ISeries

Returns

IEnumerable<DateTime>

ConvertDatesToFirstOfMonth(IEnumerable<DateTime>)

Converts monthly dates to use 1st day of month.

public static IEnumerable<DateTime> ConvertDatesToFirstOfMonth(IEnumerable<DateTime> dates)

Parameters

dates IEnumerable<DateTime>

Returns

IEnumerable<DateTime>

Exceptions

ArgumentNullException

ConvertDatesToMidMonth(ISeries)

Converts monthly dates to have day of 15th.

public static IEnumerable<DateTime> ConvertDatesToMidMonth(ISeries series)

Parameters

series ISeries

Returns

IEnumerable<DateTime>

ConvertDatesToMidMonth(IEnumerable<DateTime>)

Converts monthly dates to have day of 15th.

public static IEnumerable<DateTime> ConvertDatesToMidMonth(IEnumerable<DateTime> dates)

Parameters

dates IEnumerable<DateTime>

Returns

IEnumerable<DateTime>

ConvertDatesToMonthEnd(ISeries)

Converts the dates in the specified series to have the last day of the month.

public static IEnumerable<DateTime> ConvertDatesToMonthEnd(ISeries series)

Parameters

series ISeries

Returns

IEnumerable<DateTime>

ConvertDatesToMonthEnd(IEnumerable<DateTime>, bool)

Converts monthly dates to have the last day of the month.

public static IEnumerable<DateTime> ConvertDatesToMonthEnd(IEnumerable<DateTime> dates, bool useBusinessDays)

Parameters

dates IEnumerable<DateTime>
useBusinessDays bool

Returns

IEnumerable<DateTime>

DateToInt(DateTime)

Converts the specified date to its integer yyyyMMdd representation.

public static int DateToInt(DateTime value)

Parameters

value DateTime

Returns

int

GetBusinessDays(DateTime, DateTime)

Returns a list of business days between the specified start and end dates inclusive.

public static IEnumerable<DateTime> GetBusinessDays(DateTime startDate, DateTime endDate)

Parameters

startDate DateTime
endDate DateTime

Returns

IEnumerable<DateTime>

GetBusinessDays(DateTime, DateTime, IEnumerable<DateTime>)

Returns a list of business days between the specified start and end dates excluding the specified holidays.

public static IEnumerable<DateTime> GetBusinessDays(DateTime startDate, DateTime endDate, IEnumerable<DateTime> holidays)

Parameters

startDate DateTime
endDate DateTime
holidays IEnumerable<DateTime>

Returns

IEnumerable<DateTime>

GetBusinessDays(DateTime, int, IEnumerable<DateTime>)

Returns a list of consecutive business days from the specified start date.

public static IEnumerable<DateTime> GetBusinessDays(DateTime startDate, int days, IEnumerable<DateTime> holidays = null)

Parameters

startDate DateTime
days int
holidays IEnumerable<DateTime>

Returns

IEnumerable<DateTime>

GetBusinessDaysBetween(DateTime, DateTime)

Returns the number of business days between two dates inclusive of the start and end dates.

public static int GetBusinessDaysBetween(DateTime startDate, DateTime endDate)

Parameters

startDate DateTime
endDate DateTime

Returns

int

GetBusinessDaysBetween(DateTime, DateTime, IEnumerable<DateTime>)

Returns the number of business days between two dates inclusive of the start and end dates but excluding holidays.

public static int GetBusinessDaysBetween(DateTime firstDate, DateTime lastDate, IEnumerable<DateTime> holidays)

Parameters

firstDate DateTime
lastDate DateTime
holidays IEnumerable<DateTime>

Returns

int

GetCalendarDays(DateTime, DateTime)

Returns a list of calendar days between the specified start and end dates inclusive.

public static IEnumerable<DateTime> GetCalendarDays(DateTime startDate, DateTime endDate)

Parameters

startDate DateTime
endDate DateTime

Returns

IEnumerable<DateTime>

GetCalendarDays(DateTime, DateTime, IEnumerable<DateTime>)

Returns a list of calendar days between the specified start and end dates excluding the specified holidays.

public static IEnumerable<DateTime> GetCalendarDays(DateTime startDate, DateTime endDate, IEnumerable<DateTime> holidays)

Parameters

startDate DateTime
endDate DateTime
holidays IEnumerable<DateTime>

Returns

IEnumerable<DateTime>

GetFirstDayOfMonth(DateTime, bool)

Returns the first date of the month.

public static DateTime GetFirstDayOfMonth(DateTime date, bool useBusinessDays = true)

Parameters

date DateTime
useBusinessDays bool

Returns

DateTime

GetHolidays(ISeries)

Returns the missing business days from the specified series.

public static IEnumerable<DateTime> GetHolidays(ISeries series)

Parameters

series ISeries

Returns

IEnumerable<DateTime>

GetHolidays(IEnumerable<DateTime>)

Returns missing business days from the specified enumerable of dates.

public static IEnumerable<DateTime> GetHolidays(IEnumerable<DateTime> dates)

Parameters

dates IEnumerable<DateTime>

Returns

IEnumerable<DateTime>

Exceptions

ArgumentNullException

GetIntersectionOfDates(params ISeries[])

Returns the intersection of dates (inner join) for the specified ISeries.

public static IEnumerable<DateTime> GetIntersectionOfDates(params ISeries[] series)

Parameters

series ISeries[]

Returns

IEnumerable<DateTime>

GetLastDayOfMonth(DateTime)

Returns the last calendar date of the month.

public static DateTime GetLastDayOfMonth(DateTime date)

Parameters

date DateTime

Returns

DateTime

GetLastDayOfMonth(DateTime, bool)

Returns the last date of the month.

public static DateTime GetLastDayOfMonth(DateTime date, bool useBusinessDays)

Parameters

date DateTime
useBusinessDays bool

Returns

DateTime

GetMissingDates(ISeries, bool)

Gets missing dates from the specified series.

public static IEnumerable<DateTime> GetMissingDates(ISeries series, bool calendarDays = false)

Parameters

series ISeries
calendarDays bool

Defaults to business days. Set to true to use calendar days

Returns

IEnumerable<DateTime>

GetMissingDates(params ISeries[])

Returns the dates that aren't found in the union of the specified series.

public static IEnumerable<DateTime> GetMissingDates(params ISeries[] series)

Parameters

series ISeries[]

Returns

IEnumerable<DateTime>

GetNextTradeDate(DateTime)

Gets the next business day.

public static DateTime GetNextTradeDate(DateTime currentDate)

Parameters

currentDate DateTime

Returns

DateTime

GetNextTradeDate(DateTime, IEnumerable<DateTime>)

Gets the next business day excluding specified holidays if any.

public static DateTime GetNextTradeDate(DateTime currentDate, IEnumerable<DateTime> holidays)

Parameters

currentDate DateTime
holidays IEnumerable<DateTime>

Returns

DateTime

GetNextTradeDate(DateTime, int)

Gets the business day the specified days in the future from the specified date.

public static DateTime GetNextTradeDate(DateTime currentDate, int days)

Parameters

currentDate DateTime
days int

Returns

DateTime

GetNextTradeDate(DateTime, int, IEnumerable<DateTime>)

Gets the next business day not including specified holidays if any.

public static DateTime GetNextTradeDate(DateTime currentDate, int days, IEnumerable<DateTime> holidays)

Parameters

currentDate DateTime
days int
holidays IEnumerable<DateTime>

Returns

DateTime

GetPreviousTradeDate(DateTime)

Gets the previous business day.

public static DateTime GetPreviousTradeDate(DateTime currentDate)

Parameters

currentDate DateTime

Returns

DateTime

GetPreviousTradeDate(DateTime, IEnumerable<DateTime>)

Gets the previous business day excluding specified holidays if any.

public static DateTime GetPreviousTradeDate(DateTime currentDate, IEnumerable<DateTime> holidays)

Parameters

currentDate DateTime
holidays IEnumerable<DateTime>

Returns

DateTime

GetPreviousTradeDate(DateTime, int)

Gets the business day the specified number of days previous to the current date.

public static DateTime GetPreviousTradeDate(DateTime currentDate, int days)

Parameters

currentDate DateTime
days int

Returns

DateTime

GetPreviousTradeDate(DateTime, int, IEnumerable<DateTime>)

Gets the previous business day not including specified holidays if any.

public static DateTime GetPreviousTradeDate(DateTime currentDate, int days, IEnumerable<DateTime> holidays)

Parameters

currentDate DateTime
days int
holidays IEnumerable<DateTime>

Returns

DateTime

GetUnionOfDates(params ISeries[])

Returns the union of distinct dates in the specified array of series, ignoring any dates prior to a series' MaxBarsBack.

public static IEnumerable<DateTime> GetUnionOfDates(params ISeries[] series)

Parameters

series ISeries[]

Returns

IEnumerable<DateTime>

IntToDate(int)

Converts the specified yyyyMMdd formatted integer into a DateTime object.

public static DateTime IntToDate(int value)

Parameters

value int

Returns

DateTime

IsMonthEnd(DateTime)

Returns true when the next business day is the start of a new month

public static bool IsMonthEnd(DateTime date)

Parameters

date DateTime

Returns

bool

IsOptionsExpiration(DateTime, bool, IEnumerable<DateTime>)

Returns true if the specified date is the third Friday of the month.

public static bool IsOptionsExpiration(DateTime date, bool tripleWitch = false, IEnumerable<DateTime> holidays = null)

Parameters

date DateTime
tripleWitch bool

True for quarterly expirations only.

holidays IEnumerable<DateTime>

Optional enumerable of holiday dates. If options expiration falls on a Friday holiday, expiration is the Thursday before.

Returns

bool

IsQuarterEnd(DateTime)

Returns true when the next business day is the start of a new quarter.

public static bool IsQuarterEnd(DateTime date)

Parameters

date DateTime

Returns

bool

IsTradingDay(DateTime, IEnumerable<DateTime>)

Returns true when the specified date is a weekday and is not contained in the list of holidays.

public static bool IsTradingDay(DateTime date, IEnumerable<DateTime> holidays)

Parameters

date DateTime
holidays IEnumerable<DateTime>

Returns

bool

IsWeekDay(DateTime)

Returns true when the specified date is a weekday.

public static bool IsWeekDay(DateTime date)

Parameters

date DateTime

Returns

bool

IsYearEnd(DateTime)

Returns true when the next business day is the start of a new year.

public static bool IsYearEnd(DateTime date)

Parameters

date DateTime

Returns

bool

MaxDate(params DateTime[])

Returns the maximum date from a list of dates.

public static DateTime MaxDate(params DateTime[] dates)

Parameters

dates DateTime[]

Returns

DateTime

MinDate(params DateTime[])

Returns the minimum date from a list of dates.

public static DateTime MinDate(params DateTime[] dates)

Parameters

dates DateTime[]

Returns

DateTime