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