2022-07-06 08:45:19 +00:00
|
|
|
package supertrend
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/c9s/bbgo/pkg/bbgo"
|
|
|
|
"github.com/c9s/bbgo/pkg/indicator"
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DoubleDema struct {
|
2022-07-08 08:42:31 +00:00
|
|
|
DemaInterval types.Interval `json:"demaInterval"`
|
2022-07-06 08:45:19 +00:00
|
|
|
|
|
|
|
// FastDEMAWindow DEMA window for checking breakout
|
|
|
|
FastDEMAWindow int `json:"fastDEMAWindow"`
|
|
|
|
// SlowDEMAWindow DEMA window for checking breakout
|
|
|
|
SlowDEMAWindow int `json:"slowDEMAWindow"`
|
|
|
|
fastDEMA *indicator.DEMA
|
|
|
|
slowDEMA *indicator.DEMA
|
|
|
|
}
|
|
|
|
|
|
|
|
// getDemaSignal get current DEMA signal
|
|
|
|
func (dd *DoubleDema) getDemaSignal(openPrice float64, closePrice float64) types.Direction {
|
|
|
|
var demaSignal types.Direction = types.DirectionNone
|
|
|
|
|
|
|
|
if closePrice > dd.fastDEMA.Last() && closePrice > dd.slowDEMA.Last() && !(openPrice > dd.fastDEMA.Last() && openPrice > dd.slowDEMA.Last()) {
|
|
|
|
demaSignal = types.DirectionUp
|
|
|
|
} else if closePrice < dd.fastDEMA.Last() && closePrice < dd.slowDEMA.Last() && !(openPrice < dd.fastDEMA.Last() && openPrice < dd.slowDEMA.Last()) {
|
|
|
|
demaSignal = types.DirectionDown
|
|
|
|
}
|
|
|
|
|
|
|
|
return demaSignal
|
|
|
|
}
|
|
|
|
|
|
|
|
// preloadDema preloads DEMA indicators
|
|
|
|
func (dd *DoubleDema) preloadDema(kLineStore *bbgo.MarketDataStore) {
|
|
|
|
if klines, ok := kLineStore.KLinesOfInterval(dd.fastDEMA.Interval); ok {
|
|
|
|
for i := 0; i < len(*klines); i++ {
|
|
|
|
dd.fastDEMA.Update((*klines)[i].GetClose().Float64())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if klines, ok := kLineStore.KLinesOfInterval(dd.slowDEMA.Interval); ok {
|
|
|
|
for i := 0; i < len(*klines); i++ {
|
|
|
|
dd.slowDEMA.Update((*klines)[i].GetClose().Float64())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setupDoubleDema initializes double DEMA indicators
|
2022-07-06 09:05:38 +00:00
|
|
|
func (dd *DoubleDema) setupDoubleDema(kLineStore *bbgo.MarketDataStore, interval types.Interval) {
|
2022-07-08 08:42:31 +00:00
|
|
|
dd.DemaInterval = interval
|
2022-07-06 09:05:38 +00:00
|
|
|
|
2022-07-06 08:45:19 +00:00
|
|
|
// DEMA
|
|
|
|
if dd.FastDEMAWindow == 0 {
|
|
|
|
dd.FastDEMAWindow = 144
|
|
|
|
}
|
2022-07-08 08:42:31 +00:00
|
|
|
dd.fastDEMA = &indicator.DEMA{IntervalWindow: types.IntervalWindow{Interval: dd.DemaInterval, Window: dd.FastDEMAWindow}}
|
2022-07-06 08:45:19 +00:00
|
|
|
dd.fastDEMA.Bind(kLineStore)
|
|
|
|
|
|
|
|
if dd.SlowDEMAWindow == 0 {
|
|
|
|
dd.SlowDEMAWindow = 169
|
|
|
|
}
|
2022-07-08 08:42:31 +00:00
|
|
|
dd.slowDEMA = &indicator.DEMA{IntervalWindow: types.IntervalWindow{Interval: dd.DemaInterval, Window: dd.SlowDEMAWindow}}
|
2022-07-06 08:45:19 +00:00
|
|
|
dd.slowDEMA.Bind(kLineStore)
|
2022-07-06 09:05:38 +00:00
|
|
|
|
2022-07-06 08:45:19 +00:00
|
|
|
dd.preloadDema(kLineStore)
|
|
|
|
}
|