|
| 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 System; |
| 18 | +using System.Linq; |
| 19 | +using NUnit.Framework; |
| 20 | +using System.Threading; |
| 21 | +using QuantConnect.Data; |
| 22 | +using QuantConnect.Tests; |
| 23 | +using QuantConnect.Logging; |
| 24 | +using QuantConnect.Data.Market; |
| 25 | +using QuantConnect.Configuration; |
| 26 | +using System.Collections.Generic; |
| 27 | +using QuantConnect.Lean.Engine.DataFeeds; |
| 28 | +using QuantConnect.Lean.Engine.DataFeeds.Enumerators; |
| 29 | + |
| 30 | +namespace QuantConnect.Lean.DataSource.Polygon.Tests |
| 31 | +{ |
| 32 | + [TestFixture] |
| 33 | + [Explicit("Tests are dependent on network and take long")] |
| 34 | + public class PolygonDataProviderFairMarketValueTests |
| 35 | + { |
| 36 | + private List<SubscriptionDataConfig> GetConfigs(Resolution resolution) |
| 37 | + { |
| 38 | + var symbols = new Symbol[] |
| 39 | + { |
| 40 | + Symbols.AAPL, |
| 41 | + Symbols.MSFT, |
| 42 | + Symbol.CreateOption(Symbols.AAPL, Symbols.AAPL.ID.Market, SecurityType.Option.DefaultOptionStyle(), OptionRight.Call, 227.5m, new(2025, 08, 29)), |
| 43 | + Symbol.CreateOption(Symbols.MSFT, Symbols.MSFT.ID.Market, SecurityType.Option.DefaultOptionStyle(), OptionRight.Call, 502.5m, new(2025, 08, 29)), |
| 44 | + //Symbols.SPX, |
| 45 | + Symbol.CreateOption(Symbols.SPX, "SPXW", Symbols.SPX.ID.Market, SecurityType.IndexOption.DefaultOptionStyle(), OptionRight.Call, 6485m, new(2025, 08, 29)) |
| 46 | + }; |
| 47 | + |
| 48 | + return [.. symbols.SelectMany(symbol => GetSubscriptionDataConfigs(symbol, resolution))]; |
| 49 | + } |
| 50 | + |
| 51 | + [TestCase(Resolution.Tick, 10)] |
| 52 | + [TestCase(Resolution.Second, 5)] |
| 53 | + [TestCase(Resolution.Minute, 1)] |
| 54 | + public void StreamDataOnDifferentSecuritiesTypes(Resolution resolution, int expectedReceiveAmountData) |
| 55 | + { |
| 56 | + var mockDateTimeAfterOpenExchange = DateTime.UtcNow.Date.AddHours(9).AddMinutes(30).AddSeconds(59).ConvertToUtc(TimeZones.NewYork); |
| 57 | + TestablePolygonDataProvider.TimeProviderInstance = new ManualTimeProvider(mockDateTimeAfterOpenExchange); |
| 58 | + using var polygon = new TestablePolygonDataProvider(Config.Get("polygon-api-key"), Config.Get("polygon-license-type")); |
| 59 | + |
| 60 | + var configs = GetConfigs(resolution); |
| 61 | + |
| 62 | + var receivedData = new Dictionary<Symbol, List<BaseData>>(); |
| 63 | + |
| 64 | + var resetEvent = new AutoResetEvent(false); |
| 65 | + var receivedAllData = default(bool); |
| 66 | + |
| 67 | + foreach (var config in configs) |
| 68 | + { |
| 69 | + receivedData[config.Symbol] = []; |
| 70 | + |
| 71 | + polygon.Subscribe(config, (sender, args) => |
| 72 | + { |
| 73 | + if (args is NewDataAvailableEventArgs newData && newData.DataPoint is BaseData bd) |
| 74 | + { |
| 75 | + Log.Trace($"{bd}. Time span: {bd.Time} - {bd.EndTime}"); |
| 76 | + lock (receivedData) |
| 77 | + { |
| 78 | + receivedData[bd.Symbol].Add(bd); |
| 79 | + |
| 80 | + if ((bd is Tick t && t.TickType == TickType.OpenInterest) || bd is OpenInterest) |
| 81 | + { |
| 82 | + // Prevent from repeating request |
| 83 | + TestablePolygonDataProvider.TimeProviderInstance.SetCurrentTimeUtc(DateTime.UtcNow); |
| 84 | + } |
| 85 | + |
| 86 | + if (!receivedAllData && receivedData.Values.All(x => x.Count >= expectedReceiveAmountData)) |
| 87 | + { |
| 88 | + receivedAllData = true; |
| 89 | + resetEvent.Set(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + Assert.IsTrue(resetEvent.WaitOne(TimeSpan.FromSeconds(120))); |
| 97 | + |
| 98 | + Log.Trace("Unsubscribing symbols"); |
| 99 | + foreach (var config in configs) |
| 100 | + { |
| 101 | + polygon.Unsubscribe(config); |
| 102 | + } |
| 103 | + |
| 104 | + resetEvent.WaitOne(TimeSpan.FromSeconds(10)); |
| 105 | + |
| 106 | + var dataSummary = receivedData.ToDictionary(pair => pair.Key, pair => pair.Value.Count); |
| 107 | + Log.Trace($"Received {receivedData.Count} symbols with data points -> {string.Join(", ", dataSummary.Select(kvp => $"{kvp.Key}: {kvp.Value}"))}"); |
| 108 | + |
| 109 | + Assert.IsTrue(receivedData.Values.All(d => d.Count >= expectedReceiveAmountData)); |
| 110 | + |
| 111 | + foreach (var (symbol, baseData) in receivedData.Where(x => x.Key.SecurityType.IsOption())) |
| 112 | + { |
| 113 | + var openInterestDetected = default(bool); |
| 114 | + foreach (var data in baseData) |
| 115 | + { |
| 116 | + if (data is Tick t && t.TickType == TickType.OpenInterest) |
| 117 | + { |
| 118 | + openInterestDetected = true; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + Assert.IsTrue(openInterestDetected); |
| 123 | + } |
| 124 | + |
| 125 | + foreach (var (symbol, baseData) in receivedData) |
| 126 | + { |
| 127 | + foreach (var data in baseData) |
| 128 | + { |
| 129 | + switch (data) |
| 130 | + { |
| 131 | + case OpenInterest oi: |
| 132 | + Assert.Greater(oi.Value, 0m); |
| 133 | + break; |
| 134 | + case Tick t: |
| 135 | + switch (t.TickType) |
| 136 | + { |
| 137 | + case TickType.OpenInterest: |
| 138 | + Assert.Greater(t.Value, 0m); |
| 139 | + break; |
| 140 | + case TickType.Trade: |
| 141 | + Assert.Greater(t.LastPrice, 0m); |
| 142 | + Assert.AreEqual(t.Quantity, 0m); |
| 143 | + break; |
| 144 | + default: |
| 145 | + throw new NotSupportedException(); |
| 146 | + } |
| 147 | + break; |
| 148 | + case TradeBar tb: |
| 149 | + Assert.Greater(tb.Open, 0m); |
| 150 | + Assert.Greater(tb.High, 0m); |
| 151 | + Assert.Greater(tb.Low, 0m); |
| 152 | + Assert.Greater(tb.Close, 0m); |
| 153 | + if (resolution >= Resolution.Minute) |
| 154 | + { |
| 155 | + // For 1-minute or higher aggregates, volume is expected (may be zero). |
| 156 | + // Example: {"ev":"AM","sym":"O:MSFT250829C00502500","v":0,"av":358,...} |
| 157 | + Assert.GreaterOrEqual(tb.Volume, 0m); |
| 158 | + } |
| 159 | + else |
| 160 | + { |
| 161 | + // For tick/fmv data, volume is not reported and should remain zero |
| 162 | + Assert.AreEqual(tb.Volume, 0m); |
| 163 | + } |
| 164 | + break; |
| 165 | + default: |
| 166 | + throw new NotSupportedException(); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + private static List<SubscriptionDataConfig> GetSubscriptionDataConfigs(Symbol symbol, Resolution resolution) |
| 173 | + { |
| 174 | + var subs = new List<SubscriptionDataConfig>(); |
| 175 | + if (resolution == Resolution.Tick) |
| 176 | + { |
| 177 | + subs.Add(new SubscriptionDataConfig(PolygonDataProviderBaseTests.GetSubscriptionDataConfig<Tick>(symbol, resolution), tickType: TickType.Trade)); |
| 178 | + |
| 179 | + if (symbol.SecurityType.IsOption()) |
| 180 | + { |
| 181 | + subs.Add(new SubscriptionDataConfig(PolygonDataProviderBaseTests.GetSubscriptionDataConfig<Tick>(symbol, resolution), tickType: TickType.OpenInterest)); |
| 182 | + } |
| 183 | + |
| 184 | + return subs; |
| 185 | + } |
| 186 | + |
| 187 | + subs.Add(PolygonDataProviderBaseTests.GetSubscriptionDataConfig<TradeBar>(symbol, resolution)); |
| 188 | + |
| 189 | + if (symbol.SecurityType.IsOption()) |
| 190 | + { |
| 191 | + subs.Add(PolygonDataProviderBaseTests.GetSubscriptionDataConfig<OpenInterest>(symbol, resolution)); |
| 192 | + } |
| 193 | + |
| 194 | + return subs; |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments