|
| 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 System.Collections.Generic; |
| 18 | +using System.IO; |
| 19 | +using System.Linq; |
| 20 | +using QuantConnect.Data; |
| 21 | +using QuantConnect.Interfaces; |
| 22 | + |
| 23 | +namespace QuantConnect.Algorithm.CSharp |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Adds a universe with a custom data type and retrieves historical data |
| 27 | + /// while preserving the custom data type. |
| 28 | + /// </summary> |
| 29 | + public class PersistentCustomDataUniverseRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition |
| 30 | + { |
| 31 | + private Symbol _universeSymbol; |
| 32 | + private bool _dataReceived; |
| 33 | + |
| 34 | + public override void Initialize() |
| 35 | + { |
| 36 | + SetStartDate(2018, 6, 1); |
| 37 | + SetEndDate(2018, 6, 19); |
| 38 | + |
| 39 | + var universe = AddUniverse<StockDataSource>("my-stock-data-source", Resolution.Daily, UniverseSelector); |
| 40 | + _universeSymbol = universe.Symbol; |
| 41 | + RetrieveHistoricalData(); |
| 42 | + } |
| 43 | + |
| 44 | + private IEnumerable<Symbol> UniverseSelector(IEnumerable<BaseData> data) |
| 45 | + { |
| 46 | + foreach (var item in data.OfType<StockDataSource>()) |
| 47 | + { |
| 48 | + yield return item.Symbol; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private void RetrieveHistoricalData() |
| 53 | + { |
| 54 | + var history = History<StockDataSource>(_universeSymbol, new DateTime(2018, 1, 1), new DateTime(2018, 6, 1), Resolution.Daily).ToList(); |
| 55 | + if (history.Count == 0) |
| 56 | + { |
| 57 | + throw new RegressionTestException($"No historical data received for the symbol {_universeSymbol}."); |
| 58 | + } |
| 59 | + |
| 60 | + // Ensure all values are of type StockDataSource |
| 61 | + foreach (var item in history) |
| 62 | + { |
| 63 | + if (item is not StockDataSource) |
| 64 | + { |
| 65 | + throw new RegressionTestException($"Unexpected data type in history. Expected StockDataSource but received {item.GetType().Name}."); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public override void OnData(Slice slice) |
| 71 | + { |
| 72 | + if (!slice.ContainsKey(_universeSymbol)) |
| 73 | + { |
| 74 | + throw new RegressionTestException($"No data received for the universe symbol: {_universeSymbol}."); |
| 75 | + } |
| 76 | + if (!_dataReceived) |
| 77 | + { |
| 78 | + RetrieveHistoricalData(); |
| 79 | + } |
| 80 | + _dataReceived = true; |
| 81 | + } |
| 82 | + |
| 83 | + public override void OnEndOfAlgorithm() |
| 84 | + { |
| 85 | + if (!_dataReceived) |
| 86 | + { |
| 87 | + throw new RegressionTestException("No data was received after the universe selection."); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Our custom data type that defines where to get and how to read our backtest and live data. |
| 94 | + /// </summary> |
| 95 | + public class StockDataSource : BaseData |
| 96 | + { |
| 97 | + public List<string> Symbols { get; set; } |
| 98 | + |
| 99 | + public StockDataSource() |
| 100 | + { |
| 101 | + Symbols = new List<string>(); |
| 102 | + } |
| 103 | + |
| 104 | + public override DateTime EndTime |
| 105 | + { |
| 106 | + get { return Time + Period; } |
| 107 | + set { Time = value - Period; } |
| 108 | + } |
| 109 | + |
| 110 | + public TimeSpan Period |
| 111 | + { |
| 112 | + get { return QuantConnect.Time.OneDay; } |
| 113 | + } |
| 114 | + |
| 115 | + public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode) |
| 116 | + { |
| 117 | + var source = Path.Combine("..", "..", "..", "Tests", "TestData", "daily-stock-picker-backtest.csv"); |
| 118 | + return new SubscriptionDataSource(source); |
| 119 | + } |
| 120 | + |
| 121 | + public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode) |
| 122 | + { |
| 123 | + if (string.IsNullOrWhiteSpace(line) || !char.IsDigit(line[0])) |
| 124 | + { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + var stocks = new StockDataSource { Symbol = config.Symbol }; |
| 129 | + |
| 130 | + try |
| 131 | + { |
| 132 | + var csv = line.ToCsv(); |
| 133 | + stocks.Time = DateTime.ParseExact(csv[0], "yyyyMMdd", null); |
| 134 | + stocks.Symbols.AddRange(csv[1..]); |
| 135 | + } |
| 136 | + catch (FormatException) |
| 137 | + { |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + return stocks; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. |
| 147 | + /// </summary> |
| 148 | + public bool CanRunLocally { get; } = true; |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// This is used by the regression test system to indicate which languages this algorithm is written in. |
| 152 | + /// </summary> |
| 153 | + public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python }; |
| 154 | + |
| 155 | + /// <summary> |
| 156 | + /// Data Points count of all timeslices of algorithm |
| 157 | + /// </summary> |
| 158 | + public long DataPoints => 8767; |
| 159 | + |
| 160 | + /// <summary> |
| 161 | + /// Data Points count of the algorithm history |
| 162 | + /// </summary> |
| 163 | + public int AlgorithmHistoryDataPoints => 298; |
| 164 | + |
| 165 | + /// <summary> |
| 166 | + /// Final status of the algorithm |
| 167 | + /// </summary> |
| 168 | + public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed; |
| 169 | + |
| 170 | + /// <summary> |
| 171 | + /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm |
| 172 | + /// </summary> |
| 173 | + public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string> |
| 174 | + { |
| 175 | + {"Total Orders", "0"}, |
| 176 | + {"Average Win", "0%"}, |
| 177 | + {"Average Loss", "0%"}, |
| 178 | + {"Compounding Annual Return", "0%"}, |
| 179 | + {"Drawdown", "0%"}, |
| 180 | + {"Expectancy", "0"}, |
| 181 | + {"Start Equity", "100000"}, |
| 182 | + {"End Equity", "100000"}, |
| 183 | + {"Net Profit", "0%"}, |
| 184 | + {"Sharpe Ratio", "0"}, |
| 185 | + {"Sortino Ratio", "0"}, |
| 186 | + {"Probabilistic Sharpe Ratio", "0%"}, |
| 187 | + {"Loss Rate", "0%"}, |
| 188 | + {"Win Rate", "0%"}, |
| 189 | + {"Profit-Loss Ratio", "0"}, |
| 190 | + {"Alpha", "0"}, |
| 191 | + {"Beta", "0"}, |
| 192 | + {"Annual Standard Deviation", "0"}, |
| 193 | + {"Annual Variance", "0"}, |
| 194 | + {"Information Ratio", "-3.9"}, |
| 195 | + {"Tracking Error", "0.045"}, |
| 196 | + {"Treynor Ratio", "0"}, |
| 197 | + {"Total Fees", "$0.00"}, |
| 198 | + {"Estimated Strategy Capacity", "$0"}, |
| 199 | + {"Lowest Capacity Asset", ""}, |
| 200 | + {"Portfolio Turnover", "0%"}, |
| 201 | + {"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"} |
| 202 | + }; |
| 203 | + } |
| 204 | +} |
0 commit comments