reset activated flag when stop order is submitted

Signed-off-by: c9s <yoanlin93@gmail.com>
This commit is contained in:
c9s 2022-07-06 03:09:57 +08:00
parent 2bc12c0522
commit 03481000cc
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 8 additions and 2 deletions

View File

@ -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")

View File

@ -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)
} }
} }