-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwizard_reserve_relief.py
More file actions
46 lines (37 loc) · 1.66 KB
/
wizard_reserve_relief.py
File metadata and controls
46 lines (37 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python3
"""
The Grid Wizard Reserve Relief v2 – Prune Excess Offers to Free Reserves
"""
from decimal import Decimal
from typing import List, Dict, Callable
D = Decimal
from wizard_rlusd_grid_v2 import offer_side_vs_xrp, price_vs_xrp, cancel_offers_by_seq
def prune_reserve(client, wallet, issuer: str, mid: D, existing: List[dict],
side_caps: Dict[str, int], grace_bps: D,
max_per_cycle: int, strategy: str, tag: str,
log: Callable[[str], None]) -> int:
to_prune = []
buys = [of for of in existing if offer_side_vs_xrp(of) == "buy"]
sells = [of for of in existing if offer_side_vs_xrp(of) == "sell"]
if len(buys) > side_caps.get("buy", 0):
for of in buys:
price = price_vs_xrp(of)
if price is None: continue
bps_diff = (mid - price) / mid * D(10000)
if bps_diff > grace_bps:
to_prune.append((of["seq"], bps_diff, "buy"))
if len(sells) > side_caps.get("sell", 0):
for of in sells:
price = price_vs_xrp(of)
if price is None: continue
bps_diff = (price - mid) / mid * D(10000)
if bps_diff > grace_bps:
to_prune.append((of["seq"], bps_diff, "sell"))
if not to_prune:
return 0
# Sort by strategy: farthest (highest BPS) or oldest (lowest seq)
to_prune.sort(key=lambda x: x[1] if strategy == "farthest" else -x[0])
to_prune = to_prune[:max_per_cycle]
seqs = [seq for seq, _, _ in to_prune]
cancel_offers_by_seq(client, wallet, seqs, tag, log)
return len(seqs)