Skip to content

Commit

Permalink
Issue 68 (#71)
Browse files Browse the repository at this point in the history
* Adding rolling moving average (RMA) indicator.

* Adding RMA to index.

* Changing RSI to use RMA indicator.

Fixes #68.
  • Loading branch information
cinar committed May 5, 2022
1 parent fb3ff0d commit 9288d78
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The following list of indicators are currently supported by this package:
- [Parabolic SAR](trend_indicators.md#parabolic-sar)
- [Qstick](trend_indicators.md#qstick)
- [Random Index (KDJ)](trend_indicators.md#random-index-kdj)
- [Rolling Moving Average (RMA)](trend_indicators.md#rolling-moving-average-rma)
- [Simple Moving Average (SMA)](trend_indicators.md#simple-moving-average-sma)
- [Since Change](trend_indicators.md#since-change)
- [Triple Exponential Moving Average (TEMA)](trend_indicators.md#triple-exponential-moving-average-tema)
Expand Down
4 changes: 2 additions & 2 deletions momentum_indicators.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func Rsi(closing []float64) ([]float64, []float64) {
}
}

meanGains := Sma(14, gains)
meanLosses := Sma(14, losses)
meanGains := Rma(14, gains)
meanLosses := Rma(14, losses)

rsi := make([]float64, len(closing))
rs := make([]float64, len(closing))
Expand Down
26 changes: 26 additions & 0 deletions trend_indicators.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,32 @@ func DefaultKdj(high, low, closing []float64) ([]float64, []float64, []float64)
return Kdj(9, 3, 3, high, low, closing)
}

// Rolling Moving Average (RMA).
//
// R[0] to R[p-1] is SMA(values)
// R[p] and after is R[i] = ((R[i-1]*(p-1)) + v[i]) / p
//
// Returns r.
func Rma(period int, values []float64) []float64 {
result := make([]float64, len(values))
sum := float64(0)

for i, value := range values {
count := i + 1

if i < period {
sum += value
} else {
sum = (result[i-1] * float64(period-1)) + value
count = period
}

result[i] = sum / float64(count)
}

return result
}

// Simple Moving Average (SMA).
func Sma(period int, values []float64) []float64 {
result := make([]float64, len(values))
Expand Down
14 changes: 14 additions & 0 deletions trend_indicators.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Trend indicators measure the direction and strength of a trend.
- [Parabolic SAR](#parabolic-sar)
- [Qstick](trend_indicator.md#qstick)
- [Random Index (KDJ)](#random-index-kdj)
- [Rolling Moving Average (RMA)](#rolling-moving-average-rma)
- [Simple Moving Average (SMA)](#simple-moving-average-sma)
- [Since Change](#since-change)
- [Triple Exponential Moving Average (TEMA)](#triple-exponential-moving-average-tema)
Expand Down Expand Up @@ -242,6 +243,19 @@ By default, _rPeriod_ of 9, _kPeriod_ of 3, and _dPeriod_ of 3 are used. The [De
k, d, j := indicator.DefaultKdj(high, low, closing)
```

#### Rolling Moving Average (RMA)

The [Rma](https://pkg.go.dev/github.com/cinar/indicator#Rma) function calculates the rolling moving average for a given period.

```
R[0] to R[p-1] is SMA(values)
R[p] and after is R[i] = ((R[i-1]*(p-1)) + v[i]) / p
```

```Golang
result := indicator.Rma(2, []float64{2, 4, 6, 8, 10, 12})
```

#### Simple Moving Average (SMA)

The [Sma](https://pkg.go.dev/github.com/cinar/indicator#Sma) function calculates the simple moving average for a given period.
Expand Down
37 changes: 37 additions & 0 deletions trend_indicators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,43 @@ func TestKdj(t *testing.T) {
testEquals(t, roundDigitsAll(j, 2), expectedJ)
}

func TestRma(t *testing.T) {
values := []float64{
0,
0.00005,
0.000017,
0.000262,
0.000107,
0,
0,
0.000597,
0,
0,
0.000059,
0.000198,
0.000073,
0,
0.000006,
0,
0.000077,
0.000032,
0.000112,
}

expected := []float64{
0.00009735714286,
0.00009083163265,
0.00008434365889,
0.00008381911183,
0.0000801177467,
0.00008239505051,
}
period := 14

actual := Rma(period, values)
testEquals(t, roundDigitsAll(actual[len(actual)-6:], 14), expected)
}

func TestSma(t *testing.T) {
values := []float64{2, 4, 6, 8, 10}
expected := []float64{2, 3, 5, 7, 9}
Expand Down

0 comments on commit 9288d78

Please sign in to comment.