|
| 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 | + |
| 17 | +using QuantConnect.Data; |
| 18 | +using QuantConnect.Data.Consolidators; |
| 19 | +using QuantConnect.Data.Market; |
| 20 | +using QuantConnect.Data.UniverseSelection; |
| 21 | +using QuantConnect.Interfaces; |
| 22 | +using System; |
| 23 | +using System.Collections.Generic; |
| 24 | + |
| 25 | +namespace QuantConnect.Algorithm.CSharp |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// A demonstration of consolidating options data into larger bars for your algorithm. |
| 29 | + /// </summary> |
| 30 | + public class BasicTemplateOptionsConsolidationAlgorithm: QCAlgorithm, IRegressionAlgorithmDefinition |
| 31 | + { |
| 32 | + private Dictionary<Symbol, IDataConsolidator> _consolidators = new(); |
| 33 | + |
| 34 | + public override void Initialize() |
| 35 | + { |
| 36 | + SetStartDate(2013, 10, 7); |
| 37 | + SetEndDate(2013, 10, 11); |
| 38 | + SetCash(1000000); |
| 39 | + |
| 40 | + var option = AddOption("SPY"); |
| 41 | + option.SetFilter(-2, 2, 0, 189); |
| 42 | + } |
| 43 | + |
| 44 | + public void OnQuoteBarConsolidated(object sender, QuoteBar quoteBar) |
| 45 | + { |
| 46 | + Log($"OnQuoteBarConsolidated called on {Time}"); |
| 47 | + Log(quoteBar.ToString()); |
| 48 | + } |
| 49 | + |
| 50 | + public void OnTradeBarConsolidated(object sender, TradeBar tradeBar) |
| 51 | + { |
| 52 | + Log($"OnQuoteBarConsolidated called on {Time}"); |
| 53 | + Log(tradeBar.ToString()); |
| 54 | + } |
| 55 | + |
| 56 | + public override void OnSecuritiesChanged(SecurityChanges changes) |
| 57 | + { |
| 58 | + foreach(var security in changes.AddedSecurities) |
| 59 | + { |
| 60 | + IDataConsolidator consolidator; |
| 61 | + if (security.Type == SecurityType.Equity) |
| 62 | + { |
| 63 | + consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(5)); |
| 64 | + (consolidator as TradeBarConsolidator).DataConsolidated += OnTradeBarConsolidated; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + consolidator = new QuoteBarConsolidator(new TimeSpan(0, 5, 0)); |
| 69 | + (consolidator as QuoteBarConsolidator).DataConsolidated += OnQuoteBarConsolidated; |
| 70 | + } |
| 71 | + |
| 72 | + SubscriptionManager.AddConsolidator(security.Symbol, consolidator); |
| 73 | + _consolidators[security.Symbol] = consolidator; |
| 74 | + } |
| 75 | + |
| 76 | + foreach(var security in changes.RemovedSecurities) |
| 77 | + { |
| 78 | + _consolidators.Remove(security.Symbol, out var consolidator); |
| 79 | + SubscriptionManager.RemoveConsolidator(security.Symbol, consolidator); |
| 80 | + |
| 81 | + if (security.Type == SecurityType.Equity) |
| 82 | + { |
| 83 | + (consolidator as TradeBarConsolidator).DataConsolidated -= OnTradeBarConsolidated; |
| 84 | + } |
| 85 | + else |
| 86 | + { |
| 87 | + (consolidator as QuoteBarConsolidator).DataConsolidated -= OnQuoteBarConsolidated; |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm. |
| 94 | + /// </summary> |
| 95 | + public bool CanRunLocally { get; } = true; |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// This is used by the regression test system to indicate which languages this algorithm is written in. |
| 99 | + /// </summary> |
| 100 | + public Language[] Languages { get; } = { Language.CSharp, Language.Python }; |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Data Points count of all timeslices of algorithm |
| 104 | + /// </summary> |
| 105 | + public long DataPoints => 3943; |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Data Points count of the algorithm history |
| 109 | + /// </summary> |
| 110 | + public int AlgorithmHistoryDataPoints => 0; |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// This is used by the regression test system to indicate what the expected statistics are from running the algorithm |
| 114 | + /// </summary> |
| 115 | + public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string> |
| 116 | + { |
| 117 | + {"Total Trades", "0"}, |
| 118 | + {"Average Win", "0%"}, |
| 119 | + {"Average Loss", "0%"}, |
| 120 | + {"Compounding Annual Return", "0%"}, |
| 121 | + {"Drawdown", "0%"}, |
| 122 | + {"Expectancy", "0"}, |
| 123 | + {"Net Profit", "0%"}, |
| 124 | + {"Sharpe Ratio", "0"}, |
| 125 | + {"Sortino Ratio", "0"}, |
| 126 | + {"Probabilistic Sharpe Ratio", "0%"}, |
| 127 | + {"Loss Rate", "0%"}, |
| 128 | + {"Win Rate", "0%"}, |
| 129 | + {"Profit-Loss Ratio", "0"}, |
| 130 | + {"Alpha", "0"}, |
| 131 | + {"Beta", "0"}, |
| 132 | + {"Annual Standard Deviation", "0"}, |
| 133 | + {"Annual Variance", "0"}, |
| 134 | + {"Information Ratio", "-8.91"}, |
| 135 | + {"Tracking Error", "0.223"}, |
| 136 | + {"Treynor Ratio", "0"}, |
| 137 | + {"Total Fees", "$0.00"}, |
| 138 | + {"Estimated Strategy Capacity", "$0"}, |
| 139 | + {"Lowest Capacity Asset", ""}, |
| 140 | + {"Portfolio Turnover", "0%"}, |
| 141 | + {"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"} |
| 142 | + }; |
| 143 | + } |
| 144 | +} |
0 commit comments