VWAP & 9 EMA A+ Trend Strategy [4 ATR Lock]-ECFx_FuloMath

ECFx_Legacy · strategy · 89 行 · 点赞 19 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
strategy("VWAP & 9 EMA A+ Trend Strategy [4 ATR Lock]", overlay=true, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
 
// --- Inputs ---
emaLength     = input.int(9, title="Fast EMA Length", group="Indicator Settings")
macroLen      = input.int(200, title="Macro Trend Filter EMA", group="Indicator Settings")
useMacro      = input.bool(true, title="Filter by Macro Trend (200 EMA)?", group="Indicator Settings")
 
// Filters
useAdx        = input.bool(true, title="Use ADX Trend Strength Filter?", group="Filter Settings")
adxLen        = input.int(14, title="ADX Length", group="Filter Settings")
adxThreshold  = input.float(20.0, title="Minimum ADX Threshold", group="Filter Settings")
 
// Time-of-Day (TOD) Session Filter
useTOD        = input.bool(true, title="Use Time-of-Day (TOD) Session Filter?", group="Session Settings")
sessionInput  = input.session("0700-1600:23456", title="Active Trading Session (UTC)", group="Session Settings")
 
// Risk Management & 4 ATR Lock Inputs
atrLen        = input.int(14, title="ATR Length", group="Risk Management")
slMult        = input.float(2.0, title="Initial Stop Loss ATR Multiplier", group="Risk Management")
tpMult        = input.float(8.0, title="Take Profit ATR Multiplier", group="Risk Management")
 
useLock       = input.bool(true, title="Enable 4 ATR Profit Lock?", group="Profit Lock Settings")
lockTrigger   = input.float(4.0, title="Trigger Distance (in ATR)", group="Profit Lock Settings", tooltip="Price must move this many ATRs in your favor to activate the 4 ATR trailing lock.")
lockTrail     = input.float(4.0, title="Lock Trail Distance (in ATR)", group="Profit Lock Settings", tooltip="Distance behind price the stop-loss is held once triggered.")
 
// --- Calculations ---
emaValue      = ta.ema(close, emaLength)
macroEma      = ta.ema(close, macroLen)
vwapValue     = ta.vwap(close)
 
// ADX Calculation
dir_g = ta.change(high) > (ta.change(low) < 0 ? 0 : ta.change(low)) ? ta.change(high) : 0
sum_g = ta.rma(dir_g > 0 ? dir_g : 0, adxLen)
sum_l = ta.rma(ta.change(low) < 0 ? -ta.change(low) : 0, adxLen)
tr_s  = ta.rma(ta.atr(1), adxLen)
plus  = fixnan(100 * sum_g / tr_s)
minus = fixnan(100 * sum_l / tr_s)
sum   = plus + minus
adx   = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxLen)
 
adxFilter  = not useAdx or (adx > adxThreshold)
macroLong  = not useMacro or (close > macroEma)
macroShort = not useMacro or (close < macroEma)
inSession  = not useTOD or not na(time(timeframe.period, sessionInput))
 
// --- Crossover Signals with Filters ---
longCondition  = ta.crossover(emaValue, vwapValue) and macroLong and adxFilter and inSession
shortCondition = ta.crossunder(emaValue, vwapValue) and macroShort and adxFilter and inSession
 
// --- Strategy Execution ---
if longCondition
    strategy.entry("Long", strategy.long)
 
if shortCondition
    strategy.entry("Short", strategy.short)
 
// --- Dynamic SL / TP & 4 ATR Profit Lock Logic ---
float atr = ta.atr(atrLen)
var float activeSL = na
var float activeTP = na
 
if strategy.position_size != 0
    // Initialize SL/TP on entry bar
    if ta.crossover(strategy.position_size, 0) or ta.crossunder(strategy.position_size, 0)
        activeSL := strategy.position_avg_price + (strategy.position_size > 0 ? -1 : 1) * (atr * slMult)
        activeTP := strategy.position_avg_price + (strategy.position_size > 0 ? 1 : -1) * (atr * tpMult)
 
    // Check for 4 ATR Lock Trigger
    if useLock
        if strategy.position_size > 0 and high >= (strategy.position_avg_price + (atr * lockTrigger))
            // Shift SL to trail 4 ATR below current price action (ensuring it only moves upwards)
            float lockedSL = close - (atr * lockTrail)
            activeSL := math.max(activeSL, lockedSL)
            
        else if strategy.position_size < 0 and low <= (strategy.position_avg_price - (atr * lockTrigger))
            // Shift SL to trail 4 ATR above current price action (ensuring it only moves downwards)
            float lockedSL = close + (atr * lockTrail)
            activeSL := math.min(activeSL, lockedSL)
 
    strategy.exit("Exit", strategy.position_size > 0 ? "Long" : "Short", stop=activeSL, limit=activeTP)
else
    activeSL := na
    activeTP := na
 
// --- Plotting ---
plot(emaValue, title="9 EMA", color=color.blue, linewidth=2)
plot(macroEma, title="200 Macro EMA", color=color.purple, linewidth=2)
plot(vwapValue, title="VWAP", color=color.orange, linewidth=2)

← 返回列表