Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Indicators to Strategy Blueprint

The provided “Smart Levels Pro” script is an excellent visualization tool, identifying critical price levels based on previous daily, weekly, monthly, and session data. However, as an indicator, it generates no trades. To transform this into a production-ready execution framework, we must define a concrete trading hypothesis based on these levels and then build a robust engine to manage entries, exits, and risk.

The chosen strategy will be a “Break and Retest” of key levels. This is a classic pattern that aims to filter out false breakouts by waiting for the market to confirm the level has flipped from resistance to support (for longs) or support to resistance (for shorts).

1. Execution Triggers (Entry & Direction)

The core premise is to trade the first confirmed retest of the Previous Day’s High (PDH) or Previous Day’s Low (PDL). These levels are often the most relevant for intraday price action.

Execution Nuances:

2. Multi-Tiered Exit Logic

A professional exit strategy is not a single rule but a combination of logic to protect capital and maximize gains.

3. Capital Allocation & Risk Management

Position sizing is the most critical component for long-term survival. We will implement a dynamic, risk-based sizing model.

4. Implementation Snippet (Pine Logic)

This snippet demonstrates the transformation from an indicator to a strategy, incorporating the professional execution logic defined above. It omits the visual components (dashboard, labels) to focus purely on the trading engine.

//@version=5
// --- STRATEGY DECLARATION ---
strategy("Smart Levels Pro - Execution Engine",
     overlay=true,
     initial_capital=100000,
     commission_type=strategy.commission.percent,
     commission_value=0.04, // Example for futures/forex broker
     slippage=2, // 2 ticks of slippage
     pyramiding=0, // No pyramiding in this version
     default_qty_type=strategy.fixed,
     process_orders_on_close=true) // Ensures execution on bar close

// --- INPUTS FOR STRATEGY ---
grp_risk = "Risk Management"
riskPercent    = input.float(1.0, "Risk per Trade (%)", minval=0.1, maxval=5, step=0.1, group=grp_risk)
atrLength      = input.int(14, "ATR Length", group=grp_risk)
atrMultiplier  = input.float(2.0, "ATR Stop Multiplier", group=grp_risk)
useEodExit     = input.bool(true, "Use End-of-Day Exit?", group=grp_risk)
eodHour        = input.int(20, "EOD Exit Hour (GMT)", minval=0, maxval=23, group=grp_risk)
eodMinute      = input.int(45, "EOD Exit Minute (GMT)", minval=0, maxval=59, group=grp_risk)

// --- LEVEL CALCULATION (Simplified from original script) ---
is_new_day   = dayofmonth != dayofmonth[1]
is_new_week  = weekofyear != weekofyear[1]

var float pd_high = na, var float pd_low = na
var float pw_high = na, var float pw_low = na

[d_high, d_low] = request.security(syminfo.tickerid, "D", [high[1], low[1]], lookahead=barmerge.lookahead_off)
if is_new_day
    pd_high := d_high
    pd_low  := d_low

[w_high, w_low] = request.security(syminfo.tickerid, "W", [high[1], low[1]], lookahead=barmerge.lookahead_off)
if is_new_week
    pw_high := w_high
    pw_low  := w_low

// --- CORE STRATEGY LOGIC ---
atrValue = ta.atr(atrLength)

// 1. ENTRY TRIGGERS (Break & Retest Logic)
longEntryCondition = low[1] < pd_high and close[1] > pd_high and strategy.position_size == 0 and not na(pd_high)
shortEntryCondition = high[1] > pd_low and close[1] < pd_low and strategy.position_size == 0 and not na(pd_low)

// 2. RISK MANAGEMENT & POSITION SIZING
var float stopLossPrice = na
var float takeProfitPrice = na

if (longEntryCondition)
    stopLossPrice   := close - (atrValue * atrMultiplier)
    takeProfitPrice := pw_high // Target the previous week's high
    
    // Calculate position size based on 1% equity risk
    riskPerUnit     = (close - stopLossPrice) * syminfo.pointvalue
    equityToRisk    = strategy.equity * (riskPercent / 100)
    positionSize    = equityToRisk / riskPerUnit
    
    strategy.entry("Long", strategy.long, qty=positionSize)
    // Place a multi-stage exit order: 50% at TP1, rest managed by SL
    strategy.exit("Exit Long TP1/SL", from_entry="Long", qty_percent=50, limit=takeProfitPrice, stop=stopLossPrice)
    // This second exit order manages the stop for the remaining 50%
    strategy.exit("Exit Long SL2", from_entry="Long", stop=stopLossPrice)

if (shortEntryCondition)
    stopLossPrice   := close + (atrValue * atrMultiplier)
    takeProfitPrice := pw_low // Target the previous week's low

    riskPerUnit     = (stopLossPrice - close) * syminfo.pointvalue
    equityToRisk    = strategy.equity * (riskPercent / 100)
    positionSize    = equityToRisk / riskPerUnit

    strategy.entry("Short", strategy.short, qty=positionSize)
    strategy.exit("Exit Short TP1/SL", from_entry="Short", qty_percent=50, limit=takeProfitPrice, stop=stopLossPrice)
    strategy.exit("Exit Short SL2", from_entry="Short", stop=stopLossPrice)

// 3. TIME-BASED EXIT
isEod = useEodExit and (hour(time, "GMT") == eodHour) and (minute(time, "GMT") >= eodMinute)
if (isEod)
    strategy.close_all(comment="EOD Exit")

// --- PLOTTING FOR VISUALIZATION ---
plot(pd_high, "PDH", color.new(color.blue, 0), style=plot.style_linebr)
plot(pd_low, "PDL", color.new(color.red, 0), style=plot.style_linebr)
plot(pw_high, "PWH", color.new(color.orange, 40), style=plot.style_circles)
plot(pw_low, "PWL", color.new(color.purple, 40), style=plot.style_circles)