"""
ITERATION 1 CHANGES:
1. Minimum EXP score of 30 required (blocks weak-EXP losers)
2. Stop loss effectively removed (STOP_LOSS = 0.95)
3. Bigger bets on high-conviction trades
4. SCORE_HIGH raised to 30
"""
import re

path = r"C:\kalshibot\Automated\paper_trade.py"
with open(path, "r", encoding="utf-8") as f:
    content = f.read()

changes = []

# 1. Remove stop loss (set to 0.95 — effectively $0 stop price, only TIME exits)
old = 'STOP_LOSS             = 0.22   # tighter stop — if wrong, exit fast'
new = 'STOP_LOSS             = 0.95   # NO STOP LOSS — user instruction. TIME exit handles cleanup.'
if old in content:
    content = content.replace(old, new)
    changes.append("STOP_LOSS = 0.95 (removed)")
else:
    # try regex fallback
    content = re.sub(r'STOP_LOSS\s*=\s*0\.\d+[^\n]*', 'STOP_LOSS             = 0.95   # NO STOP LOSS — TIME exit handles cleanup', content)
    changes.append("STOP_LOSS = 0.95 (regex fallback)")

# 2. Raise SCORE_HIGH to 30
content = re.sub(r'SCORE_HIGH\s*=\s*\d+[^\n]*', 'SCORE_HIGH             = 30    # HIGH = only top-tier signals (EXP dominant)', content)
changes.append("SCORE_HIGH = 30")

# 3. New bet sizing — bigger on high conviction
old_bet = (
    "    if score < SCORE_MEDIUM: return 0.0\n"
    "    # Many-position strategy: smaller individual bets, more opportunities\n"
    "    # Score range: EXP(0-50)+LAG(0-30)+MOM(0-20)=max 100\n"
    "    if   score >= 55: pct = 0.06   # very strong: 6% = ~$30 on $500\n"
    "    elif score >= 40: pct = 0.04   # strong:      4% = ~$20\n"
    "    elif score >= 25: pct = 0.025  # HIGH:      2.5% = ~$12\n"
    "    elif score >= 18: pct = 0.015  # MED-HIGH:  1.5% = ~$7\n"
    "    else:             pct = 0.010  # MEDIUM:      1% = ~$5\n"
    "    bet = round(current_balance * pct, 2)\n"
    "    bet = max(1.00, min(bet, current_balance * 0.10))  # floor $1, cap 10% per trade\n"
    "    return bet"
)
new_bet = (
    "    if score < SCORE_MEDIUM: return 0.0\n"
    "    # ITER1: bigger bets on proven high-EXP signals\n"
    "    if   score >= 55: pct = 0.10   # very strong: 10% = ~$50 on $500\n"
    "    elif score >= 40: pct = 0.07   # strong:       7% = ~$35\n"
    "    elif score >= 30: pct = 0.04   # HIGH:         4% = ~$20\n"
    "    elif score >= 18: pct = 0.02   # MED-HIGH:     2% = ~$10\n"
    "    else:             pct = 0.01   # MEDIUM:       1% = ~$5\n"
    "    bet = round(current_balance * pct, 2)\n"
    "    bet = max(1.00, min(bet, current_balance * 0.15))  # floor $1, cap 15% per trade\n"
    "    return bet"
)
if old_bet in content:
    content = content.replace(old_bet, new_bet)
    changes.append("Bet sizing: 10% on score 55+, 7% on 40-54, 4% on 30-39")
else:
    changes.append("WARNING: bet block not found")

# 4. Add minimum EXP score requirement in get_confidence_score
# Insert after the exp_score, lag_score calculation lines
old_exp_check = (
    "    exp_score, exp_dir   = score_expiration_value(market, btc_price)\n"
    "    lag_score, lag_dir   = score_lag_detection(market, btc_price)\n"
    "    # Resolve direction: expiration value takes priority, then lag, then momentum"
)
new_exp_check = (
    "    exp_score, exp_dir   = score_expiration_value(market, btc_price)\n"
    "    lag_score, lag_dir   = score_lag_detection(market, btc_price)\n"
    "    # ITER1: Minimum EXP threshold — weak EXP = no real edge, block it\n"
    "    # Data: EXP>=46 = 4/4 wins. EXP=16 = 0/2 wins. Filter at 30.\n"
    "    MIN_EXP_TO_COUNT = 30\n"
    "    if exp_score > 0 and exp_score < MIN_EXP_TO_COUNT:\n"
    "        exp_score = 0  # treat weak EXP as no signal\n"
    "        exp_dir   = None\n"
    "    # Resolve direction: expiration value takes priority, then lag, then momentum"
)
if old_exp_check in content:
    content = content.replace(old_exp_check, new_exp_check)
    changes.append("MIN EXP score = 30 added (blocks weak-EXP losers)")
else:
    changes.append("WARNING: exp check insertion point not found")

with open(path, "w", encoding="utf-8") as f:
    f.write(content)

print("ITERATION 1 APPLIED:")
for c in changes:
    ok = "WARNING" not in c
    print(f"  {'OK' if ok else 'FAIL'} {c}")

# Verify
with open(path) as f:
    lines = f.readlines()
print("\nVERIFY:")
print(f"  STOP_LOSS:     {next((l.strip() for l in lines if 'STOP_LOSS' in l and '=' in l and 'TRAILING' not in l), 'NOT FOUND')}")
print(f"  SCORE_HIGH:    {next((l.strip() for l in lines if 'SCORE_HIGH' in l and '=' in l), 'NOT FOUND')}")
print(f"  MIN_EXP_TO_COUNT: {'FOUND' if any('MIN_EXP_TO_COUNT' in l for l in lines) else 'NOT FOUND'}")
print(f"  Bet 10%:       {'FOUND' if any('pct = 0.10' in l for l in lines) else 'NOT FOUND'}")
