nx_fin_liquidity.nx source
↩ module page · 30 lines · 1797 B
1// nx_fin_liquidity.nx -- the market-IMPACT model: finer than R1's flat ADV cap. Uses the square-root impact law
2// (Almgren): impact grows with sqrt(order/ADV), so doubling size ~1.4x's the cost, not 2x. impact_bps =
3// k_bps * sqrt(participation), participation = order/ADV. Composes me_isqrt (nx_fin_metrics). Gives the $ cost of
4// trading AND the inverse -- the largest order that keeps impact within a bound = the honest, per-name answer to
5// the SCALING WALL ("how much can a growing stake actually deploy here"). k_bps is DATA (per-name/regime). Pure,
6// i64. license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_fin_metrics.nx"
9const K_MAGIC_1000000: i64 = 1000000
10const K_MAGIC_10000: i64 = 10000
11
12// square-root impact law. participation_bps = order*10000/adv ; sqrt(order/adv) = isqrt(part_bps)/100 ;
13// impact_bps = k_bps * isqrt(part_bps) / 100. No ADV -> effectively untradeable (huge impact).
14func lq_impact_bps(order_cents: i64, adv_cents: i64, k_bps: i64) -> i64 {
15 if adv_cents <= 0 { return K_MAGIC_1000000 }
16 let part_bps: i64 = order_cents*K_MAGIC_10000/adv_cents
17 return k_bps * me_isqrt(part_bps) / 100
18}
19
20// $ (cents) cost of that impact on the order.
21func lq_slippage_cost_cents(order_cents: i64, impact_bps: i64) -> i64 { return order_cents * impact_bps / K_MAGIC_10000 }
22
23// largest order (cents) whose modelled impact stays within max_impact_bps. Inverse of lq_impact_bps:
24// part_bps <= (max*100/k)^2 ; order <= adv*part_bps/10000. THE scaling-wall ceiling per name.
25func lq_max_order_for_impact(adv_cents: i64, max_impact_bps: i64, k_bps: i64) -> i64 {
26 if k_bps <= 0 { return 0 }
27 let allowed_sqrt: i64 = max_impact_bps * 100 / k_bps
28 let part_bps_max: i64 = allowed_sqrt * allowed_sqrt
29 return adv_cents * part_bps_max / K_MAGIC_10000
30}