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 transformation of the Hurst Exponent Adaptive Supertrend indicator into a production-ready algorithmic trading framework.

1. Execution Triggers (Entry & Direction)

The core of the entry logic is the trend flip, identified by the turnedBullish and turnedBearish boolean variables. We will translate these signals into concrete order execution commands.

2. Multi-Tiered Exit Logic

A simple reversal system is brittle. A robust framework requires a layered defense of profits and capital.

3. Capital Allocation & Risk Management

Position sizing is not an afterthought; it is central to survival and profitability. We will move from fixed-lot trading to a dynamic risk-based model.

4. Implementation Snippet (Pine Logic)

This snippet demonstrates the conversion of the indicator into a strategy incorporating the professional execution logic described above.

//@version=5
// --- STRATEGY DECLARATION ---
strategy("Hurst Adaptive Supertrend [Execution Framework]",
     overlay=true,
     process_orders_on_close=true, // IMPORTANT: Execute on next bar's open
     initial_capital=100000,
     commission_type=strategy.commission.percent,
     commission_value=0.075, // Realistic commission (0.075%)
     slippage=2, // Realistic slippage in ticks
     pyramiding=0, // Set to > 0 to enable pyramiding rules
     default_qty_type=strategy.percent_of_equity, // Fallback sizing
     default_qty_value=10)

// --- RISK MANAGEMENT INPUTS ---
riskPercent = input.float(1.0, "Risk per Trade %", minval=0.1, maxval=10, step=0.1, group="Risk Management")
useEodExit  = input.bool(true, "Use End-of-Day Exit?", group="Risk Management")
eodTime     = input.session("1530-1600", "EOD Exit Time", group="Risk Management")

// --- PASTE ORIGINAL SCRIPT'S CALCULATION LOGIC HERE ---
// (Inputs, Hurst Exponent, Kalman Smoother, Trend Band, Trend States)
// ... [Original code from line 21 to 141] ...
// For brevity, we assume the original calculations for 'trend', 'turnedBullish', 'turnedBearish', and 'trendLine' are present.

// --- EXECUTION LOGIC ---

// Determine if the EOD exit condition is met
bool isEod = time(timeframe.period, eodTime) and not time(timeframe.period, eodTime)[1]

// Calculate Position Size based on Risk
stopLossLevel = trendLine[1] // Stop is the trendline of the signal bar
riskPerUnit   = math.abs(close - stopLossLevel)
riskAmount    = (riskPercent / 100) * strategy.equity
positionSize  = riskAmount / riskPerUnit

// --- ENTRY CONDITIONS ---
if (turnedBullish)
    // Close any existing short position before entering long
    strategy.close("Short", comment="Close Short for Reverse")
    // Enter new long position with calculated size
    strategy.entry("Long", strategy.long, qty=positionSize, comment="Enter Long")

if (turnedBearish)
    // Close any existing long position before entering short
    strategy.close("Long", comment="Close Long for Reverse")
    // Enter new short position with calculated size
    strategy.entry("Short", strategy.short, qty=positionSize, comment="Enter Short")

// --- EXIT CONDITIONS ---

// 1. Trailing Stop Loss based on the indicator's trendline
if (strategy.position_size > 0)
    strategy.exit("Exit Long", from_entry="Long", stop=trendLine)

if (strategy.position_size < 0)
    strategy.exit("Exit Short", from_entry="Short", stop=trendLine)

// 2. End-of-Day Exit
if (useEodExit and isEod)
    strategy.close_all(comment="EOD Exit")

// --- VISUALIZATION (Unchanged from original) ---
plot(trendLine, color=trendCol, linewidth=4, style=plot.style_line, title="Hurst Exponent Adaptive Supertrend")