Skip to content

Commit 02c9271

Browse files
committed
CTI
1 parent 11fc798 commit 02c9271

File tree

5 files changed

+78
-39
lines changed

5 files changed

+78
-39
lines changed

Tests/test_eventing.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ private static TBar GenerateRandomBar(RandomNumberGenerator rng, double baseValu
122122
return new TBar(
123123
DateTime.Now,
124124
baseValue,
125-
baseValue + Math.abs(GetRandomDouble(rng) * 10),
126-
baseValue - Math.abs(GetRandomDouble(rng) * 10),
125+
baseValue + Math.Abs(GetRandomDouble(rng) * 10),
126+
baseValue - Math.Abs(GetRandomDouble(rng) * 10),
127127
baseValue + (GetRandomDouble(rng) * 5),
128-
Math.abs(GetRandomDouble(rng) * 1000),
128+
Math.Abs(GetRandomDouble(rng) * 1000),
129129
true
130130
);
131131
}
@@ -151,7 +151,7 @@ public void ValueIndicatorEventTest(string indicatorName, object[] directParams,
151151
}
152152

153153
bool areEqual = (double.IsNaN(directIndicator.Value) && double.IsNaN(eventIndicator.Value)) ||
154-
Math.abs(directIndicator.Value - eventIndicator.Value) < Tolerance;
154+
Math.Abs(directIndicator.Value - eventIndicator.Value) < Tolerance;
155155

156156
Assert.True(areEqual, $"Value indicator {indicatorName} failed: Expected {directIndicator.Value}, Actual {eventIndicator.Value}");
157157
}
@@ -177,7 +177,7 @@ public void BarIndicatorEventTest(string indicatorName, object[] directParams, o
177177
}
178178

179179
bool areEqual = (double.IsNaN(directIndicator.Value) && double.IsNaN(eventIndicator.Value)) ||
180-
Math.abs(directIndicator.Value - eventIndicator.Value) < Tolerance;
180+
Math.Abs(directIndicator.Value - eventIndicator.Value) < Tolerance;
181181

182182
Assert.True(areEqual, $"Bar indicator {indicatorName} failed: Expected {directIndicator.Value}, Actual {eventIndicator.Value}");
183183
}

Tests/test_quantower.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public class QuantowerTests
162162
[Fact] public void Vortex() => TestIndicatorMultipleFields<momentum::QuanTAlib.VortexIndicator>(new[] { "PlusLine", "MinusLine" });
163163

164164
// Oscillators Indicators
165-
[Fact] public void Cti() => TestIndicator<oscillator::QuanTAlib.CtiIndicator>("Series");
165+
[Fact] public void Cti() => TestIndicator<oscillators::QuanTAlib.CtiIndicator>("Series");
166166

167167
}
168168
}

lib/oscillators/Cti.cs

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,37 @@ namespace QuanTAlib;
33

44
/// <summary>
55
/// CTI: Ehler's Correlation Trend Indicator
6-
/// A momentum oscillator that measures the correlation between the price and a lagged version of the price.
6+
/// Measures the correlation between price and an ideal trend line.
77
/// </summary>
88
/// <remarks>
99
/// The CTI calculation process:
10-
/// 1. Calculate the correlation between the price and a lagged version of the price over a specified period.
11-
/// 2. Normalize the correlation values to oscillate between -1 and 1.
12-
/// 3. Use the normalized correlation values to calculate the CTI.
10+
/// 1. Correlates price curve with an ideal trend line (negative count due to backwards data storage)
11+
/// 2. Uses Spearman's correlation algorithm
12+
/// 3. Returns values between -1 and 1
1313
///
1414
/// Key characteristics:
1515
/// - Oscillates between -1 and 1
16-
/// - Positive values indicate bullish momentum
17-
/// - Negative values indicate bearish momentum
16+
/// - Positive values indicate price follows uptrend
17+
/// - Negative values indicate price follows downtrend
1818
///
1919
/// Formula:
20-
/// CTI = 2 * (Correlation - 0.5)
20+
/// CTI = (n∑xy - ∑x∑y) / sqrt((n∑x² - (∑x)²)(n∑y² - (∑y)²))
21+
/// where:
22+
/// x = price curve
23+
/// y = -count (ideal trend line)
24+
/// n = period length
2125
///
2226
/// Sources:
2327
/// John Ehlers - "Cybernetic Analysis for Stocks and Futures" (2004)
24-
/// https://www.investopedia.com/terms/c/correlation-trend-indicator.asp
28+
/// John Ehlers, Correlation Trend Indicator, Stocks & Commodities May-2020
2529
/// </remarks>
2630
[SkipLocalsInit]
2731
public sealed class Cti : AbstractBase
2832
{
2933
private readonly int _period;
3034
private readonly CircularBuffer _priceBuffer;
31-
private readonly Corr _correlation;
35+
private readonly double[] _trendLine;
36+
private const int MinimumPoints = 2; // Minimum points needed for correlation
3237

3338
/// <param name="source">The data source object that publishes updates.</param>
3439
/// <param name="period">The calculation period (default: 20)</param>
@@ -44,7 +49,14 @@ public Cti(int period = 20)
4449
{
4550
_period = period;
4651
_priceBuffer = new CircularBuffer(period);
47-
_correlation = new Corr(period);
52+
53+
// Pre-calculate trend line values since they're static
54+
_trendLine = new double[period];
55+
for (int i = 0; i < period; i++)
56+
{
57+
_trendLine[i] = -i; // negative count for backwards data
58+
}
59+
4860
WarmupPeriod = period;
4961
Name = "CTI";
5062
}
@@ -62,15 +74,36 @@ protected override void ManageState(bool isNew)
6274
protected override double Calculation()
6375
{
6476
ManageState(Input.IsNew);
65-
6677
_priceBuffer.Add(Input.Value, Input.IsNew);
67-
var laggedPrice = _index >= _period ? _priceBuffer[_index - _period] : double.NaN;
6878

69-
_correlation.Calc(new TValue(Input.Time, Input.Value, Input.IsNew), new TValue(Input.Time, laggedPrice, Input.IsNew));
79+
// Use available points for early calculations
80+
int points = Math.Min(_index + 1, _period);
81+
if (points < MinimumPoints) return 0; // Need at least 2 points for correlation
82+
83+
double sx = 0, sy = 0, sxx = 0, sxy = 0, syy = 0;
7084

71-
if (_index < _period - 1) return double.NaN;
85+
// Calculate correlation components using available points
86+
for (int i = 0; i < points; i++)
87+
{
88+
double x = _priceBuffer[i]; // price curve
89+
double y = _trendLine[i]; // pre-calculated trend line
90+
91+
sx += x;
92+
sy += y;
93+
sxx += x * x;
94+
sxy += x * y;
95+
syy += y * y;
96+
}
97+
98+
// Check for numerical stability
99+
double denomX = points * sxx - sx * sx;
100+
double denomY = points * syy - sy * sy;
101+
102+
if (denomX > 0 && denomY > 0)
103+
{
104+
return (points * sxy - sx * sy) / Math.Sqrt(denomX * denomY);
105+
}
72106

73-
var correlation = _correlation.Value;
74-
return 2 * (correlation - 0.5);
107+
return 0;
75108
}
76109
}

quantower/Averages/WmaIndicator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ protected override void OnUpdate(UpdateArgs args)
5959
Series!.SetValue(result.Value);
6060
Series!.SetMarker(0, Color.Transparent); //OnPaintChart draws the line, hidden here
6161
}
62+
#pragma warning disable CA1416 // Validate platform compatibility
6263

6364
public override void OnPaintChart(PaintChartEventArgs args)
6465
{

quantower/Oscillators/CtiIndicator.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
namespace QuanTAlib
55
{
6-
public class CtiIndicator : Indicator
6+
public class CtiIndicator : Indicator, IWatchlistIndicator
77
{
88
[InputParameter("Period", 0, 1, 100, 1, 0)]
9-
public int Period = 20;
9+
public int Period { get; set; } = 20;
1010

1111
[InputParameter("Source Type", 1, variants: new object[]
1212
{
@@ -21,46 +21,51 @@ public class CtiIndicator : Indicator
2121
"OHLC4", SourceType.OHLC4,
2222
"HLCC4", SourceType.HLCC4
2323
})]
24-
public SourceType SourceType = SourceType.Close;
24+
public SourceType Source { get; set; } = SourceType.Close;
2525

2626
[InputParameter("Show Cold Values", 2)]
27-
public bool ShowColdValues = false;
27+
public bool ShowColdValues { get; set; } = true;
2828

29-
private Cti cti;
30-
protected LineSeries? CtiSeries;
29+
private Cti? cti;
30+
protected LineSeries? Series;
31+
protected string? SourceName;
3132
public int MinHistoryDepths => Period + 1;
3233
int IWatchlistIndicator.MinHistoryDepths => MinHistoryDepths;
3334

3435
public CtiIndicator()
3536
{
37+
OnBackGround = false;
38+
SeparateWindow = true;
3639
this.Name = "CTI - Ehler's Correlation Trend Indicator";
40+
SourceName = Source.ToString();
3741
this.Description = "A momentum oscillator that measures the correlation between the price and a lagged version of the price.";
38-
CtiSeries = new($"CTI {Period}", Color: IndicatorExtensions.Oscillators, 2, LineStyle.Solid);
39-
AddLineSeries(CtiSeries);
42+
Series = new($"CTI {Period}", color: IndicatorExtensions.Oscillators, width: 2, LineStyle.Solid);
43+
AddLineSeries(Series);
4044
}
4145

4246
protected override void OnInit()
4347
{
4448
cti = new Cti(this.Period);
49+
SourceName = Source.ToString();
4550
base.OnInit();
4651
}
4752

4853
protected override void OnUpdate(UpdateArgs args)
4954
{
5055
TValue input = this.GetInputValue(args, Source);
51-
cti.Calc(value);
56+
TValue result = cti!.Calc(input);
5257

53-
CtiSeries!.SetValue(cti.Value);
54-
CtiSeries!.SetMarker(0, Color.Transparent);
58+
Series!.SetValue(result);
59+
Series!.SetMarker(0, Color.Transparent);
5560
}
5661

57-
public override string ShortName => $"CTI ({Period}:{SourceName})";
62+
public override string ShortName => $"CTI ({Period}:{SourceName})";
5863

5964
#pragma warning disable CA1416 // Validate platform compatibility
60-
public override void OnPaintChart(PaintChartEventArgs args)
61-
{
62-
base.OnPaintChart(args);
63-
this.PaintSmoothCurve(args, CtiSeries!, cti!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.2);
64-
}
65+
public override void OnPaintChart(PaintChartEventArgs args)
66+
{
67+
base.OnPaintChart(args);
68+
this.PaintSmoothCurve(args, Series!, cti!.WarmupPeriod, showColdValues: ShowColdValues, tension: 0.0);
69+
}
6570
}
6671
}

0 commit comments

Comments
 (0)