Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Common/Securities/Option/DefaultOptionAssignmentModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static decimal EstimateArbitragePnL(Option option, OptionHolding holding
}

// calculating P/L of the two transactions (exercise option and then close underlying position)
var altPnL = (underlyingPrice - option.ScaledStrikePrice) * underlyingQuantity * underlying.QuoteCurrency.ConversionRate * option.ContractUnitOfTrade
var altPnL = (underlyingPrice - option.ScaledStrikePrice) * underlyingQuantity * underlying.QuoteCurrency.ConversionRate
- underlyingOrderFee2Amount
- holding.AveragePrice * holding.AbsoluteQuantity * option.SymbolProperties.ContractMultiplier * option.QuoteCurrency.ConversionRate
- optionOrderFee2;
Expand Down
47 changes: 47 additions & 0 deletions Tests/Engine/DefaultOptionAssignmentModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using QuantConnect.Securities;
using QuantConnect.Data.Market;
using QuantConnect.Data;
using QuantConnect.Orders.Fees;
using System.Linq;

namespace QuantConnect.Tests.Engine
Expand Down Expand Up @@ -115,6 +116,52 @@ public void SimulatesAssignment(SecurityType securityType)
}
}

[TestCase(OptionRight.Call, 31.50, 100, 0)]
[TestCase(OptionRight.Put, 31.50, 100, 0)]
[TestCase(OptionRight.Call, 29.50, 100, 1)]
[TestCase(OptionRight.Put, 29.50, 100, 1)]
[TestCase(OptionRight.Call, 31.50, 1, 0)]
[TestCase(OptionRight.Put, 31.50, 1, 0)]
[TestCase(OptionRight.Call, 29.50, 1, 1)]
[TestCase(OptionRight.Put, 29.50, 1, 1)]
public void SimulatesAssignmentWithContractUnitOfTrade(OptionRight right, decimal bidPrice, int contractMultiplier, int expectedQuantity)
{
var expiration = new DateTime(2016, 2, 19);
var today = expiration.AddDays(-3).AddHours(10);
var securities = new SecurityManager(new TimeKeeper(today.ConvertToUtc(TimeZones.NewYork), new[] { TimeZones.NewYork }));
var underlying = new Security(
SecurityExchangeHours,
CreateTradeBarDataConfig(SecurityType.Equity, Symbols.SPY),
new Cash(Currencies.USD, 0, 1m),
SymbolProperties.GetDefault(Currencies.USD),
ErrorCurrencyConverter.Instance,
RegisteredSecurityDataTypesProvider.Null,
new SecurityCache()
) { FeeModel = new ConstantFeeModel(0) };
securities.Add(Symbols.SPY, underlying);
underlying.SetMarketPrice(new Tick(today, Symbols.SPY, 200m, 199.99m, 200.01m));

var strikePrice = right == OptionRight.Call ? 170m : 230m;
var symbol = Symbol.CreateOption(Symbols.SPY, Market.USA, OptionStyle.American, right, strikePrice, expiration);
var option = new Option(
SecurityExchangeHours,
CreateTradeBarDataConfig(SecurityType.Option, symbol),
new Cash(Currencies.USD, 0, 1m),
new OptionSymbolProperties(string.Empty, Currencies.USD, contractMultiplier, 0.01m, 1),
ErrorCurrencyConverter.Instance,
RegisteredSecurityDataTypesProvider.Null
) { Underlying = underlying, FeeModel = new ConstantFeeModel(0) };
securities.Add(symbol, option);
option.Holdings.SetHoldings(1, -1);
option.SetMarketPrice(new Tick(today, symbol, bidPrice + 0.05m, bidPrice, bidPrice + 0.10m));

// Exercising and closing the shares returns 29.99 per share for either right.
// Selling at 31.50 preserves time value, while exercising beats selling at 29.50.
var result = new DefaultOptionAssignmentModel().GetAssignment(new OptionAssignmentParameters(option));

Assert.AreEqual(expectedQuantity, result.Quantity);
}

private SubscriptionDataConfig CreateTradeBarDataConfig(SecurityType type, Symbol symbol)
{
if (type == SecurityType.Equity)
Expand Down
Loading