Skip to content

Commit

Permalink
Refactor solution to use enumerator
Browse files Browse the repository at this point in the history
- Refactor daily strict end times solution to be through enumerator
  usage, so it applies for history providers too
  • Loading branch information
Martin-Molinero committed May 15, 2024
1 parent a78dd04 commit 7480ef0
Show file tree
Hide file tree
Showing 60 changed files with 477 additions and 188 deletions.
2 changes: 2 additions & 0 deletions Algorithm.CSharp/BasicTemplateIndexAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public override void Initialize()

_emaSlow = EMA(Spx, 80);
_emaFast = EMA(Spx, 200);

Settings.DailyStrictEndTimeEnabled = true;
}

/// <summary>
Expand Down
11 changes: 9 additions & 2 deletions Algorithm.CSharp/BasicTemplateIndexDailyAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Linq;
using QuantConnect.Data;
using System.Collections.Generic;
using QuantConnect.Data.Market;

namespace QuantConnect.Algorithm.CSharp
{
Expand Down Expand Up @@ -67,6 +68,12 @@ public override void OnEndOfAlgorithm()
return;
}

var openInterest = Securities[SpxOption].Cache.GetAll<OpenInterest>();
if (openInterest.Single().EndTime != new DateTime(2021, 1, 15, 23, 0, 0))
{
throw new ArgumentException($"Unexpected open interest time: {openInterest.Single().EndTime}");
}

foreach (var symbol in new[] { SpxOption, Spx })
{
var history = History(symbol, 10).ToList();
Expand Down Expand Up @@ -98,7 +105,7 @@ public override void OnEndOfAlgorithm()
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 122;
public override long DataPoints => 121;

/// <summary>
/// Data Points count of the algorithm history
Expand All @@ -113,7 +120,7 @@ public override void OnEndOfAlgorithm()
{"Total Orders", "11"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "653.545%"},
{"Compounding Annual Return", "621.484%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Start Equity", "1000000"},
Expand Down
2 changes: 2 additions & 0 deletions Algorithm.CSharp/BasicTemplateIndexOptionsAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public override void Initialize()

_emaSlow = EMA(_spx, 80);
_emaFast = EMA(_spx, 200);

Settings.DailyStrictEndTimeEnabled = true;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public override void Initialize()

AddIndex("SPX", Resolution.Daily);
AddEquity("SPY", FillForwardResolution);

Settings.DailyStrictEndTimeEnabled = true;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public class IndexOptionCallITMExpiryDailyRegressionAlgorithm : IndexOptionCallI
{
protected override Resolution Resolution => Resolution.Daily;

public override void Initialize()
{
Settings.DailyStrictEndTimeEnabled = true;
base.Initialize();
}

/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
Expand All @@ -32,7 +38,7 @@ public class IndexOptionCallITMExpiryDailyRegressionAlgorithm : IndexOptionCallI
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 195;
public override long DataPoints => 194;

/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public class IndexOptionCallOTMExpiryDailyRegressionAlgorithm : IndexOptionCallO
{
protected override Resolution Resolution => Resolution.Daily;

public override void Initialize()
{
Settings.DailyStrictEndTimeEnabled = true;
base.Initialize();
}

/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
Expand All @@ -32,7 +38,7 @@ public class IndexOptionCallOTMExpiryDailyRegressionAlgorithm : IndexOptionCallO
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 185;
public override long DataPoints => 184;

/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override void Initialize()
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 114;
public override long DataPoints => 121;

/// <summary>
/// Data Points count of the algorithm history
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void Initialize()
/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public override long DataPoints => 114;
public override long DataPoints => 121;

/// <summary>
/// Data Points count of the algorithm history
Expand Down
2 changes: 2 additions & 0 deletions Algorithm.Python/BasicTemplateIndexDailyAlgorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def initialize(self) -> None:
self.ExpectedBarCount = 10
self.BarCounter = 0

self.settings.daily_strict_end_time_enabled = True

def on_data(self, data: Slice):
if not self.Portfolio.Invested:
# SPX Index is not tradable, but we can trade an option
Expand Down
5 changes: 5 additions & 0 deletions Common/AlgorithmSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public class AlgorithmSettings : IAlgorithmSettings
/// </remarks>
public int? TradingDaysPerYear { get; set; }

/// <summary>
/// True if daily strict end times are enabled
/// </summary>
public bool DailyStrictEndTimeEnabled { get; set; }

/// <summary>
/// Initializes a new instance of the <see cref="AlgorithmSettings"/> class
/// </summary>
Expand Down
48 changes: 30 additions & 18 deletions Common/Data/Consolidators/MarketHourAwareConsolidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ namespace QuantConnect.Data.Common
/// </summary>
public class MarketHourAwareConsolidator : IDataConsolidator
{
private readonly bool _dailyStrictEndTimeEnabled;
private readonly bool _extendedMarketHours;
private readonly TimeSpan _period;
private bool _strictEndTime;
private bool _useStrictEndTime;

/// <summary>
/// The consolidation period requested
/// </summary>
protected TimeSpan Period { get; }

/// <summary>
/// The consolidator instance
Expand Down Expand Up @@ -74,9 +79,10 @@ public class MarketHourAwareConsolidator : IDataConsolidator
/// <param name="dataType">The target data type</param>
/// <param name="tickType">The target tick type</param>
/// <param name="extendedMarketHours">True if extended market hours should be consolidated</param>
public MarketHourAwareConsolidator(Resolution resolution, Type dataType, TickType tickType, bool extendedMarketHours)
public MarketHourAwareConsolidator(bool dailyStrictEndTimeEnabled, Resolution resolution, Type dataType, TickType tickType, bool extendedMarketHours)
{
_period = resolution.ToTimeSpan();
_dailyStrictEndTimeEnabled = dailyStrictEndTimeEnabled;
Period = resolution.ToTimeSpan();
_extendedMarketHours = extendedMarketHours;

if (dataType == typeof(Tick))
Expand All @@ -85,41 +91,38 @@ public MarketHourAwareConsolidator(Resolution resolution, Type dataType, TickTyp
{
Consolidator = resolution == Resolution.Daily
? new TickConsolidator(DailyStrictEndTime)
: new TickConsolidator(_period);
: new TickConsolidator(Period);
}
else
{
Consolidator = resolution == Resolution.Daily
? new TickQuoteBarConsolidator(DailyStrictEndTime)
: new TickQuoteBarConsolidator(_period);
: new TickQuoteBarConsolidator(Period);
}
}
else if (dataType == typeof(TradeBar))
{
Consolidator = resolution == Resolution.Daily
? new TradeBarConsolidator(DailyStrictEndTime)
: new TradeBarConsolidator(_period);
: new TradeBarConsolidator(Period);
}
else if (dataType == typeof(QuoteBar))
{
Consolidator = resolution == Resolution.Daily
? new QuoteBarConsolidator(DailyStrictEndTime)
: new QuoteBarConsolidator(_period);
: new QuoteBarConsolidator(Period);
}
else
{
throw new ArgumentNullException(nameof(dataType), $"{dataType.Name} not supported");
}
Consolidator.DataConsolidated += ForwardConsolidatedBar;
}

/// <summary>
/// Event handler that fires when a new piece of data is produced
/// </summary>
public event DataConsolidatedHandler DataConsolidated
{
add => Consolidator.DataConsolidated += value;
remove => Consolidator.DataConsolidated -= value;
}
public event DataConsolidatedHandler DataConsolidated;

/// <summary>
/// Updates this consolidator with the specified data
Expand Down Expand Up @@ -149,6 +152,7 @@ public void Scan(DateTime currentLocalTime)
/// </summary>
public void Dispose()
{
Consolidator.DataConsolidated -= ForwardConsolidatedBar;
Consolidator.Dispose();
}

Expand All @@ -164,7 +168,7 @@ protected void Initialize(IBaseData data)
ExchangeHours = marketHoursDatabase.GetExchangeHours(symbol.ID.Market, symbol, symbol.SecurityType);
DataTimeZone = marketHoursDatabase.GetDataTimeZone(symbol.ID.Market, symbol, symbol.SecurityType);

_strictEndTime = UseStrictEndTime(data.Symbol.SecurityType);
_useStrictEndTime = !ExchangeHours.IsMarketAlwaysOpen && UseStrictEndTime(data.Symbol);
}
}

Expand All @@ -173,19 +177,27 @@ protected void Initialize(IBaseData data)
/// </summary>
protected virtual CalendarInfo DailyStrictEndTime(DateTime dateTime)
{
if (!_strictEndTime)
if (!_useStrictEndTime)
{
return new (_period > Time.OneDay ? dateTime : dateTime.RoundDown(_period), _period);
return new (Period > Time.OneDay ? dateTime : dateTime.RoundDown(Period), Period);
}
return LeanData.GetDailyCalendar(dateTime, ExchangeHours, _extendedMarketHours);
}

/// <summary>
/// Useful for testing
/// </summary>
protected virtual bool UseStrictEndTime(SecurityType securityType)
protected virtual bool UseStrictEndTime(Symbol symbol)
{
return LeanData.UseStrictEndTime(_dailyStrictEndTimeEnabled, symbol, Period);
}

/// <summary>
/// Will forward the underlying consolidated bar to consumers on this object
/// </summary>
protected virtual void ForwardConsolidatedBar(object sender, IBaseData consolidated)
{
return LeanData.UseStrictEndTime(securityType, _period);
DataConsolidated?.Invoke(this, consolidated);
}
}
}
30 changes: 30 additions & 0 deletions Common/Data/DataAggregatorInitializeParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.Data
{
/// <summary>
/// The <see cref="IDataAggregator"/> parameters initialize dto
/// </summary>
public class DataAggregatorInitializeParameters
{
/// <summary>
/// The algorithm settings instance to use
/// </summary>
public IAlgorithmSettings AlgorithmSettings { get; set; }
}
}
10 changes: 9 additions & 1 deletion Common/Data/HistoryProviderInitializeParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ public class HistoryProviderInitializeParameters
/// </summary>
public IObjectStore ObjectStore { get; }

/// <summary>
/// The algorithm settings instance to use
/// </summary>
public IAlgorithmSettings AlgorithmSettings { get; }

/// <summary>
/// Initializes a new instance of the <see cref="HistoryProviderInitializeParameters"/> class from the specified parameters
/// </summary>
Expand All @@ -89,6 +94,7 @@ public class HistoryProviderInitializeParameters
/// <param name="parallelHistoryRequestsEnabled">True if parallel history requests are enabled</param>
/// <param name="dataPermissionManager">The data permission manager to use</param>
/// <param name="objectStore">The object store to use</param>
/// <param name="algorithmSettings">The algorithm settings instance to use</param>
public HistoryProviderInitializeParameters(
AlgorithmNodePacket job,
IApi api,
Expand All @@ -99,7 +105,8 @@ public class HistoryProviderInitializeParameters
Action<int> statusUpdateAction,
bool parallelHistoryRequestsEnabled,
IDataPermissionManager dataPermissionManager,
IObjectStore objectStore)
IObjectStore objectStore,
IAlgorithmSettings algorithmSettings)
{
Job = job;
Api = api;
Expand All @@ -111,6 +118,7 @@ public class HistoryProviderInitializeParameters
ParallelHistoryRequestsEnabled = parallelHistoryRequestsEnabled;
DataPermissionManager = dataPermissionManager;
ObjectStore = objectStore;
AlgorithmSettings = algorithmSettings;
}
}
}
8 changes: 7 additions & 1 deletion Common/Data/IDataAggregator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
Expand All @@ -23,6 +23,12 @@ namespace QuantConnect.Data
/// </summary>
public interface IDataAggregator : IDisposable
{
/// <summary>
/// Initialize this instance
/// </summary>
/// <param name="parameters">The parameters dto instance</param>
void Initialize(DataAggregatorInitializeParameters parameters);

/// <summary>
/// Add new subscription to current <see cref="IDataAggregator"/> instance
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions Common/Data/Market/QuoteBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,6 @@ private QuoteBar ParseQuote(SubscriptionDataConfig config, DateTime date, Stream
}

quoteBar.Value = quoteBar.Close;
LeanData.TrySetStrictEndTimes(quoteBar, config.Increment);
return quoteBar;
}

Expand Down Expand Up @@ -630,7 +629,6 @@ private QuoteBar ParseQuote(SubscriptionDataConfig config, DateTime date, string
}

quoteBar.Value = quoteBar.Close;
LeanData.TrySetStrictEndTimes(quoteBar, config.Increment);
return quoteBar;
}

Expand Down
2 changes: 0 additions & 2 deletions Common/Data/Market/TradeBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,6 @@ private static TradeBar LineParseNoScale(SubscriptionDataConfig config, string l
{
tradeBar.Volume = csv[5].ToDecimal();
}
LeanData.TrySetStrictEndTimes(tradeBar, config.Increment);
return tradeBar;
}

Expand Down Expand Up @@ -680,7 +679,6 @@ private static TradeBar StreamParseNoScale(SubscriptionDataConfig config, Stream
{
tradeBar.Volume = streamReader.GetDecimal();
}
LeanData.TrySetStrictEndTimes(tradeBar, config.Increment);
return tradeBar;
}

Expand Down

0 comments on commit 7480ef0

Please sign in to comment.