-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
exit_lower_shadow_take_profit.go
66 lines (51 loc) · 1.57 KB
/
exit_lower_shadow_take_profit.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package bbgo
import (
"context"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
type LowerShadowTakeProfit struct {
// inherit from the strategy
types.IntervalWindow
// inherit from the strategy
Symbol string `json:"symbol"`
Ratio fixedpoint.Value `json:"ratio"`
session *ExchangeSession
orderExecutor *GeneralOrderExecutor
}
func (s *LowerShadowTakeProfit) Subscribe(session *ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: s.Interval})
}
func (s *LowerShadowTakeProfit) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
s.session = session
s.orderExecutor = orderExecutor
stdIndicatorSet := session.StandardIndicatorSet(s.Symbol)
ewma := stdIndicatorSet.EWMA(s.IntervalWindow)
position := orderExecutor.Position()
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
closePrice := kline.Close
if position.IsClosed() || position.IsDust(closePrice) {
return
}
roi := position.ROI(closePrice)
if roi.Sign() < 0 {
return
}
if s.Ratio.IsZero() {
return
}
// skip close price higher than the ewma
if closePrice.Float64() > ewma.Last() {
return
}
if kline.GetLowerShadowHeight().Div(kline.Close).Compare(s.Ratio) > 0 {
Notify("%s TakeProfit triggered by shadow ratio %f, price = %f",
position.Symbol,
kline.GetLowerShadowRatio().Float64(),
kline.Close.Float64(),
kline)
_ = orderExecutor.ClosePosition(context.Background(), fixedpoint.One)
return
}
}))
}