Skip to content

Commit

Permalink
Exclude expired options from IB GetAccountHoldings
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanoRaggi committed Oct 18, 2019
1 parent 979577e commit e4bbddc
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
7 changes: 6 additions & 1 deletion Brokerages/InteractiveBrokers/InteractiveBrokersBrokerage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
using QuantConnect.IBAutomater;
using QuantConnect.Orders.Fees;
using QuantConnect.Orders.TimeInForces;
using QuantConnect.Securities.Option;
using Bar = QuantConnect.Data.Market.Bar;
using HistoryRequest = QuantConnect.Data.HistoryRequest;

Expand Down Expand Up @@ -501,7 +502,11 @@ public override List<Holding> GetAccountHoldings()
Connect();
}

var holdings = _accountData.AccountHoldings.Select(x => ObjectActivator.Clone(x.Value)).Where(x => x.Quantity != 0).ToList();
var utcNow = DateTime.UtcNow;
var holdings = _accountData.AccountHoldings
.Select(x => ObjectActivator.Clone(x.Value))
.Where(x => x.Quantity != 0 && !OptionSymbol.IsOptionContractExpired(x.Symbol, utcNow))
.ToList();

return holdings;
}
Expand Down
48 changes: 41 additions & 7 deletions Common/Securities/Option/OptionSymbol.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;

namespace QuantConnect.Securities.Option
{
Expand All @@ -12,7 +23,7 @@ namespace QuantConnect.Securities.Option
public static class OptionSymbol
{
/// <summary>
/// Returns true is the option is a standard contract that expire 3rd Friday of the month
/// Returns true if the option is a standard contract that expires 3rd Friday of the month
/// </summary>
/// <param name="symbol">Option symbol</param>
/// <returns></returns>
Expand All @@ -32,7 +43,7 @@ public static bool IsStandardContract(Symbol symbol)
}

/// <summary>
/// Returns lat trading date for the option contract
/// Returns the last trading date for the option contract
/// </summary>
/// <param name="symbol">Option symbol</param>
/// <returns></returns>
Expand Down Expand Up @@ -63,5 +74,28 @@ public static DateTime GetLastDayOfTrading(Symbol symbol)

return symbolDateTime.AddDays(daysBefore).Date;
}

/// <summary>
/// Returns true if the option contract is expired at the specified time
/// </summary>
/// <param name="symbol">The option contract symbol</param>
/// <param name="currentTimeUtc">The current time (UTC)</param>
/// <returns>True if the option contract is expired at the specified time, false otherwise</returns>
public static bool IsOptionContractExpired(Symbol symbol, DateTime currentTimeUtc)
{
if (symbol.SecurityType != SecurityType.Option)
{
return false;
}

var exchangeHours = MarketHoursDatabase.FromDataFolder()
.GetExchangeHours(symbol.ID.Market, symbol, symbol.SecurityType);

var currentTime = currentTimeUtc.ConvertFromUtc(exchangeHours.TimeZone);
var expiryTime = exchangeHours.GetNextMarketClose(symbol.ID.Date, false);

return currentTime >= expiryTime;
}

}
}
59 changes: 59 additions & 0 deletions Tests/Common/Securities/Options/OptionSymbolTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using NUnit.Framework;
using QuantConnect.Securities.Option;

namespace QuantConnect.Tests.Common.Securities.Options
{
[TestFixture]
public class OptionSymbolTests
{
[Test]
public void IsOptionContractExpiredReturnsFalseForNonOptionSymbol()
{
Assert.IsFalse(OptionSymbol.IsOptionContractExpired(Symbols.SPY, DateTime.UtcNow));
}

[Test]
public void IsOptionContractExpiredReturnsTrueIfExpiredContract()
{
var symbol = Symbol.CreateOption(
"BHP",
Market.USA,
OptionStyle.American,
OptionRight.Call,
55m,
new DateTime(2019, 9, 20));

Assert.IsTrue(OptionSymbol.IsOptionContractExpired(symbol, DateTime.UtcNow));
}

[Test]
public void IsOptionContractExpiredReturnsFalseIfActiveContract()
{
var symbol = Symbol.CreateOption(
"BHP",
Market.USA,
OptionStyle.American,
OptionRight.Call,
55m,
new DateTime(2019, 9, 20));

Assert.IsFalse(OptionSymbol.IsOptionContractExpired(symbol, new DateTime(2019, 1, 1)));
}
}
}
1 change: 1 addition & 0 deletions Tests/QuantConnect.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@
<Compile Include="Common\Securities\IdentityCurrencyConverterTests.cs" />
<Compile Include="Common\Securities\AccountCurrencyImmediateSettlementModelTests.cs" />
<Compile Include="Common\Securities\Options\OptionPortfolioModelTests.cs" />
<Compile Include="Common\Securities\Options\OptionSymbolTests.cs" />
<Compile Include="Common\Securities\ProcessVolatilityHistoryRequirementsTests.cs" />
<Compile Include="Common\Securities\DynamicSecurityDataTests.cs" />
<Compile Include="Common\Securities\SecurityHoldingTests.cs" />
Expand Down

0 comments on commit e4bbddc

Please sign in to comment.