-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathHMA_QT.cs
More file actions
115 lines (95 loc) · 4.48 KB
/
HMA_QT.cs
File metadata and controls
115 lines (95 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Drawing;
using System.Reflection.PortableExecutable;
using System.Runtime.Intrinsics.X86;
using TradingPlatform.BusinessLayer;
using TradingPlatform.BusinessLayer.Chart;
using TradingPlatform.BusinessLayer.Utils;
namespace QuantowerIndicators
{
public class HullMovingAverage : Indicator
{
// Input parameter for the HMA period
[InputParameter("Period", 0, 1, 999, 1, 0)]
public int Period = 14;
private int sqrtPeriod;
// Working buffers to store intermediate results
private double[] priceBuffer;
private double[] wmaFull;
private double[] wmaHalf;
private double[] wmaDiff;
private double[] wmaSmoothed;
public HullMovingAverage()
{
// Indicator info
this.Name = "Hull Moving Average";
this.Description = "An implementation of the Hull Moving Average";
this.SeparateWindow = false; // Plot on the same chart as the price
// Adding one line series for the final HMA plot
this.AddLineSeries("HMA", Color.Cyan, 2, LineStyle.Solid);
}
// Called once when the indicator is created
protected override void OnInit()
{
// Calculate sqrtPeriod in advance
sqrtPeriod = (int)Math.Sqrt(Period);
// Prepare arrays based on the available historical data count
int dataCount = this.HistoricalData.Count;
priceBuffer = new double[dataCount];
wmaFull = new double[dataCount];
wmaHalf = new double[dataCount];
wmaDiff = new double[dataCount];
wmaSmoothed = new double[dataCount];
}
// Called on every bar update (historical or new bar)
protected override void OnUpdate(UpdateArgs args)
{
// We only calculate values on new or historical bars
// (Skip if something else triggered OnUpdate, like a visual change)
if (args.Reason == UpdateReason.NewBar || args.Reason == UpdateReason.HistoricalBar)
{
int lastIndex = this.Count - 1;
// Current bar's closing price (you can change this to open/high/low if you wish)
double closePrice = this.GetPrice(PriceType.Close, 0);
// Store in our price buffer
priceBuffer[lastIndex] = closePrice;
// Calculate WMA(Price, Period)
wmaFull[lastIndex] = WeightedMovingAverage(priceBuffer, Period, lastIndex);
// Calculate WMA(Price, Period/2)
int halfPeriod = Math.Max(1, Period / 2); // ensure no zero
wmaHalf[lastIndex] = WeightedMovingAverage(priceBuffer, halfPeriod, lastIndex);
// Intermediate difference array: 2 * WMA(Price, n/2) - WMA(Price, n)
wmaDiff[lastIndex] = 2.0d * wmaHalf[lastIndex] - wmaFull[lastIndex];
// Now apply WMA on the difference array with length = sqrt(Period)
wmaSmoothed[lastIndex] = WeightedMovingAverage(wmaDiff, sqrtPeriod, lastIndex);
// Assign final HMA value to the indicator output
this.SetValue(wmaSmoothed[lastIndex]);
}
}
//-------------------------------------------------------------------------------------------
// HELPER: Weighted Moving Average
//-------------------------------------------------------------------------------------------
private double WeightedMovingAverage(double[] source, int length, int currentIndex)
{
// If the requested length or index range is invalid, return 0
if (length <= 0 || currentIndex - length + 1 < 0)
return 0d;
double sum = 0d;
double weightSum = 0d;
int weight = length;
// We iterate backward from currentIndex to (currentIndex - length + 1)
for (int i = currentIndex; i > currentIndex - length; i--)
{
sum += source[i] * weight;
weightSum += weight;
weight--;
}
// Avoid division by zero, though weightSum shouldn’t be zero if length > 0
if (weightSum == 0d)
return 0d;
return sum / weightSum;
}
}
}