nx_carflip_roi.nx source
↩ module page · 111 lines · 3617 B
1// nx_carflip_roi.nx -- R6 ROI-scorer organ for the Car-Flip lane.
2// Reads a used-car deal as argv (whole USD) and emits a downside-gated BUY/PASS.
3// Gate = P10 (bad case: LOW resale + HIGH repair) -- buy only winners.
4// v1 = deterministic calculator over operator-supplied REAL figures (no fabricated
5// data; the inspector enters findings at call time). v2 rung: read carflip-fail-
6// priors + carflip-sys- to auto-estimate repair_high.
7// license_tier: ORIGINAL expect_exit: 0
8import "nx_syscalls.nx"
9
10const CF_BPS: i64 = 10000 // ROI reported in basis points (10000 = 100%)
11
12func cf_puts(s: *u8) -> i64 {
13 var n: i64 = 0
14 while s[n] != (0 as u8) { n = n + 1 }
15 return sys_write(1, s, n)
16}
17
18func cf_atoi(s: *u8) -> i64 {
19 var i: i64 = 0
20 var neg: i64 = 0
21 if s[0] == (45 as u8) { neg = 1; i = 1 }
22 var v: i64 = 0
23 var done: i64 = 0
24 while done == 0 {
25 let c: i64 = s[i] as i64
26 if c < 48 { done = 1 }
27 if c > 57 { done = 1 }
28 if done == 0 { v = v * 10 + (c - 48); i = i + 1 }
29 }
30 if neg == 1 { return 0 - v }
31 return v
32}
33
34func cf_putn(n: i64) -> i64 {
35 if n == 0 { return sys_write(1, "0" as *u8, 1) }
36 var x: i64 = n
37 var neg: i64 = 0
38 if n < 0 { neg = 1; x = 0 - n }
39 let tmp: *u8 = sys_mmap(32)
40 var t: i64 = 0
41 while x > 0 {
42 let d: i64 = x - (x / 10) * 10
43 tmp[t] = (48 + d) as u8
44 t = t + 1
45 x = x / 10
46 }
47 let out: *u8 = sys_mmap(40)
48 var i: i64 = 0
49 if neg == 1 { out[0] = 45 as u8; i = 1 }
50 var k: i64 = t - 1
51 while k >= 0 {
52 out[i] = tmp[k]
53 i = i + 1
54 k = k - 1
55 }
56 return sys_write(1, out, i)
57}
58
59func cf_kv(key: *u8, val: i64) -> i64 {
60 cf_puts(key)
61 cf_puts("=" as *u8)
62 cf_putn(val)
63 cf_puts("\n" as *u8)
64 return 0
65}
66
67func main(argc: i64, argv: *i64) -> i64 {
68 if argc < 11 {
69 cf_puts("usage: nx_carflip_roi <asking> <resale_typ> <resale_low> <repair_typ> <repair_high> <recon> <transport> <fees> <sell_cost> <holding> (whole USD)\n" as *u8)
70 sys_exit(2)
71 return 2
72 }
73 let asking: i64 = cf_atoi(argv[1] as *u8)
74 let resale_typ: i64 = cf_atoi(argv[2] as *u8)
75 let resale_low: i64 = cf_atoi(argv[3] as *u8)
76 let repair_typ: i64 = cf_atoi(argv[4] as *u8)
77 let repair_high: i64 = cf_atoi(argv[5] as *u8)
78 let recon: i64 = cf_atoi(argv[6] as *u8)
79 let transport: i64 = cf_atoi(argv[7] as *u8)
80 let fees: i64 = cf_atoi(argv[8] as *u8)
81 let sell_cost: i64 = cf_atoi(argv[9] as *u8)
82 let holding: i64 = cf_atoi(argv[10] as *u8)
83
84 let base: i64 = asking + recon + transport + fees + holding
85 let allin_typ: i64 = base + repair_typ
86 let allin_bad: i64 = base + repair_high
87 let profit_typ: i64 = resale_typ - sell_cost - allin_typ
88 let profit_p10: i64 = resale_low - sell_cost - allin_bad
89 var roi_typ: i64 = 0
90 if allin_typ > 0 { roi_typ = profit_typ * CF_BPS / allin_typ }
91 var roi_p10: i64 = 0
92 if allin_bad > 0 { roi_p10 = profit_p10 * CF_BPS / allin_bad }
93
94 cf_puts("CARFLIP-ROI\n" as *u8)
95 cf_kv("allin_typ" as *u8, allin_typ)
96 cf_kv("allin_bad" as *u8, allin_bad)
97 cf_kv("profit_typ" as *u8, profit_typ)
98 cf_kv("profit_p10" as *u8, profit_p10)
99 cf_kv("roi_typ_bps" as *u8, roi_typ)
100 cf_kv("roi_p10_bps" as *u8, roi_p10)
101 if profit_p10 >= 0 {
102 cf_puts("verdict=BUY\n" as *u8)
103 cf_puts("rule=P10>=0 downside-positive\n" as *u8)
104 sys_exit(0)
105 return 0
106 }
107 cf_puts("verdict=PASS\n" as *u8)
108 cf_puts("rule=P10<0 downside-negative walk-away\n" as *u8)
109 sys_exit(0)
110 return 0
111}