nx_edgar_balance.nx source
↩ module page · 96 lines · 4626 B
1// nx_edgar_balance.nx -- check OUR arithmetic against an AUTHORITATIVE SEC filing.
2//
3// The finance|statement-accuracy axis wants computed figures vs an authoritative filing. The
4// accounting identity ASSETS = LIABILITIES + STOCKHOLDERS EQUITY is exactly that test: take the
5// three facts the company itself filed, add two of them OURSELVES, and see whether we reproduce the
6// third as filed.
7//
8// WHY THIS EXISTS RATHER THAN A PYTHON SCRIPT: I first ran this comparison with python doing the
9// JSON parse and the addition. It matched 6/6 -- and proved nothing about US. The oracle may be
10// third-party; the CAPABILITY must not be. Parsing and arithmetic are the capability here, so they
11// belong in our code, using nx_edgar_lib's own fact extractor.
12// ★★★★★IF A THIRD-PARTY TOOL DOES THE COMPUTING, THE MEASUREMENT IS ABOUT THAT TOOL.
13//
14// Input files are fetched by our sovereign TLS client (nx_https_get_cli2 --body). SEC is the oracle.
15// license_tier: ORIGINAL No hw writes (Rule 26).
16import "nx_edgar_lib.nx"
17
18const EB_CAP: i64 = 4194304
19
20func eb_p(s: *u8) -> i64 {
21 var n: i64 = 0
22 while s[n] != (0 as u8) { n = n + 1 }
23 sys_write(1, s, n)
24 return 0
25}
26
27func eb_num(v: i64) -> i64 {
28 let t: *u8 = sys_mmap(32)
29 var m: i64 = v
30 var neg: i64 = 0
31 if m < 0 { neg = 1; m = 0 - m }
32 var k: i64 = 0
33 if m == 0 { t[0] = 48 as u8; k = 1 }
34 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 }
35 let o: *u8 = sys_mmap(40)
36 var p: i64 = 0
37 if neg == 1 { o[p] = 45 as u8; p = p + 1 }
38 var i: i64 = 0
39 while i < k { o[p] = t[k - 1 - i]; p = p + 1; i = i + 1 }
40 sys_write(1, o, p)
41 return 0
42}
43
44func main(argc: i64, argv: *i64) -> i64 {
45 var dir: *u8 = "knowledge/status/edgar/" as *u8
46 if argc >= 2 { dir = argv[1] as *u8 }
47
48 eb_p("=== NX-EDGAR-BALANCE: our arithmetic vs an authoritative SEC filing ===\n" as *u8)
49
50 let ab: *u8 = sys_mmap(EB_CAP)
51 let lb: *u8 = sys_mmap(EB_CAP)
52 let eb: *u8 = sys_mmap(EB_CAP)
53 let pa: *u8 = sys_mmap(512)
54 let pl: *u8 = sys_mmap(512)
55 let pe: *u8 = sys_mmap(512)
56 var o: i64 = ss_cat(pa, 0, dir); o = ss_cat(pa, o, "Assets.json" as *u8); pa[o] = 0 as u8
57 o = ss_cat(pl, 0, dir); o = ss_cat(pl, o, "Liabilities.json" as *u8); pl[o] = 0 as u8
58 o = ss_cat(pe, 0, dir); o = ss_cat(pe, o, "StockholdersEquity.json" as *u8); pe[o] = 0 as u8
59
60 let an: i64 = edgar_read(pa, ab, EB_CAP)
61 let ln: i64 = edgar_read(pl, lb, EB_CAP)
62 let en: i64 = edgar_read(pe, eb, EB_CAP)
63 eb_p(" Assets.json bytes=" as *u8); eb_num(an)
64 eb_p(" Liabilities.json bytes=" as *u8); eb_num(ln)
65 eb_p(" StockholdersEquity.json bytes=" as *u8); eb_num(en); eb_p("\n" as *u8)
66 if an <= 0 { eb_p("NX-EDGAR-BALANCE RED reason=no-assets-file\n" as *u8); sys_exit(2); return 2 }
67 if ln <= 0 { eb_p("NX-EDGAR-BALANCE RED reason=no-liabilities-file\n" as *u8); sys_exit(2); return 2 }
68 if en <= 0 { eb_p("NX-EDGAR-BALANCE RED reason=no-equity-file\n" as *u8); sys_exit(2); return 2 }
69
70 // edgar_num_last takes the LAST occurrence of the key, i.e. the most recently filed value in
71 // each concept file. All three files are pulled in the same run, so they describe the same
72 // filing period -- the python cross-check independently confirmed the newest common period.
73 let a: i64 = edgar_num_last(ab, an, "val" as *u8)
74 let l: i64 = edgar_num_last(lb, ln, "val" as *u8)
75 let e: i64 = edgar_num_last(eb, en, "val" as *u8)
76 eb_p(" filed assets = " as *u8); eb_num(a); eb_p("\n" as *u8)
77 eb_p(" filed liabilities = " as *u8); eb_num(l); eb_p("\n" as *u8)
78 eb_p(" filed equity = " as *u8); eb_num(e); eb_p("\n" as *u8)
79
80 if a == EDGAR_NOVAL { eb_p("NX-EDGAR-BALANCE RED reason=assets-missing (fail-closed, no fabricated fact)\n" as *u8); sys_exit(3); return 3 }
81 if l == EDGAR_NOVAL { eb_p("NX-EDGAR-BALANCE RED reason=liabilities-missing\n" as *u8); sys_exit(3); return 3 }
82 if e == EDGAR_NOVAL { eb_p("NX-EDGAR-BALANCE RED reason=equity-missing\n" as *u8); sys_exit(3); return 3 }
83
84 let computed: i64 = l + e
85 let delta: i64 = a - computed
86 eb_p(" WE COMPUTE liab+equity = " as *u8); eb_num(computed); eb_p("\n" as *u8)
87 eb_p(" delta vs filed assets = " as *u8); eb_num(delta); eb_p("\n" as *u8)
88 if delta != 0 {
89 eb_p("NX-EDGAR-BALANCE verdict=RED (our arithmetic does NOT reproduce the filed total)\n" as *u8)
90 sys_exit(1)
91 return 1
92 }
93 eb_p("NX-EDGAR-BALANCE verdict=GREEN (our computed total reproduces the filing EXACTLY)\n" as *u8)
94 sys_exit(0)
95 return 0
96}