Skip to content

Commit

Permalink
Cache option expiration date time
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Dec 13, 2024
1 parent c648de8 commit e2937a6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion Common/Securities/Option/OptionSymbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public static class OptionSymbol
{
private static readonly Dictionary<string, byte> _optionExpirationErrorLog = new();

private static readonly Dictionary<Symbol, DateTime> _expirationDateTimes = new();

/// <summary>
/// Returns true if the option is a standard contract that expires 3rd Friday of the month
/// </summary>
Expand Down Expand Up @@ -130,7 +132,7 @@ public static DateTime GetSettlementDateTime(Symbol symbol)
{
if (!TryGetExpirationDateTime(symbol, out var expiryTime, out var exchangeHours))
{
throw new ArgumentException("The symbol must be an option type");
throw new ArgumentException($"The symbol {symbol} is not an option type");
}

// Standard index options are AM-settled, which means they settle on market open of the expiration date
Expand Down Expand Up @@ -171,6 +173,11 @@ private static bool TryGetExpirationDateTime(Symbol symbol, out DateTime expiryT

exchangeHours = MarketHoursDatabase.FromDataFolder().GetExchangeHours(symbol.ID.Market, symbol, symbol.SecurityType);

if (_expirationDateTimes.TryGetValue(symbol, out expiryTime))
{
return true;
}

// Ideally we can calculate expiry on the date of the symbol ID, but if that exchange is not open on that day we
// will consider expired on the last trading day close before this; Example in AddOptionContractExpiresRegressionAlgorithm
var lastTradingDay = exchangeHours.IsDateOpen(symbol.ID.Date)
Expand All @@ -196,6 +203,8 @@ private static bool TryGetExpirationDateTime(Symbol symbol, out DateTime expiryT
expiryTime = symbol.ID.Date.AddDays(1).Date;
}

_expirationDateTimes[symbol] = expiryTime;

return true;
}
}
Expand Down

0 comments on commit e2937a6

Please sign in to comment.