Skip to content

Commit

Permalink
all: move v2 indicator to indicator/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jul 10, 2023
1 parent 3293866 commit 5853434
Show file tree
Hide file tree
Showing 73 changed files with 443 additions and 396 deletions.
64 changes: 32 additions & 32 deletions pkg/bbgo/indicator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package bbgo
import (
"github.com/sirupsen/logrus"

"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/indicator/v2"
"github.com/c9s/bbgo/pkg/types"
)

Expand All @@ -16,8 +16,8 @@ type IndicatorSet struct {
store *MarketDataStore

// caches
kLines map[types.Interval]*indicator.KLineStream
closePrices map[types.Interval]*indicator.PriceStream
kLines map[types.Interval]*indicatorv2.KLineStream
closePrices map[types.Interval]*indicatorv2.PriceStream
}

func NewIndicatorSet(symbol string, stream types.Stream, store *MarketDataStore) *IndicatorSet {
Expand All @@ -26,17 +26,17 @@ func NewIndicatorSet(symbol string, stream types.Stream, store *MarketDataStore)
store: store,
stream: stream,

kLines: make(map[types.Interval]*indicator.KLineStream),
closePrices: make(map[types.Interval]*indicator.PriceStream),
kLines: make(map[types.Interval]*indicatorv2.KLineStream),
closePrices: make(map[types.Interval]*indicatorv2.PriceStream),
}
}

func (i *IndicatorSet) KLines(interval types.Interval) *indicator.KLineStream {
func (i *IndicatorSet) KLines(interval types.Interval) *indicatorv2.KLineStream {
if kLines, ok := i.kLines[interval]; ok {
return kLines
}

kLines := indicator.KLines(i.stream, i.Symbol, interval)
kLines := indicatorv2.KLines(i.stream, i.Symbol, interval)
if kLinesWindow, ok := i.store.KLinesOfInterval(interval); ok {
kLines.BackFill(*kLinesWindow)
} else {
Expand All @@ -47,60 +47,60 @@ func (i *IndicatorSet) KLines(interval types.Interval) *indicator.KLineStream {
return kLines
}

func (i *IndicatorSet) OPEN(interval types.Interval) *indicator.PriceStream {
return indicator.OpenPrices(i.KLines(interval))
func (i *IndicatorSet) OPEN(interval types.Interval) *indicatorv2.PriceStream {
return indicatorv2.OpenPrices(i.KLines(interval))
}

func (i *IndicatorSet) HIGH(interval types.Interval) *indicator.PriceStream {
return indicator.HighPrices(i.KLines(interval))
func (i *IndicatorSet) HIGH(interval types.Interval) *indicatorv2.PriceStream {
return indicatorv2.HighPrices(i.KLines(interval))
}

func (i *IndicatorSet) LOW(interval types.Interval) *indicator.PriceStream {
return indicator.LowPrices(i.KLines(interval))
func (i *IndicatorSet) LOW(interval types.Interval) *indicatorv2.PriceStream {
return indicatorv2.LowPrices(i.KLines(interval))
}

func (i *IndicatorSet) CLOSE(interval types.Interval) *indicator.PriceStream {
func (i *IndicatorSet) CLOSE(interval types.Interval) *indicatorv2.PriceStream {
if closePrices, ok := i.closePrices[interval]; ok {
return closePrices
}

closePrices := indicator.ClosePrices(i.KLines(interval))
closePrices := indicatorv2.ClosePrices(i.KLines(interval))
i.closePrices[interval] = closePrices
return closePrices
}

func (i *IndicatorSet) VOLUME(interval types.Interval) *indicator.PriceStream {
return indicator.Volumes(i.KLines(interval))
func (i *IndicatorSet) VOLUME(interval types.Interval) *indicatorv2.PriceStream {
return indicatorv2.Volumes(i.KLines(interval))
}

func (i *IndicatorSet) RSI(iw types.IntervalWindow) *indicator.RSIStream {
return indicator.RSI2(i.CLOSE(iw.Interval), iw.Window)
func (i *IndicatorSet) RSI(iw types.IntervalWindow) *indicatorv2.RSIStream {
return indicatorv2.RSI2(i.CLOSE(iw.Interval), iw.Window)
}

func (i *IndicatorSet) EMA(iw types.IntervalWindow) *indicator.EWMAStream {
func (i *IndicatorSet) EMA(iw types.IntervalWindow) *indicatorv2.EWMAStream {
return i.EWMA(iw)
}

func (i *IndicatorSet) EWMA(iw types.IntervalWindow) *indicator.EWMAStream {
return indicator.EWMA2(i.CLOSE(iw.Interval), iw.Window)
func (i *IndicatorSet) EWMA(iw types.IntervalWindow) *indicatorv2.EWMAStream {
return indicatorv2.EWMA2(i.CLOSE(iw.Interval), iw.Window)
}

func (i *IndicatorSet) STOCH(iw types.IntervalWindow, dPeriod int) *indicator.StochStream {
return indicator.Stoch2(i.KLines(iw.Interval), iw.Window, dPeriod)
func (i *IndicatorSet) STOCH(iw types.IntervalWindow, dPeriod int) *indicatorv2.StochStream {
return indicatorv2.Stoch(i.KLines(iw.Interval), iw.Window, dPeriod)
}

func (i *IndicatorSet) BOLL(iw types.IntervalWindow, k float64) *indicator.BOLLStream {
return indicator.BOLL2(i.CLOSE(iw.Interval), iw.Window, k)
func (i *IndicatorSet) BOLL(iw types.IntervalWindow, k float64) *indicatorv2.BOLLStream {
return indicatorv2.BOLL(i.CLOSE(iw.Interval), iw.Window, k)
}

func (i *IndicatorSet) MACD(interval types.Interval, shortWindow, longWindow, signalWindow int) *indicator.MACDStream {
return indicator.MACD2(i.CLOSE(interval), shortWindow, longWindow, signalWindow)
func (i *IndicatorSet) MACD(interval types.Interval, shortWindow, longWindow, signalWindow int) *indicatorv2.MACDStream {
return indicatorv2.MACD2(i.CLOSE(interval), shortWindow, longWindow, signalWindow)
}

func (i *IndicatorSet) ATR(interval types.Interval, window int) *indicator.ATRStream {
return indicator.ATR2(i.KLines(interval), window)
func (i *IndicatorSet) ATR(interval types.Interval, window int) *indicatorv2.ATRStream {
return indicatorv2.ATR2(i.KLines(interval), window)
}

func (i *IndicatorSet) ATRP(interval types.Interval, window int) *indicator.ATRPStream {
return indicator.ATRP2(i.KLines(interval), window)
func (i *IndicatorSet) ATRP(interval types.Interval, window int) *indicatorv2.ATRPStream {
return indicatorv2.ATRP2(i.KLines(interval), window)
}
4 changes: 2 additions & 2 deletions pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func (inc *EWMA) PushK(k types.KLine) {
inc.EmitUpdate(inc.Last(0))
}

func CalculateKLinesEMA(allKLines []types.KLine, priceF KLineValueMapper, window int) float64 {
func CalculateKLinesEMA(allKLines []types.KLine, priceF types.KLineValueMapper, window int) float64 {
var multiplier = 2.0 / (float64(window) + 1)
return ewma(MapKLinePrice(allKLines, priceF), multiplier)
return ewma(types.MapKLinePrice(allKLines, priceF), multiplier)
}

// see https://www.investopedia.com/ask/answers/122314/what-exponential-moving-average-ema-formula-and-how-ema-calculated.asp
Expand Down
8 changes: 4 additions & 4 deletions 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 KLineValueMapper
priceF types.KLineValueMapper
window int
}
var input []fixedpoint.Value
Expand All @@ -1043,7 +1043,7 @@ func Test_calculateEWMA(t *testing.T) {
name: "ETHUSDT EMA 7",
args: args{
allKLines: buildKLines(input),
priceF: KLineClosePriceMapper,
priceF: types.KLineClosePriceMapper,
window: 7,
},
want: 571.72, // with open price, binance desktop returns 571.45, trading view returns 570.8957, for close price, binance mobile returns 571.72
Expand All @@ -1052,7 +1052,7 @@ func Test_calculateEWMA(t *testing.T) {
name: "ETHUSDT EMA 25",
args: args{
allKLines: buildKLines(input),
priceF: KLineClosePriceMapper,
priceF: types.KLineClosePriceMapper,
window: 25,
},
want: 571.30,
Expand All @@ -1061,7 +1061,7 @@ func Test_calculateEWMA(t *testing.T) {
name: "ETHUSDT EMA 99",
args: args{
allKLines: buildKLines(input),
priceF: KLineClosePriceMapper,
priceF: types.KLineClosePriceMapper,
window: 99,
},
want: 577.62, // binance mobile uses 577.58
Expand Down
15 changes: 0 additions & 15 deletions pkg/indicator/float64updater_callbacks.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/indicator/ghfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6070,7 +6070,7 @@ func Test_GHFilter(t *testing.T) {
func Test_GHFilterEstimationAccurate(t *testing.T) {
type args struct {
allKLines []types.KLine
priceF KLineValueMapper
priceF types.KLineValueMapper
window int
}
var klines []types.KLine
Expand Down
33 changes: 0 additions & 33 deletions pkg/indicator/mapper.go

This file was deleted.

12 changes: 2 additions & 10 deletions pkg/indicator/pivot.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (inc *Pivot) CalculateAndUpdate(klines []types.KLine) {

recentT := klines[end-(inc.Window-1) : end+1]

l, h, err := calculatePivot(recentT, inc.Window, KLineLowPriceMapper, KLineHighPriceMapper)
l, h, err := calculatePivot(recentT, inc.Window, types.KLineLowPriceMapper, types.KLineHighPriceMapper)
if err != nil {
log.WithError(err).Error("can not calculate pivots")
return
Expand Down Expand Up @@ -90,7 +90,7 @@ func (inc *Pivot) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

func calculatePivot(klines []types.KLine, window int, valLow KLineValueMapper, valHigh KLineValueMapper) (float64, float64, error) {
func calculatePivot(klines []types.KLine, window int, valLow types.KLineValueMapper, valHigh types.KLineValueMapper) (float64, float64, error) {
length := len(klines)
if length == 0 || length < window {
return 0., 0., fmt.Errorf("insufficient elements for calculating with window = %d", window)
Expand All @@ -115,11 +115,3 @@ func calculatePivot(klines []types.KLine, window int, valLow KLineValueMapper, v

return pl, ph, nil
}

func KLineLowPriceMapper(k types.KLine) float64 {
return k.Low.Float64()
}

func KLineHighPriceMapper(k types.KLine) float64 {
return k.High.Float64()
}
22 changes: 0 additions & 22 deletions pkg/indicator/types.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/indicator/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ func max(x, y int) int {
return y
}

func min(x, y int) int {
func Min(x, y int) int {
if x < y {
return x
}
Expand Down
19 changes: 0 additions & 19 deletions pkg/indicator/v2.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/indicator/v2_atr.go → pkg/indicator/v2/atr.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package indicator
package indicatorv2

type ATRStream struct {
// embedded struct
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package indicator
package indicatorv2

import (
"encoding/json"
Expand Down
8 changes: 5 additions & 3 deletions pkg/indicator/v2_atrp.go → pkg/indicator/v2/atrp.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package indicator
package indicatorv2

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

type ATRPStream struct {
*Float64Series
*types.Float64Series
}

func ATRP2(source KLineSubscription, window int) *ATRPStream {
s := &ATRPStream{
Float64Series: NewFloat64Series(),
Float64Series: types.NewFloat64Series(),
}
tr := TR2(source)
atr := RMA2(tr, window, true)
Expand Down
20 changes: 12 additions & 8 deletions pkg/indicator/v2_boll.go → pkg/indicator/v2/boll.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package indicator
package indicatorv2

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

type BOLLStream struct {
// the band series
*Float64Series
*types.Float64Series

UpBand, DownBand *Float64Series
UpBand, DownBand *types.Float64Series

window int
k float64
Expand All @@ -20,15 +24,15 @@ type BOLLStream struct {
//
// -> calculate SMA
// -> calculate stdDev -> calculate bandWidth -> get latest SMA -> upBand, downBand
func BOLL2(source Float64Source, window int, k float64) *BOLLStream {
func BOLL(source types.Float64Source, window int, k float64) *BOLLStream {
// bind these indicators before our main calculator
sma := SMA2(source, window)
stdDev := StdDev2(source, window)
stdDev := StdDev(source, window)

s := &BOLLStream{
Float64Series: NewFloat64Series(),
UpBand: NewFloat64Series(),
DownBand: NewFloat64Series(),
Float64Series: types.NewFloat64Series(),
UpBand: types.NewFloat64Series(),
DownBand: types.NewFloat64Series(),
window: window,
k: k,
SMA: sma,
Expand Down
Loading

0 comments on commit 5853434

Please sign in to comment.