-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
457 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) 2021-2023 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package volatility | ||
|
||
import ( | ||
"github.com/cinar/indicator/helper" | ||
"github.com/cinar/indicator/trend" | ||
) | ||
|
||
const ( | ||
// DefaultAccelerationBandsPeriod is the default period for the Acceleration Bands. | ||
DefaultAccelerationBandsPeriod = 20 | ||
) | ||
|
||
// AccelerationBands represents the configuration parameters for calculating the Acceleration Bands. | ||
// | ||
// Upper Band = SMA(High * (1 + 4 * (High - Low) / (High + Low))) | ||
// Middle Band = SMA(Closing) | ||
// Lower Band = SMA(Low * (1 - 4 * (High - Low) / (High + Low))) | ||
// | ||
// Example: | ||
// | ||
// accelerationBands := NewAccelerationBands[float64]() | ||
// accelerationBands.Compute(values) | ||
type AccelerationBands[T helper.Number] struct { | ||
// Time period. | ||
Period int | ||
} | ||
|
||
// NewAccelerationBands function initializes a new Acceleration Bands instance with the default parameters. | ||
func NewAccelerationBands[T helper.Number]() *AccelerationBands[T] { | ||
return &AccelerationBands[T]{ | ||
Period: DefaultAccelerationBandsPeriod, | ||
} | ||
} | ||
|
||
// Compute function takes a channel of numbers and computes the Acceleration Bands over the specified period. | ||
func (a *AccelerationBands[T]) Compute(high, low, closing <-chan T) (<-chan T, <-chan T, <-chan T) { | ||
highs := helper.Duplicate(high, 3) | ||
lows := helper.Duplicate(low, 3) | ||
|
||
ks := helper.Duplicate( | ||
helper.Divide( | ||
helper.Subtract(highs[0], lows[0]), | ||
helper.Add(highs[1], lows[1]), | ||
), | ||
2, | ||
) | ||
|
||
sma := trend.NewSmaWithPeriod[T](a.Period) | ||
|
||
upper := sma.Compute( | ||
helper.Multiply( | ||
highs[2], | ||
helper.IncrementBy( | ||
helper.MultiplyBy( | ||
ks[0], | ||
4, | ||
), | ||
1, | ||
), | ||
), | ||
) | ||
|
||
middle := sma.Compute(closing) | ||
|
||
lower := sma.Compute( | ||
helper.Multiply( | ||
lows[2], | ||
helper.IncrementBy( | ||
helper.MultiplyBy( | ||
ks[1], | ||
-4, | ||
), | ||
1, | ||
), | ||
), | ||
) | ||
|
||
return upper, middle, lower | ||
} | ||
|
||
// IdlePeriod is the initial period that Acceleration Bands won't yield any results. | ||
func (a *AccelerationBands[T]) IdlePeriod() int { | ||
return a.Period - 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2021-2023 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package volatility_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/indicator/helper" | ||
"github.com/cinar/indicator/volatility" | ||
) | ||
|
||
func TestAccelerationBands(t *testing.T) { | ||
type Data struct { | ||
High float64 | ||
Low float64 | ||
Close float64 | ||
Upper float64 | ||
Middle float64 | ||
Lower float64 | ||
} | ||
|
||
input, err := helper.ReadFromCsvFile[Data]("testdata/acceleration_bands.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
inputs := helper.Duplicate(input, 6) | ||
highs := helper.Map(inputs[0], func(d *Data) float64 { return d.High }) | ||
lows := helper.Map(inputs[1], func(d *Data) float64 { return d.Low }) | ||
closings := helper.Map(inputs[2], func(d *Data) float64 { return d.Close }) | ||
upper := helper.Map(inputs[3], func(d *Data) float64 { return d.Upper }) | ||
middle := helper.Map(inputs[4], func(d *Data) float64 { return d.Middle }) | ||
lower := helper.Map(inputs[5], func(d *Data) float64 { return d.Lower }) | ||
|
||
ab := volatility.NewAccelerationBands[float64]() | ||
actualUpper, actualMiddle, actualLower := ab.Compute(highs, lows, closings) | ||
actualUpper = helper.RoundDigits(actualUpper, 2) | ||
actualMiddle = helper.RoundDigits(actualMiddle, 2) | ||
actualLower = helper.RoundDigits(actualLower, 2) | ||
|
||
upper = helper.Skip(upper, ab.IdlePeriod()) | ||
middle = helper.Skip(middle, ab.IdlePeriod()) | ||
lower = helper.Skip(lower, ab.IdlePeriod()) | ||
|
||
err = helper.CheckEquals(actualUpper, upper, actualMiddle, middle, actualLower, lower) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.