mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
add updater to indicators
This commit is contained in:
parent
17fd6a405b
commit
f4aee5234a
|
@ -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 {
|
||||
|
@ -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 {
|
15
pkg/indicator/ewma_callbacks.go
Normal file
15
pkg/indicator/ewma_callbacks.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Code generated by "callbackgen -type EWMA"; DO NOT EDIT.
|
||||
|
||||
package indicator
|
||||
|
||||
import ()
|
||||
|
||||
func (inc *EWMA) OnUpdate(cb func(value float64)) {
|
||||
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (inc *EWMA) EmitUpdate(value float64) {
|
||||
for _, cb := range inc.UpdateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
@ -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) {
|
||||
|
|
15
pkg/indicator/sma_callbacks.go
Normal file
15
pkg/indicator/sma_callbacks.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// Code generated by "callbackgen -type SMA"; DO NOT EDIT.
|
||||
|
||||
package indicator
|
||||
|
||||
import ()
|
||||
|
||||
func (inc *SMA) OnUpdate(cb func(value float64)) {
|
||||
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
|
||||
}
|
||||
|
||||
func (inc *SMA) EmitUpdate(value float64) {
|
||||
for _, cb := range inc.UpdateCallbacks {
|
||||
cb(value)
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue
Block a user