-
-
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
9 changed files
with
546 additions
and
5 deletions.
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,46 @@ | ||
// Copyright (c) 2021-2023 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package helper | ||
|
||
// Operate3 applies the provided operate function to corresponding values from three | ||
// numeric input channels and sends the resulting values to an output channel. | ||
// | ||
// Example: | ||
// | ||
// add := helper.Operate3(ac, bc, cc, func(a, b, c int) int { | ||
// return a + b + c | ||
// }) | ||
func Operate3[A any, B any, C any, R any](ac <-chan A, bc <-chan B, cc <-chan C, o func(A, B, C) R) <-chan R { | ||
rc := make(chan R) | ||
|
||
go func() { | ||
defer close(rc) | ||
|
||
for { | ||
an, ok := <-ac | ||
if !ok { | ||
break | ||
} | ||
|
||
bn, ok := <-bc | ||
if !ok { | ||
break | ||
} | ||
|
||
cn, ok := <-cc | ||
if !ok { | ||
break | ||
} | ||
|
||
rc <- o(an, bn, cn) | ||
} | ||
|
||
Drain(ac) | ||
Drain(bc) | ||
Drain(cc) | ||
}() | ||
|
||
return rc | ||
} |
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,76 @@ | ||
// Copyright (c) 2021-2023 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package helper_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/cinar/indicator/helper" | ||
) | ||
|
||
func TestOperate3(t *testing.T) { | ||
ac := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) | ||
bc := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) | ||
cc := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) | ||
|
||
expected := []int{3, 6, 9, 12, 15, 18, 21, 24, 27, 30} | ||
|
||
actual := helper.ChanToSlice(helper.Operate3(ac, bc, cc, func(a, b, c int) int { | ||
return a + b + c | ||
})) | ||
|
||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("actual %v expected %v", actual, expected) | ||
} | ||
} | ||
|
||
func TestOperate3FirstEnds(t *testing.T) { | ||
ac := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8}) | ||
bc := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) | ||
cc := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) | ||
|
||
expected := []int{3, 6, 9, 12, 15, 18, 21, 24} | ||
|
||
actual := helper.ChanToSlice(helper.Operate3(ac, bc, cc, func(a, b, c int) int { | ||
return a + b + c | ||
})) | ||
|
||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("actual %v expected %v", actual, expected) | ||
} | ||
} | ||
|
||
func TestOperate3SecondEnds(t *testing.T) { | ||
ac := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) | ||
bc := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8}) | ||
cc := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) | ||
|
||
expected := []int{3, 6, 9, 12, 15, 18, 21, 24} | ||
|
||
actual := helper.ChanToSlice(helper.Operate3(ac, bc, cc, func(a, b, c int) int { | ||
return a + b + c | ||
})) | ||
|
||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("actual %v expected %v", actual, expected) | ||
} | ||
} | ||
|
||
func TestOperate3ThirdEnds(t *testing.T) { | ||
ac := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) | ||
bc := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) | ||
cc := helper.SliceToChan([]int{1, 2, 3, 4, 5, 6, 7, 8}) | ||
|
||
expected := []int{3, 6, 9, 12, 15, 18, 21, 24} | ||
|
||
actual := helper.ChanToSlice(helper.Operate3(ac, bc, cc, func(a, b, c int) int { | ||
return a + b + c | ||
})) | ||
|
||
if !reflect.DeepEqual(actual, expected) { | ||
t.Fatalf("actual %v expected %v", actual, expected) | ||
} | ||
} |
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,51 @@ | ||
// Copyright (c) 2021-2023 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package volatility | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/cinar/indicator/helper" | ||
"github.com/cinar/indicator/trend" | ||
) | ||
|
||
// Atr represents the configuration parameters for calculating the Average True Range (ATR). | ||
// It is a technical analysis indicator that measures market volatility by decomposing the | ||
// entire range of stock prices for that period. | ||
// | ||
// TR = Max((High - Low), (High - Closing), (Closing - Low)) | ||
// ATR = SMA TR | ||
// | ||
// Example: | ||
// | ||
// atr := volatility.NewAtr() | ||
// atr.Compute(values) | ||
type Atr[T helper.Number] struct { | ||
// Sma is the SMA for the ATR. | ||
Sma *trend.Sma[T] | ||
} | ||
|
||
// NewAtr function initializes a new ATR instance with the default parameters. | ||
func NewAtr[T helper.Number]() *Atr[T] { | ||
return &Atr[T]{ | ||
Sma: trend.NewSma[T](), | ||
} | ||
} | ||
|
||
// Compute function takes a channel of numbers and computes the ATR over the specified period. | ||
func (a *Atr[T]) Compute(highs, lows, closings <-chan T) <-chan T { | ||
tr := helper.Operate3(highs, lows, closings, func(high, low, closing T) T { | ||
return T(math.Max(float64(high-low), math.Max(float64(high-closing), float64(closing-low)))) | ||
}) | ||
|
||
atr := a.Sma.Compute(tr) | ||
|
||
return atr | ||
} | ||
|
||
// IdlePeriod is the initial period that Acceleration Bands won't yield any results. | ||
func (a *Atr[T]) IdlePeriod() int { | ||
return a.Sma.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,43 @@ | ||
// 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 TestAtr(t *testing.T) { | ||
type Data struct { | ||
High float64 | ||
Low float64 | ||
Close float64 | ||
Atr float64 | ||
} | ||
|
||
input, err := helper.ReadFromCsvFile[Data]("testdata/atr.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
inputs := helper.Duplicate(input, 4) | ||
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 }) | ||
expected := helper.Map(inputs[3], func(d *Data) float64 { return d.Atr }) | ||
|
||
atr := volatility.NewAtr[float64]() | ||
actual := atr.Compute(highs, lows, closings) | ||
actual = helper.RoundDigits(actual, 2) | ||
|
||
expected = helper.Skip(expected, atr.IdlePeriod()) | ||
|
||
err = helper.CheckEquals(actual, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
Oops, something went wrong.