mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
reset activated flag when stop order is submitted
Signed-off-by: c9s <yoanlin93@gmail.com>
This commit is contained in:
parent
2bc12c0522
commit
03481000cc
|
@ -47,6 +47,7 @@ func (s *TrailingStop2) Subscribe(session *ExchangeSession) {
|
||||||
func (s *TrailingStop2) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
|
func (s *TrailingStop2) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
|
||||||
s.session = session
|
s.session = session
|
||||||
s.orderExecutor = orderExecutor
|
s.orderExecutor = orderExecutor
|
||||||
|
s.latestHigh = fixedpoint.Zero
|
||||||
|
|
||||||
position := orderExecutor.Position()
|
position := orderExecutor.Position()
|
||||||
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
|
session.MarketDataStream.OnKLineClosed(types.KLineWith(s.Symbol, s.Interval, func(kline types.KLine) {
|
||||||
|
@ -139,6 +140,11 @@ func (s *TrailingStop2) checkStopPrice(price fixedpoint.Value, position *types.P
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TrailingStop2) triggerStop(price fixedpoint.Value) error {
|
func (s *TrailingStop2) triggerStop(price fixedpoint.Value) error {
|
||||||
|
// reset activated flag
|
||||||
|
defer func() {
|
||||||
|
s.activated = false
|
||||||
|
s.latestHigh = fixedpoint.Zero
|
||||||
|
}()
|
||||||
Notify("[TrailingStop] %s stop loss triggered. price: %f callback rate: %f", s.Symbol, price.Float64(), s.CallbackRate.Float64())
|
Notify("[TrailingStop] %s stop loss triggered. price: %f callback rate: %f", s.Symbol, price.Float64(), s.CallbackRate.Float64())
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
return s.orderExecutor.ClosePosition(ctx, fixedpoint.One, "trailingStop")
|
return s.orderExecutor.ClosePosition(ctx, fixedpoint.One, "trailingStop")
|
||||||
|
|
|
@ -96,7 +96,7 @@ func TestTrailingStop(t *testing.T) {
|
||||||
err = stop.checkStopPrice(currentPrice, position)
|
err = stop.checkStopPrice(currentPrice, position)
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, fixedpoint.NewFromFloat(19798.02), currentPrice)
|
assert.Equal(t, fixedpoint.NewFromFloat(19798.02), currentPrice)
|
||||||
assert.Equal(t, fixedpoint.NewFromFloat(19602.0), stop.latestHigh)
|
assert.Equal(t, fixedpoint.Zero, stop.latestHigh)
|
||||||
assert.True(t, stop.activated)
|
assert.False(t, stop.activated)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user