39 private readonly
int _minTrendLength;
46 private readonly decimal _sensitivity;
58 private bool _lastPivotWasLow;
84 public ZigZag(
string name, decimal sensitivity = 0.05m,
int minTrendLength = 1) : base(name)
86 if (sensitivity <= 0 || sensitivity >= 1)
88 throw new ArgumentException(
"Sensitivity must be between 0 and 1.", nameof(sensitivity));
91 if (minTrendLength < 1)
93 throw new ArgumentException(
"Minimum trend length must be greater than 0.", nameof(minTrendLength));
97 _sensitivity = sensitivity;
98 _minTrendLength = minTrendLength;
107 public ZigZag(decimal sensitivity = 0.05m,
int minTrendLength = 1)
108 : this($
"ZZ({sensitivity},{minTrendLength})", sensitivity, minTrendLength)
116 public override bool IsReady => Samples > _minTrendLength;
134 if (_lastPivot ==
null)
136 UpdatePivot(input,
true);
140 var currentPivot = _lastPivotWasLow ? _lastPivot.
Low : _lastPivot.
High;
142 if (_lastPivotWasLow)
144 if (input.
High >= _lastPivot.
Low * (1m + _sensitivity) && _count >= _minTrendLength)
146 UpdatePivot(input,
false);
149 else if (input.
Low <= _lastPivot.
Low)
151 UpdatePivot(input,
true);
157 if (input.
Low <= _lastPivot.
High * (1m - _sensitivity) && _count >= _minTrendLength)
159 UpdatePivot(input,
true);
162 else if (input.
High >= _lastPivot.
High)
164 UpdatePivot(input,
false);
178 private void UpdatePivot(
IBaseDataBar input,
bool pivotDirection)
182 if (_lastPivotWasLow == pivotDirection)
193 _lastPivotWasLow = pivotDirection;