Merge pull request #681 from andycheng123/indicator/supertrend

Indicator/supertrend
This commit is contained in:
Andy Cheng 2022-06-07 18:04:21 +08:00 committed by GitHub
commit 8770557e2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 171 additions and 12 deletions

138
pkg/indicator/supertrend.go Normal file
View File

@ -0,0 +1,138 @@
package indicator
import (
"math"
"time"
"github.com/c9s/bbgo/pkg/types"
)
//go:generate callbackgen -type Supertrend
type Supertrend struct {
types.IntervalWindow
ATRMultiplier float64 `json:"atrMultiplier"`
AverageTrueRange *ATR
trendPrices types.Float64Slice
closePrice float64
previousClosePrice float64
uptrendPrice float64
previousUptrendPrice float64
downtrendPrice float64
previousDowntrendPrice float64
trend types.Direction
previousTrend types.Direction
tradeSignal types.Direction
EndTime time.Time
UpdateCallbacks []func(value float64)
}
func (inc *Supertrend) Last() float64 {
return inc.trendPrices.Last()
}
func (inc *Supertrend) Index(i int) float64 {
length := inc.Length()
if length == 0 || length-i-1 < 0 {
return 0
}
return inc.trendPrices[length-i-1]
}
func (inc *Supertrend) Length() int {
return len(inc.trendPrices)
}
func (inc *Supertrend) Update(highPrice, lowPrice, closePrice float64) {
if inc.Window <= 0 {
panic("window must be greater than 0")
}
// Start with DirectionUp
if inc.trend != types.DirectionUp && inc.trend != types.DirectionDown {
inc.trend = types.DirectionUp
}
// Update ATR
inc.AverageTrueRange.Update(highPrice, lowPrice, closePrice)
// Update last prices
inc.previousUptrendPrice = inc.uptrendPrice
inc.previousDowntrendPrice = inc.downtrendPrice
inc.previousClosePrice = inc.closePrice
inc.previousTrend = inc.trend
inc.closePrice = closePrice
src := (highPrice + lowPrice) / 2
// Update uptrend
inc.uptrendPrice = src - inc.AverageTrueRange.Last()*inc.ATRMultiplier
if inc.previousClosePrice > inc.previousUptrendPrice {
inc.uptrendPrice = math.Max(inc.uptrendPrice, inc.previousUptrendPrice)
}
// Update downtrend
inc.downtrendPrice = src + inc.AverageTrueRange.Last()*inc.ATRMultiplier
if inc.previousClosePrice < inc.previousDowntrendPrice {
inc.downtrendPrice = math.Min(inc.downtrendPrice, inc.previousDowntrendPrice)
}
// Update trend
if inc.previousTrend == types.DirectionUp && inc.closePrice < inc.previousUptrendPrice {
inc.trend = types.DirectionDown
} else if inc.previousTrend == types.DirectionDown && inc.closePrice > inc.previousDowntrendPrice {
inc.trend = types.DirectionUp
} else {
inc.trend = inc.previousTrend
}
// Update signal
if inc.trend == types.DirectionUp && inc.previousTrend == types.DirectionDown {
inc.tradeSignal = types.DirectionUp
} else if inc.trend == types.DirectionDown && inc.previousTrend == types.DirectionUp {
inc.tradeSignal = types.DirectionDown
} else {
inc.tradeSignal = types.DirectionNone
}
// Update trend price
if inc.trend == types.DirectionDown {
inc.trendPrices.Push(inc.downtrendPrice)
} else {
inc.trendPrices.Push(inc.uptrendPrice)
}
}
func (inc *Supertrend) GetSignal() types.Direction {
return inc.tradeSignal
}
var _ types.Series = &Supertrend{}
func (inc *Supertrend) calculateAndUpdate(kLines []types.KLine) {
for _, k := range kLines {
if inc.EndTime != zeroTime && !k.EndTime.After(inc.EndTime) {
continue
}
inc.Update(k.GetHigh().Float64(), k.GetLow().Float64(), k.GetClose().Float64())
}
inc.EmitUpdate(inc.Last())
inc.EndTime = kLines[len(kLines)-1].EndTime.Time()
}
func (inc *Supertrend) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) {
if inc.Interval != interval {
return
}
inc.calculateAndUpdate(window)
}
func (inc *Supertrend) Bind(updater KLineWindowUpdater) {
updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate)
}

View File

@ -0,0 +1,15 @@
// Code generated by "callbackgen -type Supertrend"; DO NOT EDIT.
package indicator
import ()
func (inc *Supertrend) OnUpdate(cb func(value float64)) {
inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb)
}
func (inc *Supertrend) EmitUpdate(value float64) {
for _, cb := range inc.UpdateCallbacks {
cb(value)
}
}

View File

@ -136,7 +136,12 @@ type Strategy struct {
slowDEMA *indicator.DEMA
// SuperTrend indicator
SuperTrend SuperTrend `json:"superTrend"`
//SuperTrend SuperTrend `json:"superTrend"`
Supertrend *indicator.Supertrend
// SupertrendWindow ATR window for calculation of supertrend
SupertrendWindow int `json:"supertrendWindow"`
// SupertrendMultiplier ATR multiplier for calculation of supertrend
SupertrendMultiplier float64 `json:"supertrendMultiplier"`
// Leverage
Leverage float64 `json:"leverage"`
@ -238,14 +243,15 @@ func (s *Strategy) setupIndicators() {
}
s.slowDEMA = &indicator.DEMA{IntervalWindow: types.IntervalWindow{Interval: s.Interval, Window: s.SlowDEMAWindow}}
if s.SuperTrend.AverageTrueRangeWindow == 0 {
s.SuperTrend.AverageTrueRangeWindow = 39
if s.SupertrendWindow == 0 {
s.SupertrendWindow = 39
}
s.SuperTrend.averageTrueRange = &indicator.ATR{IntervalWindow: types.IntervalWindow{Window: s.SuperTrend.AverageTrueRangeWindow, Interval: s.Interval}}
s.SuperTrend.trend = types.DirectionUp
if s.SuperTrend.AverageTrueRangeMultiplier == 0 {
s.SuperTrend.AverageTrueRangeMultiplier = 3
if s.SupertrendMultiplier == 0 {
s.SupertrendMultiplier = 3
}
s.Supertrend = &indicator.Supertrend{IntervalWindow: types.IntervalWindow{Window: s.SupertrendWindow, Interval: s.Interval}, ATRMultiplier: s.SupertrendMultiplier}
s.Supertrend.AverageTrueRange = &indicator.ATR{IntervalWindow: types.IntervalWindow{Window: s.SupertrendWindow, Interval: s.Interval}}
}
// updateIndicators updates indicators
@ -259,8 +265,8 @@ func (s *Strategy) updateIndicators(kline types.KLine) {
if kline.Interval == s.slowDEMA.Interval {
s.slowDEMA.Update(closePrice)
}
if kline.Interval == s.SuperTrend.averageTrueRange.Interval {
s.SuperTrend.update(kline)
if kline.Interval == s.Supertrend.Interval {
s.Supertrend.Update(kline.GetHigh().Float64(), kline.GetLow().Float64(), closePrice)
}
}
@ -349,7 +355,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
// Get signals
closePrice := kline.GetClose().Float64()
openPrice := kline.GetOpen().Float64()
stSignal := s.SuperTrend.getSignal()
stSignal := s.Supertrend.GetSignal()
var demaSignal types.Direction
if closePrice > s.fastDEMA.Last() && closePrice > s.slowDEMA.Last() && !(openPrice > s.fastDEMA.Last() && openPrice > s.slowDEMA.Last()) {
demaSignal = types.DirectionUp
@ -401,7 +407,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.currentStopLossPrice = kline.GetLow()
}
if s.TakeProfitMultiplier > 0 {
s.currentTakeProfitPrice = kline.GetClose().Add(fixedpoint.NewFromFloat(s.SuperTrend.averageTrueRange.Last() * s.TakeProfitMultiplier))
s.currentTakeProfitPrice = kline.GetClose().Add(fixedpoint.NewFromFloat(s.Supertrend.AverageTrueRange.Last() * s.TakeProfitMultiplier))
}
} else if stSignal == types.DirectionDown && demaSignal == types.DirectionDown {
side = types.SideTypeSell
@ -409,7 +415,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.currentStopLossPrice = kline.GetHigh()
}
if s.TakeProfitMultiplier > 0 {
s.currentTakeProfitPrice = kline.GetClose().Sub(fixedpoint.NewFromFloat(s.SuperTrend.averageTrueRange.Last() * s.TakeProfitMultiplier))
s.currentTakeProfitPrice = kline.GetClose().Sub(fixedpoint.NewFromFloat(s.Supertrend.AverageTrueRange.Last() * s.TakeProfitMultiplier))
}
}