|
| 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; |
| 17 | +using QuantConnect.Data; |
| 18 | +using QuantConnect.Interfaces; |
| 19 | +using QuantConnect.Data.Market; |
| 20 | +using System.Collections.Generic; |
| 21 | +using QuantConnect.Data.Consolidators; |
| 22 | + |
| 23 | +namespace QuantConnect.Algorithm.CSharp |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Regression algorithm show casing and asserting the behavior of creating a consolidator specifying the start time |
| 27 | + /// </summary> |
| 28 | + public class ConsolidatorStartTimeRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition |
| 29 | + { |
| 30 | + private readonly Queue<TimeSpan> _expectedConsolidationTime = new([ |
| 31 | + new TimeSpan(9, 30, 0), |
| 32 | + new TimeSpan(10, 30, 0), |
| 33 | + new TimeSpan(11, 30, 0), |
| 34 | + new TimeSpan(12, 30, 0), |
| 35 | + new TimeSpan(13, 30, 0), |
| 36 | + new TimeSpan(14, 30, 0) |
| 37 | + ]); |
| 38 | + private TradeBarConsolidator consolidator; |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. |
| 42 | + /// </summary> |
| 43 | + public override void Initialize() |
| 44 | + { |
| 45 | + SetStartDate(2013, 10, 04); |
| 46 | + SetEndDate(2013, 10, 04); |
| 47 | + |
| 48 | + AddEquity("SPY", Resolution.Minute); |
| 49 | + |
| 50 | + consolidator = new TradeBarConsolidator(TimeSpan.FromHours(1), startTime: new TimeSpan(9, 30, 0)); |
| 51 | + consolidator.DataConsolidated += BarHandler; |
| 52 | + |
| 53 | + SubscriptionManager.AddConsolidator("SPY", consolidator); |
| 54 | + } |
| 55 | + |
| 56 | + private void BarHandler(object _, TradeBar bar) |
| 57 | + { |
| 58 | + if (Time != bar.EndTime) |
| 59 | + { |
| 60 | + throw new RegressionTestException($"Unexpected consolidation time {bar.Time} != {Time}!"); |
| 61 | + } |
| 62 | + |
| 63 | + var expected = _expectedConsolidationTime.Dequeue(); |
| 64 | + if (bar.Time.TimeOfDay != expected) |
| 65 | + { |
| 66 | + throw new RegressionTestException($"Unexpected consolidation time {bar.Time.TimeOfDay} != {expected}!"); |
| 67 | + } |
| 68 | + |
| 69 | + if (bar.Period != TimeSpan.FromHours(1)) |
| 70 | + { |
| 71 | + throw new RegressionTestException($"Unexpected consolidation period {bar.Period}!"); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public override void OnEndOfAlgorithm() |
| 76 | + { |
| 77 | + if (_expectedConsolidationTime.Count > 0) |
| 78 | + { |
| 79 | + throw new RegressionTestException("Unexpected consolidation times!"); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. |
| 85 | + /// </summary> |
| 86 | + public bool CanRunLocally { get; } = true; |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// This is used by the regression test system to indicate which languages this algorithm is written in. |
| 90 | + /// </summary> |
| 91 | + public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python }; |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Data Points count of all timeslices of algorithm |
| 95 | + /// </summary> |
| 96 | + public long DataPoints => 795; |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Data Points count of the algorithm history |
| 100 | + /// </summary> |
| 101 | + public int AlgorithmHistoryDataPoints => 0; |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Final status of the algorithm |
| 105 | + /// </summary> |
| 106 | + public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed; |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm |
| 110 | + /// </summary> |
| 111 | + public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string> |
| 112 | + { |
| 113 | + {"Total Orders", "0"}, |
| 114 | + {"Average Win", "0%"}, |
| 115 | + {"Average Loss", "0%"}, |
| 116 | + {"Compounding Annual Return", "0%"}, |
| 117 | + {"Drawdown", "0%"}, |
| 118 | + {"Expectancy", "0"}, |
| 119 | + {"Start Equity", "100000"}, |
| 120 | + {"End Equity", "100000"}, |
| 121 | + {"Net Profit", "0%"}, |
| 122 | + {"Sharpe Ratio", "0"}, |
| 123 | + {"Sortino Ratio", "0"}, |
| 124 | + {"Probabilistic Sharpe Ratio", "0%"}, |
| 125 | + {"Loss Rate", "0%"}, |
| 126 | + {"Win Rate", "0%"}, |
| 127 | + {"Profit-Loss Ratio", "0"}, |
| 128 | + {"Alpha", "0"}, |
| 129 | + {"Beta", "0"}, |
| 130 | + {"Annual Standard Deviation", "0"}, |
| 131 | + {"Annual Variance", "0"}, |
| 132 | + {"Information Ratio", "0"}, |
| 133 | + {"Tracking Error", "0"}, |
| 134 | + {"Treynor Ratio", "0"}, |
| 135 | + {"Total Fees", "$0.00"}, |
| 136 | + {"Estimated Strategy Capacity", "$0"}, |
| 137 | + {"Lowest Capacity Asset", ""}, |
| 138 | + {"Portfolio Turnover", "0%"}, |
| 139 | + {"Drawdown Recovery", "0"}, |
| 140 | + {"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"} |
| 141 | + }; |
| 142 | + } |
| 143 | +} |
0 commit comments