stops.gno
3.46 Kb · 110 lines
1package lastcall
2
3// Stop-loss / take-profit for open perps. A stop is an instruction attached to a
4// position: close it at the current mark once the oracle price crosses the trigger.
5// Like limit orders, execution is permissionless (TriggerStop) — the price bot acts
6// as the keeper and anyone may trigger a position whose stop has been reached. The
7// close settles exactly like a normal close (same fees, same payout).
8
9import (
10 "time"
11
12 "gno.land/p/nt/ufmt/v0"
13)
14
15// validateStops rejects stop prices on the wrong side of the current mark, which would
16// trigger instantly, and keeps SL below TP for a long / above TP for a short. A 0 clears
17// that leg. Prices are micro-USD per share.
18func validateStops(p *perp, sl, tp int64) {
19 if sl < 0 || tp < 0 {
20 panic("desk: stop prices must be >= 0")
21 }
22 mark, _ := GetPrice(p.ticker)
23 if p.isLong {
24 if sl > 0 && sl >= mark {
25 panic("desk: long stop-loss must be below mark")
26 }
27 if tp > 0 && tp <= mark {
28 panic("desk: long take-profit must be above mark")
29 }
30 } else {
31 if sl > 0 && sl <= mark {
32 panic("desk: short stop-loss must be above mark")
33 }
34 if tp > 0 && tp >= mark {
35 panic("desk: short take-profit must be below mark")
36 }
37 }
38}
39
40// SetStops sets, updates or clears the stop-loss and take-profit of the caller's own
41// position. Each price is micro-USD per share; pass 0 to clear that leg.
42func SetStops(cur realm, id uint64, stopLoss, takeProfit int64) {
43 p := mustPerp(id)
44 if p.owner != cur.Previous().Address() {
45 panic("desk: not your position")
46 }
47 validateStops(p, stopLoss, takeProfit)
48 p.stopLoss = stopLoss
49 p.takeProfit = takeProfit
50}
51
52// stopReady reports whether a position's stop-loss or take-profit has been reached at
53// `mark`. Long: SL when mark <= SL, TP when mark >= TP. Short: mirrored.
54func stopReady(p *perp, mark int64) bool {
55 if p.isLong {
56 if p.stopLoss > 0 && mark <= p.stopLoss {
57 return true
58 }
59 if p.takeProfit > 0 && mark >= p.takeProfit {
60 return true
61 }
62 } else {
63 if p.stopLoss > 0 && mark >= p.stopLoss {
64 return true
65 }
66 if p.takeProfit > 0 && mark <= p.takeProfit {
67 return true
68 }
69 }
70 return false
71}
72
73// TriggerStop closes a position whose stop-loss or take-profit has been reached, at the
74// current mark. Permissionless (keeper / UI); panics if no stop is set or not reached,
75// so the keeper simply retries later.
76func TriggerStop(cur realm, id uint64) {
77 p := mustPerp(id)
78 if p.stopLoss == 0 && p.takeProfit == 0 {
79 panic("desk: no stop set")
80 }
81 mark := freshPrice(p.ticker)
82 if !stopReady(p, mark) {
83 panic("desk: stop not reached")
84 }
85 // Anyone can call this, so it must not close a winner for less than it is
86 // owed: the position waits until the reserve can pay it in full.
87 if payout, _, _, _ := sliceSettle(p, p.size, mark, time.Now().Unix()); payout > freeReserve()+p.margin {
88 panic("desk: the reserve cannot pay this stop in full right now")
89 }
90 settlePerp(p, mark, "stop")
91}
92
93// --- reads ---
94
95// AllStops lists, within one page of open positions, those with a stop set, for
96// the keeper to scan, ';'-joined, fields comma-separated:
97// id,ticker,L|S,stopLoss,takeProfit,mark. The keeper triggers the ones whose
98// condition holds.
99func AllStops(offset, count int) string {
100 out := ""
101 eachPage(perps, offset, count, func(item any) {
102 p := item.(*perp)
103 if p.stopLoss == 0 && p.takeProfit == 0 {
104 return
105 }
106 mark, _ := GetPrice(p.ticker)
107 out = joinRow(out, ufmt.Sprintf("%d,%s,%s,%d,%d,%d", p.id, p.ticker, sideOf(p.isLong), p.stopLoss, p.takeProfit, mark))
108 })
109 return out
110}