-
-
Notifications
You must be signed in to change notification settings - Fork 104
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
8 changed files
with
492 additions
and
13 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,94 @@ | ||
// Copyright (c) 2021-2023 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package trend | ||
|
||
import ( | ||
"github.com/cinar/indicator/helper" | ||
) | ||
|
||
const ( | ||
// DefaultCciPeriod is the default time period for CCI. | ||
DefaultCciPeriod = 20 | ||
) | ||
|
||
// Cci represents the configuration parameters for calculating the Community Channel Index (CCI). CCI is a | ||
// momentum-based oscillator used to help determine when an investment vehicle is reaching a condition of | ||
// being overbought or oversold. | ||
// | ||
// Moving Average = Sma(Period, Typical Price) | ||
// Mean Deviation = Sma(Period, Abs(Typical Price - Moving Average)) | ||
// CCI = (Typical Price - Moving Average) / (0.015 * Mean Deviation) | ||
// | ||
// Example: | ||
// | ||
// cmi := trend.NewCmi() | ||
// cmi.Period = 20 | ||
// values = cmi.Compute(highs, lows, closings) | ||
type Cci[T helper.Number] struct { | ||
// Time period. | ||
Period int | ||
} | ||
|
||
// NewCci function initializes a new CCI instance with the default parameters. | ||
func NewCci[T helper.Number]() *Cci[T] { | ||
return &Cci[T]{ | ||
Period: DefaultCciPeriod, | ||
} | ||
} | ||
|
||
// NewCciWithPeriod function initializes a new CCI instance with the given period. | ||
func NewCciWithPeriod[T helper.Number](period int) *Cci[T] { | ||
return &Cci[T]{ | ||
Period: period, | ||
} | ||
} | ||
|
||
// Compute function takes a channel of numbers and computes the CCI and the signal line. | ||
func (c *Cci[T]) Compute(highs, lows, closings <-chan T) <-chan T { | ||
typicalPrice := NewTypicalPrice[T]() | ||
sma1 := NewSmaWithPeriod[T](c.Period) | ||
sma2 := NewSmaWithPeriod[T](c.Period) | ||
|
||
tps := helper.Duplicate[T]( | ||
typicalPrice.Compute(highs, lows, closings), | ||
3, | ||
) | ||
|
||
mas := helper.Duplicate[T]( | ||
sma1.Compute(tps[0]), | ||
2, | ||
) | ||
|
||
tps[1] = helper.Skip(tps[1], sma1.Period-1) | ||
tps[2] = helper.Skip(tps[2], sma1.Period-1) | ||
|
||
md := sma2.Compute( | ||
helper.Abs( | ||
helper.Subtract(tps[1], mas[0]), | ||
), | ||
) | ||
|
||
mas[1] = helper.Skip(mas[1], sma2.Period-1) | ||
tps[2] = helper.Skip(tps[2], sma2.Period-1) | ||
|
||
multiplier := 0.015 | ||
|
||
cci := helper.Divide( | ||
helper.Subtract( | ||
tps[2], mas[1], | ||
), | ||
helper.MultiplyBy[T]( | ||
md, | ||
T(multiplier), | ||
), | ||
) | ||
|
||
return cci | ||
} | ||
|
||
// IdlePeriod is the initial period that CCI won't yield any results. | ||
func (c *Cci[T]) IdlePeriod() int { | ||
return (c.Period * 2) - 2 | ||
} |
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,44 @@ | ||
// Copyright (c) 2021-2023 Onur Cinar. | ||
// The source code is provided under GNU AGPLv3 License. | ||
// https://github.com/cinar/indicator | ||
|
||
package trend_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cinar/indicator/helper" | ||
"github.com/cinar/indicator/trend" | ||
) | ||
|
||
func TestCci(t *testing.T) { | ||
type Data struct { | ||
High float64 | ||
Low float64 | ||
Close float64 | ||
Cci float64 | ||
} | ||
|
||
input, err := helper.ReadFromCsvFile[Data]("testdata/cci.csv", true) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
inputs := helper.Duplicate(input, 4) | ||
high := helper.Map(inputs[0], func(d *Data) float64 { return d.High }) | ||
low := helper.Map(inputs[1], func(d *Data) float64 { return d.Low }) | ||
closing := helper.Map(inputs[2], func(d *Data) float64 { return d.Close }) | ||
expected := helper.Map(inputs[3], func(d *Data) float64 { return d.Cci }) | ||
|
||
cci := trend.NewCci[float64]() | ||
|
||
actual := cci.Compute(high, low, closing) | ||
actual = helper.RoundDigits(actual, 2) | ||
|
||
expected = helper.Skip(expected, cci.IdlePeriod()) | ||
|
||
err = helper.CheckEquals(actual, expected) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.