"""
Applies all targeted fixes to paper_trade.py.
Run once, then delete this file.
"""

with open(r"C:\kalshibot\paper_trade.py", "r") as f:
    code = f.read()

changes = []

# ── FIX 1: Raise MAX_ENTRY_PRICE 0.82 → 0.93
# Allows EXP trades on high-priced YES contracts (e.g. YES@0.90 when model says 0.97)
old = "MAX_ENTRY_PRICE       = 0.82"
new = "MAX_ENTRY_PRICE       = 0.93  # raised from 0.82 — EXP trades need high prices"
assert old in code, "FIX 1 not found"
code = code.replace(old, new, 1)
changes.append("FIX 1: MAX_ENTRY_PRICE 0.82 → 0.93")

# ── FIX 2: Lower SCORE_MEDIUM 7 → 5
# Pairs with MIN_MISPRICING drop — 1.0% mispricing now scores 5 and fires
old = "SCORE_MEDIUM           = 7     # score >= 7  → MEDIUM (2% mispricing fires)"
new = "SCORE_MEDIUM           = 5     # score >= 5  → MEDIUM (1.0% mispricing fires)"
assert old in code, "FIX 2 not found"
code = code.replace(old, new, 1)
changes.append("FIX 2: SCORE_MEDIUM 7 → 5")

# ── FIX 3: Lower ENTRY_COOLDOWN 30 → 15
# Re-enter faster after a trade — no win rate impact, just more responsive
old = "ENTRY_COOLDOWN        = 30    # 30s between entries"
new = "ENTRY_COOLDOWN        = 15    # 15s between entries (was 30)"
assert old in code, "FIX 3 not found"
code = code.replace(old, new, 1)
changes.append("FIX 3: ENTRY_COOLDOWN 30s → 15s")

# ── FIX 4: Lower MIN_MISPRICING 1.5% → 1.0%
# Catch more genuine mispricings
old = "MIN_MISPRICING         = 0.015  # 1.5% mispricing needed to score any points"
new = "MIN_MISPRICING         = 0.010  # 1.0% mispricing needed to score (was 1.5%)"
assert old in code, "FIX 4 not found"
code = code.replace(old, new, 1)
changes.append("FIX 4: MIN_MISPRICING 1.5% → 1.0%")

# ── FIX 5: Lower LAG_MIN_BTC_MOVE 0.06% → 0.04%
# Detect smaller BTC moves for lag signal
old = "LAG_MIN_BTC_MOVE       = 0.0006  # lowered from 0.0008 — easier to detect lag"
new = "LAG_MIN_BTC_MOVE       = 0.0004  # lowered from 0.0006 — easier to detect lag"
assert old in code, "FIX 5 not found"
code = code.replace(old, new, 1)
changes.append("FIX 5: LAG_MIN_BTC_MOVE 0.06% → 0.04%")

# ── FIX 6: Lower lag gap threshold in score_lag_detection 0.03 → 0.02 (two places)
# Detects smaller lag gaps between BTC move and Kalshi reprice
old1 = "        if lag > 0.03:  # Kalshi lagged by 3%+ on a BTC up move"
new1 = "        if lag > 0.02:  # Kalshi lagged by 2%+ on a BTC up move (was 3%)"
assert old1 in code, "FIX 6a not found"
code = code.replace(old1, new1, 1)

old2 = "        if lag > 0.03:\n            score = int(min(30, lag * 250))\n            return score, \"DOWN\""
new2 = "        if lag > 0.02:  # Kalshi lagged by 2%+ on a BTC down move (was 3%)\n            score = int(min(30, lag * 250))\n            return score, \"DOWN\""
assert old2 in code, "FIX 6b not found"
code = code.replace(old2, new2, 1)
changes.append("FIX 6: Lag gap threshold 0.03 → 0.02 (both directions)")

# ── FIX 7: Block momentum-only trades in get_confidence_score
# Momentum can never fire a trade alone — requires EXP or LAG signal
old = '    breakdown = f"EXP:{exp_score} LAG:{lag_score} MOM:{mom_score}"\n    if   total >= SCORE_HIGH:   tier = "HIGH"'
new = ('    breakdown = f"EXP:{exp_score} LAG:{lag_score} MOM:{mom_score}"\n'
       '    # CRITICAL: Never trade on momentum alone — must have EXP or LAG signal\n'
       '    # Momentum is a confirming signal only; alone it has ~39% win rate (proven bad)\n'
       '    if exp_score == 0 and lag_score == 0:\n'
       '        return 0, None, None, breakdown\n'
       '    if   total >= SCORE_HIGH:   tier = "HIGH"')
assert old in code, "FIX 7 not found"
code = code.replace(old, new, 1)
changes.append("FIX 7: Block momentum-only trades (require EXP or LAG signal)")

# ── FIX 8a: Fix undefined 'tier' variable in paper_enter print line
# 'tier' is not defined inside paper_enter — should be 'signal_type'
old = "    print(f\"  PAPER TRADE [{signal_type}]  {'YES BTC UP' if side=='yes' else 'NO  BTC DOWN'}  [{tier}]\")"
new = "    print(f\"  PAPER TRADE [{signal_type}]  {'YES BTC UP' if side=='yes' else 'NO  BTC DOWN'}\")"
assert old in code, "FIX 8a not found"
code = code.replace(old, new, 1)
changes.append("FIX 8a: Removed undefined 'tier' from paper_enter print (was crashing every trade entry)")

# ── FIX 8b: Fix undefined 'tier' in paper_enter return statement
old = "    return True, tier"
new = "    return True, signal_type"
assert old in code, "FIX 8b not found"
code = code.replace(old, new, 1)
changes.append("FIX 8b: Fixed 'return True, tier' → 'return True, signal_type'")

# Write the updated file
with open(r"C:\kalshibot\paper_trade.py", "w") as f:
    f.write(code)

print("=" * 60)
print("  ALL FIXES APPLIED SUCCESSFULLY")
print("=" * 60)
for c in changes:
    print(f"  ✅ {c}")
print(f"\n  Total: {len(changes)} changes made")
print("=" * 60)
