stops.gno
3.16 Kb · 107 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 "gno.land/p/nt/ufmt/v0"
10
11// validateStops rejects stop prices on the wrong side of the current mark, which would
12// trigger instantly, and keeps SL below TP for a long / above TP for a short. A 0 clears
13// that leg. Prices are micro-USD per share.
14func validateStops(p *perp, sl, tp int64) {
15 if sl < 0 || tp < 0 {
16 panic("desk: stop prices must be >= 0")
17 }
18 mark, _ := GetPrice(p.ticker)
19 if p.isLong {
20 if sl > 0 && sl >= mark {
21 panic("desk: long stop-loss must be below mark")
22 }
23 if tp > 0 && tp <= mark {
24 panic("desk: long take-profit must be above mark")
25 }
26 } else {
27 if sl > 0 && sl <= mark {
28 panic("desk: short stop-loss must be above mark")
29 }
30 if tp > 0 && tp >= mark {
31 panic("desk: short take-profit must be below mark")
32 }
33 }
34}
35
36// SetStops sets, updates or clears the stop-loss and take-profit of the caller's own
37// position. Each price is micro-USD per share; pass 0 to clear that leg.
38func SetStops(cur realm, id uint64, stopLoss, takeProfit int64) {
39 p := mustPerp(id)
40 if p.owner != cur.Previous().Address() {
41 panic("desk: not your position")
42 }
43 validateStops(p, stopLoss, takeProfit)
44 p.stopLoss = stopLoss
45 p.takeProfit = takeProfit
46}
47
48// stopReady reports whether a position's stop-loss or take-profit has been reached at
49// `mark`. Long: SL when mark <= SL, TP when mark >= TP. Short: mirrored.
50func stopReady(p *perp, mark int64) bool {
51 if p.isLong {
52 if p.stopLoss > 0 && mark <= p.stopLoss {
53 return true
54 }
55 if p.takeProfit > 0 && mark >= p.takeProfit {
56 return true
57 }
58 } else {
59 if p.stopLoss > 0 && mark >= p.stopLoss {
60 return true
61 }
62 if p.takeProfit > 0 && mark <= p.takeProfit {
63 return true
64 }
65 }
66 return false
67}
68
69// TriggerStop closes a position whose stop-loss or take-profit has been reached, at the
70// current mark. Permissionless (keeper / UI); panics if no stop is set or not reached,
71// so the keeper simply retries later.
72func TriggerStop(cur realm, id uint64) {
73 p := mustPerp(id)
74 if p.stopLoss == 0 && p.takeProfit == 0 {
75 panic("desk: no stop set")
76 }
77 mark := freshPrice(p.ticker)
78 if !stopReady(p, mark) {
79 panic("desk: stop not reached")
80 }
81 settlePerp(p, mark)
82}
83
84// --- reads ---
85
86// AllStops lists every open position that has a stop set, for the keeper to scan,
87// ';'-joined, fields comma-separated: id,ticker,L|S,stopLoss,takeProfit,mark. The
88// keeper triggers the ones whose condition holds.
89func AllStops() string {
90 out := ""
91 for id := uint64(1); id <= nextPerpID; id++ {
92 p, ok := perps[id]
93 if !ok || (p.stopLoss == 0 && p.takeProfit == 0) {
94 continue
95 }
96 mark, _ := GetPrice(p.ticker)
97 side := "S"
98 if p.isLong {
99 side = "L"
100 }
101 if out != "" {
102 out += ";"
103 }
104 out += ufmt.Sprintf("%d,%s,%s,%d,%d,%d", p.id, p.ticker, side, p.stopLoss, p.takeProfit, mark)
105 }
106 return out
107}