Skip to content

Commit

Permalink
feat: fixbug for position update
Browse files Browse the repository at this point in the history
  • Loading branch information
yubing744 committed Apr 20, 2024
1 parent 7d59bbc commit 9a2e2b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
11 changes: 9 additions & 2 deletions pkg/exchange/okex/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -313,19 +313,26 @@ 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
}

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)
}
}
Expand Down
38 changes: 24 additions & 14 deletions pkg/types/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
)
}

Expand Down

0 comments on commit 9a2e2b9

Please sign in to comment.