// @ts-nocheck
/**
 * Execution-model calibration.
 *
 * Compare live realised trade outcomes against what two research models predict:
 *   optimistic (simple bar-close soft SL + intrabar hard SL) — used in full-validation
 *   realistic  (maker-fill-on-reclaim, hard-on-touch, timeout-on-no-reclaim) — used in
 *              stop-zone-sharpness and volatility-adaptive research
 *
 * Report which model better predicts the actual live sequence in terms of
 * win rate, mean net bps, and stop rate.
 */
import fs from 'node:fs'

const state = JSON.parse(fs.readFileSync('/tmp/txocap-combined/state.json', 'utf8'))
const closed = state.closedTrades ?? []

if (closed.length === 0) {
  console.log('No closed trades yet.')
  process.exit(0)
}

console.log(`Live closed trades: ${closed.length}`)
console.log()

// ---- Live actuals ----
const liveNets = closed.map((t: any) => t.netBps ?? 0)
const liveMean = liveNets.reduce((a: number, b: number) => a + b, 0) / liveNets.length
const liveWins = liveNets.filter((x: number) => x > 0).length
const liveWR   = liveWins / liveNets.length
const liveStops = closed.filter((t: any) => t.reason?.includes('sl') || t.reason?.includes('SL')).length
const liveStopRate = liveStops / closed.length
const liveTotalUsd = closed.reduce((a: number, t: any) => a + (t.netUsd ?? 0), 0)

console.log(`Live actuals:`)
console.log(`  trades: ${closed.length}`)
console.log(`  wins:   ${liveWins}/${closed.length}  WR=${(liveWR * 100).toFixed(1)}%`)
console.log(`  mean:   ${liveMean.toFixed(1)} bps`)
console.log(`  stops:  ${liveStops}/${closed.length}  rate=${(liveStopRate * 100).toFixed(1)}%`)
console.log(`  total:  $${liveTotalUsd.toFixed(2)}`)
console.log()

// ---- Model expectations from validation outputs ----
// These are from the actual validation outputs, not re-simulated.
// Optimistic model = /tmp/txocap-full-validation-l16s8-cap420-wait30-minsoft25.out
//   (but that was pre-roundMoat and pre-adaptive-imm)
// Realistic model = /tmp/txocap-volatility-adaptive-validation.out "prod" row
//   Trades: 3605, mean=2.28, WR=41.8%, SL=38.5%, hard=28.8%

// For fair comparison, use the realistic model's full-period stats as the
// "expected" and the optimistic full-validation stats as the "optimistic".
// Note: these are from slightly different profiles (optimistic predates roundMoat+adaptive-imm),
// so this is approximate.

const optimistic = {
  name:    'Optimistic (full-val)',
  mean:    10.34,
  wr:      0.429,
  stopRate: 0.406,
  hardRate: 0.098,
  moIR:    1.20,
  maxDD:   0.383,
}

const realistic = {
  name:    'Realistic (maker model)',
  mean:    2.28,
  wr:      0.418,
  stopRate: 0.385,
  hardRate: 0.288,
  moIR:    0.23,
  maxDD:   0.818,
}

console.log(`Model expectations (full 4yr):`)
console.log(`  ${optimistic.name.padEnd(30)} mean=${optimistic.mean.toFixed(1).padStart(5)}bps  WR=${(optimistic.wr * 100).toFixed(1).padStart(5)}%  SL=${(optimistic.stopRate * 100).toFixed(1).padStart(4)}%  hard=${(optimistic.hardRate * 100).toFixed(1).padStart(4)}%  moIR=${optimistic.moIR.toFixed(2)}  maxDD=${(optimistic.maxDD * 100).toFixed(1).padStart(5)}%`)
console.log(`  ${realistic.name.padEnd(30)} mean=${realistic.mean.toFixed(1).padStart(5)}bps  WR=${(realistic.wr * 100).toFixed(1).padStart(5)}%  SL=${(realistic.stopRate * 100).toFixed(1).padStart(4)}%  hard=${(realistic.hardRate * 100).toFixed(1).padStart(4)}%  moIR=${realistic.moIR.toFixed(2)}  maxDD=${(realistic.maxDD * 100).toFixed(1).padStart(5)}%`)
console.log()

// ---- Distance from actuals ----
function dist(act: number, pred: number) { return Math.abs(act - pred) }

const liveWRPct    = liveWR * 100
const liveStopPct  = liveStopRate * 100
const optMeanDist  = dist(liveMean, optimistic.mean)
const realMeanDist = dist(liveMean, realistic.mean)
const optWRDist    = dist(liveWRPct, optimistic.wr * 100)
const realWRDist   = dist(liveWRPct, realistic.wr * 100)
const optStopDist  = dist(liveStopPct, optimistic.stopRate * 100)
const realStopDist = dist(liveStopPct, realistic.stopRate * 100)

console.log(`Distance from live actuals (lower = closer):`)
console.log(`  Metric        Live    Optimistic  Realistic`)
console.log(`  mean bps      ${liveMean.toFixed(1).padStart(5)}   ${optimistic.mean.toFixed(1).padStart(5)}     ${realistic.mean.toFixed(1).padStart(5)}`)
console.log(`  WR %          ${liveWRPct.toFixed(1).padStart(5)}   ${(optimistic.wr*100).toFixed(1).padStart(5)}     ${(realistic.wr*100).toFixed(1).padStart(5)}`)
console.log(`  stop rate %   ${liveStopPct.toFixed(1).padStart(5)}   ${(optimistic.stopRate*100).toFixed(1).padStart(5)}     ${(realistic.stopRate*100).toFixed(1).padStart(5)}`)
console.log()

const optTotal = optMeanDist + optWRDist + optStopDist
const realTotal = realMeanDist + realWRDist + realStopDist

console.log(`Closest model: ${optTotal < realTotal ? 'OPTIMISTIC (full-validation)' : 'REALISTIC (maker model)'}`)
console.log(`  Optimistic total distance: ${optTotal.toFixed(1)}`)
console.log(`  Realistic   total distance: ${realTotal.toFixed(1)}`)
console.log()

if (closed.length < 10) {
  console.log('⚠  Small sample (<10 trades). Re-check after 20+ trades for a reliable calibration.')
}
