Skip to content

Commit

Permalink
Minor fix in symbol mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Jan 5, 2024
1 parent 56270b8 commit b203cd9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
22 changes: 20 additions & 2 deletions QuantConnect.Polygon.Tests/PolygonSymbolMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public SecurityType ConvertsOptionSymbolWithCorrectUnderlyingSecurityType(string
}

[Test]
public void ConvertsWeeklyIndexOptionSymbol()
public void ConvertsWeeklyIndexOptionSymbolWithParameters()
{
var mapper = new PolygonSymbolMapper();
var expiry = new DateTime(2024, 01, 04);
var strike = 400m;
var strike = 4685m;
// The underlying symbol should be specified for weekly index options,
// otherwise the underlying will be set to SPXW/VIXW/... when it should actually be SPX/VIX/...
var symbol = mapper.GetLeanSymbol("O:SPXW240104C04685000", SecurityType.IndexOption, Market.USA, OptionStyle.American, expiry, strike,
Expand All @@ -76,5 +76,23 @@ public void ConvertsWeeklyIndexOptionSymbol()
Assert.That(symbol.ID.OptionStyle, Is.EqualTo(OptionStyle.American));
Assert.That(symbol.ID.StrikePrice, Is.EqualTo(strike));
}

[Test]
public void ConvertsWeeklyIndexOptionSymbolWithoutParameters()
{
var mapper = new PolygonSymbolMapper();
var expiry = new DateTime(2024, 01, 04);
var strike = 4685m;
// The underlying symbol should be specified for weekly index options,
// otherwise the underlying will be set to SPXW/VIXW/... when it should actually be SPX/VIX/...
var symbol = mapper.GetLeanSymbol("O:SPXW240104C04685000");

Assert.That(symbol.Underlying, Is.EqualTo(Symbols.SPX));
Assert.That(symbol, Is.EqualTo(Symbol.CreateOption(Symbols.SPX, "SPXW", Market.USA, OptionStyle.American, OptionRight.Call, strike, expiry)));
Assert.That(symbol.ID.Date, Is.EqualTo(expiry));
Assert.That(symbol.ID.OptionRight, Is.EqualTo(OptionRight.Call));
Assert.That(symbol.ID.OptionStyle, Is.EqualTo(OptionStyle.American));
Assert.That(symbol.ID.StrikePrice, Is.EqualTo(strike));
}
}
}
20 changes: 13 additions & 7 deletions QuantConnect.Polygon/PolygonSymbolMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

using QuantConnect.Brokerages;
using QuantConnect.Securities.IndexOption;
using System.Globalization;

namespace QuantConnect.Polygon
Expand Down Expand Up @@ -154,12 +155,15 @@ public Symbol GetLeanSymbol(string brokerageSymbol, SecurityType securityType, s
/// </remarks>
public Symbol GetLeanSymbol(string polygonSymbol)
{
if (!_leanSymbolsCache.TryGetValue(polygonSymbol, out var symbol))
lock (_locker)
{
symbol = GetLeanSymbolInternal(polygonSymbol);
}
if (!_leanSymbolsCache.TryGetValue(polygonSymbol, out var symbol))
{
symbol = GetLeanSymbolInternal(polygonSymbol);
}

return symbol;
return symbol;
}
}

private Symbol GetLeanSymbolInternal(string polygonSymbol)
Expand All @@ -186,10 +190,12 @@ private Symbol GetLeanOptionSymbol(string polygonSymbol)
var strike = long.Parse(polygonSymbol.Substring(polygonSymbol.Length - 8)) / 1000m;
var optionRight = polygonSymbol.Substring(polygonSymbol.Length - 9, 1) == "C" ? OptionRight.Call : OptionRight.Put;
var expirationDate = DateTime.ParseExact(polygonSymbol.Substring(polygonSymbol.Length - 15, 6), "yyMMdd", CultureInfo.InvariantCulture);
var underlyingTicker = polygonSymbol.Substring(2, polygonSymbol.Length - 15 - 2);
var ticker = polygonSymbol.Substring(2, polygonSymbol.Length - 15 - 2);

var symbol = Symbol.CreateOption(Symbol.Create(underlyingTicker, SecurityType.Equity, Market.USA), Market.USA, OptionStyle.American,
optionRight, strike, expirationDate);
var underlying = IndexOptionSymbol.IsIndexOption(ticker)
? Symbol.Create(IndexOptionSymbol.MapToUnderlying(ticker), SecurityType.Index, Market.USA)
: Symbol.Create(ticker, SecurityType.Equity, Market.USA);
var symbol = Symbol.CreateOption(underlying, ticker, Market.USA, OptionStyle.American, optionRight, strike, expirationDate);
_leanSymbolsCache[polygonSymbol] = symbol;

return symbol;
Expand Down

0 comments on commit b203cd9

Please sign in to comment.