|
| 1 | +package indicators |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | +) |
| 6 | + |
| 7 | +// KeltnerChannels holds the configuration for computing the middle line (EMA of typical price) |
| 8 | +// and bands offset by a multiple of ATR. |
| 9 | +type KeltnerChannels struct { |
| 10 | + EmaPeriod int // Period for the EMA of typical price (middle line) |
| 11 | + AtrPeriod int // Period for the ATR calculation |
| 12 | + Mult float64 // Multiplier for the ATR offset |
| 13 | +} |
| 14 | + |
| 15 | +// NewKeltnerChannels creates a KeltnerChannels instance with a given EMA period, ATR period, and ATR multiplier. |
| 16 | +func NewKeltnerChannels(emaPeriod, atrPeriod int, multiplier float64) *KeltnerChannels { |
| 17 | + return &KeltnerChannels{ |
| 18 | + EmaPeriod: emaPeriod, |
| 19 | + AtrPeriod: atrPeriod, |
| 20 | + Mult: multiplier, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// Calculate returns three slices (middle, upper, lower), each the same length as the inputs. |
| 25 | +// - middle line = EMA of typical price |
| 26 | +// - upper line = middle line + (mult * ATR) |
| 27 | +// - lower line = middle line - (mult * ATR) |
| 28 | +// |
| 29 | +// The function needs high, low, close arrays of equal length. (Volume is not used here.) |
| 30 | +func (kc *KeltnerChannels) Calculate(high, low, close []float64) ([]float64, []float64, []float64, error) { |
| 31 | + length := len(high) |
| 32 | + if length != len(low) || length != len(close) { |
| 33 | + return nil, nil, nil, errors.New("high, low, and close must have the same length") |
| 34 | + } |
| 35 | + if length == 0 { |
| 36 | + return nil, nil, nil, errors.New("no price data provided") |
| 37 | + } |
| 38 | + |
| 39 | + // 1) Compute typical price: (High + Low + Close) / 3 |
| 40 | + typicalPrices := make([]float64, length) |
| 41 | + for i := 0; i < length; i++ { |
| 42 | + typicalPrices[i] = (high[i] + low[i] + close[i]) / 3.0 |
| 43 | + } |
| 44 | + |
| 45 | + // 2) Compute EMA of typical price |
| 46 | + emaCalc := NewEMA(kc.EmaPeriod) |
| 47 | + emaValues, err := emaCalc.Calculate(typicalPrices) |
| 48 | + if err != nil { |
| 49 | + return nil, nil, nil, err |
| 50 | + } |
| 51 | + |
| 52 | + // 3) Compute ATR for the same length |
| 53 | + // Note: We already have an ATR indicator that expects (high, low, close). |
| 54 | + // We'll re-use it here. |
| 55 | + atrCalc := NewATR(kc.AtrPeriod) |
| 56 | + atrValues, err := atrCalc.Calculate(high, low, close) |
| 57 | + if err != nil { |
| 58 | + return nil, nil, nil, err |
| 59 | + } |
| 60 | + |
| 61 | + // 4) Build the final Keltner Channels |
| 62 | + middle := make([]float64, length) |
| 63 | + upper := make([]float64, length) |
| 64 | + lower := make([]float64, length) |
| 65 | + |
| 66 | + for i := 0; i < length; i++ { |
| 67 | + m := emaValues[i] |
| 68 | + a := atrValues[i] |
| 69 | + middle[i] = m |
| 70 | + upper[i] = m + kc.Mult*a |
| 71 | + lower[i] = m - kc.Mult*a |
| 72 | + } |
| 73 | + |
| 74 | + return middle, upper, lower, nil |
| 75 | +} |
0 commit comments