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 "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") } settlePerp(p, mark) } // --- reads --- // AllStops lists every open position that has 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() string { out := "" for id := uint64(1); id <= nextPerpID; id++ { p, ok := perps[id] if !ok || (p.stopLoss == 0 && p.takeProfit == 0) { continue } mark, _ := GetPrice(p.ticker) side := "S" if p.isLong { side = "L" } if out != "" { out += ";" } out += ufmt.Sprintf("%d,%s,%s,%d,%d,%d", p.id, p.ticker, side, p.stopLoss, p.takeProfit, mark) } return out }