Skip to content

Commit f815198

Browse files
committed
Add new regression algo
1 parent 3599dd1 commit f815198

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace QuantConnect.Algorithm.CSharp
7+
{
8+
/// <summary>
9+
/// Tests indicator behavior with "DailyPreciseEndTime" disabled, making indicator values equal at all times.
10+
/// </summary>
11+
public class DailyResolutionVsTimeSpanNoPreciseEndRegressionAlgorithm : DailyResolutionVsTimeSpanRegressionAlgorithm
12+
{
13+
/// <summary>
14+
/// Disables precise end time, considering the full day instead.
15+
/// </summary>
16+
protected override void SetDailyPreciseEndTime()
17+
{
18+
// By default, DailyPreciseEndTime is true
19+
Settings.DailyPreciseEndTime = false;
20+
}
21+
22+
protected override void CompareValuesDuringMarketHours()
23+
{
24+
var value1 = RelativeStrengthIndex1.Current.Value;
25+
var value2 = RelativeStrengthIndex2.Current.Value;
26+
if (value1 != value2)
27+
{
28+
throw new RegressionTestException("The values must be equal during market hours");
29+
}
30+
}
31+
32+
protected override void CompareValuesAfterMarketHours()
33+
{
34+
var value1 = RelativeStrengthIndex1.Current.Value;
35+
var value2 = RelativeStrengthIndex2.Current.Value;
36+
37+
if (value1 != value2)
38+
{
39+
throw new RegressionTestException("The values must be equal after market hours");
40+
}
41+
}
42+
43+
}
44+
}

Algorithm.CSharp/DailyResolutionVsTimeSpanRegressionAlgorithm.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ namespace QuantConnect.Algorithm.CSharp
1414
public class DailyResolutionVsTimeSpanRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
1515
{
1616
private Symbol _spy;
17-
private RelativeStrengthIndex _relativeStrengthIndex1;
18-
private RelativeStrengthIndex _relativeStrengthIndex2;
17+
protected RelativeStrengthIndex RelativeStrengthIndex1;
18+
protected RelativeStrengthIndex RelativeStrengthIndex2;
1919
private bool _dataPointsReceived;
2020

2121
public override void Initialize()
@@ -25,22 +25,24 @@ public override void Initialize()
2525

2626
_spy = AddEquity("SPY", Resolution.Hour).Symbol;
2727

28+
SetDailyPreciseEndTime();
29+
2830
// First RSI: Updates at market close (4 PM) using daily resolution (9:30 AM to 4 PM)
29-
_relativeStrengthIndex1 = new RelativeStrengthIndex(14, MovingAverageType.Wilders);
30-
RegisterIndicator(_spy, _relativeStrengthIndex1, Resolution.Daily);
31+
RelativeStrengthIndex1 = new RelativeStrengthIndex(14, MovingAverageType.Wilders);
32+
RegisterIndicator(_spy, RelativeStrengthIndex1, Resolution.Daily);
3133

3234
// Second RSI: Updates every 24 hours (from 12:00 AM to 12:00 AM) using a time span
33-
_relativeStrengthIndex2 = new RelativeStrengthIndex(14, MovingAverageType.Wilders);
34-
RegisterIndicator(_spy, _relativeStrengthIndex2, TimeSpan.FromDays(1));
35+
RelativeStrengthIndex2 = new RelativeStrengthIndex(14, MovingAverageType.Wilders);
36+
RegisterIndicator(_spy, RelativeStrengthIndex2, TimeSpan.FromDays(1));
3537

3638
// Warm up indicators with historical data
3739
var history = History<TradeBar>(_spy, 20, Resolution.Daily).ToList();
3840
foreach (var bar in history)
3941
{
40-
_relativeStrengthIndex1.Update(bar.EndTime, bar.Close);
41-
_relativeStrengthIndex2.Update(bar.EndTime, bar.Close);
42+
RelativeStrengthIndex1.Update(bar.EndTime, bar.Close);
43+
RelativeStrengthIndex2.Update(bar.EndTime, bar.Close);
4244
}
43-
if (!_relativeStrengthIndex1.IsReady || !_relativeStrengthIndex2.IsReady)
45+
if (!RelativeStrengthIndex1.IsReady || !RelativeStrengthIndex2.IsReady)
4446
{
4547
throw new RegressionTestException("Indicators not ready.");
4648
}
@@ -51,20 +53,25 @@ public override void Initialize()
5153
Schedule.On(DateRules.EveryDay(), TimeRules.At(17, 0, 0), CompareValuesAfterMarketHours);
5254
}
5355

54-
private void CompareValuesDuringMarketHours()
56+
protected virtual void SetDailyPreciseEndTime()
57+
{
58+
Settings.DailyPreciseEndTime = true;
59+
}
60+
61+
protected virtual void CompareValuesDuringMarketHours()
5562
{
56-
var value1 = _relativeStrengthIndex1.Current.Value;
57-
var value2 = _relativeStrengthIndex2.Current.Value;
63+
var value1 = RelativeStrengthIndex1.Current.Value;
64+
var value2 = RelativeStrengthIndex2.Current.Value;
5865
if (value1 != value2)
5966
{
6067
throw new RegressionTestException("The values must be equal during market hours");
6168
}
6269
}
6370

64-
private void CompareValuesAfterMarketHours()
71+
protected virtual void CompareValuesAfterMarketHours()
6572
{
66-
var value1 = _relativeStrengthIndex1.Current.Value;
67-
var value2 = _relativeStrengthIndex2.Current.Value;
73+
var value1 = RelativeStrengthIndex1.Current.Value;
74+
var value2 = RelativeStrengthIndex2.Current.Value;
6875

6976
if (value1 == value2 && _dataPointsReceived == true)
7077
{

0 commit comments

Comments
 (0)