Skip to content

Commit

Permalink
Fix bug and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinovsky committed Jan 25, 2024
1 parent 20a5f69 commit 7a6167a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Common/Brokerages/InteractiveBrokersBrokerageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ public override bool CanSubmitOrder(Security security, Order order, out Brokerag

return false;
}
else if (order.Type == OrderType.MarketOnClose && (security.Type == SecurityType.Option || security.Type == SecurityType.IndexOption))
{
message = new BrokerageMessageEvent(BrokerageMessageType.Warning,
"InteractiveBrokers does not support Market-on-Close orders for Options",
Messages.DefaultBrokerageModel.UnsupportedOrderType(this, order, _supportedOrderTypes.Where(x => x != OrderType.MarketOnClose)));
return false;
}

// validate security type
if (security.Type != SecurityType.Equity &&
Expand Down
47 changes: 47 additions & 0 deletions Tests/Common/Brokerages/InteractiveBrokersBrokerageModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using QuantConnect.Data;
using QuantConnect.Securities.Option;
using QuantConnect.Securities.Forex;
using QuantConnect.Securities.IndexOption;

namespace QuantConnect.Tests.Common.Brokerages
{
Expand Down Expand Up @@ -71,6 +72,52 @@ public void CanSubmitOrder_ForexWithinAllowableOrderSize(Forex security, decimal
}
}

[Test]
public void CannotSubmitMOCOrdersForOptions()
{
var option = Symbols.SPY_Option_Chain;
var securityOption = new QuantConnect.Securities.Option.Option(
option,
MarketHoursDatabase.FromDataFolder().GetExchangeHours(Market.USA, option, option.SecurityType),
new Cash("USD", 100000m, 1m),
new OptionSymbolProperties(string.Empty, "USD", 1m, 0.01m, 1m),
new CashBook(),
new RegisteredSecurityDataTypesProvider(),
new SecurityCache(),
null);

var optionOrder = new MarketOnCloseOrder(option, 1, DateTime.UtcNow);
var result = _interactiveBrokersBrokerageModel.CanSubmitOrder(securityOption, optionOrder, out var message);
Assert.IsFalse(result);
var expectedMessage = "The InteractiveBrokersBrokerageModel does not support MarketOnClose order type. Only supports [Market,MarketOnOpen,Limit,StopMarket,StopLimit,TrailingStop,LimitIfTouched,ComboMarket,ComboLimit,ComboLegLimit,OptionExercise]";
Assert.AreEqual(expectedMessage, message.Message);
}

[Test]
public void CannotSubmitMOCOrdersForIndexOptions()
{
var index = Symbol.Create(IndexOptionSymbol.MapToUnderlying("SPY"), SecurityType.Index, Market.USA);
var indexOption = Symbol.CreateOption(index, "SPY", Market.USA, OptionStyle.European,
OptionRight.Call, 3700, DateTime.UtcNow);
var securityIndexOption = new QuantConnect.Securities.IndexOption.IndexOption(
indexOption,
MarketHoursDatabase.FromDataFolder().GetExchangeHours(Market.USA, indexOption, indexOption.SecurityType),
new Cash("USD", 100000m, 1m),
new IndexOptionSymbolProperties(string.Empty, "USD", 1m, 0.01m, 1m),
new CashBook(),
new RegisteredSecurityDataTypesProvider(),
new SecurityCache(),
null);

var indexOptionOrder = new MarketOnCloseOrder(indexOption, 1, DateTime.UtcNow);
var optionIndexOrder = new MarketOnCloseOrder(indexOption, 1, DateTime.UtcNow);
var result = _interactiveBrokersBrokerageModel.CanSubmitOrder(securityIndexOption, indexOptionOrder, out var message);
Assert.IsFalse(result);
var expectedMessage = "The InteractiveBrokersBrokerageModel does not support MarketOnClose order type. Only supports [Market,MarketOnOpen,Limit,StopMarket,StopLimit,TrailingStop,LimitIfTouched,ComboMarket,ComboLimit,ComboLegLimit,OptionExercise]";
Assert.AreEqual(expectedMessage, message.Message);
}


private static List<Security> GetUnsupportedOptions()
{
// Index option
Expand Down

0 comments on commit 7a6167a

Please sign in to comment.