-
Notifications
You must be signed in to change notification settings - Fork 2
/
MassIndex.cs
90 lines (81 loc) · 3.03 KB
/
MassIndex.cs
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
using QuantConnect.Data.Market;
namespace QuantConnect.Indicators
{
/// <summary>
/// The Mass Index uses the high-low range to identify trend reversals based on range expansions.
/// In this sense, the Mass Index is a volatility indicator that does not have a directional
/// bias. Instead, the Mass Index identifies range bulges that can foreshadow a reversal of the
/// current trend. Developed by Donald Dorsey.
/// </summary>
/// <seealso cref="IndicatorBase{TradeBar}"/>
public class MassIndex : IndicatorBase<TradeBar>
{
private readonly ExponentialMovingAverage _ema;
private readonly ExponentialMovingAverage _ema2;
private readonly Sum _sum;
/// <summary>
/// Initializes a new instance of the <see cref="MassIndex"/> class.
/// </summary>
/// <param name="name">The name for this instance.</param>
/// <param name="emaPeriod">The period used by both EMA.</param>
/// <param name="sumPeriod">The sum period.</param>
public MassIndex(string name, int emaPeriod, int sumPeriod)
: base(name)
{
_ema = new ExponentialMovingAverage(emaPeriod);
_ema2 = new ExponentialMovingAverage(emaPeriod);
_sum = new Sum(sumPeriod);
}
/// <summary>
/// Initializes a new instance of the <see cref="MassIndex"/> class.
/// </summary>
/// <param name="emaPeriod">The period used by both EMA.</param>
/// <param name="sumPeriod">The sum period.</param>
public MassIndex(int emaPeriod = 9, int sumPeriod = 25)
: this(string.Format("MII_{0}_{1}", emaPeriod, sumPeriod), emaPeriod, sumPeriod)
{ }
/// <summary>
/// Gets a flag indicating when this indicator is ready and fully initialized
/// </summary>
public override bool IsReady => _sum.IsReady;
/// <summary>
/// Resets this indicator to its initial state
/// </summary>
public override void Reset()
{
base.Reset();
_ema.Reset();
_ema2.Reset();
_sum.Reset();
}
/// <summary>
/// Computes the next value of this indicator from the given state
/// </summary>
/// <param name="input">The input given to the indicator</param>
/// <returns>
/// A new value for this indicator
/// </returns>
protected override decimal ComputeNextValue(TradeBar input)
{
_ema.Update(new IndicatorDataPoint
{
Time = input.Time,
Value = input.High - input.Low
});
_ema2.Update(_ema.Current);
_sum.Update(new IndicatorDataPoint
{
Time = input.Time,
Value = _ema.Current / _ema2.Current
});
if (!_sum.IsReady)
{
return _sum.Period;
}
else
{
return _sum;
}
}
}
}