Skip to content

Commit

Permalink
add updater to indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 3, 2020
1 parent 17fd6a4 commit f4aee52
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/indicator/ema.go → pkg/indicator/ewma.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"github.com/c9s/bbgo/pkg/types"
)

//go:generate callbackgen -type EWMA
type EWMA struct {
types.IntervalWindow
Values Float64Slice
EndTime time.Time
Values Float64Slice
EndTime time.Time

UpdateCallbacks []func(value float64)
}

func (inc *EWMA) Last() float64 {
Expand All @@ -32,18 +35,20 @@ func (inc *EWMA) calculateAndUpdate(kLines []types.KLine) {
return
}

inc.EndTime = kLines[index].EndTime

var recentK = kLines[index-(inc.Window-1) : index+1]
if len(inc.Values) > 0 {
var previousEWMA = inc.Values[len(inc.Values)-1]
var ewma = lastK.Close*multiplier + previousEWMA*(1-multiplier)
inc.Values.Push(ewma)
inc.EmitUpdate(ewma)
} else {
// The first EWMA is actually SMA
var sma = calculateSMA(recentK)
inc.Values.Push(sma)
inc.EmitUpdate(sma)
}

inc.EndTime = kLines[index].EndTime
}

type KLineWindowUpdater interface {
Expand Down
15 changes: 15 additions & 0 deletions pkg/indicator/ewma_callbacks.go

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

6 changes: 6 additions & 0 deletions pkg/indicator/sma.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ func (s *Float64Slice) Push(v float64) {

var zeroTime time.Time


//go:generate callbackgen -type SMA
type SMA struct {
types.IntervalWindow
Values Float64Slice
EndTime time.Time

UpdateCallbacks []func(value float64)
}

func (inc *SMA) Last() float64 {
Expand All @@ -40,6 +44,8 @@ func (inc *SMA) calculateAndUpdate(kLines []types.KLine) {
var sma = calculateSMA(recentK)
inc.Values.Push(sma)
inc.EndTime = kLines[index].EndTime

inc.EmitUpdate(sma)
}

func (inc *SMA) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
Expand Down
15 changes: 15 additions & 0 deletions pkg/indicator/sma_callbacks.go

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

2 changes: 2 additions & 0 deletions pkg/strategy/movingstop/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func (s *Strategy) place(ctx context.Context, orderExecutor *bbgo.ExchangeOrderE
stopPrice = stopPrice * s.StopPriceRatio.Float64()
}

log.Infof("placing movingstop order %s at stop price %f, quantity %f", s.Symbol, stopPrice, quantity.Float64())

retOrders, err := orderExecutor.SubmitOrders(ctx, types.SubmitOrder{
Symbol: s.Symbol,
Side: types.SideTypeSell,
Expand Down

0 comments on commit f4aee52

Please sign in to comment.