//@version=5 strategy("1H -> 1M Strategy [Backtest Fixed]", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10) // ========================================== // 1. INPUTS // ========================================== grp_time = "Session & Time Settings (EST)" tz_input = input.string("America/New_York", "Timezone", group=grp_time) grp_risk = "Risk Management" rr_ratio = input.float(2.0, "Risk-to-Reward Ratio", minval=0.5, step=0.1, group=grp_risk) // ========================================== // 2. SESSION LOGIC // ========================================== in_execution_window = not na(time(timeframe.period, "1100-1200:23456", tz_input)) // ========================================== // 3. HTF CONTEXT (1H) // ========================================== // Fetch confirmed closed 1H candles using [1] offset to avoid repainting/backtest issues [h1_time, h1_open, h1_high, h1_low, h1_close] = request.security(syminfo.tickerid, "60", [time[1], open[1], high[1], low[1], close[1]], barmerge.gaps_off, barmerge.lookahead_on) [h1_prev_high, h1_prev_low] = request.security(syminfo.tickerid, "60", [high[2], low[2]], barmerge.gaps_off, barmerge.lookahead_on) var bool h1_bullish_context = false var bool h1_bearish_context = false // Reset daily if hour(time, tz_input) == 0 and minute(time, tz_input) == 0 h1_bullish_context := false h1_bearish_context := false // Evaluate Context right after 10:00 AM EST candle closes (11:00 AM EST) if hour(time, tz_input) == 11 and minute(time, tz_input) == 0 swept_low_reversal = (h1_low < h1_prev_low) and (h1_close > h1_prev_low) swept_high_reversal = (h1_high > h1_prev_high) and (h1_close < h1_prev_high) bullish_expansion = (h1_close > h1_open) and (h1_close > h1_prev_high) bearish_expansion = (h1_close < h1_open) and (h1_close < h1_prev_low) h1_bullish_context := swept_low_reversal or bullish_expansion h1_bearish_context := swept_high_reversal or bearish_expansion // ========================================== // 4. LOWER TIMEFRAME EXECUTION (1M - CISD) // ========================================== is_down_candle = close[1] < open[1] cisd_bullish = is_down_candle and (close > high[1]) is_up_candle = close[1] > open[1] cisd_bearish = is_up_candle and (close < low[1]) // ========================================== // 5. STRATEGY EXECUTION // ========================================== var float entry_price = na var float stop_loss = na var float take_profit = na if in_execution_window and strategy.position_size == 0 if h1_bullish_context and cisd_bullish entry_price := close stop_loss := low[1] sl_dist = entry_price - stop_loss take_profit := entry_price + (sl_dist * rr_ratio) strategy.entry("Long", strategy.long) strategy.exit("TP/SL Long", "Long", stop=stop_loss, limit=take_profit) if h1_bearish_context and cisd_bearish entry_price := close stop_loss := high[1] sl_dist = stop_loss - entry_price take_profit := entry_price - (sl_dist * rr_ratio) strategy.entry("Short", strategy.short) strategy.exit("TP/SL Short", "Short", stop=stop_loss, limit=take_profit) if hour(time, tz_input) == 12 and minute(time, tz_input) == 0 strategy.close_all(comment="Window Closed") bgcolor(in_execution_window ? color.new(color.purple, 90) : na)