Skip to content

Commit

Permalink
Add initialize method to chain providers classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhonabreul committed Feb 13, 2025
1 parent 6d654d2 commit fbf0277
Show file tree
Hide file tree
Showing 18 changed files with 125 additions and 94 deletions.
4 changes: 3 additions & 1 deletion DownloaderDataProvider/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ public static void InitializeConfigurations()
var historyManager = Composer.Instance.GetExportedValueByTypeName<HistoryProviderManager>(nameof(HistoryProviderManager));
historyManager.Initialize(new HistoryProviderInitializeParameters(null, null, dataProvider, _dataCacheProvider,
mapFileProvider, factorFileProvider, _ => { }, false, new DataPermissionManager(), null, new AlgorithmSettings()));
optionChainProvider = new CachingOptionChainProvider(new LiveOptionChainProvider(mapFileProvider, historyManager));
var baseOptionChainProvider = new LiveOptionChainProvider();
baseOptionChainProvider.Initialize(new(mapFileProvider, historyManager));
optionChainProvider = new CachingOptionChainProvider(baseOptionChainProvider);
Composer.Instance.AddPart(optionChainProvider);
}

Expand Down
29 changes: 23 additions & 6 deletions Engine/DataFeeds/BacktestingChainProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

using System;
using QuantConnect.Util;
using QuantConnect.Logging;
using QuantConnect.Interfaces;
using QuantConnect.Securities;
using System.Collections.Generic;
Expand All @@ -30,15 +29,33 @@ namespace QuantConnect.Lean.Engine.DataFeeds
/// </summary>
public abstract class BacktestingChainProvider
{
private readonly IHistoryProvider _historyProvider;
/// <summary>
/// The map file provider instance to use
/// </summary>
protected IMapFileProvider MapFileProvider { get; private set; }

/// <summary>
/// The history provider instance to use
/// </summary>
protected IHistoryProvider HistoryProvider { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="BacktestingChainProvider"/> class
/// </summary>
protected BacktestingChainProvider()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="BacktestingChainProvider"/> class
/// </summary>
/// <param name="historyProvider">The history provider to use</param>
protected BacktestingChainProvider(IHistoryProvider historyProvider)
/// <param name="parameters">The initialization parameters</param>
// TODO: This should be in the chain provider interfaces.
// They might be even be unified in a single interface (futures and options chains providers)
public void Initialize(ChainProviderInitializeParameters parameters)
{
_historyProvider = historyProvider;
HistoryProvider = parameters.HistoryProvider;
MapFileProvider = parameters.MapFileProvider;
}

/// <summary>
Expand Down Expand Up @@ -68,7 +85,7 @@ protected IEnumerable<Symbol> GetSymbols(Symbol canonicalSymbol, DateTime date)
false,
DataNormalizationMode.Raw,
TickType.Quote);
var history = _historyProvider.GetHistory(new[] { request }, marketHoursEntry.DataTimeZone)?.ToList();
var history = HistoryProvider.GetHistory(new[] { request }, marketHoursEntry.DataTimeZone)?.ToList();

var symbols = history == null || history.Count == 0
? Enumerable.Empty<Symbol>()
Expand Down
26 changes: 26 additions & 0 deletions Engine/DataFeeds/BacktestingChainProviderInitializeParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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 QuantConnect.Interfaces;

namespace QuantConnect.Lean.Engine.DataFeeds
{
/// <summary>
/// DTO for initializing the <see cref="BacktestingOptionChainProvider"/>
/// </summary>
/// <param name="MapFileProvider"> The map file provider instance to use </param>
/// <param name="HistoryProvider"> The history provider to use </param>
public record ChainProviderInitializeParameters(IMapFileProvider MapFileProvider, IHistoryProvider HistoryProvider);
}
9 changes: 0 additions & 9 deletions Engine/DataFeeds/BacktestingFutureChainProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,6 @@ namespace QuantConnect.Lean.Engine.DataFeeds
/// </summary>
public class BacktestingFutureChainProvider : BacktestingChainProvider, IFutureChainProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="BacktestingFutureChainProvider"/> class
/// </summary>
/// <param name="historyProvider">The history provider to use</param>
public BacktestingFutureChainProvider(IHistoryProvider historyProvider)
: base(historyProvider)
{
}

/// <summary>
/// Gets the list of future contracts for a given underlying symbol
/// </summary>
Expand Down
17 changes: 2 additions & 15 deletions Engine/DataFeeds/BacktestingOptionChainProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,11 @@ namespace QuantConnect.Lean.Engine.DataFeeds
/// </summary>
public class BacktestingOptionChainProvider : BacktestingChainProvider, IOptionChainProvider
{
private IMapFileProvider _mapFileProvider;

/// <summary>
/// Creates a new instance
/// </summary>
/// <param name="mapFileProvider">The map file provider instance to use</param>
/// <param name="historyProvider">The history provider to use</param>
public BacktestingOptionChainProvider(IMapFileProvider mapFileProvider, IHistoryProvider historyProvider)
: base(historyProvider)
{
_mapFileProvider = mapFileProvider;
}

/// <summary>
/// Gets the list of option contracts for a given underlying symbol
/// </summary>
/// <param name="symbol">The option or the underlying symbol to get the option chain for.
/// Providing the option allows targetting an option ticker different than the default e.g. SPXW</param>
/// Providing the option allows targeting an option ticker different than the default e.g. SPXW</param>
/// <param name="date">The date for which to request the option chain (only used in backtesting)</param>
/// <returns>The list of option contracts</returns>
public virtual IEnumerable<Symbol> GetOptionContractList(Symbol symbol, DateTime date)
Expand Down Expand Up @@ -91,7 +78,7 @@ private Symbol MapUnderlyingSymbol(Symbol underlying, DateTime date)
{
if (underlying.RequiresMapping())
{
var mapFileResolver = _mapFileProvider.Get(AuxiliaryDataKey.Create(underlying));
var mapFileResolver = MapFileProvider.Get(AuxiliaryDataKey.Create(underlying));
var mapFile = mapFileResolver.ResolveMapFile(underlying);
var ticker = mapFile.GetMappedSymbol(date, underlying.Value);
return underlying.UpdateMappedSymbol(ticker);
Expand Down
9 changes: 0 additions & 9 deletions Engine/DataFeeds/LiveFutureChainProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ namespace QuantConnect.Lean.Engine.DataFeeds
/// </summary>
public class LiveFutureChainProvider : BacktestingFutureChainProvider
{
/// <summary>
/// Initializes a new instance of the <see cref="LiveFutureChainProvider"/> class
/// </summary>
/// <param name="historyProvider">The history provider to use</param>
public LiveFutureChainProvider(IHistoryProvider historyProvider)
: base(historyProvider)
{
}

/// <summary>
/// Gets the list of future contracts for a given underlying symbol
/// </summary>
Expand Down
10 changes: 0 additions & 10 deletions Engine/DataFeeds/LiveOptionChainProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,6 @@ static LiveOptionChainProvider()
_client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue("en-US", 0.5));
}

/// <summary>
/// Creates a new instance
/// </summary>
/// <param name="mapFileProvider">The map file provider instance to use</param>
/// <param name="historyProvider">The history provider to use</param>
public LiveOptionChainProvider(IMapFileProvider mapFileProvider, IHistoryProvider historyProvider)
: base(mapFileProvider, historyProvider)
{
}

/// <summary>
/// Gets the option chain associated with the underlying Symbol
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion Engine/DataFeeds/Queues/FakeDataQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public FakeDataQueue(IDataAggregator dataAggregator, int dataPointsPerSecondPerS
{
historyManager = Composer.Instance.GetPart<IHistoryProvider>();
}
_optionChainProvider = new LiveOptionChainProvider(mapFileProvider, historyManager);
var optionChainProvider = new LiveOptionChainProvider();
optionChainProvider.Initialize(new(mapFileProvider, historyManager));
_optionChainProvider = optionChainProvider;

_marketHoursDatabase = MarketHoursDatabase.FromDataFolder();
_symbolExchangeTimeZones = new Dictionary<Symbol, TimeZoneOffsetProvider>();
Expand Down
9 changes: 7 additions & 2 deletions Engine/Setup/BacktestingSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,15 @@ public bool Setup(SetupHandlerParameters parameters)
algorithm.Schedule.SetEventSchedule(parameters.RealTimeHandler);

// set the option chain provider
algorithm.SetOptionChainProvider(new CachingOptionChainProvider(new BacktestingOptionChainProvider(parameters.MapFileProvider, algorithm.HistoryProvider)));
var optionChainProvider = new BacktestingOptionChainProvider();
var initParameters = new ChainProviderInitializeParameters(parameters.MapFileProvider, algorithm.HistoryProvider);
optionChainProvider.Initialize(initParameters);
algorithm.SetOptionChainProvider(new CachingOptionChainProvider(optionChainProvider));

// set the future chain provider
algorithm.SetFutureChainProvider(new CachingFutureChainProvider(new BacktestingFutureChainProvider(algorithm.HistoryProvider)));
var futureChainProvider = new BacktestingFutureChainProvider();
futureChainProvider.Initialize(initParameters);
algorithm.SetFutureChainProvider(new CachingFutureChainProvider(futureChainProvider));

// before we call initialize
BaseSetupHandler.LoadBacktestJobAccountCurrency(algorithm, job);
Expand Down
9 changes: 6 additions & 3 deletions Engine/Setup/BrokerageSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,9 @@ public bool Setup(SetupHandlerParameters parameters)
var optionChainProvider = Composer.Instance.GetPart<IOptionChainProvider>();
if (optionChainProvider == null)
{
optionChainProvider = new CachingOptionChainProvider(
new LiveOptionChainProvider(parameters.MapFileProvider, algorithm.HistoryProvider));
var baseOptionChainProvider = new LiveOptionChainProvider();
baseOptionChainProvider.Initialize(new(parameters.MapFileProvider, algorithm.HistoryProvider));
optionChainProvider = new CachingOptionChainProvider(baseOptionChainProvider);
Composer.Instance.AddPart(optionChainProvider);
}
// set the option chain provider
Expand All @@ -264,7 +265,9 @@ public bool Setup(SetupHandlerParameters parameters)
var futureChainProvider = Composer.Instance.GetPart<IFutureChainProvider>();
if (futureChainProvider == null)
{
futureChainProvider = new CachingFutureChainProvider(new LiveFutureChainProvider(algorithm.HistoryProvider));
var baseFutureChainProvider = new LiveFutureChainProvider();
baseFutureChainProvider.Initialize(new(parameters.MapFileProvider, algorithm.HistoryProvider));
futureChainProvider = new CachingFutureChainProvider(baseFutureChainProvider);
Composer.Instance.AddPart(futureChainProvider);
}
// set the future chain provider
Expand Down
9 changes: 7 additions & 2 deletions Research/QuantBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,13 @@ public QuantBook() : base()
)
);

SetOptionChainProvider(new CachingOptionChainProvider(new BacktestingOptionChainProvider(mapFileProvider, HistoryProvider)));
SetFutureChainProvider(new CachingFutureChainProvider(new BacktestingFutureChainProvider(HistoryProvider)));
var initParameters = new ChainProviderInitializeParameters(mapFileProvider, HistoryProvider);
var optionChainProvider = new BacktestingOptionChainProvider();
optionChainProvider.Initialize(initParameters);
var futureChainProvider = new BacktestingFutureChainProvider();
futureChainProvider.Initialize(initParameters);
SetOptionChainProvider(new CachingOptionChainProvider(optionChainProvider));
SetFutureChainProvider(new CachingFutureChainProvider(futureChainProvider));

SetAlgorithmMode(AlgorithmMode.Research);
SetDeploymentTarget(Config.GetValue("deployment-target", DeploymentTarget.LocalPlatform));
Expand Down
7 changes: 5 additions & 2 deletions Tests/Algorithm/AlgorithmChainsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ public void SetUp()
_algorithm.SetHistoryProvider(TestGlobals.HistoryProvider);
_algorithm.SubscriptionManager.SetDataManager(new DataManagerStub(_algorithm));

_optionChainProvider = new BacktestingOptionChainProvider(TestGlobals.MapFileProvider, TestGlobals.HistoryProvider);
var initParameters = new ChainProviderInitializeParameters(TestGlobals.MapFileProvider, TestGlobals.HistoryProvider);
_optionChainProvider = new BacktestingOptionChainProvider();
_optionChainProvider.Initialize(initParameters);
_algorithm.SetOptionChainProvider(_optionChainProvider);

_futureChainProvider = new BacktestingFutureChainProvider(TestGlobals.HistoryProvider);
_futureChainProvider = new BacktestingFutureChainProvider();
_futureChainProvider.Initialize(initParameters);
_algorithm.SetFutureChainProvider(_futureChainProvider);
}

Expand Down
4 changes: 3 additions & 1 deletion Tests/Algorithm/AlgorithmHistoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4334,7 +4334,9 @@ private static void AssertFuturesHistoryWithDifferentContractDepthOffsetsResults
{
CheckThatHistoryResultsHaveEqualBarCount(historyResults, expectedHistoryCount);

var futureChainProvider = new BacktestingFutureChainProvider(TestGlobals.HistoryProvider);
var futureChainProvider = new BacktestingFutureChainProvider();
futureChainProvider.Initialize(new(TestGlobals.MapFileProvider, TestGlobals.HistoryProvider));

var firstDateTime = historyResults[0][0].EndTime;
var futureChain = futureChainProvider.GetFutureContractList(expectedSymbol, firstDateTime).ToList();

Expand Down
Loading

0 comments on commit fbf0277

Please sign in to comment.