nx_fin_liquidity_gate.nx source
↩ module page · 35 lines · 1834 B
1// nx_fin_liquidity_gate.nx -- GATE for the square-root market-impact model: impact scales with sqrt(size),
2// slippage cost, and the inverse max-order-for-impact ceiling -- hand-computed. Exits 0 iff all pass.
3// license_tier: ORIGINAL
4import "nx_gate.nx"
5import "nx_fin_metrics.nx"
6import "nx_fin_liquidity.nx"
7
8func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 {
9 if got == want { st[0] = st[0] + 1; gw(" PASS " as *u8); gw(name); gw("\n" as *u8) }
10 else { st[1] = st[1] + 1; gw(" FAIL " as *u8); gw(name); gw(" got=" as *u8); gn(got); gw(" want=" as *u8); gn(want); gw("\n" as *u8) }
11 return 0
12}
13
14func main() -> i64 {
15 let st: *i64 = sys_mmap(16) as *i64
16 st[0] = 0; st[1] = 0
17
18 // $10k order into $1M ADV (1% participation), k=500 -> impact = 500 * sqrt(100bps=10) / 100 = 50 bps
19 chk("impact 1% ADV = 50 bps" as *u8, lq_impact_bps(1000000, 100000000, 500), 50, st)
20 // 4x the order (4% participation) -> impact = 500 * sqrt(400=20) / 100 = 100 bps (2x, sqrt law)
21 chk("impact 4% ADV = 100 bps (sqrt law, 4x size=2x cost)" as *u8, lq_impact_bps(4000000, 100000000, 500), 100, st)
22 // no ADV -> untradeable
23 chk("no ADV -> huge impact" as *u8, lq_impact_bps(1000000, 0, 500), 1000000, st)
24
25 // slippage cost of a $10k order at 50 bps = $50 (5000c)
26 chk("slippage cost @50bps = 5000c" as *u8, lq_slippage_cost_cents(1000000, 50), 5000, st)
27
28 // inverse: max order into $1M ADV keeping impact <= 100 bps, k=500 -> $40k (4,000,000c)
29 chk("max order for <=100bps impact = $40k" as *u8, lq_max_order_for_impact(100000000, 100, 500), 4000000, st)
30
31 gw("nx_fin_liquidity_gate: PASS=" as *u8); gn(st[0]); gw(" FAIL=" as *u8); gn(st[1]); gw("\n" as *u8)
32 if st[1] == 0 { gw("nx_fin_liquidity: GREEN\n" as *u8); return 0 }
33 gw("nx_fin_liquidity: RED\n" as *u8)
34 return 1
35}