-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite cci indicator in v2 indicator
- Loading branch information
Showing
9 changed files
with
140 additions
and
9 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,65 @@ | ||
package indicatorv2 | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/c9s/bbgo/pkg/types" | ||
) | ||
|
||
// Refer: Commodity Channel Index | ||
// Refer URL: http://www.andrewshamlet.net/2017/07/08/python-tutorial-cci | ||
// with modification of ddof=0 to let standard deviation to be divided by N instead of N-1 | ||
// | ||
// CCI = (Typical Price - n-period SMA of TP) / (Constant x Mean Deviation) | ||
// | ||
// Typical Price (TP) = (High + Low + Close)/3 | ||
// | ||
// Constant = .015 | ||
// | ||
// The Commodity Channel Index (CCI) is a technical analysis indicator that is used to identify potential overbought or oversold conditions | ||
// in a security's price. It was originally developed for use in commodity markets, but can be applied to any security that has a sufficient | ||
// amount of price data. The CCI is calculated by taking the difference between the security's typical price (the average of its high, low, and | ||
// closing prices) and its moving average, and then dividing the result by the mean absolute deviation of the typical price. This resulting value | ||
// is then plotted as a line on the price chart, with values above +100 indicating overbought conditions and values below -100 indicating | ||
// oversold conditions. The CCI can be used by traders to identify potential entry and exit points for trades, or to confirm other technical | ||
// analysis signals. | ||
|
||
type CCIStream struct { | ||
*types.Float64Series | ||
|
||
TypicalPrice *types.Float64Series | ||
|
||
source types.Float64Source | ||
window int | ||
} | ||
|
||
func CCI(source types.Float64Source, window int) *CCIStream { | ||
s := &CCIStream{ | ||
Float64Series: types.NewFloat64Series(), | ||
TypicalPrice: types.NewFloat64Series(), | ||
source: source, | ||
window: window, | ||
} | ||
s.Bind(source, s) | ||
return s | ||
} | ||
|
||
func (s *CCIStream) Calculate(value float64) float64 { | ||
var tp = value | ||
if s.TypicalPrice.Length() > 0 { | ||
tp = s.TypicalPrice.Last(0) - s.source.Last(s.window) + value | ||
} | ||
|
||
s.TypicalPrice.Push(tp) | ||
|
||
ma := tp / float64(s.window) | ||
md := 0. | ||
for i := 0; i < s.window; i++ { | ||
diff := s.source.Last(i) - ma | ||
md += diff * diff | ||
} | ||
|
||
md = math.Sqrt(md / float64(s.window)) | ||
cci := (value - ma) / (0.015 * md) | ||
return cci | ||
} |
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,39 @@ | ||
package indicatorv2 | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
/* | ||
python: | ||
import pandas as pd | ||
s = pd.Series([0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9]) | ||
cci = pd.Series((s - s.rolling(16).mean()) / (0.015 * s.rolling(16).std(ddof=0)), name="CCI") | ||
print(cci) | ||
*/ | ||
func Test_CCI(t *testing.T) { | ||
var randomPrices = []byte(`[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]`) | ||
var input []float64 | ||
var delta = 4.3e-2 | ||
if err := json.Unmarshal(randomPrices, &input); err != nil { | ||
panic(err) | ||
} | ||
t.Run("random_case", func(t *testing.T) { | ||
price := Price(nil, nil) | ||
cci := CCI(price, 16) | ||
for _, value := range input { | ||
price.PushAndEmit(value) | ||
} | ||
|
||
t.Logf("cci: %+v", cci.Slice) | ||
|
||
last := cci.Last(0) | ||
assert.InDelta(t, 93.250481, last, delta) | ||
assert.InDelta(t, 81.813449, cci.Index(1), delta) | ||
assert.Equal(t, 50, cci.Length(), "length") | ||
}) | ||
} |
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,5 @@ | ||
package indicatorv2 | ||
|
||
import "github.com/c9s/bbgo/pkg/fixedpoint" | ||
|
||
var three = fixedpoint.NewFromInt(3) |
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
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