Skip to content

Commit eff5dcb

Browse files
committed
Add warning for DailyConsolidationUseExtendedMarketHours mismatch
1 parent c02a8fa commit eff5dcb

File tree

3 files changed

+147
-0
lines changed

3 files changed

+147
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
using System.Collections.Generic;
17+
using QuantConnect.Data.Market;
18+
using QuantConnect.Interfaces;
19+
20+
namespace QuantConnect.Algorithm.CSharp
21+
{
22+
public class DailyConsolidationExtendedMarketHoursWarningRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
23+
{
24+
private Symbol _spy;
25+
private TradeBar _lastBar;
26+
private int _mismatchCount;
27+
28+
/// <summary>
29+
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm.
30+
/// </summary>
31+
public override void Initialize()
32+
{
33+
SetStartDate(2013, 10, 07);
34+
SetEndDate(2013, 10, 31);
35+
36+
_spy = AddEquity("SPY", Resolution.Hour, extendedMarketHours: true).Symbol;
37+
38+
// Daily consolidator that excludes extended market hours
39+
// Requires both the subscription and the algorithm setting to enable them
40+
// the subscription has ExtendedMarketHours=true, but the setting is false by default
41+
Consolidate(_spy, Resolution.Daily, OnNormalMarketHours); // This will show a warning
42+
43+
// Daily consolidator that includes extended market hours,
44+
// since both the subscription and the algorithm setting are enabled
45+
Settings.DailyConsolidationUseExtendedMarketHours = true;
46+
Consolidate(_spy, Resolution.Daily, OnExtendedMarketHours);
47+
}
48+
49+
private void OnNormalMarketHours(TradeBar dailyBar)
50+
{
51+
// Save the last consolidated bar for comparison
52+
_lastBar = dailyBar;
53+
}
54+
private void OnExtendedMarketHours(TradeBar dailyBar)
55+
{
56+
if (dailyBar.Open != _lastBar.Open || dailyBar.High != _lastBar.High || dailyBar.Low != _lastBar.Low || dailyBar.Close != _lastBar.Close)
57+
{
58+
// Track bar mismatches between normal and extended market hours
59+
_mismatchCount++;
60+
}
61+
}
62+
63+
public override void OnEndOfAlgorithm()
64+
{
65+
if (_mismatchCount == 0)
66+
{
67+
throw new RegressionTestException("Expected differences between daily consolidations with and without extended market hours.");
68+
}
69+
}
70+
71+
/// <summary>
72+
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
73+
/// </summary>
74+
public bool CanRunLocally { get; } = true;
75+
76+
/// <summary>
77+
/// This is used by the regression test system to indicate which languages this algorithm is written in.
78+
/// </summary>
79+
public List<Language> Languages { get; } = new() { Language.CSharp };
80+
81+
/// <summary>
82+
/// Data Points count of all timeslices of algorithm
83+
/// </summary>
84+
public long DataPoints => 440;
85+
86+
/// <summary>
87+
/// Data Points count of the algorithm history
88+
/// </summary>
89+
public int AlgorithmHistoryDataPoints => 0;
90+
91+
/// <summary>
92+
/// Final status of the algorithm
93+
/// </summary>
94+
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;
95+
96+
/// <summary>
97+
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
98+
/// </summary>
99+
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
100+
{
101+
{"Total Orders", "0"},
102+
{"Average Win", "0%"},
103+
{"Average Loss", "0%"},
104+
{"Compounding Annual Return", "0%"},
105+
{"Drawdown", "0%"},
106+
{"Expectancy", "0"},
107+
{"Start Equity", "100000"},
108+
{"End Equity", "100000"},
109+
{"Net Profit", "0%"},
110+
{"Sharpe Ratio", "0"},
111+
{"Sortino Ratio", "0"},
112+
{"Probabilistic Sharpe Ratio", "0%"},
113+
{"Loss Rate", "0%"},
114+
{"Win Rate", "0%"},
115+
{"Profit-Loss Ratio", "0"},
116+
{"Alpha", "0"},
117+
{"Beta", "0"},
118+
{"Annual Standard Deviation", "0"},
119+
{"Annual Variance", "0"},
120+
{"Information Ratio", "-6.224"},
121+
{"Tracking Error", "0.108"},
122+
{"Treynor Ratio", "0"},
123+
{"Total Fees", "$0.00"},
124+
{"Estimated Strategy Capacity", "$0"},
125+
{"Lowest Capacity Asset", ""},
126+
{"Portfolio Turnover", "0%"},
127+
{"Drawdown Recovery", "0"},
128+
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
129+
};
130+
}
131+
}

Algorithm/QCAlgorithm.Indicators.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4237,6 +4237,21 @@ private IDataConsolidator CreateConsolidator(Symbol symbol, Func<DateTime, Calen
42374237
{
42384238
if (resolution.HasValue)
42394239
{
4240+
if (subscription.ExtendedMarketHours && !Settings.DailyConsolidationUseExtendedMarketHours)
4241+
{
4242+
// Show this warning only once
4243+
if (!_hasShownDailyConsolidationWarning)
4244+
{
4245+
Debug($"Warning: The subscription for {symbol} has ExtendedMarketHours=true, " +
4246+
$"but Settings.DailyConsolidationUseExtendedMarketHours=false. " +
4247+
$"Daily consolidations will exclude extended market hours. " +
4248+
$"Enable algorithm.Settings.DailyConsolidationUseExtendedMarketHours to include them."
4249+
);
4250+
4251+
_hasShownDailyConsolidationWarning = true;
4252+
}
4253+
}
4254+
42404255
if (resolution.Value == Resolution.Daily)
42414256
{
42424257
consolidator = new MarketHourAwareConsolidator(Settings.DailyPreciseEndTime, resolution.Value, subscription.Type, subscription.TickType,

Algorithm/QCAlgorithm.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public partial class QCAlgorithm : MarshalByRefObject, IAlgorithm
106106
private HashSet<string> _tags;
107107
private bool _tagsLimitReachedLogSent;
108108
private bool _tagsCollectionTruncatedLogSent;
109+
private bool _hasShownDailyConsolidationWarning;
109110
private DateTime _start;
110111
private DateTime _startDate; //Default start and end dates.
111112
private DateTime _endDate; //Default end to yesterday

0 commit comments

Comments
 (0)