|
| 1 | +package techan |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/sdcoffey/big" |
| 5 | +) |
| 6 | + |
| 7 | +type stochasticRSIIndicator struct { |
| 8 | + curRSI Indicator |
| 9 | + minRSI Indicator |
| 10 | + maxRSI Indicator |
| 11 | +} |
| 12 | + |
| 13 | +// NewStochasticRSIIndicator returns a derivative Indicator which returns the stochastic RSI indicator for the given |
| 14 | +// RSI window. |
| 15 | +// https://www.investopedia.com/terms/s/stochrsi.asp |
| 16 | +func NewStochasticRSIIndicator(indicator Indicator, timeframe int) Indicator { |
| 17 | + rsiIndicator := NewRelativeStrengthIndexIndicator(indicator, timeframe) |
| 18 | + return stochasticRSIIndicator{ |
| 19 | + curRSI: rsiIndicator, |
| 20 | + minRSI: NewMinimumValueIndicator(rsiIndicator, timeframe), |
| 21 | + maxRSI: NewMaximumValueIndicator(rsiIndicator, timeframe), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func (sri stochasticRSIIndicator) Calculate(index int) big.Decimal { |
| 26 | + curRSI := sri.curRSI.Calculate(index) |
| 27 | + minRSI := sri.minRSI.Calculate(index) |
| 28 | + maxRSI := sri.maxRSI.Calculate(index) |
| 29 | + |
| 30 | + if minRSI.EQ(maxRSI) { |
| 31 | + return big.NewDecimal(100) |
| 32 | + } |
| 33 | + |
| 34 | + return curRSI.Sub(minRSI).Div(maxRSI.Sub(minRSI)).Mul(big.NewDecimal(100)) |
| 35 | +} |
| 36 | + |
| 37 | +type stochRSIKIndicator struct { |
| 38 | + stochasticRSI Indicator |
| 39 | + window int |
| 40 | +} |
| 41 | + |
| 42 | +// NewFastStochasticRSIIndicator returns a derivative Indicator which returns the fast stochastic RSI indicator (%K) |
| 43 | +// for the given stochastic window. |
| 44 | +func NewFastStochasticRSIIndicator(stochasticRSI Indicator, timeframe int) Indicator { |
| 45 | + return stochRSIKIndicator{stochasticRSI, timeframe} |
| 46 | +} |
| 47 | + |
| 48 | +func (k stochRSIKIndicator) Calculate(index int) big.Decimal { |
| 49 | + return NewSimpleMovingAverage(k.stochasticRSI, k.window).Calculate(index) |
| 50 | +} |
| 51 | + |
| 52 | +type stochRSIDIndicator struct { |
| 53 | + fastStochasticRSI Indicator |
| 54 | + window int |
| 55 | +} |
| 56 | + |
| 57 | +// NewSlowStochasticRSIIndicator returns a derivative Indicator which returns the slow stochastic RSI indicator (%D) |
| 58 | +// for the given stochastic window. |
| 59 | +func NewSlowStochasticRSIIndicator(fastStochasticRSI Indicator, timeframe int) Indicator { |
| 60 | + return stochRSIDIndicator{fastStochasticRSI, timeframe} |
| 61 | +} |
| 62 | + |
| 63 | +func (d stochRSIDIndicator) Calculate(index int) big.Decimal { |
| 64 | + return NewSimpleMovingAverage(d.fastStochasticRSI, d.window).Calculate(index) |
| 65 | +} |
0 commit comments