pivotshort: add stopEMA

This commit is contained in:
c9s 2022-06-09 18:16:32 +08:00
parent aa2ba265f1
commit a8134561f5
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 26 additions and 3 deletions

View File

@ -18,7 +18,10 @@ exchangeStrategies:
breakLow:
ratio: 0.1%
quantity: 10.0
# stopLossPercentage: 1%
stopEMARange: 5%
stopEMA:
interval: 1h
window: 99
bounceShort:
quantity: 10.0

View File

@ -26,8 +26,10 @@ type IntervalWindowSetting struct {
// BreakLow -- when price breaks the previous pivot low, we set a trade entry
type BreakLow struct {
Ratio fixedpoint.Value `json:"ratio"`
Quantity fixedpoint.Value `json:"quantity"`
Ratio fixedpoint.Value `json:"ratio"`
Quantity fixedpoint.Value `json:"quantity"`
StopEMARange fixedpoint.Value `json:"stopEMARange"`
StopEMA *types.IntervalWindow `json:"stopEMA"`
}
type Entry struct {
@ -76,6 +78,7 @@ type Strategy struct {
lastLow fixedpoint.Value
pivot *indicator.Pivot
ewma *indicator.EWMA
pivotLowPrices []fixedpoint.Value
// StrategyController
@ -210,6 +213,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.pivot = &indicator.Pivot{IntervalWindow: iw}
s.pivot.Bind(store)
standardIndicator, _ := session.StandardIndicatorSet(s.Symbol)
if s.BreakLow.StopEMA != nil {
s.ewma = standardIndicator.EWMA(*s.BreakLow.StopEMA)
}
s.lastLow = fixedpoint.Zero
session.UserDataStream.OnStart(func() {
@ -281,6 +289,18 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.pivotLowPrices = s.pivotLowPrices[len(s.pivotLowPrices)-10:]
}
if s.ewma != nil && !s.BreakLow.StopEMARange.IsZero() {
ema := fixedpoint.NewFromFloat(s.ewma.Last())
if ema.IsZero() {
return
}
emaStopShortPrice := ema.Mul(fixedpoint.One.Sub(s.BreakLow.StopEMARange))
if kline.Close.Compare(emaStopShortPrice) < 0 {
return
}
}
ratio := fixedpoint.One.Sub(s.BreakLow.Ratio)
breakPrice := previousLow.Mul(ratio)
if kline.Close.Compare(breakPrice) > 0 {