Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strategy/pivotshort: add failed break high #904

Merged
merged 5 commits into from
Aug 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions pkg/bbgo/standard_indicator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,23 @@ type StandardIndicatorSet struct {
// Standard indicators
// interval -> window
iwbIndicators map[types.IntervalWindowBandWidth]*indicator.BOLL
iwIndicators map[types.IntervalWindow]indicator.KLinePusher
iwIndicators map[indicatorKey]indicator.KLinePusher

stream types.Stream
store *MarketDataStore
}

type indicatorKey struct {
iw types.IntervalWindow
id string
}

func NewStandardIndicatorSet(symbol string, stream types.Stream, store *MarketDataStore) *StandardIndicatorSet {
return &StandardIndicatorSet{
Symbol: symbol,
store: store,
stream: stream,
iwIndicators: make(map[types.IntervalWindow]indicator.KLinePusher),
iwIndicators: make(map[indicatorKey]indicator.KLinePusher),
iwbIndicators: make(map[types.IntervalWindowBandWidth]*indicator.BOLL),
}
}
Expand All @@ -50,69 +55,77 @@ func (s *StandardIndicatorSet) initAndBind(inc indicator.KLinePusher, interval t
s.stream.OnKLineClosed(types.KLineWith(s.Symbol, interval, inc.PushK))
}

func (s *StandardIndicatorSet) allocateSimpleIndicator(t indicator.KLinePusher, iw types.IntervalWindow) indicator.KLinePusher {
inc, ok := s.iwIndicators[iw]
func (s *StandardIndicatorSet) allocateSimpleIndicator(t indicator.KLinePusher, iw types.IntervalWindow, id string) indicator.KLinePusher {
k := indicatorKey{
iw: iw,
id: id,
}
inc, ok := s.iwIndicators[k]
if ok {
return inc
}

inc = t
s.initAndBind(inc, iw.Interval)
s.iwIndicators[iw] = inc
s.iwIndicators[k] = inc
return t
}

// SMA is a helper function that returns the simple moving average indicator of the given interval and the window size.
func (s *StandardIndicatorSet) SMA(iw types.IntervalWindow) *indicator.SMA {
inc := s.allocateSimpleIndicator(&indicator.SMA{IntervalWindow: iw}, iw)
inc := s.allocateSimpleIndicator(&indicator.SMA{IntervalWindow: iw}, iw, "sma")
return inc.(*indicator.SMA)
}

// EWMA is a helper function that returns the exponential weighed moving average indicator of the given interval and the window size.
func (s *StandardIndicatorSet) EWMA(iw types.IntervalWindow) *indicator.EWMA {
inc := s.allocateSimpleIndicator(&indicator.EWMA{IntervalWindow: iw}, iw)
inc := s.allocateSimpleIndicator(&indicator.EWMA{IntervalWindow: iw}, iw, "ewma")
return inc.(*indicator.EWMA)
}

// VWMA
func (s *StandardIndicatorSet) VWMA(iw types.IntervalWindow) *indicator.VWMA {
inc := s.allocateSimpleIndicator(&indicator.VWMA{IntervalWindow: iw}, iw)
inc := s.allocateSimpleIndicator(&indicator.VWMA{IntervalWindow: iw}, iw, "vwma")
return inc.(*indicator.VWMA)
}

func (s *StandardIndicatorSet) PivotHigh(iw types.IntervalWindow) *indicator.PivotHigh {
inc := s.allocateSimpleIndicator(&indicator.PivotHigh{IntervalWindow: iw}, iw, "pivothigh")
return inc.(*indicator.PivotHigh)
}

func (s *StandardIndicatorSet) PivotLow(iw types.IntervalWindow) *indicator.PivotLow {
inc := s.allocateSimpleIndicator(&indicator.PivotLow{IntervalWindow: iw}, iw)
inc := s.allocateSimpleIndicator(&indicator.PivotLow{IntervalWindow: iw}, iw, "pivotlow")
return inc.(*indicator.PivotLow)
}

func (s *StandardIndicatorSet) ATR(iw types.IntervalWindow) *indicator.ATR {
inc := s.allocateSimpleIndicator(&indicator.ATR{IntervalWindow: iw}, iw)
inc := s.allocateSimpleIndicator(&indicator.ATR{IntervalWindow: iw}, iw, "atr")
return inc.(*indicator.ATR)
}

func (s *StandardIndicatorSet) ATRP(iw types.IntervalWindow) *indicator.ATRP {
inc := s.allocateSimpleIndicator(&indicator.ATRP{IntervalWindow: iw}, iw)
inc := s.allocateSimpleIndicator(&indicator.ATRP{IntervalWindow: iw}, iw, "atrp")
return inc.(*indicator.ATRP)
}

func (s *StandardIndicatorSet) EMV(iw types.IntervalWindow) *indicator.EMV {
inc := s.allocateSimpleIndicator(&indicator.EMV{IntervalWindow: iw}, iw)
inc := s.allocateSimpleIndicator(&indicator.EMV{IntervalWindow: iw}, iw, "emv")
return inc.(*indicator.EMV)
}

func (s *StandardIndicatorSet) CCI(iw types.IntervalWindow) *indicator.CCI {
inc := s.allocateSimpleIndicator(&indicator.CCI{IntervalWindow: iw}, iw)
inc := s.allocateSimpleIndicator(&indicator.CCI{IntervalWindow: iw}, iw, "cci")
return inc.(*indicator.CCI)
}

func (s *StandardIndicatorSet) HULL(iw types.IntervalWindow) *indicator.HULL {
inc := s.allocateSimpleIndicator(&indicator.HULL{IntervalWindow: iw}, iw)
inc := s.allocateSimpleIndicator(&indicator.HULL{IntervalWindow: iw}, iw, "hull")
return inc.(*indicator.HULL)
}

func (s *StandardIndicatorSet) STOCH(iw types.IntervalWindow) *indicator.STOCH {
inc := s.allocateSimpleIndicator(&indicator.STOCH{IntervalWindow: iw}, iw)
inc := s.allocateSimpleIndicator(&indicator.STOCH{IntervalWindow: iw}, iw, "stoch")
return inc.(*indicator.STOCH)
}

Expand Down
65 changes: 65 additions & 0 deletions pkg/indicator/pivothigh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package indicator

import (
"time"

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

//go:generate callbackgen -type PivotHigh
type PivotHigh struct {
types.SeriesBase

types.IntervalWindow

Highs floats.Slice
Values floats.Slice
EndTime time.Time

updateCallbacks []func(value float64)
}

func (inc *PivotHigh) Length() int {
return inc.Values.Length()
}

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

return inc.Values.Last()
}

func (inc *PivotHigh) Update(value float64) {
if len(inc.Highs) == 0 {
inc.SeriesBase.Series = inc
}

inc.Highs.Push(value)

if len(inc.Highs) < inc.Window {
return
}

low, ok := calculatePivotHigh(inc.Highs, inc.Window, inc.RightWindow)
if !ok {
return
}

if low > 0.0 {
inc.Values.Push(low)
}
}

func (inc *PivotHigh) PushK(k types.KLine) {
if k.EndTime.Before(inc.EndTime) {
return
}

inc.Update(k.Low.Float64())
inc.EndTime = k.EndTime.Time()
inc.EmitUpdate(inc.Last())
}

15 changes: 15 additions & 0 deletions pkg/indicator/pivothigh_callbacks.go

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

File renamed without changes.
File renamed without changes.
Loading