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

Based on the provided “Scalper Pro 3 Min Gold” indicator, here is the architectural blueprint for its transformation into a production-grade automated execution framework.

1. Execution Triggers (Entry & Direction)

The core logic of the indicator is a breakout from a consolidation zone, confirmed by market structure. We will translate these visual cues into precise, machine-executable commands.

Execution Nuances

2. Multi-Tiered Exit Logic

The original script’s fixed Risk:Reward target is a good starting point but lacks adaptability. A professional system requires a more dynamic and layered exit strategy to manage risk and capitalize on momentum.

3. Capital Allocation & Risk Management

This is the most critical component in transitioning from a signal generator to a trading business. We will implement a fixed-fractional position sizing model.

4. Implementation Snippet (Pine Logic)

This snippet demonstrates the conversion of the indicator into a strategy incorporating the professional-grade features discussed above.

//@version=5
// STRATEGY CONVERSION: From Indicator to Execution Engine
strategy("Scalper Pro 3 Min Gold [ARCHITECT]", 
     overlay=true, 
     pyramiding=0, // No pyramiding allowed
     initial_capital=10000,
     default_qty_type=strategy.calculated, // Position size determined by our logic
     commission_type=strategy.commission.cash_per_order,
     commission_value=2.5, // Example: $2.50 per order (entry/exit)
     slippage=2) // Example: 2 ticks of slippage per order

// --- INPUTS ---
// Risk Management
riskPercent = input.float(1.0, title="Risk Per Trade (%)", minval=0.1, maxval=5.0)
// Original Logic Inputs
targetMult = input.float(1.5, title="Target 1 Multiplier (R:R)")
pivotLength = input.int(3, title="Structure Pivot Length")
// Filters
useConsolidation = input.bool(true, title="Require Consolidation Before Breakout", group="Filters")
consLength = input.int(10, title="Consolidation Length (Bars)", group="Filters")
atrMult = input.float(3.0, title="Max Range (ATR Multiplier)", group="Filters")
useCooldown = input.bool(true, title="Enable Cooldown Between Trades", group="Filters")
cooldownBars = input.int(30, title="Cooldown Length (Bars)", group="Filters")
// Exit Logic
useEodExit = input.bool(true, "Use End of Day Exit?", group="Exits")
eodTime = input.session("1645-1700", "EOD Exit Time", group="Exits") // Closes positions 15 mins before market close

// --- CORE LOGIC (Unchanged from original) ---
var float lastPivotHigh = na
var float lastPivotLow = na
var int lastTradeEntryBar = na

ph = ta.pivothigh(high, pivotLength, pivotLength)
pl = ta.pivotlow(low, pivotLength, pivotLength)
if not na(ph)
    lastPivotHigh := ph
if not na(pl)
    lastPivotLow := pl

pastHigh = ta.highest(high[1], consLength)
pastLow = ta.lowest(low[1], consLength)
currentAtr = ta.atr(14)
isConsolidating = useConsolidation ? (pastHigh - pastLow <= (currentAtr * atrMult)) : true
canEnter = useCooldown ? (na(lastTradeEntryBar) or (bar_index - lastTradeEntryBar >= cooldownBars)) : true

bullishBreakout = close > open and ta.crossover(close, lastPivotHigh) and low > lastPivotLow and isConsolidating and canEnter
bearishBreakout = close < open and ta.crossunder(close, lastPivotLow) and high < lastPivotHigh and isConsolidating and canEnter

// --- EXECUTION ENGINE ---

// Check if we are in a position
bool inTrade = strategy.position_size != 0
bool isFlat = strategy.position_size == 0
bool isTimeForEodExit = useEodExit and time(timeframe.period, eodTime)

// Position Sizing Calculation
float calculatePositionSize(float stopPrice) =>
    float riskPerUnit = math.abs(close - stopPrice)
    if riskPerUnit == 0
        0 // Avoid division by zero
    else
        float dollarRisk = (riskPercent / 100.0) * strategy.equity
        dollarRisk / riskPerUnit

// --- ENTRY LOGIC ---
if (isFlat)
    // Long Entry
    if (bullishBreakout)
        float stopPrice = lastPivotLow
        float targetPrice = close + (math.abs(close - stopPrice) * targetMult)
        float qty = calculatePositionSize(stopPrice)
        
        if qty > 0
            strategy.entry("Long", strategy.long, qty=qty)
            strategy.exit("Long Exit", from_entry="Long", stop=stopPrice, limit=targetPrice)
            lastTradeEntryBar := bar_index

    // Short Entry
    if (bearishBreakout)
        float stopPrice = lastPivotHigh
        float targetPrice = close - (math.abs(close - stopPrice) * targetMult)
        float qty = calculatePositionSize(stopPrice)

        if qty > 0
            strategy.entry("Short", strategy.short, qty=qty)
            strategy.exit("Short Exit", from_entry="Short", stop=stopPrice, limit=targetPrice)
            lastTradeEntryBar := bar_index

// --- EXIT LOGIC ---
// End of Day Exit
if (inTrade and isTimeForEodExit)
    strategy.close_all(comment="EOD Exit")

// NOTE: The multi-stage exit with trailing ATR requires more complex state management
// and is best implemented using `var` variables to track the state of the trade (e.g., if TP1 was hit).
// The `strategy.exit` call above represents the initial bracket order for Stop Loss and the FIRST Take Profit.
// A more advanced implementation would cancel this and submit a new trailing stop order once TP1 is filled.