chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:02:50 +08:00
commit 0fc60fdcb1
5008 changed files with 910633 additions and 0 deletions
@@ -0,0 +1,91 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using NodaTime;
using QuantConnect.Data;
using QuantConnect.Data.Market;
using QuantConnect.Interfaces;
using System.Collections.Generic;
using HistoryRequest = QuantConnect.Data.HistoryRequest;
namespace QuantConnect.Tests.Engine.HistoricalData
{
/// <summary>
/// Provides FAKE implementation of <see cref="IHistoryProvider"/>
/// </summary>
internal class TestHistoryProvider : HistoryProviderBase
{
public override int DataPointCount => 2;
/// <summary>
/// Initializes this history provider to work for the specified job
/// </summary>
/// <param name="parameters">The initialization parameters</param>
public override void Initialize(HistoryProviderInitializeParameters parameters)
{
}
/// <summary>
/// Gets the history for the requested securities
/// </summary>
/// <param name="requests">The historical data requests</param>
/// <param name="sliceTimeZone">The time zone used when time stamping the slice instances</param>
/// <returns>An enumerable of the slices of data covering the span specified in each request</returns>
public override IEnumerable<Slice> GetHistory(IEnumerable<HistoryRequest> requests, DateTimeZone sliceTimeZone)
{
List<Slice> result = new();
var slice1Date = new DateTime(2008, 01, 03, 5, 0, 0);
var slice2Date = new DateTime(2013, 06, 28, 13, 32, 0);
TradeBar tradeBar1 = new TradeBar { Symbol = Symbols.SPY, Time = DateTime.Now };
TradeBar tradeBar2 = new TradeBar { Symbol = Symbols.AAPL, Time = DateTime.Now };
var quoteBar1 = new QuoteBar { Symbol = Symbols.SPY, Time = DateTime.Now };
var tick1 = new Tick(DateTime.Now, Symbols.SPY, 1.1m, 2.1m) { TickType = TickType.Trade };
var split1 = new Split(Symbols.SPY, DateTime.Now, 1, 1, SplitType.SplitOccurred);
var dividend1 = new Dividend(Symbols.SPY, DateTime.Now, 1, 1);
var delisting1 = new Delisting(Symbols.SPY, DateTime.Now, 1, DelistingType.Delisted);
var symbolChangedEvent1 = new SymbolChangedEvent(Symbols.SPY, DateTime.Now, "SPY", "SP");
Slice slice1 = new Slice(slice1Date, new BaseData[] { tradeBar1, tradeBar2,
quoteBar1, tick1, split1, dividend1, delisting1, symbolChangedEvent1
}, slice1Date);
TradeBar tradeBar3 = new TradeBar { Symbol = Symbols.MSFT, Time = DateTime.Now };
TradeBar tradeBar4 = new TradeBar { Symbol = Symbols.SBIN, Time = DateTime.Now };
var quoteBar2 = new QuoteBar { Symbol = Symbols.SBIN, Time = DateTime.Now };
var tick2 = new Tick(DateTime.Now, Symbols.SBIN, 1.1m, 2.1m) { TickType = TickType.Trade };
var split2 = new Split(Symbols.SBIN, DateTime.Now, 1, 1, SplitType.SplitOccurred);
var dividend2 = new Dividend(Symbols.SBIN, DateTime.Now, 1, 1);
var delisting2 = new Delisting(Symbols.SBIN, DateTime.Now, 1, DelistingType.Delisted);
var symbolChangedEvent2 = new SymbolChangedEvent(Symbols.SBIN, DateTime.Now, "SBIN", "BIN");
Slice slice2 = new Slice(slice2Date, new BaseData[] { tradeBar3, tradeBar4,
quoteBar2, tick2, split2, dividend2, delisting2, symbolChangedEvent2
}, slice2Date);
result.Add(slice1);
result.Add(slice2);
return result;
}
public void TriggerEvents()
{
OnInvalidConfigurationDetected(new InvalidConfigurationDetectedEventArgs(Symbols.SPY, "invalid config"));
OnNumericalPrecisionLimited(new NumericalPrecisionLimitedEventArgs(Symbols.SPY, "invalid config"));
OnStartDateLimited(new StartDateLimitedEventArgs(Symbols.SPY, "invalid config"));
OnDownloadFailed(new DownloadFailedEventArgs(Symbols.SPY, "invalid config"));
OnReaderErrorDetected(new ReaderErrorDetectedEventArgs(Symbols.SPY, "invalid config"));
}
}
}
@@ -0,0 +1,177 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Linq;
using NUnit.Framework;
using QuantConnect.Data;
using QuantConnect.Packets;
using QuantConnect.Util;
using QuantConnect.Interfaces;
using QuantConnect.Tests.Brokerages;
using QuantConnect.Brokerages.Paper;
using QuantConnect.Lean.Engine.DataFeeds;
using QuantConnect.Lean.Engine.HistoricalData;
namespace QuantConnect.Tests.Engine.HistoricalData
{
[TestFixture]
public class HistoryProviderManagerTests
{
private HistoryProviderManager _historyProviderWrapper;
private PaperBrokerage _paperBrokerage;
[SetUp]
public void Setup()
{
Setup(DeploymentTarget.LocalPlatform);
}
[TearDown]
public void TearDown()
{
Composer.Instance.Reset();
_paperBrokerage.DisposeSafely();
}
[Test]
public void DataPointCount()
{
var symbol = Symbol.Create("WM", SecurityType.Equity, Market.USA);
var request = TestsHelpers.GetHistoryRequest(symbol, new DateTime(2008, 01, 01), new DateTime(2008, 01, 05), Resolution.Daily, TickType.Trade);
var result = _historyProviderWrapper.GetHistory(new[] { request }, TimeZones.NewYork).ToList();
Assert.IsNotNull(result);
Assert.IsNotEmpty(result);
Assert.AreEqual(5, _historyProviderWrapper.DataPointCount);
}
[Test]
public void TestEvents()
{
bool invalidConfigurationDetected = new();
bool numericalPrecisionLimited = new();
bool startDateLimited = new();
bool downloadFailed = new();
bool readerErrorDetected = new();
_historyProviderWrapper.InvalidConfigurationDetected += (sender, args) => { invalidConfigurationDetected = true; };
_historyProviderWrapper.NumericalPrecisionLimited += (sender, args) => { numericalPrecisionLimited = true; };
_historyProviderWrapper.StartDateLimited += (sender, args) => { startDateLimited = true; };
_historyProviderWrapper.DownloadFailed += (sender, args) => { downloadFailed = true; };
_historyProviderWrapper.ReaderErrorDetected += (sender, args) => { readerErrorDetected = true; };
var historyProvider = Composer.Instance.GetExportedValueByTypeName<IHistoryProvider>(nameof(TestHistoryProvider));
(historyProvider as TestHistoryProvider).TriggerEvents();
Assert.IsTrue(invalidConfigurationDetected);
Assert.IsTrue(numericalPrecisionLimited);
Assert.IsTrue(startDateLimited);
Assert.IsTrue(downloadFailed);
Assert.IsTrue(readerErrorDetected);
}
[Test]
public void OptionsAreMappedCorrectly()
{
var symbol = Symbol.CreateOption(
"FOXA",
Market.USA,
OptionStyle.American,
OptionRight.Call,
32,
new DateTime(2013, 07, 20));
var request = TestsHelpers.GetHistoryRequest(symbol, new DateTime(2013, 06, 28), new DateTime(2013, 07, 03), Resolution.Minute, TickType.Quote);
var result = _historyProviderWrapper.GetHistory(new[] { request }, TimeZones.NewYork).ToList();
Assert.IsNotEmpty(result);
// assert we fetch the data for the previous and new symbol
var firstBar = result[1].Values.Single();
var lastBar = result.Last().Values.Single();
Assert.IsTrue(firstBar.Symbol.Value.Contains("NWSA"));
Assert.AreEqual(28, firstBar.Time.Date.Day);
Assert.IsTrue(lastBar.Symbol.Value.Contains("FOXA"));
Assert.AreEqual(2, lastBar.Time.Date.Day);
Assert.AreEqual(425, result.Count);
Assert.AreEqual(426, _historyProviderWrapper.DataPointCount);
}
[TestCase(DeploymentTarget.CloudPlatform)]
[TestCase(DeploymentTarget.LocalPlatform)]
public void EquitiesMergedCorrectly(DeploymentTarget deploymentTarget)
{
TearDown();
Setup(deploymentTarget);
var symbol = Symbol.Create("WM", SecurityType.Equity, Market.USA);
var request = TestsHelpers.GetHistoryRequest(symbol, new DateTime(2008, 01, 01), new DateTime(2008, 01, 05), Resolution.Daily, TickType.Trade);
var result = _historyProviderWrapper.GetHistory(new[] { request }, TimeZones.NewYork).ToList();
Assert.IsNotEmpty(result);
var firstBar = result.First().Values.Single();
Assert.AreEqual("WMI", firstBar.Symbol.Value);
Assert.AreEqual(deploymentTarget == DeploymentTarget.CloudPlatform ? 3 : 4, result.Count);
Assert.AreEqual(5, _historyProviderWrapper.DataPointCount);
}
[Test]
public void DataIncreasesInTime()
{
var symbol = Symbol.Create("WM", SecurityType.Equity, Market.USA);
var request = TestsHelpers.GetHistoryRequest(symbol, new DateTime(2008, 01, 01), new DateTime(2008, 01, 05), Resolution.Daily, TickType.Trade);
var result = _historyProviderWrapper.GetHistory(new[] { request }, TimeZones.NewYork).ToList();
var initialTime = DateTime.MinValue;
foreach (var slice in result)
{
Assert.That(slice.UtcTime, Is.GreaterThan(initialTime));
initialTime = slice.UtcTime;
}
}
private void Setup(DeploymentTarget deploymentTarget)
{
_historyProviderWrapper = new();
var historyProviders = Newtonsoft.Json.JsonConvert.SerializeObject(new[] { nameof(SubscriptionDataReaderHistoryProvider), nameof(TestHistoryProvider) });
var jobWithArrayHistoryProviders = new LiveNodePacket
{
HistoryProvider = historyProviders,
DeploymentTarget = deploymentTarget
};
_paperBrokerage = new PaperBrokerage(null, null);
_historyProviderWrapper.SetBrokerage(_paperBrokerage);
_historyProviderWrapper.Initialize(new HistoryProviderInitializeParameters(
jobWithArrayHistoryProviders,
null,
TestGlobals.DataProvider,
TestGlobals.DataCacheProvider,
TestGlobals.MapFileProvider,
TestGlobals.FactorFileProvider,
null,
false,
new DataPermissionManager(),
null,
new AlgorithmSettings()));
}
}
}
@@ -0,0 +1,132 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Linq;
using NUnit.Framework;
using QuantConnect.Data;
using QuantConnect.Data.Auxiliary;
using QuantConnect.Data.Market;
using QuantConnect.Lean.Engine.DataFeeds;
using QuantConnect.Lean.Engine.HistoricalData;
using QuantConnect.Lean.Engine.Storage;
using QuantConnect.Securities;
using QuantConnect.Storage;
using QuantConnect.Util;
using HistoryRequest = QuantConnect.Data.HistoryRequest;
namespace QuantConnect.Tests.Engine.HistoricalData
{
[TestFixture]
public class SubscriptionDataReaderHistoryProviderTests
{
[Test]
public void OptionsAreMappedCorrectly()
{
var historyProvider = new SubscriptionDataReaderHistoryProvider();
historyProvider.Initialize(new HistoryProviderInitializeParameters(
null,
null,
TestGlobals.DataProvider,
TestGlobals.DataCacheProvider,
TestGlobals.MapFileProvider,
TestGlobals.FactorFileProvider,
null,
false,
new DataPermissionManager(),
null,
new AlgorithmSettings()));
var symbol = Symbol.CreateOption(
"FOXA",
Market.USA,
OptionStyle.American,
OptionRight.Call,
32,
new DateTime(2013, 07, 20));
var result = historyProvider.GetHistory(
new[]
{
new HistoryRequest(new DateTime(2013, 06,28),
new DateTime(2013, 07,03),
typeof(QuoteBar),
symbol,
Resolution.Minute,
SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork),
TimeZones.NewYork,
null,
false,
false,
DataNormalizationMode.Raw,
TickType.Quote)
},
TimeZones.NewYork).ToList();
Assert.IsNotEmpty(result);
// assert we fetch the data for the previous and new symbol
var firstBar = result.First().Values.Single();
var lastBar = result.Last().Values.Single();
Assert.IsTrue(firstBar.Symbol.Value.Contains("NWSA"));
Assert.AreEqual(28, firstBar.Time.Date.Day);
Assert.IsTrue(lastBar.Symbol.Value.Contains("FOXA"));
Assert.AreEqual(2, lastBar.Time.Date.Day);
}
[Test]
public void EquitiesAreMappedCorrectly()
{
var historyProvider = new SubscriptionDataReaderHistoryProvider();
historyProvider.Initialize(new HistoryProviderInitializeParameters(
null,
null,
TestGlobals.DataProvider,
TestGlobals.DataCacheProvider,
TestGlobals.MapFileProvider,
TestGlobals.FactorFileProvider,
null,
false,
new DataPermissionManager(),
null,
new AlgorithmSettings()));
var symbol = Symbol.Create("WM",SecurityType.Equity,Market.USA);
var result = historyProvider.GetHistory(
new[]
{
new HistoryRequest(new DateTime(2008, 01,01),
new DateTime(2008, 01,05),
typeof(TradeBar),
symbol,
Resolution.Daily,
SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork),
TimeZones.NewYork,
null,
false,
false,
DataNormalizationMode.Raw,
TickType.Trade)
},
TimeZones.NewYork).ToList();
var firstBar = result.First().Values.Single();
Assert.AreEqual("WMI", firstBar.Symbol.Value);
Assert.IsNotEmpty(result);
}
}
}