import json

progress_file = 'manual_replay_progress.json'
with open(progress_file, 'r') as f:
    prog = json.load(f)

# Evaluate Candidate 1: 2026-06-23T22:15:00Z
# H4 is bearish, recent high 4145 -> down to 4080
# M30 shows 4126 -> 4107 -> bounce to 4115.
# At 22:15, price pushes to 4115.235, sweeping previous minor highs 4113.035/4111.95 (Asian session/late NY). 
# Rejection M5 at 22:15: O: 4113.985, H: 4115.235, L: 4111.19, C: 4111.515 (Bearish pin bar)
# Next candle (22:20): O: 4111.51, H: 4112.18, L: 4105.3, C: 4106.5 (strong drop)
# This is a solid M5 entry. Entry ~4111.5, SL > 4115.5 (~40pips), TP target 4097 (previous low). R ~3.6

c1 = {
  "candidate_id": "candidate_2026-06-23T22-15-00Z_short_liquidity_sweep_reversal",
  "timestamp": "2026-06-23T22:15:00Z",
  "decision": "accepted",
  "reason": "Chronological evidence shows bearish pinbar at 22:15 sweeping 4113.0 liquidity followed by strong displacement.",
  "htf_context": "H4 bearish trend intact after rejection at 4145. Downward structural flow.",
  "m30_structure": "M30 lower highs, brief consolidation/bounce before Asian session liquidity sweep to 4115.",
  "m15_m5_execution": "M5 bearish pinbar at 22:15 closed at 4111.5 after sweeping 4115.2, followed by immediate displacement to 4106.",
  "entry": 4111.5,
  "stop": 4115.5,
  "target": 4097.5,
  "r": 3.5,
  "promoted_to_journal": True
}

# Evaluate Candidate 2: 2026-06-24T01:30:00Z
# M30: 23:30 sweep low 4080, rally to 4112 (00:00). Drop to 4068 (01:00). Then rally back to 4101 (01:15) and 4102.51 (01:30).
# M5 at 01:30: O: 4098.305, H: 4102.51, L: 4093.535, C: 4098.335 (Rejection pin bar off 4102, back into FVG maybe?)
# Next candle (01:35): C: 4095.21
# This sweep (4102.5 vs earlier 4112) is a lower high in M30 context. The drop goes to 4086, then 4071.
# SL would be above 4102.5 (say 4103.5), entry ~4098.3. Target 4071. R = (4098.3 - 4071) / 5.2 = 5.25.
# Let's verify M5 at 01:30: H:4102.51, C:4098.335. Solid rejection.

c2 = {
  "candidate_id": "candidate_2026-06-24T01-30-00Z_short_liquidity_sweep_reversal",
  "timestamp": "2026-06-24T01:30:00Z",
  "decision": "accepted",
  "reason": "M5 pinbar rejection at 4102.5 after retracing into premium, forming M30 lower high.",
  "htf_context": "H4 bearish momentum continues. Asian session lower high formation.",
  "m30_structure": "M30 strong drop to 4068, retracement to 4102 creating a lower high before continuation.",
  "m15_m5_execution": "M5 pinbar at 01:30 closing 4098.3. Price follows through to new lows.",
  "entry": 4098.3,
  "stop": 4103.5,
  "target": 4071.0,
  "r": 5.25,
  "promoted_to_journal": True
}

# Evaluate Candidate 3: 2026-06-24T02:00:00Z
# At 02:00, M5 O:4088.96 H:4096.335 L:4086.335 C:4087.545
# This is a sweep of local highs (01:45 H:4093, 01:55 H:4090).
# But it's in the middle of a leg down from 4102. It's valid as continuation.
# Wait, entry 4087.5, SL 4097, Target 4052. R = (4087.5 - 4052) / 9.5 = 3.7.
# But M30 shows this is just a minor pullback on M5. Given we accepted the 01:30, this is a scale-in.
# Let's keep it conservative and shadow it since the primary move started at 01:30.

c3 = {
  "candidate_id": "candidate_2026-06-24T02-00-00Z_short_liquidity_sweep_reversal",
  "timestamp": "2026-06-24T02:00:00Z",
  "decision": "shadow",
  "reason": "Valid continuation M5 sweep, but primary setup already triggered at 01:30.",
  "htf_context": "H4 bearish trend.",
  "m30_structure": "M30 continuation downward.",
  "m15_m5_execution": "M5 sweep of local highs to 4096.3 before resuming downtrend, but overlaps with prior trade.",
  "entry": 4087.5,
  "stop": 4097.5,
  "target": 4052.0,
  "r": 3.5,
  "promoted_to_journal": False
}

out_file = 'manual_replay_results/replay_result_00693_00696.json'
with open(out_file, 'w') as f:
    json.dump({"results": [c1, c2, c3]}, f, indent=2)

prog['cursor']['next_index'] = 696
prog['cursor']['processed_count'] = 696
prog['cursor']['accepted_count'] += 2
prog['cursor']['shadow_count'] += 1
prog['cursor']['last_candidate_id'] = c3['candidate_id']
prog['runs'][0]['result_files'].append(out_file)
prog['runs'][0]['decision_counts']['accepted'] += 2
prog['runs'][0]['decision_counts']['shadow'] += 1

with open(progress_file, 'w') as f:
    json.dump(prog, f, indent=2)

print(f"Processed 3 items. Next index: 696. Decisions: 2 accepted, 1 shadow. File: {out_file}")
