package lastcall // Stop-loss / take-profit for open perps. A stop is an instruction attached to a // position: close it at the current mark once the oracle price crosses the trigger. // Like limit orders, execution is permissionless (TriggerStop) — the price bot acts // as the keeper and anyone may trigger a position whose stop has been reached. The // close settles exactly like a normal close (same fees, same payout). 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") } validateStops(p, stopLoss, takeProfit) p.stopLoss = stopLoss p.takeProfit = takeProfit } // 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") } 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 --- // AllStops lists, within one page of open positions, those with a stop set, for // the keeper to scan, ';'-joined, fields comma-separated: // id,ticker,L|S,stopLoss,takeProfit,mark. The keeper triggers the ones whose // condition holds. func AllStops(offset, count int) string { out := "" eachPage(perps, offset, count, func(item any) { p := item.(*perp) if p.stopLoss == 0 && p.takeProfit == 0 { return } mark, _ := GetPrice(p.ticker) out = joinRow(out, ufmt.Sprintf("%d,%s,%s,%d,%d,%d", p.id, p.ticker, sideOf(p.isLong), p.stopLoss, p.takeProfit, mark)) }) return out }