forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
indicator_maximum_drawdown.go
28 lines (23 loc) · 1.01 KB
/
indicator_maximum_drawdown.go
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
package techan
import "github.com/sdcoffey/big"
// NewMaximumDrawdownIndicator returns a derivative Indicator which returns the maximum
// drawdown of the underlying indicator over a window. Maximum drawdown is defined as the
// maximum observed loss from peak of an underlying indicator in a given timeframe.
// Maximum drawdown is given as a percentage of the peak. Use a window value of -1 to include
// all values present in the underlying indicator.
// See: https://www.investopedia.com/terms/m/maximum-drawdown-mdd.asp
func NewMaximumDrawdownIndicator(ind Indicator, window int) Indicator {
return maximumDrawdownIndicator{
indicator: ind,
window: window,
}
}
type maximumDrawdownIndicator struct {
indicator Indicator
window int
}
func (mdi maximumDrawdownIndicator) Calculate(index int) big.Decimal {
minVal := NewMinimumValueIndicator(mdi.indicator, mdi.window).Calculate(index)
maxVal := NewMaximumValueIndicator(mdi.indicator, mdi.window).Calculate(index)
return (minVal.Sub(maxVal)).Div(maxVal)
}