Skip to content

Commit

Permalink
all: refactor and rename indicator.MACD to indicator.MACDLegacy
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed May 26, 2023
1 parent 171e067 commit 648e99f
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 91 deletions.
8 changes: 4 additions & 4 deletions pkg/bbgo/standard_indicator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type StandardIndicatorSet struct {
// interval -> window
iwbIndicators map[types.IntervalWindowBandWidth]*indicator.BOLL
iwIndicators map[indicatorKey]indicator.KLinePusher
macdIndicators map[indicator.MACDConfig]*indicator.MACD
macdIndicators map[indicator.MACDConfig]*indicator.MACDLegacy

stream types.Stream
store *MarketDataStore
Expand All @@ -47,7 +47,7 @@ func NewStandardIndicatorSet(symbol string, stream types.Stream, store *MarketDa
stream: stream,
iwIndicators: make(map[indicatorKey]indicator.KLinePusher),
iwbIndicators: make(map[types.IntervalWindowBandWidth]*indicator.BOLL),
macdIndicators: make(map[indicator.MACDConfig]*indicator.MACD),
macdIndicators: make(map[indicator.MACDConfig]*indicator.MACDLegacy),
}
}

Expand Down Expand Up @@ -154,14 +154,14 @@ func (s *StandardIndicatorSet) BOLL(iw types.IntervalWindow, bandWidth float64)
return inc
}

func (s *StandardIndicatorSet) MACD(iw types.IntervalWindow, shortPeriod, longPeriod int) *indicator.MACD {
func (s *StandardIndicatorSet) MACD(iw types.IntervalWindow, shortPeriod, longPeriod int) *indicator.MACDLegacy {
config := indicator.MACDConfig{IntervalWindow: iw, ShortPeriod: shortPeriod, LongPeriod: longPeriod}

inc, ok := s.macdIndicators[config]
if ok {
return inc
}
inc = &indicator.MACD{MACDConfig: config}
inc = &indicator.MACDLegacy{MACDConfig: config}
s.macdIndicators[config] = inc
s.initAndBind(inc, config.IntervalWindow.Interval)
return inc
Expand Down
6 changes: 6 additions & 0 deletions pkg/indicator/float64updater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package indicator

//go:generate callbackgen -type Float64Updater
type Float64Updater struct {
updateCallbacks []func(v float64)
}
37 changes: 37 additions & 0 deletions pkg/indicator/klinestream.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package indicator

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

const MaxNumOfKLines = 4_000

//go:generate callbackgen -type KLineStream
type KLineStream struct {
updateCallbacks []func(k types.KLine)

kLines []types.KLine
}

// AddSubscriber adds the subscriber function and push histrical data to the subscriber
func (s *KLineStream) AddSubscriber(f func(k types.KLine)) {
if len(s.kLines) > 0 {
// push historical klines to the subscriber
}

s.OnUpdate(f)
}

// KLines creates a KLine stream that pushes the klines to the subscribers
func KLines(source types.Stream) *KLineStream {
s := &KLineStream{}

source.OnKLineClosed(func(k types.KLine) {
s.kLines = append(s.kLines, k)

if len(s.kLines) > MaxNumOfKLines {
s.kLines = s.kLines[len(s.kLines)-1-MaxNumOfKLines:]
}
s.EmitUpdate(k)
})

return s
}
8 changes: 4 additions & 4 deletions pkg/indicator/klinestream_callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 0 additions & 49 deletions pkg/indicator/macd2.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,50 +23,6 @@ signal := EMA(macd, 16)
histogram := Subtract(macd, signal)
*/

//go:generate callbackgen -type KLineStream
type KLineStream struct {
updateCallbacks []func(k types.KLine)
}

func KLines(source types.Stream) *KLineStream {
stream := &KLineStream{}
source.OnKLineClosed(stream.EmitUpdate)
return stream
}

type KLineSource interface {
OnUpdate(f func(k types.KLine))
}

type PriceStream struct {
types.SeriesBase
Float64Updater

slice floats.Slice
mapper KLineValueMapper
}

func Price(source KLineSource, mapper KLineValueMapper) *PriceStream {
s := &PriceStream{
mapper: mapper,
}
s.SeriesBase.Series = s.slice
source.OnUpdate(func(k types.KLine) {
v := s.mapper(k)
s.slice.Push(v)
s.EmitUpdate(v)
})
return s
}

func ClosePrices(source KLineSource) *PriceStream {
return Price(source, KLineClosePriceMapper)
}

func OpenPrices(source KLineSource) *PriceStream {
return Price(source, KLineOpenPriceMapper)
}

type Float64Source interface {
types.Series
OnUpdate(f func(v float64))
Expand Down Expand Up @@ -104,11 +60,6 @@ func (s *EWMAStream) calculate(v float64) float64 {
return (1.0-m)*last + m*v
}

//go:generate callbackgen -type Float64Updater
type Float64Updater struct {
updateCallbacks []func(v float64)
}

type SubtractStream struct {
Float64Updater
types.SeriesBase
Expand Down
6 changes: 1 addition & 5 deletions pkg/indicator/macd2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,5 @@ func TestSubtract(t *testing.T) {

assert.Equal(t, len(subtract.a), len(subtract.b))
assert.Equal(t, len(subtract.a), len(subtract.c))
assert.Equal(t, subtract.c[0], subtract.a[0]-subtract.b[0])

t.Logf("subtract.a: %+v", subtract.a)
t.Logf("subtract.b: %+v", subtract.b)
t.Logf("subtract.c: %+v", subtract.c)
assert.InDelta(t, subtract.c[0], subtract.a[0]-subtract.b[0], 0.0001)
}
15 changes: 0 additions & 15 deletions pkg/indicator/macd_callbacks.go

This file was deleted.

23 changes: 11 additions & 12 deletions pkg/indicator/macd.go → pkg/indicator/macdlegacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ type MACDConfig struct {
LongPeriod int `json:"long"`
}

//go:generate callbackgen -type MACD
type MACD struct {
//go:generate callbackgen -type MACDLegacy
type MACDLegacy struct {
MACDConfig

Values floats.Slice `json:"-"`
fastEWMA, slowEWMA, signalLine *EWMA
Histogram floats.Slice `json:"-"`

EndTime time.Time

updateCallbacks []func(macd, signal, histogram float64)
EndTime time.Time
}

func (inc *MACD) Update(x float64) {
func (inc *MACDLegacy) Update(x float64) {
if len(inc.Values) == 0 {
// apply default values
inc.fastEWMA = &EWMA{IntervalWindow: types.IntervalWindow{Window: inc.ShortPeriod}}
Expand Down Expand Up @@ -76,35 +75,35 @@ func (inc *MACD) Update(x float64) {
inc.EmitUpdate(macd, signal, histogram)
}

func (inc *MACD) Last() float64 {
func (inc *MACDLegacy) Last() float64 {
if len(inc.Values) == 0 {
return 0.0
}

return inc.Values[len(inc.Values)-1]
}

func (inc *MACD) Length() int {
func (inc *MACDLegacy) Length() int {
return len(inc.Values)
}

func (inc *MACD) PushK(k types.KLine) {
func (inc *MACDLegacy) PushK(k types.KLine) {
inc.Update(k.Close.Float64())
}

func (inc *MACD) MACD() types.SeriesExtend {
out := &MACDValues{MACD: inc}
func (inc *MACDLegacy) MACD() types.SeriesExtend {
out := &MACDValues{MACDLegacy: inc}
out.SeriesBase.Series = out
return out
}

func (inc *MACD) Singals() types.SeriesExtend {
func (inc *MACDLegacy) Singals() types.SeriesExtend {
return inc.signalLine
}

type MACDValues struct {
types.SeriesBase
*MACD
*MACDLegacy
}

func (inc *MACDValues) Last() float64 {
Expand Down
15 changes: 15 additions & 0 deletions pkg/indicator/macdlegacy_callbacks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Test_calculateMACD(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
iw := types.IntervalWindow{Window: 9}
macd := MACD{MACDConfig: MACDConfig{IntervalWindow: iw, ShortPeriod: 12, LongPeriod: 26}}
macd := MACDLegacy{MACDConfig: MACDConfig{IntervalWindow: iw, ShortPeriod: 12, LongPeriod: 26}}
for _, k := range tt.kLines {
macd.PushK(k)
}
Expand Down
47 changes: 47 additions & 0 deletions pkg/indicator/price.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package indicator

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

type KLineSource interface {
OnUpdate(f func(k types.KLine))
}

type PriceStream struct {
types.SeriesBase
Float64Updater

slice floats.Slice
mapper KLineValueMapper
}

func Price(source KLineSource, mapper KLineValueMapper) *PriceStream {
s := &PriceStream{
mapper: mapper,
}
s.SeriesBase.Series = s.slice
source.OnUpdate(func(k types.KLine) {
v := s.mapper(k)
s.slice.Push(v)
s.EmitUpdate(v)
})
return s
}

func ClosePrices(source KLineSource) *PriceStream {
return Price(source, KLineClosePriceMapper)
}

func LowPrices(source KLineSource) *PriceStream {
return Price(source, KLineLowPriceMapper)
}

func HighPrices(source KLineSource) *PriceStream {
return Price(source, KLineHighPriceMapper)
}

func OpenPrices(source KLineSource) *PriceStream {
return Price(source, KLineOpenPriceMapper)
}
2 changes: 1 addition & 1 deletion pkg/strategy/pivotshort/failedbreakhigh.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type FailedBreakHigh struct {

MACDDivergence *MACDDivergence `json:"macdDivergence"`

macd *indicator.MACD
macd *indicator.MACDLegacy

macdTopDivergence bool

Expand Down

0 comments on commit 648e99f

Please sign in to comment.