|
| 1 | +/* |
| 2 | + * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. |
| 3 | + * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System.Collections.Generic; |
| 17 | +using QuantConnect.Data; |
| 18 | +using QuantConnect.Data.Market; |
| 19 | +using QuantConnect.Interfaces; |
| 20 | +using QuantConnect.Orders; |
| 21 | +using QuantConnect.Orders.Slippage; |
| 22 | + |
| 23 | +namespace QuantConnect.Algorithm.CSharp |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Regression algorithm asserting that market on open orders using daily data are filled at the bar open |
| 27 | + /// with the slippage referenced to that same open price, not to the bar close which is not known at the open. |
| 28 | + /// See GH issue 9753 |
| 29 | + /// </summary> |
| 30 | + public class MarketOnOpenOrderSlippageRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition |
| 31 | + { |
| 32 | + private const decimal SlippagePercent = 0.01m; |
| 33 | + private Symbol _symbol; |
| 34 | + private int _fills; |
| 35 | + |
| 36 | + public override void Initialize() |
| 37 | + { |
| 38 | + SetStartDate(2013, 10, 07); |
| 39 | + SetEndDate(2013, 10, 11); |
| 40 | + SetCash(100000); |
| 41 | + |
| 42 | + var security = AddEquity("SPY", Resolution.Daily); |
| 43 | + security.SetSlippageModel(new ConstantSlippageModel(SlippagePercent)); |
| 44 | + _symbol = security.Symbol; |
| 45 | + } |
| 46 | + |
| 47 | + public override void OnData(Slice slice) |
| 48 | + { |
| 49 | + if (!slice.Bars.ContainsKey(_symbol) || Transactions.GetOpenOrders(_symbol).Count > 0) |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // alternate buys and sells so both directions are checked |
| 55 | + MarketOnOpenOrder(_symbol, Portfolio[_symbol].Invested ? -100 : 100); |
| 56 | + } |
| 57 | + |
| 58 | + public override void OnOrderEvent(OrderEvent orderEvent) |
| 59 | + { |
| 60 | + if (orderEvent.Status != OrderStatus.Filled) |
| 61 | + { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + // the fill happens when the daily bar arrives, so this is the bar the order was filled with |
| 66 | + var bar = Securities[_symbol].Cache.GetData<TradeBar>(); |
| 67 | + if (bar.Open == bar.Close) |
| 68 | + { |
| 69 | + throw new RegressionTestException($"Expected the fill bar open and close to differ so the slippage reference can be asserted: {bar}"); |
| 70 | + } |
| 71 | + |
| 72 | + var slippage = bar.Open * SlippagePercent; |
| 73 | + var expectedFillPrice = orderEvent.Direction == OrderDirection.Buy ? bar.Open + slippage : bar.Open - slippage; |
| 74 | + if (orderEvent.FillPrice != expectedFillPrice) |
| 75 | + { |
| 76 | + throw new RegressionTestException($"Expected {orderEvent.Direction} fill price {expectedFillPrice} (open {bar.Open} +/- {SlippagePercent:P} slippage) but was {orderEvent.FillPrice}. Bar: {bar}"); |
| 77 | + } |
| 78 | + |
| 79 | + _fills++; |
| 80 | + } |
| 81 | + |
| 82 | + public override void OnEndOfAlgorithm() |
| 83 | + { |
| 84 | + if (_fills < 2) |
| 85 | + { |
| 86 | + throw new RegressionTestException($"Expected at least a buy and a sell fill but got {_fills}"); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. |
| 92 | + /// </summary> |
| 93 | + public bool CanRunLocally { get; } = true; |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// This is used by the regression test system to indicate which languages this algorithm is written in. |
| 97 | + /// </summary> |
| 98 | + public List<Language> Languages { get; } = new() { Language.CSharp }; |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Data Points count of all timeslices of algorithm |
| 102 | + /// </summary> |
| 103 | + public long DataPoints => 48; |
| 104 | + |
| 105 | + /// <summary> |
| 106 | + /// Data Points count of the algorithm history |
| 107 | + /// </summary> |
| 108 | + public int AlgorithmHistoryDataPoints => 0; |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Final status of the algorithm |
| 112 | + /// </summary> |
| 113 | + public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed; |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm |
| 117 | + /// </summary> |
| 118 | + public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string> |
| 119 | + { |
| 120 | + {"Total Orders", "5"}, |
| 121 | + {"Average Win", "0%"}, |
| 122 | + {"Average Loss", "-0.29%"}, |
| 123 | + {"Compounding Annual Return", "-36.910%"}, |
| 124 | + {"Drawdown", "0.600%"}, |
| 125 | + {"Expectancy", "-1"}, |
| 126 | + {"Start Equity", "100000"}, |
| 127 | + {"End Equity", "99412.82"}, |
| 128 | + {"Net Profit", "-0.587%"}, |
| 129 | + {"Sharpe Ratio", "-14.31"}, |
| 130 | + {"Sortino Ratio", "-19.441"}, |
| 131 | + {"Probabilistic Sharpe Ratio", "1.568%"}, |
| 132 | + {"Loss Rate", "100%"}, |
| 133 | + {"Win Rate", "0%"}, |
| 134 | + {"Profit-Loss Ratio", "0"}, |
| 135 | + {"Alpha", "-0.502"}, |
| 136 | + {"Beta", "0.093"}, |
| 137 | + {"Annual Standard Deviation", "0.022"}, |
| 138 | + {"Annual Variance", "0"}, |
| 139 | + {"Information Ratio", "-11.354"}, |
| 140 | + {"Tracking Error", "0.202"}, |
| 141 | + {"Treynor Ratio", "-3.402"}, |
| 142 | + {"Total Fees", "$4.00"}, |
| 143 | + {"Estimated Strategy Capacity", "$1300000000.00"}, |
| 144 | + {"Lowest Capacity Asset", "SPY R735QTJ8XC9X"}, |
| 145 | + {"Portfolio Turnover", "11.63%"}, |
| 146 | + {"Drawdown Recovery", "0"}, |
| 147 | + {"OrderListHash", "4bbdfd7aaf0f2e4fa6cc9226fbf3d9e8"} |
| 148 | + }; |
| 149 | + } |
| 150 | +} |
0 commit comments