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

Here is the architectural breakdown for transitioning the “1M Smart Scalping” indicator into a production-grade automated execution framework.

1. Execution Triggers (Entry & Direction)

The provided script’s logic is based on a confluence of factors confirming a trend continuation after a brief pause. The execution must be precise to capture the intended momentum.

2. Multi-Tiered Exit Logic

A scalping strategy’s profitability is defined more by its exit discipline than its entries. The following tiered exit system provides a robust defense and profit-capture mechanism.

3. Capital Allocation & Risk Management

Position sizing is the engine of risk control. We will move from fixed-lot thinking to a dynamic, risk-based model.

4. Implementation Snippet (Pine Logic)

This snippet demonstrates the conversion of the indicator into a professional strategy, incorporating the architectural components discussed above.

//@version=5
// 1. STRATEGY DECLARATION WITH REALISTIC FRICTION
strategy("1M Smart Scalping - Execution Framework", 
     overlay=true, 
     process_orders_on_close=true, // Execute on the open of the next bar
     slippage=2, // 2 ticks of slippage for market orders
     commission_type=strategy.commission.percent, 
     commission_value=0.04) // Realistic commission for retail brokers

// =======================
// ⚙️ INPUTS FOR OPTIMIZATION
// =======================
// Risk Management
risk_per_trade = input.float(1.0, "Risk per Trade (%)", minval=0.1, maxval=5.0) / 100
// Exit Logic
atr_period = input.int(14, "ATR Period")
sl_atr_multiplier = input.float(1.5, "Stop Loss ATR Multiplier", minval=0.5)
tp_rr_ratio = input.float(1.5, "Take Profit R:R Ratio", minval=0.5)
stagnation_bars = input.int(15, "Max Bars in Trade")

// Original Logic Inputs
pivot_len = 5

// =======================
// 🔁 CORE LOGIC (FROM ORIGINAL SCRIPT)
// =======================
// No-Repaint ZigZag
ph = ta.pivothigh(high, pivot_len, pivot_len)
pl = ta.pivotlow(low, pivot_len, pivot_len)
var float last_high = na, var float prev_high = na, var float last_low = na, var float prev_low = na
if not na(ph) { prev_high := last_high; last_high := ph }
if not na(pl) { prev_low := last_low; last_low := pl }

// Trend
trend_up = not na(prev_low) and last_low > prev_low
trend_down = not na(prev_high) and last_high < prev_high

// S&R
support = ta.lowest(low, 10)[1]
resistance = ta.highest(high, 10)[1]
atr = ta.atr(atr_period)
sr_distance = atr * 0.5
near_support = math.abs(close - support) < sr_distance
near_resistance = math.abs(close - resistance) < sr_distance

// Candle & Breakout
bull = close > open and (close - open) > (high - low) * 0.5
bear = open > close and (open - close) > (high - low) * 0.5
breakout_up = high > ta.highest(high, 5)[1]
breakout_down = low < ta.lowest(low, 5)[1]

// Final Signals
long_condition = timeframe.period == "1" and trend_up and bull[2] and bear[1] and bull and breakout_up and not near_resistance
short_condition = timeframe.period == "1" and trend_down and bear[2] and bull[1] and bear and breakout_down and not near_support

// =======================
// 💰 CAPITAL ALLOCATION & RISK MANAGEMENT
// =======================
// Calculate Stop Loss Price *before* entry to determine size
long_stop_price = low[1] - (atr * sl_atr_multiplier)
short_stop_price = high[1] + (atr * sl_atr_multiplier)

// Risk-based position sizing
risk_per_unit_long = close - long_stop_price
risk_per_unit_short = short_stop_price - close
position_size_long = (strategy.equity * risk_per_trade) / risk_per_unit_long
position_size_short = (strategy.equity * risk_per_trade) / risk_per_unit_short

// =======================
// 📈 EXECUTION ENGINE
// =======================
// --- ENTRY LOGIC ---
if (long_condition)
    // Close any existing short and go long
    strategy.close("Short", comment="Short Reverse")
    strategy.entry("Long", strategy.long, qty=position_size_long)

if (short_condition)
    // Close any existing long and go short
    strategy.close("Long", comment="Long Reverse")
    strategy.entry("Short", strategy.short, qty=position_size_short)

// --- EXIT LOGIC ---
// Set SL/TP for the long position
if strategy.position_size > 0
    long_tp_price = strategy.position_avg_price + (risk_per_unit_long * tp_rr_ratio)
    strategy.exit("Long Exit", from_entry="Long", stop=long_stop_price, limit=long_tp_price)

// Set SL/TP for the short position
if strategy.position_size < 0
    short_tp_price = strategy.position_avg_price - (risk_per_unit_short * tp_rr_ratio)
    strategy.exit("Short Exit", from_entry="Short", stop=short_stop_price, limit=short_tp_price)

// Time-based Stagnation Exit
if barssince(strategy.opentrades > 0) > stagnation_bars
    strategy.close_all(comment="Stagnation Exit")

// End of Session Exit (Example: Close all trades at 20:45 UTC)
is_eod = hour(time_close) == 20 and minute(time_close) >= 45
if is_eod
    strategy.close_all(comment="End of Session")

// Plotting for visual confirmation
plot(strategy.position_size > 0 ? long_stop_price : na, "Long SL", color.red, style=plot.style_linebr)
plot(strategy.position_size < 0 ? short_stop_price : na, "Short SL", color.red, style=plot.style_linebr)