|
| 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 System.Linq; |
| 18 | +using QuantConnect.Brokerages; |
| 19 | +using QuantConnect.Data; |
| 20 | +using QuantConnect.Data.UniverseSelection; |
| 21 | +using QuantConnect.Interfaces; |
| 22 | + |
| 23 | +namespace QuantConnect.Algorithm.CSharp |
| 24 | +{ |
| 25 | + /// <summary> |
| 26 | + /// Regression algorithm asserting that a currency added at runtime (here BTCEUR, from a scheduled event) has its |
| 27 | + /// conversion rate seeded right away, so using it immediately no longer throws because the rate is still 0. |
| 28 | + /// </summary> |
| 29 | + public class RuntimeCurrencyConversionSeedingRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition |
| 30 | + { |
| 31 | + private Symbol _ltcusd; |
| 32 | + private bool _addedAtRuntime; |
| 33 | + private bool _assertedSeeded; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized. |
| 37 | + /// </summary> |
| 38 | + public override void Initialize() |
| 39 | + { |
| 40 | + SetStartDate(2018, 4, 5); |
| 41 | + SetEndDate(2018, 4, 5); |
| 42 | + SetBrokerageModel(BrokerageName.GDAX, AccountType.Cash); |
| 43 | + SetCash(100000); |
| 44 | + |
| 45 | + // Account currency asset that funds the loop |
| 46 | + _ltcusd = AddCrypto("LTCUSD", Resolution.Minute).Symbol; |
| 47 | + |
| 48 | + // Add a non-account-currency asset at runtime, mirroring users that add assets from a scheduled event |
| 49 | + Schedule.On(DateRules.EveryDay(), TimeRules.At(10, 0), () => |
| 50 | + { |
| 51 | + if (_addedAtRuntime) |
| 52 | + { |
| 53 | + return; |
| 54 | + } |
| 55 | + _addedAtRuntime = true; |
| 56 | + AddCrypto("BTCEUR", Resolution.Minute); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Runs right after the runtime-added security is wired up, the earliest point it can be used |
| 62 | + /// </summary> |
| 63 | + public override void OnSecuritiesChanged(SecurityChanges changes) |
| 64 | + { |
| 65 | + if (!changes.AddedSecurities.Any(security => security.Symbol.Value == "BTCEUR")) |
| 66 | + { |
| 67 | + return; |
| 68 | + } |
| 69 | + _assertedSeeded = true; |
| 70 | + |
| 71 | + // With the fix these are already seeded here. Without it they would still be 0 and the conversion below would throw. |
| 72 | + var eur = Portfolio.CashBook["EUR"]; |
| 73 | + var btc = Portfolio.CashBook["BTC"]; |
| 74 | + if (eur.ConversionRate == 0 || btc.ConversionRate == 0) |
| 75 | + { |
| 76 | + throw new RegressionTestException( |
| 77 | + $"Runtime-added currency conversion rates were not seeded (EUR={eur.ConversionRate}, BTC={btc.ConversionRate})"); |
| 78 | + } |
| 79 | + |
| 80 | + if (Portfolio.CashBook.ConvertToAccountCurrency(100m, "EUR") <= 0) |
| 81 | + { |
| 82 | + throw new RegressionTestException("Expected a positive EUR -> account currency conversion"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here. |
| 88 | + /// </summary> |
| 89 | + /// <param name="slice">Slice object keyed by symbol containing the stock data</param> |
| 90 | + public override void OnData(Slice slice) |
| 91 | + { |
| 92 | + if (!_addedAtRuntime || Portfolio.Invested) |
| 93 | + { |
| 94 | + return; |
| 95 | + } |
| 96 | + |
| 97 | + if (Securities[_ltcusd].Price != 0) |
| 98 | + { |
| 99 | + SetHoldings(_ltcusd, 0.5); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// <summary> |
| 104 | + /// Makes sure the seeding path was actually exercised so the test can't silently pass |
| 105 | + /// </summary> |
| 106 | + public override void OnEndOfAlgorithm() |
| 107 | + { |
| 108 | + if (!_assertedSeeded) |
| 109 | + { |
| 110 | + throw new RegressionTestException("BTCEUR was never added at runtime, the seeding path was not exercised"); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. |
| 116 | + /// </summary> |
| 117 | + public bool CanRunLocally { get; } = true; |
| 118 | + |
| 119 | + /// <summary> |
| 120 | + /// This is used by the regression test system to indicate which languages this algorithm is written in. |
| 121 | + /// </summary> |
| 122 | + public List<Language> Languages { get; } = new() { Language.CSharp }; |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// Data Points count of all timeslices of algorithm |
| 126 | + /// </summary> |
| 127 | + public long DataPoints => 6005; |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Data Points count of the algorithm history |
| 131 | + /// </summary> |
| 132 | + public int AlgorithmHistoryDataPoints => 591; |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Final status of the algorithm |
| 136 | + /// </summary> |
| 137 | + public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed; |
| 138 | + |
| 139 | + /// <summary> |
| 140 | + /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm |
| 141 | + /// </summary> |
| 142 | + public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string> |
| 143 | + { |
| 144 | + {"Total Orders", "1"}, |
| 145 | + {"Average Win", "0%"}, |
| 146 | + {"Average Loss", "0%"}, |
| 147 | + {"Compounding Annual Return", "0%"}, |
| 148 | + {"Drawdown", "0%"}, |
| 149 | + {"Expectancy", "0"}, |
| 150 | + {"Start Equity", "100000.00"}, |
| 151 | + {"End Equity", "99064.52"}, |
| 152 | + {"Net Profit", "0%"}, |
| 153 | + {"Sharpe Ratio", "0"}, |
| 154 | + {"Sortino Ratio", "0"}, |
| 155 | + {"Probabilistic Sharpe Ratio", "0%"}, |
| 156 | + {"Loss Rate", "0%"}, |
| 157 | + {"Win Rate", "0%"}, |
| 158 | + {"Profit-Loss Ratio", "0"}, |
| 159 | + {"Alpha", "0"}, |
| 160 | + {"Beta", "0"}, |
| 161 | + {"Annual Standard Deviation", "0"}, |
| 162 | + {"Annual Variance", "0"}, |
| 163 | + {"Information Ratio", "0"}, |
| 164 | + {"Tracking Error", "0"}, |
| 165 | + {"Treynor Ratio", "0"}, |
| 166 | + {"Total Fees", "$149.18"}, |
| 167 | + {"Estimated Strategy Capacity", "$160000.00"}, |
| 168 | + {"Lowest Capacity Asset", "LTCUSD 2XR"}, |
| 169 | + {"Portfolio Turnover", "50.20%"}, |
| 170 | + {"Drawdown Recovery", "0"}, |
| 171 | + {"OrderListHash", "69d27a394cffbd938ec23fbb451f37ae"} |
| 172 | + }; |
| 173 | + } |
| 174 | +} |
0 commit comments