@@ -68,16 +68,16 @@ type ResistanceShort struct {
68
68
resistancePrices []float64
69
69
nextResistancePrice fixedpoint.Value
70
70
71
- resistanceOrders []types. Order
71
+ activeOrders * bbgo. ActiveOrderBook
72
72
}
73
73
74
74
func (s * ResistanceShort ) Bind (session * bbgo.ExchangeSession , orderExecutor * bbgo.GeneralOrderExecutor ) {
75
75
s .session = session
76
76
s .orderExecutor = orderExecutor
77
+ s .activeOrders = bbgo .NewActiveOrderBook (s .Symbol )
78
+ s .activeOrders .BindStream (session .UserDataStream )
77
79
78
- position := orderExecutor .Position ()
79
- symbol := position .Symbol
80
- store , _ := session .MarketDataStore (symbol )
80
+ store , _ := session .MarketDataStore (s .Symbol )
81
81
82
82
s .resistancePivot = & indicator.Pivot {IntervalWindow : s .IntervalWindow }
83
83
s .resistancePivot .Bind (store )
@@ -87,22 +87,25 @@ func (s *ResistanceShort) Bind(session *bbgo.ExchangeSession, orderExecutor *bbg
87
87
lastKLine := preloadPivot (s .resistancePivot , store )
88
88
89
89
// use the last kline from the history before we get the next closed kline
90
- s .findNextResistancePriceAndPlaceOrders (lastKLine .Close )
90
+ if lastKLine != nil {
91
+ s .findNextResistancePriceAndPlaceOrders (lastKLine .Close )
92
+ }
91
93
92
94
session .MarketDataStream .OnKLineClosed (func (kline types.KLine ) {
93
95
if kline .Symbol != s .Symbol || kline .Interval != s .Interval {
94
96
return
95
97
}
96
98
99
+ position := s .orderExecutor .Position ()
100
+ if position .IsOpened (kline .Close ) {
101
+ return
102
+ }
103
+
97
104
s .findNextResistancePriceAndPlaceOrders (kline .Close )
98
105
})
99
106
}
100
107
101
108
func (s * ResistanceShort ) findNextResistancePriceAndPlaceOrders (closePrice fixedpoint.Value ) {
102
- position := s .orderExecutor .Position ()
103
- if position .IsOpened (closePrice ) {
104
- return
105
- }
106
109
107
110
minDistance := s .MinDistance .Float64 ()
108
111
lows := s .resistancePivot .Lows
@@ -114,6 +117,7 @@ func (s *ResistanceShort) findNextResistancePriceAndPlaceOrders(closePrice fixed
114
117
if len (resistancePrices ) > 0 {
115
118
nextResistancePrice := fixedpoint .NewFromFloat (resistancePrices [0 ])
116
119
if nextResistancePrice .Compare (s .nextResistancePrice ) != 0 {
120
+ bbgo .Notify ("Found next resistance price: %f" , nextResistancePrice .Float64 ())
117
121
s .nextResistancePrice = nextResistancePrice
118
122
s .placeResistanceOrders (ctx , nextResistancePrice )
119
123
}
@@ -134,10 +138,9 @@ func (s *ResistanceShort) placeResistanceOrders(ctx context.Context, resistanceP
134
138
layerSpread := s .LayerSpread
135
139
quantity := totalQuantity .Div (numLayersF )
136
140
137
- if err := s .orderExecutor .CancelOrders (ctx , s .resistanceOrders ... ); err != nil {
138
- log .WithError (err ).Errorf ("can not cancel resistance orders: %+v" , s .resistanceOrders )
141
+ if err := s .orderExecutor .CancelOrders (ctx , s .activeOrders . Orders () ... ); err != nil {
142
+ log .WithError (err ).Errorf ("can not cancel resistance orders: %+v" , s .activeOrders . Orders () )
139
143
}
140
- s .resistanceOrders = nil
141
144
142
145
log .Infof ("placing resistance orders: resistance price = %f, layer quantity = %f, num of layers = %d" , resistancePrice .Float64 (), quantity .Float64 (), numLayers )
143
146
@@ -179,7 +182,7 @@ func (s *ResistanceShort) placeResistanceOrders(ctx context.Context, resistanceP
179
182
if err != nil {
180
183
log .WithError (err ).Errorf ("can not place resistance order" )
181
184
}
182
- s .resistanceOrders = createdOrders
185
+ s .activeOrders . Add ( createdOrders ... )
183
186
}
184
187
185
188
type Strategy struct {
@@ -206,12 +209,11 @@ type Strategy struct {
206
209
session * bbgo.ExchangeSession
207
210
orderExecutor * bbgo.GeneralOrderExecutor
208
211
209
- lastLow fixedpoint.Value
210
- pivot * indicator.Pivot
211
- resistancePivot * indicator.Pivot
212
- stopEWMA * indicator.EWMA
213
- pivotLowPrices []fixedpoint.Value
214
- currentBounceShortPrice fixedpoint.Value
212
+ lastLow fixedpoint.Value
213
+ pivot * indicator.Pivot
214
+ resistancePivot * indicator.Pivot
215
+ stopEWMA * indicator.EWMA
216
+ pivotLowPrices []fixedpoint.Value
215
217
216
218
// StrategyController
217
219
bbgo.StrategyController
0 commit comments