code wiki / (root) / nx_fin_marketdata_gate.nx

nx_fin_marketdata_gate.nx source

↩ module page · 48 lines · 2817 B

1// nx_fin_marketdata_gate.nx -- R1 GATE: proves the OHLCV parser + ADV/capacity math on KNOWN bytes (no network, 2// no false-green). Exits 0 iff all pass. The CSV is a SYNTHETIC gate fixture, chosen so ADV/capacity are hand- 3// checkable (like nx_fin_mgmt_grade_gate feeds mg_score known inputs). license_tier: ORIGINAL 4import "nx_gate.nx" 5import "nx_fin_marketdata.nx" 6 7func chk(name: *u8, got: i64, want: i64, st: *i64) -> i64 { 8 if got == want { st[0] = st[0] + 1; gw(" PASS " as *u8); gw(name); gw("\n" as *u8) } 9 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) } 10 return 0 11} 12 13func main() -> i64 { 14 let st: *i64 = sys_mmap(16) as *i64 15 st[0] = 0; st[1] = 0 16 17 // -- field parsers -- 18 chk("price 10.20 -> 1020 cents" as *u8, md_price_cents("10.20" as *u8, 0, 5), 1020, st) 19 chk("price 9.80 -> 980 cents" as *u8, md_price_cents("9.80" as *u8, 0, 4), 980, st) 20 chk("price 12 -> 1200 cents (no dot)" as *u8, md_price_cents("12" as *u8, 0, 2), 1200, st) 21 chk("date 2024-01-02 -> 20240102" as *u8, md_date_key("2024-01-02" as *u8, 0, 10), 20240102, st) 22 chk("atoi 1500000" as *u8, md_atoi("1500000" as *u8, 0, 7), 1500000, st) 23 24 // -- CSV parse (SYNTHETIC fixture: header + 3 bars) -- 25 let csv: *u8 = "Date,Open,High,Low,Close,Volume\n2024-01-02,10.00,10.50,9.80,10.20,1000000\n2024-01-03,10.20,10.40,10.00,10.10,2000000\n2024-01-04,10.10,10.30,9.90,10.25,1500000\n" as *u8 26 var n: i64 = 0; while csv[n] != (0 as u8) { n = n + 1 } 27 let bars: *i64 = sys_mmap(8 * MD_FIELDS * 16) as *i64 28 let cnt: i64 = md_parse_csv(csv, n, bars, 16) 29 chk("parsed 3 bars (header skipped)" as *u8, cnt, 3, st) 30 chk("bar0 date" as *u8, bars[0*MD_FIELDS+0], 20240102, st) 31 chk("bar0 close cents" as *u8, bars[0*MD_FIELDS+4], 1020, st) 32 chk("bar0 volume" as *u8, bars[0*MD_FIELDS+5], 1000000, st) 33 chk("bar1 low cents" as *u8, bars[1*MD_FIELDS+3], 1000, st) 34 chk("bar2 close cents" as *u8, bars[2*MD_FIELDS+4], 1025, st) 35 chk("bar2 high cents" as *u8, bars[2*MD_FIELDS+2], 1030, st) 36 37 // -- liquidity / scaling-wall math -- 38 chk("bar0 dollar-vol cents (1020*1e6)" as *u8, md_dollar_vol(bars, 0), 1020000000, st) 39 let adv: i64 = md_adv(bars, cnt, 3) // (1020*1e6 + 1010*2e6 + 1025*1.5e6)/3 = 1,525,833,333 cents 40 chk("ADV(3d) cents" as *u8, adv, 1525833333, st) 41 chk("capacity @1% ADV cents (~$152,583/day)" as *u8, md_capacity(adv, 100), 15258333, st) 42 chk("capacity @100% == ADV" as *u8, md_capacity(adv, 10000), adv, st) 43 44 gw("nx_fin_marketdata_gate: PASS=" as *u8); gn(st[0]); gw(" FAIL=" as *u8); gn(st[1]); gw("\n" as *u8) 45 if st[1] == 0 { gw("R1 nx_fin_marketdata: GREEN\n" as *u8); return 0 } 46 gw("R1 nx_fin_marketdata: RED\n" as *u8) 47 return 1 48}