Skip to content

Commit

Permalink
Manual option contract addition for delisted underlyings error (Quant…
Browse files Browse the repository at this point in the history
…Connect#7799)

* Throw when trying to add option contracts for delisted underlyings

* Minor change
  • Loading branch information
jhonabreul authored Feb 21, 2024
1 parent 09db45c commit d3928fb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Algorithm/QCAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2341,6 +2341,11 @@ public Option AddOptionContract(Symbol symbol, Resolution? resolution = null, bo
underlyingConfigs = SubscriptionManager.SubscriptionDataConfigService
.GetSubscriptionDataConfigs(underlying);
}
else if (underlyingSecurity != null && underlyingSecurity.IsDelisted)
{
throw new ArgumentException($"The underlying {underlying.SecurityType} asset ({underlying.Value}) is delisted " +
$"(current time is {Time})");
}
else
{
underlyingConfigs = SubscriptionManager.SubscriptionDataConfigService
Expand Down
28 changes: 27 additions & 1 deletion Tests/Algorithm/AlgorithmAddDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Python.Runtime;
using Newtonsoft.Json;
using NodaTime;
using NUnit.Framework;
Expand Down Expand Up @@ -645,6 +644,33 @@ public void DoesNotCauseCollision_WhenRegisteringMultipleDifferentCustomDataType
Assert.AreNotSame(unlinkedData, bitcoin);
}

[TestCase(SecurityType.Equity)]
[TestCase(SecurityType.Index)]
[TestCase(SecurityType.Future)]
public void AddOptionContractWithDelistedUnderlyingThrows(SecurityType underlyingSecurityType)
{
var algorithm = Algorithm();
algorithm.SetStartDate(2007, 05, 25);

Security underlying = underlyingSecurityType switch
{
SecurityType.Equity => algorithm.AddEquity("SPY"),
SecurityType.Index => algorithm.AddIndex("SPX"),
SecurityType.Future => algorithm.AddFuture("ES"),
_ => throw new ArgumentException($"Invalid test underlying security type {underlyingSecurityType}")
};

underlying.IsDelisted = true;
// let's remove the underlying since it's delisted
algorithm.RemoveSecurity(underlying.Symbol);

var optionContractSymbol = Symbol.CreateOption(underlying.Symbol, Market.USA, OptionStyle.American, OptionRight.Call, 100,
new DateTime(2007, 06, 15));

var exception = Assert.Throws<ArgumentException>(() => algorithm.AddOptionContract(optionContractSymbol));
Assert.IsTrue(exception.Message.Contains("is delisted"), $"Unexpected exception message: {exception.Message}");
}

private static SubscriptionDataConfig GetMatchingSubscription(QCAlgorithm algorithm, Symbol symbol, Type type)
{
// find a subscription matchin the requested type with a higher resolution than requested
Expand Down

0 comments on commit d3928fb

Please sign in to comment.