code wiki / _hdl_build / nx_flip_score.nx

nx_flip_score.nx source

↩ module page · 283 lines · 12746 B

1// nx_flip_score.nx -- THE ASSET-AGNOSTIC COMPONENT-LEVEL FLIP ENGINE. 2// Operator: "an engine not me typing every part" -- reads the part list ITSELF from the planes and 3// CHOOSES the repair set component-by-component. Same math for a car, a house, any asset with 4// value-bearing components (class/component/finding are DATA, not code). 5// PER-COMPONENT (all downside-priced): margin_typ=lift_typ-repair_typ ; margin_p10=lift_low-repair_high 6// MANDATORY (cost forced) / FLIPPOINT (margin_p10>=0) / SPECULATIVE (class-gated) / LEAVE. 7// COVERAGE GATE: every MANDATORY catalog component for the class needs a finding, else REFUSED. 8// DEAL GATE: BUY iff profit_p10>=gate_min AND days<=dts_cap. Every threshold from flip-class-. 9// 10// CONVERTED ONTO THE SHARED BASE nx_flip_lib.nx (fx_*); the ~17 private helpers are GONE. Behaviour 11// PROVEN identical by nx_flip_gate staying 16/16 across the swap. 12// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0 13import "nx_flip_lib.nx" 14import "nx_syscalls.nx" 15 16const FL_ARGC_MIN: i64 = 10 17const FL_ARG_PFX: i64 = 10 18const FL_CC_GATE: i64 = 2 19const FL_CC_SPEC: i64 = 3 20const FL_CC_DTS: i64 = 4 21const FL_CC_SELLBPS: i64 = 5 22const FL_CC_HOLD: i64 = 6 23const FL_CK_CLASS: i64 = 1 24const FL_CK_MAND: i64 = 3 25const FL_CF_DEAL: i64 = 1 26const FL_CF_COMP: i64 = 2 27const FL_CF_COND: i64 = 3 28const FL_CF_REPLOW: i64 = 4 29const FL_CF_REPTYP: i64 = 5 30const FL_CF_REPHIGH: i64 = 6 31const FL_CF_LIFTLOW: i64 = 7 32const FL_CF_LIFTTYP: i64 = 8 33const FL_A_LEAVE: i64 = 0 34const FL_A_MAND: i64 = 1 35const FL_A_FLIP: i64 = 2 36const FL_A_SPEC: i64 = 3 37 38func main(argc: i64, argv: *i64) -> i64 { 39 if argc < FL_ARGC_MIN { 40 fx_puts("usage: nx_flip_score <class> <deal> <asking> <resale_typ> <resale_low> <recon> <transport> <fees> <days> [plane-prefix-root]\n" as *u8) 41 fx_puts(" parts are NOT typed -- they are read from <root>comp- (catalog) + <root>find- (findings)\n" as *u8) 42 sys_exit(FX_EXIT_USAGE) 43 return FX_EXIT_USAGE 44 } 45 let cls: *u8 = argv[1] as *u8 46 let deal: *u8 = argv[2] as *u8 47 let asking: i64 = fx_atoi(argv[3] as *u8) 48 let resale_typ: i64 = fx_atoi(argv[4] as *u8) 49 let resale_low: i64 = fx_atoi(argv[5] as *u8) 50 let recon: i64 = fx_atoi(argv[6] as *u8) 51 let transport: i64 = fx_atoi(argv[7] as *u8) 52 let fees: i64 = fx_atoi(argv[8] as *u8) 53 let days: i64 = fx_atoi(argv[9] as *u8) 54 55 var root: *u8 = "flip-" as *u8 56 if argc > FL_ARG_PFX { root = argv[FL_ARG_PFX] as *u8 } 57 let ncp: *i64 = sts_mm(FX_SCRATCH) as *i64 58 let nkp: *i64 = sts_mm(FX_SCRATCH) as *i64 59 let nfp: *i64 = sts_mm(FX_SCRATCH) as *i64 60 let bc: *u8 = fx_plane(root, "class-" as *u8, ncp) 61 let bk: *u8 = fx_plane(root, "comp-" as *u8, nkp) 62 let bf: *u8 = fx_plane(root, "find-" as *u8, nfp) 63 let nc: i64 = ncp[0] 64 let nk: i64 = nkp[0] 65 let nf: i64 = nfp[0] 66 67 let sp: *i64 = sts_mm(FX_SCRATCH) as *i64 68 let cr: *i64 = sts_mm(FX_SCRATCH) as *i64 69 let kr: *i64 = sts_mm(FX_SCRATCH) as *i64 70 let cs: *i64 = sts_mm(FX_SCRATCH) as *i64 71 72 if nc == 0 { 73 fx_puts("FLIP-RED reason=class-plane-empty prefix=" as *u8); fx_puts(fx_cat2(root, "class-" as *u8)); fx_puts("\n" as *u8) 74 sys_exit(FX_EXIT_REFUSED) 75 return FX_EXIT_REFUSED 76 } 77 if nk == 0 { 78 fx_puts("FLIP-RED reason=component-plane-empty prefix=" as *u8); fx_puts(fx_cat2(root, "comp-" as *u8)); fx_puts("\n" as *u8) 79 sys_exit(FX_EXIT_REFUSED) 80 return FX_EXIT_REFUSED 81 } 82 if fx_row_by_id(bc, nc, cls, cr, sp) == 0 { 83 fx_puts("FLIP-RED reason=unknown-asset-class class=" as *u8); fx_puts(cls); fx_puts("\n" as *u8) 84 sys_exit(FX_EXIT_REFUSED) 85 return FX_EXIT_REFUSED 86 } 87 let gate_min: i64 = fx_col_atoi(bc, cr[0], cr[1], FL_CC_GATE, sp) 88 let take_spec: i64 = fx_col_atoi(bc, cr[0], cr[1], FL_CC_SPEC, sp) 89 let dts_cap: i64 = fx_col_atoi(bc, cr[0], cr[1], FL_CC_DTS, sp) 90 let sell_bps: i64 = fx_col_atoi(bc, cr[0], cr[1], FL_CC_SELLBPS, sp) 91 let hold_day: i64 = fx_col_atoi(bc, cr[0], cr[1], FL_CC_HOLD, sp) 92 93 let pb: *u8 = sts_mm(FX_BUF) 94 var po: i64 = 0 95 var rep_t: i64 = 0 96 var rep_h: i64 = 0 97 var lift_t: i64 = 0 98 var lift_l: i64 = 0 99 var mand_h: i64 = 0 100 var flip_gain: i64 = 0 101 var n_mand: i64 = 0 102 var n_flip: i64 = 0 103 var n_spec: i64 = 0 104 var n_leave: i64 = 0 105 var n_seen: i64 = 0 106 var n_unk: i64 = 0 107 108 var i: i64 = 0 109 while i < nf { 110 let le: i64 = fx_line_end(bf, nf, i) 111 if le > i { 112 if fx_col(bf, i, le, FL_CF_DEAL, sp) == 1 { 113 if fx_slice_eqs(bf, sp[0], sp[1], deal) == 1 { 114 n_seen = n_seen + 1 115 fx_col(bf, i, le, FL_CF_COMP, cs) 116 let cond: i64 = fx_col_atoi(bf, i, le, FL_CF_COND, sp) 117 let rt: i64 = fx_col_atoi(bf, i, le, FL_CF_REPTYP, sp) 118 let rh: i64 = fx_col_atoi(bf, i, le, FL_CF_REPHIGH, sp) 119 let ll: i64 = fx_col_atoi(bf, i, le, FL_CF_LIFTLOW, sp) 120 let lt: i64 = fx_col_atoi(bf, i, le, FL_CF_LIFTTYP, sp) 121 122 var mand: i64 = 1 123 var known: i64 = 0 124 if fx_row_by_slice(bk, nk, bf, cs[0], cs[1], kr, sp) == 1 { 125 known = 1 126 mand = fx_col_atoi(bk, kr[0], kr[1], FL_CK_MAND, sp) 127 } 128 let m_t: i64 = lt - rt 129 let m_p: i64 = ll - rh 130 var act: i64 = FL_A_LEAVE 131 var take: i64 = 0 132 if mand == 1 { 133 act = FL_A_MAND 134 take = 1 135 } else { 136 if m_p >= 0 { 137 act = FL_A_FLIP 138 take = 1 139 } else { 140 if m_t > 0 { act = FL_A_SPEC; take = take_spec } else { act = FL_A_LEAVE; take = 0 } 141 } 142 } 143 if take == 1 { 144 rep_t = rep_t + rt 145 rep_h = rep_h + rh 146 lift_t = lift_t + lt 147 lift_l = lift_l + ll 148 } 149 if act == FL_A_MAND { n_mand = n_mand + 1; mand_h = mand_h + rh } 150 if act == FL_A_FLIP { n_flip = n_flip + 1; flip_gain = flip_gain + m_p } 151 if act == FL_A_SPEC { n_spec = n_spec + 1 } 152 if act == FL_A_LEAVE { n_leave = n_leave + 1 } 153 if known == 0 { n_unk = n_unk + 1 } 154 155 po = fx_bcat(pb, po, "PART " as *u8) 156 po = fx_bcatsl(pb, po, bf, cs[0], cs[1]) 157 po = fx_bcat(pb, po, " cond=" as *u8); po = fx_bcatn(pb, po, cond) 158 po = fx_bcat(pb, po, " rep_typ=" as *u8); po = fx_bcatn(pb, po, rt) 159 po = fx_bcat(pb, po, " rep_high=" as *u8); po = fx_bcatn(pb, po, rh) 160 po = fx_bcat(pb, po, " lift_typ=" as *u8); po = fx_bcatn(pb, po, lt) 161 po = fx_bcat(pb, po, " lift_low=" as *u8); po = fx_bcatn(pb, po, ll) 162 po = fx_bcat(pb, po, " margin_typ=" as *u8); po = fx_bcatn(pb, po, m_t) 163 po = fx_bcat(pb, po, " margin_p10=" as *u8); po = fx_bcatn(pb, po, m_p) 164 po = fx_bcat(pb, po, " action=" as *u8) 165 if act == FL_A_MAND { po = fx_bcat(pb, po, "MANDATORY" as *u8) } 166 if act == FL_A_FLIP { po = fx_bcat(pb, po, "FLIPPOINT" as *u8) } 167 if act == FL_A_SPEC { 168 if take == 1 { po = fx_bcat(pb, po, "SPECULATIVE-TAKE" as *u8) } else { po = fx_bcat(pb, po, "SPECULATIVE-SKIP" as *u8) } 169 } 170 if act == FL_A_LEAVE { po = fx_bcat(pb, po, "LEAVE" as *u8) } 171 if known == 0 { po = fx_bcat(pb, po, " WARN=uncatalogued-component-forced-mandatory" as *u8) } 172 po = fx_bcat(pb, po, "\n" as *u8) 173 } 174 } 175 } 176 i = le + 1 177 } 178 179 // COVERAGE GATE: every MANDATORY catalog component for this class needs a finding. 180 var n_missing: i64 = 0 181 let cs2: *i64 = sts_mm(FX_SCRATCH) as *i64 182 var ci: i64 = 0 183 while ci < nk { 184 let le2: i64 = fx_line_end(bk, nk, ci) 185 if le2 > ci { 186 if fx_col(bk, ci, le2, FL_CK_CLASS, sp) == 1 { 187 if fx_slice_eqs(bk, sp[0], sp[1], cls) == 1 { 188 if fx_col_atoi(bk, ci, le2, FL_CK_MAND, sp) == 1 { 189 if fx_col(bk, ci, le2, 0, cs2) == 1 { 190 if fx_has_finding(bf, nf, deal, FL_CF_DEAL, FL_CF_COMP, bk, cs2[0], cs2[1], sp) == 0 { 191 n_missing = n_missing + 1 192 po = fx_bcat(pb, po, "UNINSPECTED " as *u8) 193 po = fx_bcatsl(pb, po, bk, cs2[0], cs2[1]) 194 po = fx_bcat(pb, po, " mandatory-system-no-finding\n" as *u8) 195 } 196 } 197 } 198 } 199 } 200 } 201 ci = le2 + 1 202 } 203 204 if n_seen == 0 { 205 fx_puts("FLIP-RED reason=no-findings-for-deal deal=" as *u8); fx_puts(deal); fx_puts("\n" as *u8) 206 fx_puts("verdict=REFUSED\n" as *u8) 207 fx_puts("rule=absent-inspection-is-not-a-clean-inspection\n" as *u8) 208 sys_exit(FX_EXIT_REFUSED) 209 return FX_EXIT_REFUSED 210 } 211 212 let holding: i64 = hold_day * days 213 let base: i64 = asking + recon + transport + fees + holding 214 let allin_typ: i64 = base + rep_t 215 let allin_bad: i64 = base + rep_h 216 let rs_typ: i64 = resale_typ + lift_t 217 let rs_low: i64 = resale_low + lift_l 218 let sc_typ: i64 = rs_typ * sell_bps / FX_BPS 219 let sc_low: i64 = rs_low * sell_bps / FX_BPS 220 let profit_typ: i64 = rs_typ - sc_typ - allin_typ 221 let profit_p10: i64 = rs_low - sc_low - allin_bad 222 var roi_t: i64 = 0 223 if allin_typ > 0 { roi_t = profit_typ * FX_BPS / allin_typ } 224 var roi_p: i64 = 0 225 if allin_bad > 0 { roi_p = profit_p10 * FX_BPS / allin_bad } 226 227 var vcode: i64 = 0 228 if n_missing > 0 { vcode = 4 } else { if days > dts_cap { vcode = 2 } else { if profit_p10 >= gate_min { vcode = 1 } } } 229 230 fx_puts("FLIP-ASSESS\n" as *u8) 231 fx_puts("class=" as *u8); fx_puts(cls); fx_puts(" deal=" as *u8); fx_puts(deal); fx_puts("\n" as *u8) 232 fx_puts("verdict=" as *u8) 233 if vcode == 4 { fx_puts("REFUSED" as *u8) } 234 if vcode == 1 { fx_puts("BUY" as *u8) } 235 if vcode == 2 { fx_puts("PASS" as *u8) } 236 if vcode == 0 { fx_puts("PASS" as *u8) } 237 fx_puts(" uninspected_mandatory=" as *u8); fx_putn(n_missing) 238 fx_puts(" profit_p10=" as *u8); fx_putn(profit_p10) 239 fx_puts(" profit_typ=" as *u8); fx_putn(profit_typ) 240 fx_puts(" allin_bad=" as *u8); fx_putn(allin_bad) 241 fx_puts(" mand=" as *u8); fx_putn(n_mand) 242 fx_puts(" flip=" as *u8); fx_putn(n_flip) 243 fx_puts(" spec=" as *u8); fx_putn(n_spec) 244 fx_puts(" leave=" as *u8); fx_putn(n_leave) 245 fx_puts("\n" as *u8) 246 fx_puts("rule=" as *u8) 247 if vcode == 4 { fx_puts("mandatory-systems-never-inspected-absent-finding-is-not-a-clean-finding\n" as *u8) } 248 if vcode == 2 { fx_puts("hold-period-exceeds-class-days-to-sell-cap\n" as *u8) } 249 if vcode == 1 { fx_puts("P10>=gate_min downside-positive after mandatory repairs\n" as *u8) } 250 if vcode == 0 { fx_puts("P10<gate_min downside-negative walk-away\n" as *u8) } 251 252 fx_kv("parts_assessed" as *u8, n_seen) 253 fx_kv("parts_mandatory" as *u8, n_mand) 254 fx_kv("parts_flippoint" as *u8, n_flip) 255 fx_kv("parts_speculative" as *u8, n_spec) 256 fx_kv("parts_leave" as *u8, n_leave) 257 fx_kv("parts_uncatalogued" as *u8, n_unk) 258 fx_kv("uninspected_mandatory" as *u8, n_missing) 259 fx_kv("mandatory_cost_high" as *u8, mand_h) 260 fx_kv("flippoint_gain_p10" as *u8, flip_gain) 261 fx_kv("repair_taken_typ" as *u8, rep_t) 262 fx_kv("repair_taken_high" as *u8, rep_h) 263 fx_kv("lift_taken_typ" as *u8, lift_t) 264 fx_kv("lift_taken_low" as *u8, lift_l) 265 fx_kv("holding" as *u8, holding) 266 fx_kv("sell_cost_typ" as *u8, sc_typ) 267 fx_kv("sell_cost_low" as *u8, sc_low) 268 fx_kv("allin_typ" as *u8, allin_typ) 269 fx_kv("allin_bad" as *u8, allin_bad) 270 fx_kv("resale_eff_typ" as *u8, rs_typ) 271 fx_kv("resale_eff_low" as *u8, rs_low) 272 fx_kv("profit_typ" as *u8, profit_typ) 273 fx_kv("profit_p10" as *u8, profit_p10) 274 fx_kv("roi_typ_bps" as *u8, roi_t) 275 fx_kv("roi_p10_bps" as *u8, roi_p) 276 fx_kv("gate_min" as *u8, gate_min) 277 fx_kv("days" as *u8, days) 278 fx_kv("days_cap" as *u8, dts_cap) 279 280 sys_write(FX_OUT, pb, po) 281 sys_exit(0) 282 return 0 283}