forked from c9s/bbgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stop_ema.go
42 lines (34 loc) · 1.04 KB
/
stop_ema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package bbgo
import (
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/types"
)
type StopEMA struct {
types.IntervalWindow
Range fixedpoint.Value `json:"range"`
stopEWMA *indicator.EWMA
}
func (s *StopEMA) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
symbol := orderExecutor.Position().Symbol
s.stopEWMA = session.StandardIndicatorSet(symbol).EWMA(s.IntervalWindow)
}
func (s *StopEMA) Allowed(closePrice fixedpoint.Value) bool {
ema := fixedpoint.NewFromFloat(s.stopEWMA.Last(0))
if ema.IsZero() {
logrus.Infof("stopEMA protection: value is zero, skip")
return false
}
emaStopShortPrice := ema.Mul(fixedpoint.One.Sub(s.Range))
if closePrice.Compare(emaStopShortPrice) < 0 {
Notify("stopEMA %s protection: close price %f less than stopEMA %f = EMA(%f) * (1 - RANGE %f)",
s.IntervalWindow.String(),
closePrice.Float64(),
emaStopShortPrice.Float64(),
ema.Float64(),
s.Range.Float64())
return false
}
return true
}