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 “Heikin Ashi Oscillator Trend Engine,” we will architect a professional-grade, automated execution framework. The original script is a sophisticated indicator that provides a wealth of visual information; our task is to distill this into a set of precise, non-discretionary rules for live trading.

The core signal we will build upon is the Adaptive MA Crossover (fastLine and slowLine), as it is designed to be the primary momentum-shift detector within the oscillator’s engine.

1. Execution Triggers (Entry & Direction)

The transition from a visual indicator to an execution system requires absolute clarity on what constitutes a trade signal. We will use the adaptive crossover on the oscillator as our primary trigger, refined with a momentum filter to avoid entering at potential exhaustion points.

2. Multi-Tiered Exit Logic

A robust exit strategy is more critical than a perfect entry. We will move away from simplistic exits and implement a multi-layered system to manage risk and protect profits dynamically.

3. Capital Allocation & Risk Management

Professional trading is defined by its risk management protocol. We will implement a fixed-fractional position sizing model to ensure consistent risk exposure on every trade.

4. Implementation Snippet (Pine Logic)

The following code block demonstrates how to transform the indicator into a strategy by incorporating the architectural components defined above. This snippet should be integrated into the main body of the original script after all the oscillator calculations are complete.

// ==============================================================================
// 🟢 REGION: STRATEGY EXECUTION FRAMEWORK
// ==============================================================================

// ──────────────────────────────────────────────
// 🟦 Strategy Declaration & Inputs
// ──────────────────────────────────────────────
strategy("HA Oscillator Execution Engine", 
     overlay=true, // Plot trades on the main chart
     pyramiding=0, // No pyramiding by default
     initial_capital=10000, 
     default_qty_type=strategy.fixed, 
     commission_type=strategy.commission.percent, 
     commission_value=0.075, // Realistic commission (e.g., 0.075%)
     slippage=2, // Realistic slippage in ticks
     process_orders_on_close=true) // Execute on bar close for reliability

// Risk Management Inputs
risk_pct = input.float(1.0, "Risk per Trade %", group="Risk Management", minval=0.1, maxval=5.0)
atr_len = input.int(14, "ATR Length for Stop Loss", group="Risk Management")
atr_mult = input.float(2.0, "ATR Multiplier for Stop Loss", group="Risk Management")
tp1_rr = input.float(1.5, "Take Profit 1 R:R", group="Risk Management")
stagnation_bars = input.int(50, "Max Bars in Trade (Stagnation Exit)", group="Risk Management")

// ──────────────────────────────────────────────
// 🟦 Risk & Position Sizing Calculation
// ──────────────────────────────────────────────
atr_val = ta.atr(atr_len)
stop_loss_distance = atr_val * atr_mult

// Calculate position size based on fixed-fractional risk
risk_amount = (strategy.equity * risk_pct) / 100
position_size = risk_amount / stop_loss_distance

// ──────────────────────────────────────────────
// 🟦 Entry & Exit Conditions
// ──────────────────────────────────────────────
// Entry Conditions (from Section 1)
long_condition = ta.crossover(fastLine, slowLine) and osc < upperGuide
short_condition = ta.crossunder(fastLine, slowLine) and osc > lowerGuide

// Exit Conditions
exit_long_trail = ta.crossunder(osc, slowLine)
exit_short_trail = ta.crossover(osc, slowLine)
stagnation_exit = barssince(strategy.opentrades > 0) > stagnation_bars

// ──────────────────────────────────────────────
// 🟦 Strategy Execution Logic
// ──────────────────────────────────────────────
// Only trade if not already in a position (for a clean, non-reversing system)
// To enable close-and-reverse, remove the strategy.opentrades == 0 check.
if (strategy.opentrades == 0)
    // Long Entry
    if (long_condition)
        // Define TP1 price based on R:R
        tp1_price_long = strategy.position_avg_price + (stop_loss_distance * tp1_rr)
        strategy.entry("Long", strategy.long, qty=position_size)
        // Use strategy.exit to place both SL and TP orders for 50% of the position
        strategy.exit("Long TP1/SL", from_entry="Long", qty_percent=50, limit=tp1_price_long, stop=strategy.position_avg_price - stop_loss_distance)
        // Place a stop loss for the remaining 50%
        strategy.exit("Long SL2", from_entry="Long", qty_percent=100, stop=strategy.position_avg_price - stop_loss_distance)

    // Short Entry
    if (short_condition)
        // Define TP1 price based on R:R
        tp1_price_short = strategy.position_avg_price - (stop_loss_distance * tp1_rr)
        strategy.entry("Short", strategy.short, qty=position_size)
        // Use strategy.exit to place both SL and TP orders for 50% of the position
        strategy.exit("Short TP1/SL", from_entry="Short", qty_percent=50, limit=tp1_price_short, stop=strategy.position_avg_price + stop_loss_distance)
        // Place a stop loss for the remaining 50%
        strategy.exit("Short SL2", from_entry="Short", qty_percent=100, stop=strategy.position_avg_price + stop_loss_distance)

// Trailing and Stagnation Exit Logic
if (strategy.position_size > 0) // If in a long position
    if (exit_long_trail or stagnation_exit)
        strategy.close("Long", comment = exit_long_trail ? "Trail Exit" : "Stagnation")

if (strategy.position_size < 0) // If in a short position
    if (exit_short_trail or stagnation_exit)
        strategy.close("Short", comment = exit_short_trail ? "Trail Exit" : "Stagnation")

// EOD Exit Logic (Example for assets with a session)
is_eod_exit_time = (hour(time, "America/New_York") == 15 and minute(time, "America/New_York") >= 45)
if (is_eod_exit_time)
    strategy.close_all(comment="EOD Exit")

// ==============================================================================
// 🔴 END REGION: STRATEGY EXECUTION FRAMEWORK
// ==============================================================================