package lastcall // Stop-loss / take-profit for open perps. A stop is an instruction attached to a // position: close it at the desk price once that price crosses the trigger. Like // orders, a stop acts only on a price observed after it was set, and execution is // permissionless (TriggerStop). The close settles like a normal close. import ( "time" "gno.land/p/nt/ufmt/v0" ) // validateStops rejects stop prices on the wrong side of the current mark, which would // trigger instantly, and keeps SL below TP for a long / above TP for a short. A 0 clears // that leg. Prices are micro-USD per share. func validateStops(p *perp, sl, tp int64) { if sl < 0 || tp < 0 { panic("desk: stop prices must be >= 0") } mark, _ := GetPrice(p.ticker) if p.isLong { if sl > 0 && sl >= mark { panic("desk: long stop-loss must be below mark") } if tp > 0 && tp <= mark { panic("desk: long take-profit must be above mark") } } else { if sl > 0 && sl <= mark { panic("desk: short stop-loss must be above mark") } if tp > 0 && tp >= mark { panic("desk: short take-profit must be below mark") } } } // SetStops sets, updates or clears the stop-loss and take-profit of the caller's own // position. Each price is micro-USD per share; pass 0 to clear that leg. func SetStops(cur realm, id uint64, stopLoss, takeProfit int64) { p := mustPerp(id) if p.owner != cur.Previous().Address() { panic("desk: not your position") } if stopFillable(p) { panic("desk: your stop has been reached, it closes at this price") } validateStops(p, stopLoss, takeProfit) p.stopLoss = stopLoss p.takeProfit = takeProfit p.stopsAt = time.Now().Unix() } func stopFillable(p *perp) bool { if (p.stopLoss == 0 && p.takeProfit == 0) || !pricedAfter(p.ticker, p.stopsAt) || !IsFresh(p.ticker) { return false } mark, _ := GetPrice(p.ticker) return stopReady(p, mark) } // stopReady reports whether a position's stop-loss or take-profit has been reached at // `mark`. Long: SL when mark <= SL, TP when mark >= TP. Short: mirrored. func stopReady(p *perp, mark int64) bool { if p.isLong { if p.stopLoss > 0 && mark <= p.stopLoss { return true } if p.takeProfit > 0 && mark >= p.takeProfit { return true } } else { if p.stopLoss > 0 && mark >= p.stopLoss { return true } if p.takeProfit > 0 && mark <= p.takeProfit { return true } } return false } // TriggerStop closes a position whose stop-loss or take-profit has been reached, at the // current mark. Permissionless (keeper / UI); panics if no stop is set or not reached, // so the keeper simply retries later. func TriggerStop(cur realm, id uint64) { p := mustPerp(id) if p.stopLoss == 0 && p.takeProfit == 0 { panic("desk: no stop set") } if !pricedAfter(p.ticker, p.stopsAt) { panic("desk: the stop waits for a price after it was set") } mark := freshPrice(p.ticker) if !stopReady(p, mark) { panic("desk: stop not reached") } // Anyone can call this, so it must not close a winner for less than it is // owed: the position waits until the reserve can pay it in full. if payout, _, _, _ := sliceSettle(p, p.size, mark, time.Now().Unix()); payout > freeReserve()+p.margin { panic("desk: the reserve cannot pay this stop in full right now") } settlePerp(p, mark, "stop") } // --- reads --- // ReadyStops lists, within one page of open positions, the ids whose stop // TriggerStop would act on now, ';'-joined, for the keeper. func ReadyStops(offset, count int) string { out := "" eachPage(perps, offset, count, func(item any) { if p := item.(*perp); stopFillable(p) { out = joinRow(out, ufmt.Sprintf("%d", p.id)) } }) return out }