//@version=5
strategy("Triple Pattern with Round Level Entry", overlay=true)

// Inputs
min_move_pips = input.int(100, "Minimum Move (Pips)")
target_extension_min = input.int(150, "Target Extension Min (Pips)")
target_extension_max = input.int(200, "Target Extension Max (Pips)")
stop_loss_pips = input.int(50, "Stop Loss (Pips)")
take_profit_pips = input.int(150, "Take Profit (Pips)")
recovery_threshold = input.float(0.8, "Recovery Threshold")
short_sl_pips = input.int(30, "Short Stop Loss (Pips)")
short_tp_pips = input.int(40, "Short Take Profit (Pips)")

// Pip conversion
pip_value = syminfo.mintick * (syminfo.type == "forex" ? 10 : 1)

// Variables
var sunday_open = 0.0
var weekly_high = 0.0
var weekly_low = 0.0
var drop_low = 0.0
var phase1_complete = false
var phase2_complete = false
var phase3_complete = false
var phase4_complete = false
var next_round_level = 0.0
var round_level_crossed = false

// Reset on new week
is_new_week = ta.change(time("1W")) != 0
if is_new_week
    sunday_open := open
    weekly_high := high
    weekly_low := low
    drop_low := 0.0
    phase1_complete := false
    phase2_complete := false
    phase3_complete := false
    phase4_complete := false
    next_round_level := 0.0
    round_level_crossed := false

// Update weekly levels
if not is_new_week
    weekly_high := math.max(weekly_high, high)
    weekly_low := math.min(weekly_low, low)

current_day = dayofweek(time)

// Phase detection
if not phase1_complete and current_day >= 1 and current_day <= 3 and sunday_open > 0
    move_from_sunday = (high - sunday_open) / pip_value
    if move_from_sunday >= min_move_pips
        phase1_complete := true

if phase1_complete and not phase2_complete and sunday_open > 0
    extension_from_sunday = (high - sunday_open) / pip_value
    if extension_from_sunday >= target_extension_min and extension_from_sunday <= target_extension_max
        phase2_complete := true

if phase2_complete and not phase3_complete and weekly_high > 0
    reversal_from_high = (weekly_high - low) / pip_value
    if reversal_from_high >= min_move_pips
        phase3_complete := true
        drop_low := low

if phase3_complete and not phase4_complete and weekly_high > 0 and drop_low > 0
    total_drop_distance = weekly_high - drop_low
    current_recovery = high - drop_low
    recovery_percentage = current_recovery / total_drop_distance
    recovery_pips = (high - drop_low) / pip_value
    if recovery_percentage >= recovery_threshold and recovery_pips >= min_move_pips
        phase4_complete := true

// Calculate next round 100-pip level after pattern completion
if phase4_complete and next_round_level == 0.0
    current_price = close
    // Find next round 100-pip level (0.0100 increments for forex)
    current_major = math.floor(current_price * 100) / 100
    next_round_level := current_major + 0.0100

// Check if round level has been crossed
if phase4_complete and next_round_level > 0.0 and not round_level_crossed
    if high >= next_round_level
        round_level_crossed := true

// Entry condition - SHORT at 0.015 level after round level cross
short_condition = false
entry_price_target = 0.0

if phase4_complete and round_level_crossed and next_round_level > 0.0
    entry_price_target := next_round_level + (15 * pip_value)
    // Trigger short when price reaches the 15 pip level above round number
    if high >= entry_price_target and strategy.position_size == 0
        short_condition := true

// Execute SHORT trades
if short_condition
    sl_price = entry_price_target + (short_sl_pips * pip_value)
    tp_price = entry_price_target - (short_tp_pips * pip_value)
    
    strategy.entry("Short", strategy.short, stop=entry_price_target)
    strategy.exit("Exit Short", "Short", stop=sl_price, limit=tp_price)

// Visual markers
bgcolor(phase1_complete ? color.new(color.yellow, 90) : na, title="Phase 1")
bgcolor(phase2_complete ? color.new(color.orange, 90) : na, title="Phase 2") 
bgcolor(phase3_complete ? color.new(color.blue, 90) : na, title="Phase 3")
bgcolor(phase4_complete ? color.new(color.lime, 90) : na, title="Phase 4")

// Plot round levels and entry points
plot(phase4_complete and next_round_level > 0.0 ? next_round_level : na, "Round Level", color.purple, 2)
plot(phase4_complete and entry_price_target > 0.0 ? entry_price_target : na, "Entry Target", color.red, 2, plot.style_circles)

// Entry signals
plotshape(short_condition, "Short Entry", shape.triangledown, location.abovebar, color.red, size=size.normal)
plotshape(round_level_crossed and phase4_complete, "Round Level Crossed", shape.diamond, location.belowbar, color.purple, size=size.small)

// Labels for debugging
if phase4_complete and next_round_level > 0.0
    label.new(bar_index, high, "Round: " + str.tostring(next_round_level) + "\nEntry: " + str.tostring(entry_price_target), 
              color=color.white, textcolor=color.black, style=label.style_label_down, size=size.small)