Compare commits

...

1 Commits

Author SHA1 Message Date
Gerardo Salazar
4134b05bfd Fixes options order crash in ReportGenerator/PortfolioLooper (#4992) 2020-12-07 12:39:53 -03:00
4 changed files with 89 additions and 0 deletions

View File

@@ -307,6 +307,10 @@ namespace QuantConnect
throw new ArgumentNullException(nameof(value));
}
ID = sid;
if (ID.HasUnderlying)
{
Underlying = new Symbol(ID.Underlying, ID.Underlying.Symbol);
}
Value = value.LazyToUpper();
}

View File

@@ -128,6 +128,9 @@ namespace QuantConnect.Report
// More initialization, this time with Algorithm and other misc. classes
_resultHandler.Initialize(job, new Messaging.Messaging(), new Api.Api(), transactions);
_resultHandler.SetAlgorithm(Algorithm, Algorithm.Portfolio.TotalPortfolioValue);
Algorithm.Transactions.SetOrderProcessor(transactions);
transactions.Initialize(Algorithm, new BacktestingBrokerage(Algorithm), _resultHandler);
feed.Initialize(Algorithm, job, _resultHandler, null, null, null, _dataManager, null, null);

View File

@@ -587,6 +587,23 @@ namespace QuantConnect.Tests.Common
Assert.IsTrue(nonCanonicalFutureOption.Value.StartsWith(expectedFutureOptionTicker));
}
[Test]
public void SymbolWithSidContainingUnderlyingCreatedWithoutNullUnderlying()
{
var future = Symbol.CreateFuture("ES", Market.CME, new DateTime(2020, 6, 19));
var optionSid = SecurityIdentifier.GenerateOption(
future.ID.Date,
future.ID,
future.ID.Market,
3500m,
OptionRight.Call,
OptionStyle.American);
var option = new Symbol(optionSid, "ES");
Assert.IsNotNull(option.Underlying);
Assert.AreEqual(future, option.Underlying);
}
class OldSymbol
{
public string Value { get; set; }

View File

@@ -113,5 +113,70 @@ namespace QuantConnect.Tests.Report
Assert.AreEqual(80000, pointInTimePortfolio[1].TotalPortfolioValue);
Assert.AreEqual(80000, pointInTimePortfolio[2].TotalPortfolioValue);
}
[Test]
public void OptionOrderDoesNotThrow()
{
var equityPoints = new SortedList<DateTime, double>
{
{ new DateTime(2019, 1, 3, 5, 0, 5), 100000 },
{ new DateTime(2019, 1, 4, 5, 0, 5), 90000 },
};
var series = new Series<DateTime, double>(equityPoints);
var equity = Symbol.Create("SPY", SecurityType.Equity, Market.USA);
var optionSid = SecurityIdentifier.GenerateOption(
equity.ID.Date,
equity.ID,
equity.ID.Market,
200m,
OptionRight.Call,
OptionStyle.American);
var option = new Symbol(optionSid, optionSid.Symbol);
var entryOrder = Order.CreateOrder(new SubmitOrderRequest(
OrderType.Market,
SecurityType.Option,
option,
1,
0m,
0m,
new DateTime(2019, 1, 3, 5, 0, 5),
string.Empty
));
var exitOrder = Order.CreateOrder(new SubmitOrderRequest(
OrderType.Market,
SecurityType.Option,
option,
-1,
0m,
0m,
new DateTime(2019, 1, 4, 5, 0, 5),
string.Empty
));
entryOrder.LastFillTime = new DateTime(2019, 1, 3, 5, 0, 5);
exitOrder.LastFillTime = new DateTime(2019, 1, 4, 5, 0, 5);
entryOrder.GetType().GetProperty("Id").SetValue(entryOrder, 1);
entryOrder.GetType().GetProperty("Price").SetValue(entryOrder, 100000m);
Order marketOnFillOrder = null;
exitOrder.GetType().GetProperty("Id").SetValue(exitOrder, 2);
exitOrder.GetType().GetProperty("Price").SetValue(exitOrder, 80000m);
exitOrder.GetType().GetProperty("Status").SetValue(exitOrder, OrderStatus.Filled);
var orders = new[] { entryOrder, marketOnFillOrder, exitOrder }.Where(x => x != null);
var looper = PortfolioLooper.FromOrders(series, orders);
Assert.DoesNotThrow(() =>
{
foreach (var pointInTimePortfolio in looper)
{
Assert.AreEqual(option, pointInTimePortfolio.Order.Symbol);
Assert.AreEqual(option.Underlying, pointInTimePortfolio.Order.Symbol.Underlying);
}
});
}
}
}