mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
exits/protectivestoploss: fix shouldStop()
This commit is contained in:
parent
5455ae810b
commit
6c2fc3fee0
|
@ -79,12 +79,18 @@ func (s *ProtectiveStopLoss) placeStopOrder(ctx context.Context, position *types
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ProtectiveStopLoss) shouldStop(closePrice fixedpoint.Value) bool {
|
func (s *ProtectiveStopLoss) shouldStop(closePrice fixedpoint.Value, position *types.Position) bool {
|
||||||
if s.stopLossPrice.IsZero() {
|
if s.stopLossPrice.IsZero() {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return closePrice.Compare(s.stopLossPrice) >= 0
|
if position.IsShort() {
|
||||||
|
return closePrice.Compare(s.stopLossPrice) >= 0
|
||||||
|
} else if position.IsLong() {
|
||||||
|
return closePrice.Compare(s.stopLossPrice) <= 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ProtectiveStopLoss) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
|
func (s *ProtectiveStopLoss) Bind(session *ExchangeSession, orderExecutor *GeneralOrderExecutor) {
|
||||||
|
@ -187,7 +193,7 @@ func (s *ProtectiveStopLoss) checkStopPrice(closePrice fixedpoint.Value, positio
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if s.shouldStop(closePrice) {
|
if s.shouldStop(closePrice, position) {
|
||||||
log.Infof("[ProtectiveStopLoss] protection stop order is triggered at price %f, position = %+v", closePrice.Float64(), position)
|
log.Infof("[ProtectiveStopLoss] protection stop order is triggered at price %f, position = %+v", closePrice.Float64(), position)
|
||||||
if err := s.orderExecutor.ClosePosition(context.Background(), one, "protectiveStopLoss"); err != nil {
|
if err := s.orderExecutor.ClosePosition(context.Background(), one, "protectiveStopLoss"); err != nil {
|
||||||
log.WithError(err).Errorf("failed to close position")
|
log.WithError(err).Errorf("failed to close position")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user