Skip to content

Commit

Permalink
Merge pull request QuantConnect#4496 from QuantConnect/bug-4446-delis…
Browse files Browse the repository at this point in the history
…ted-future-liquidate-on-expiry

Fix Delisted.Warning event time
  • Loading branch information
Martin-Molinero authored Jun 10, 2020
2 parents 6735c2b + 03e1010 commit 9931940
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
138 changes: 138 additions & 0 deletions Algorithm.CSharp/DelistedFutureLiquidateRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* 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 System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Interfaces;
using QuantConnect.Orders;
using QuantConnect.Securities;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm which reproduces GH issue 4446
/// </summary>
public class DelistedFutureLiquidateRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private Symbol _contractSymbol;

/// <summary>
/// Initialize your algorithm and add desired assets.
/// </summary>
public override void Initialize()
{
SetStartDate(2013, 10, 08);
SetEndDate(2013, 12, 30);

var futureSP500 = AddFuture(Futures.Indices.SP500EMini);
futureSP500.SetFilter(0, 182);
}

/// <summary>
/// Event - v3.0 DATA EVENT HANDLER: (Pattern) Basic template for user to override for receiving all subscription data in a single event
/// </summary>
/// <param name="slice">The current slice of data keyed by symbol string</param>
public override void OnData(Slice slice)
{
if (_contractSymbol == null)
{
foreach (var chain in slice.FutureChains)
{
var contract = chain.Value.OrderBy(x => x.Expiry).FirstOrDefault();
// if found, trade it
if (contract != null)
{
_contractSymbol = contract.Symbol;
MarketOrder(_contractSymbol, 1);
}
}
}
}

public override void OnEndOfAlgorithm()
{
Log($"{_contractSymbol}: {Securities[_contractSymbol].Invested}");
if (Securities[_contractSymbol].Invested)
{
throw new Exception($"Position should be closed when {_contractSymbol} got delisted {_contractSymbol.ID.Date}");
}
}

public override void OnOrderEvent(OrderEvent orderEvent)
{
Log($"{orderEvent}. Delisting on: {_contractSymbol.ID.Date}");
}

/// <summary>
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
/// </summary>
public bool CanRunLocally { get; } = true;

/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
public Language[] Languages { get; } = { Language.CSharp };

/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
/// </summary>
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Trades", "2"},
{"Average Win", "1.63%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "7.292%"},
{"Drawdown", "1.300%"},
{"Expectancy", "0"},
{"Net Profit", "1.634%"},
{"Sharpe Ratio", "2.476"},
{"Probabilistic Sharpe Ratio", "92.194%"},
{"Loss Rate", "0%"},
{"Win Rate", "100%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0.006"},
{"Beta", "0.158"},
{"Annual Standard Deviation", "0.032"},
{"Annual Variance", "0.001"},
{"Information Ratio", "-4.89"},
{"Tracking Error", "0.08"},
{"Treynor Ratio", "0.509"},
{"Total Fees", "$3.70"},
{"Fitness Score", "0.019"},
{"Kelly Criterion Estimate", "0"},
{"Kelly Criterion Probability Value", "0"},
{"Sortino Ratio", "1.362"},
{"Return Over Maximum Drawdown", "9.699"},
{"Portfolio Turnover", "0.022"},
{"Total Insights Generated", "0"},
{"Total Insights Closed", "0"},
{"Total Insights Analysis Completed", "0"},
{"Long Insight Count", "0"},
{"Short Insight Count", "0"},
{"Long/Short Ratio", "100%"},
{"Estimated Monthly Alpha Value", "$0"},
{"Total Accumulated Estimated Alpha Value", "$0"},
{"Mean Population Estimated Insight Value", "$0"},
{"Mean Population Direction", "0%"},
{"Mean Population Magnitude", "0%"},
{"Rolling Averaged Population Direction", "0%"},
{"Rolling Averaged Population Magnitude", "0%"},
{"OrderListHash", "-1252326142"}
};
}
}
1 change: 1 addition & 0 deletions Algorithm.CSharp/QuantConnect.Algorithm.CSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<Compile Include="AltData\TiingoNewsAlgorithm.cs" />
<Compile Include="AutomaticIndicatorWarmupDataTypeRegressionAlgorithm.cs" />
<Compile Include="AutomaticIndicatorWarmupRegressionAlgorithm.cs" />
<Compile Include="DelistedFutureLiquidateRegressionAlgorithm.cs" />
<Compile Include="EmaCrossFuturesFrontMonthAlgorithm.cs" />
<Compile Include="OpenInterestFuturesRegressionAlgorithm.cs" />
<Compile Include="CustomPartialFillModelAlgorithm.cs" />
Expand Down
5 changes: 4 additions & 1 deletion Engine/DataFeeds/Enumerators/DelistingEventProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ public IEnumerable<BaseData> GetEvents(NewTradableDateEventArgs eventArgs)
{
if (_config.Symbol == eventArgs.Symbol)
{
if (!_delistedWarning && eventArgs.Date >= _delistingDate)
// we send the delisting warning when we reach the delisting date, here we make sure we compare using the date component
// of the delisting date since for example some futures can trade a few hours in their delisting date, else we would skip on
// emitting the delisting warning, which triggers us to handle liquidation once delisted
if (!_delistedWarning && eventArgs.Date >= _delistingDate.Date)
{
_delistedWarning = true;
var price = eventArgs.LastBaseData?.Price ?? 0;
Expand Down

0 comments on commit 9931940

Please sign in to comment.