Skip to content

Commit

Permalink
indicator: rename KLinePriceMapper to KLineValueMapper
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Aug 24, 2022
1 parent 09cc91b commit d71fd36
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (inc *EWMA) PushK(k types.KLine) {
inc.EmitUpdate(inc.Last())
}

func CalculateKLinesEMA(allKLines []types.KLine, priceF KLinePriceMapper, window int) float64 {
func CalculateKLinesEMA(allKLines []types.KLine, priceF KLineValueMapper, window int) float64 {
var multiplier = 2.0 / (float64(window) + 1)
return ewma(MapKLinePrice(allKLines, priceF), multiplier)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/ewma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ func buildKLines(prices []fixedpoint.Value) (klines []types.KLine) {
func Test_calculateEWMA(t *testing.T) {
type args struct {
allKLines []types.KLine
priceF KLinePriceMapper
priceF KLineValueMapper
window int
}
var input []fixedpoint.Value
Expand Down
4 changes: 2 additions & 2 deletions pkg/indicator/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package indicator

import "github.com/c9s/bbgo/pkg/types"

type KLinePriceMapper func(k types.KLine) float64
type KLineValueMapper func(k types.KLine) float64

func KLineOpenPriceMapper(k types.KLine) float64 {
return k.Open.Float64()
Expand All @@ -24,7 +24,7 @@ func KLineVolumeMapper(k types.KLine) float64 {
return k.Volume.Float64()
}

func MapKLinePrice(kLines []types.KLine, f KLinePriceMapper) (prices []float64) {
func MapKLinePrice(kLines []types.KLine, f KLineValueMapper) (prices []float64) {
for _, k := range kLines {
prices = append(prices, f(k))
}
Expand Down
1 change: 0 additions & 1 deletion pkg/indicator/pivot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

type KLineValueMapper func(k types.KLine) float64

//go:generate callbackgen -type Pivot
type Pivot struct {
Expand Down
6 changes: 6 additions & 0 deletions pkg/indicator/pivot_low.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func calculatePivotF(values types.Float64Slice, left, right int, f func(a, pivot
return val, true
}

func calculatePivotHigh(highs types.Float64Slice, left, right int) (float64, bool) {
return calculatePivotF(highs, left, right, func(a, pivot float64) bool {
return a < pivot
})
}

func calculatePivotLow(lows types.Float64Slice, left, right int) (float64, bool) {
return calculatePivotF(lows, left, right, func(a, pivot float64) bool {
return a > pivot
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (inc *SMA) LoadK(allKLines []types.KLine) {
}
}

func calculateSMA(kLines []types.KLine, window int, priceF KLinePriceMapper) (float64, error) {
func calculateSMA(kLines []types.KLine, window int, priceF KLineValueMapper) (float64, error) {
length := len(kLines)
if length == 0 || length < window {
return 0.0, fmt.Errorf("insufficient elements for calculating SMA with window = %d", window)
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/volatility.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (inc *Volatility) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

func calculateVOLATILITY(klines []types.KLine, window int, priceF KLinePriceMapper) (float64, error) {
func calculateVOLATILITY(klines []types.KLine, window int, priceF KLineValueMapper) (float64, error) {
length := len(klines)
if length == 0 || length < window {
return 0.0, fmt.Errorf("insufficient elements for calculating VOL with window = %d", window)
Expand Down
2 changes: 1 addition & 1 deletion pkg/indicator/vwap.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (inc *VWAP) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

func calculateVWAP(klines []types.KLine, priceF KLinePriceMapper, window int) float64 {
func calculateVWAP(klines []types.KLine, priceF KLineValueMapper, window int) float64 {
vwap := VWAP{IntervalWindow: types.IntervalWindow{Window: window}}
for _, k := range klines {
vwap.Update(priceF(k), k.Volume.Float64())
Expand Down

0 comments on commit d71fd36

Please sign in to comment.