forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request QuantConnect#4496 from QuantConnect/bug-4446-delis…
…ted-future-liquidate-on-expiry Fix Delisted.Warning event time
- Loading branch information
Showing
3 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
Algorithm.CSharp/DelistedFutureLiquidateRegressionAlgorithm.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters