diff --git a/pkg/exchange/okex/stream.go b/pkg/exchange/okex/stream.go index 726780f42e..0302bd9767 100644 --- a/pkg/exchange/okex/stream.go +++ b/pkg/exchange/okex/stream.go @@ -271,7 +271,7 @@ func (e *Stream) QueryAlgoOpenOrders(ctx context.Context, symbol string) (orders params, _ := req.GetQueryParameters() log.WithField("symbol", symbol). WithField("params", params). - Info("QueryAlgoOpenOrders_start") + Info("Stream#QueryAlgoOpenOrders_start") orders, err = req.Do(ctx) if err != nil { @@ -280,7 +280,7 @@ func (e *Stream) QueryAlgoOpenOrders(ctx context.Context, symbol string) (orders log.WithField("symbol", symbol). WithField("openOrders", orders). - Info("QueryAlgoOpenOrders_result") + Info("Stream#QueryAlgoOpenOrders_result") orderLen := len(orders) // a defensive programming to ensure the length of order response is expected. @@ -313,6 +313,8 @@ func (s *Stream) handlePositionDetailsEvent(positionDetails []PositionUpdateEven slTriggerPx, err := fixedpoint.NewFromString(algoOrder.SlTriggerPx) if err != nil { + log.WithError(err).Error("handlePositionDetailsEvent_parse_SlTriggerPx_error") + } else { position.SlTriggerPxType = algoOrder.SlTriggerPxType position.SlOrdPx = algoOrder.SlOrdPx position.SlTriggerPx = &slTriggerPx @@ -320,12 +322,17 @@ func (s *Stream) handlePositionDetailsEvent(positionDetails []PositionUpdateEven tpTriggerPx, err := fixedpoint.NewFromString(algoOrder.TpTriggerPx) if err != nil { + log.WithError(err).Error("handlePositionDetailsEvent_parse_TpTriggerPx_error") + } else { position.TpTriggerPxType = algoOrder.TpTriggerPxType position.TpOrdPx = algoOrder.TpOrdPx position.TpTriggerPx = &tpTriggerPx } } + log.WithField("position", position). + Info("handlePositionDetailsEvent") + s.EmitPositionUpdate(position) } } diff --git a/pkg/types/position.go b/pkg/types/position.go index 6a0e93a0fe..ab2a91f950 100644 --- a/pkg/types/position.go +++ b/pkg/types/position.go @@ -272,28 +272,36 @@ func (p *Position) EmitModify(baseQty fixedpoint.Value, quoteQty fixedpoint.Valu } func (p *Position) Update(pos PositionInfo) bool { + triggerPxUpdate := false + + if pos.SlTriggerPx != nil && + (p.SlTriggerPx == nil || !p.SlTriggerPx.Eq(*pos.SlTriggerPx)) { + p.SlTriggerPx = pos.SlTriggerPx + p.SlTriggerPxType = pos.SlTriggerPxType + p.SlOrdPx = pos.SlOrdPx + + triggerPxUpdate = true + } + + if pos.TpTriggerPx != nil && + (p.TpTriggerPx == nil || !p.TpTriggerPx.Eq(*pos.TpTriggerPx)) { + p.TpTriggerPx = pos.TpTriggerPx + p.TpTriggerPxType = pos.TpTriggerPxType + p.TpOrdPx = pos.TpOrdPx + + triggerPxUpdate = true + } + if p.Base.Compare(pos.Base) != 0 || p.Quote.Compare(pos.Quote) != 0 || p.AverageCost.Compare(pos.AverageCost) != 0 || - p.TradeID != pos.TradeID { + p.TradeID != pos.TradeID || triggerPxUpdate { p.Base = pos.Base p.Quote = pos.Quote p.AverageCost = pos.AverageCost p.TradeID = pos.TradeID p.ChangedAt = pos.ChangedAt - if pos.SlTriggerPx != nil { - p.SlTriggerPx = pos.SlTriggerPx - p.SlTriggerPxType = pos.SlTriggerPxType - p.SlOrdPx = pos.SlOrdPx - } - - if pos.TpTriggerPx != nil { - p.TpTriggerPx = pos.TpTriggerPx - p.TpTriggerPxType = pos.TpTriggerPxType - p.TpOrdPx = pos.TpOrdPx - } - p.EmitModify(p.Base, p.Quote, p.AverageCost) return true } @@ -500,11 +508,13 @@ func (p *Position) PlainText() (msg string) { } func (p *Position) String() string { - return fmt.Sprintf("POSITION %s: average cost = %v, base = %v, quote = %v", + return fmt.Sprintf("POSITION %s: average cost = %v, base = %v, quote = %v, tp = %v, sl = %v", p.Symbol, p.AverageCost, p.Base, p.Quote, + p.TpTriggerPx, + p.SlTriggerPx, ) }