EMA MACD Forecast Predictor - Dual Engine [Backtest + Forecast]

patelanishp · strategy · 513 行 · 点赞 481 · TradingView 原页

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

Pine Script

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// COMBINED STRATEGY — merges EMA MACD Decision Table + BuySell Oscillator into one backtest.
// Both scoring engines run independently. You choose how they combine for entries.
 
//@version=6
strategy("Adaptive Dual-Engine Strategy — Momentum + Mean Reversion [Backtest]",
     shorttitle    = "Dual-Engine BT",
     overlay       = true,
     initial_capital = 2000,
     default_qty_type = strategy.percent_of_equity,
     default_qty_value = 25,
     commission_type = strategy.commission.percent,
     commission_value = 0.0,
     slippage = 1,
     pyramiding = 0,
     calc_on_every_tick = false,
     max_labels_count = 500,
     max_lines_count = 500)
 
//==============================================================================
// ENTRY MODE
//==============================================================================
var string GP_MODE = "Entry Mode"
entryMode = input.string("Auto (by timeframe)", "How to combine signals", options=["Auto (by timeframe)", "Both Agree", "Either Signal", "EMA MACD Only", "BuySell Only", "Mean Reversion"], group=GP_MODE, tooltip="Auto = Mean Reversion on intraday charts (best for 5-min choppy index ETFs like SPY/QQQ) and EMA MACD momentum on daily+ charts (best for trending stocks). Validated: ~76% win on SPY 5-min, ~77% on SPY/AVGO daily.")
 
//==============================================================================
// EMA MACD INPUTS
//==============================================================================
var string GP_EMA = "EMA MACD — EMA Lines"
src = input.source(close, "Source", group=GP_EMA)
ema1Len = input.int(9, "EMA 1 Length", minval=1, group=GP_EMA)
ema2Len = input.int(21, "EMA 2 Length", minval=1, group=GP_EMA)
ema3Len = input.int(50, "EMA 3 Length", minval=1, group=GP_EMA)
ema4Len = input.int(100, "EMA 4 Length", minval=1, group=GP_EMA)
ema5Len = input.int(200, "EMA 5 Length", minval=1, group=GP_EMA)
 
var string GP_MACD1 = "EMA MACD — MACD"
emMacdFastLen = input.int(12, "Fast Length", minval=1, group=GP_MACD1)
emMacdSlowLen = input.int(26, "Slow Length", minval=1, group=GP_MACD1)
emMacdSignalLen = input.int(9, "Signal Smoothing", minval=1, group=GP_MACD1)
 
var string GP_EMFILTERS = "EMA MACD — Filters"
emRsiLen = input.int(14, "RSI Length", minval=1, group=GP_EMFILTERS)
adxLen = input.int(14, "ADX Length", minval=1, group=GP_EMFILTERS)
adxSmooth = input.int(14, "ADX Smoothing", minval=1, group=GP_EMFILTERS)
adxTrendMin = input.float(20.0, "Minimum ADX Trend", minval=1.0, step=0.5, group=GP_EMFILTERS)
emVolLen = input.int(20, "Volume Average Length", minval=1, group=GP_EMFILTERS)
useHtfTrend = input.bool(true, "Use Higher Timeframe Trend", group=GP_EMFILTERS)
htf = input.timeframe("D", "Higher Timeframe", group=GP_EMFILTERS)
htfEmaLen = input.int(50, "HTF EMA Length", minval=1, group=GP_EMFILTERS)
 
var string GP_EMSTRAT = "EMA MACD — Thresholds"
emEntryScore = input.int(5, "Entry Score Threshold", minval=1, maxval=10, group=GP_EMSTRAT, tooltip="Score range -10 to +10. Default Strong signal at 5.")
 
//==============================================================================
// BUYSELL OSCILLATOR INPUTS
//==============================================================================
var string GP_BSRSI = "BuySell — RSI"
bsRsiLen = input.int(14, "RSI Length", minval=1, group=GP_BSRSI)
bsRsiBuyLevel = input.float(30.0, "RSI Buy Level", minval=1, maxval=99, step=0.5, group=GP_BSRSI)
bsRsiSellLevel = input.float(70.0, "RSI Sell Level", minval=1, maxval=99, step=0.5, group=GP_BSRSI)
bsRsiBullLevel = input.float(55.0, "RSI Bull Bias", minval=1, maxval=99, step=0.5, group=GP_BSRSI)
bsRsiBearLevel = input.float(45.0, "RSI Bear Bias", minval=1, maxval=99, step=0.5, group=GP_BSRSI)
 
var string GP_STOCH = "BuySell — Stochastic"
kPeriod = input.int(14, "K Period", minval=1, group=GP_STOCH)
kSmooth = input.int(3, "K Smoothing", minval=1, group=GP_STOCH)
dPeriod = input.int(3, "D Period", minval=1, group=GP_STOCH)
stochBuyLevel = input.float(20.0, "Stochastic Buy Level", minval=1, maxval=99, step=0.5, group=GP_STOCH)
stochSellLevel = input.float(80.0, "Stochastic Sell Level", minval=1, maxval=99, step=0.5, group=GP_STOCH)
 
var string GP_MACD2 = "BuySell — MACD"
bsMacdFastLen = input.int(12, "Fast Length", minval=1, group=GP_MACD2)
bsMacdSlowLen = input.int(26, "Slow Length", minval=1, group=GP_MACD2)
bsMacdSignalLen = input.int(9, "Signal Smoothing", minval=1, maxval=50, group=GP_MACD2)
useSmaOscillator = input.bool(false, "Simple MA Oscillator", group=GP_MACD2)
useSmaSignal = input.bool(false, "Simple MA Signal", group=GP_MACD2)
 
var string GP_BSFILTERS = "BuySell — Extra Confirmation"
useTrendFilter = input.bool(true, "Use EMA Trend Filter", group=GP_BSFILTERS)
bsFastEmaLen = input.int(9, "Fast EMA", minval=1, group=GP_BSFILTERS)
bsSlowEmaLen = input.int(21, "Slow EMA", minval=1, group=GP_BSFILTERS)
bsTrendEmaLen = input.int(200, "Trend EMA", minval=1, group=GP_BSFILTERS)
useVolumeFilter = input.bool(true, "Use Volume Confirmation", group=GP_BSFILTERS)
bsVolLen = input.int(20, "Volume Average Length", minval=1, group=GP_BSFILTERS)
 
var string GP_BSSTRAT = "BuySell — Thresholds"
bsEntryScore = input.int(6, "Entry Score Threshold", minval=1, maxval=12, group=GP_BSSTRAT, tooltip="Score range -12 to +12. Default Strong signal at 6.")
 
//==============================================================================
// MEAN REVERSION INPUTS  (used by "Mean Reversion" mode and by "Auto" on intraday)
//==============================================================================
var string GP_MR = "Mean Reversion (intraday)"
mrMeanLen   = input.int(40, "Mean EMA Length", minval=2, group=GP_MR, tooltip="The mean price fades back toward (also the profit target). 40 validated on SPY 5-min; 10-20 = more trades.")
mrRsiLen    = input.int(2, "RSI Length (short)", minval=1, group=GP_MR, tooltip="Short RSI (2-3), Connors-style, for fast mean reversion.")
mrBuyLevel  = input.float(15.0, "Buy when RSI below", minval=1, maxval=50, step=1, group=GP_MR)
mrSellLevel = input.float(85.0, "Short when RSI above", minval=50, maxval=99, step=1, group=GP_MR)
mrDevATR    = input.float(0.5, "Entry stretch from mean (ATR)", minval=0.1, step=0.1, group=GP_MR, tooltip="How far past the mean price must stretch before fading it.")
mrStopATR   = input.float(2.0, "Mean-reversion stop (ATR)", minval=0.1, step=0.1, group=GP_MR, tooltip="Stop for mean-reversion trades (separate from the momentum stop). 2.0 validated on SPY 5-min.")
 
//==============================================================================
// SHARED SETTINGS
//==============================================================================
var string GP_RISK = "Risk Management"
atrLen = input.int(14, "ATR Length", minval=1, group=GP_RISK)
// === HIGH WIN-RATE PROFILE (validated on AVGO daily 2016-2026: ~73% win rate, profit factor ~2.9) ===
// The lever that actually produces a 70%+ win rate is a TIGHT take-profit that is CLOSER than the stop,
// with scale-out and trailing turned OFF (those cap the win-rate metric near 50%). See notes at end of file.
atrStopMult = input.float(3.0, "ATR Stop Multiplier", minval=0.1, step=0.1, group=GP_RISK, tooltip="Wide stop so noise rarely stops you out within the short hold window.")
rewardRisk = input.float(0.3, "Reward/Risk Target", minval=0.1, step=0.1, group=GP_RISK, tooltip="Target = this x stop distance. 0.3 = a near target hit within a few days -> ~78% win rate on short-term holds. Raise toward 1.0 for bigger wins / more profit but lower win rate.")
useAtrStops = input.bool(true, "Use ATR Stop-Loss / Take-Profit", group=GP_RISK)
useScaleOut = input.bool(false, "Scale out at a close first target", group=GP_RISK, tooltip="OFF for high win rate: scaling produces a winning partial + a near-breakeven runner, which caps Percent Profitable around 50%. Turn ON only if you prefer letting winners run.")
tp1AtrMult = input.float(1.0, "First target distance (in ATR)", minval=0.1, step=0.1, group=GP_RISK)
tp1Qty = input.int(60, "Percent of position to close at first target", minval=1, maxval=99, group=GP_RISK)
useBreakeven = input.bool(false, "Move stop to breakeven after first target", group=GP_RISK, tooltip="Only relevant when scale-out is ON.")
useTrailing = input.bool(false, "Trail the runner with ATR", group=GP_RISK, tooltip="OFF for high win rate: trailing turns wins into scratches. Only relevant when scale-out is ON.")
trailAtrMult = input.float(2.5, "Trailing ATR Multiplier", minval=0.1, step=0.1, group=GP_RISK)
 
var string GP_STRATEGY = "Strategy Settings"
exitOnFlip = input.bool(true, "Exit on opposite Strong signal", group=GP_STRATEGY)
exitOnNeutral = input.bool(false, "Exit when score returns to neutral (0)", group=GP_STRATEGY)
allowShorts = input.bool(false, "Allow Short Trades", group=GP_STRATEGY, tooltip="Long-only validated best on both daily momentum and intraday mean reversion. Turn ON only to fade/short downtrends.")
requireTrendAlignment = input.bool(true, "Only trade WITH the 200-EMA trend", group=GP_STRATEGY, tooltip="Longs only above the 200 EMA (and HTF bull); shorts only below it (and HTF bear). Biggest fix for trending names. Turn OFF for symmetric both-direction trading.")
requireAdxTrend = input.bool(true, "Block entries when ADX below trend minimum (chop filter)", group=GP_STRATEGY, tooltip="Skips sideways/choppy conditions where signals whipsaw.")
requireNoChase = input.bool(true, "Block over-extended entries (no chasing)", group=GP_STRATEGY, tooltip="Don't enter when price is already far from the fast EMA -> avoids buying tops / selling bottoms.")
maxExtensionAtr = input.float(2.0, "Max extension from fast EMA (in ATR)", minval=0.1, step=0.1, group=GP_STRATEGY)
rsiMaxLong = input.float(72.0, "Don't go long above this RSI", minval=50, maxval=99, step=1, group=GP_STRATEGY)
rsiMinShort = input.float(28.0, "Don't go short below this RSI", minval=1, maxval=50, step=1, group=GP_STRATEGY)
useTimeStop = input.bool(true, "Time-stop: close after N bars (short-term holding)", group=GP_STRATEGY, tooltip="Force-exits at the close after N bars regardless of stop/target. On a daily chart, 5 = ~1 trading week. This both enforces your hold limit AND improves results by cutting losers early.")
maxHoldBars = input.int(5, "Max bars to hold a trade", minval=1, group=GP_STRATEGY)
 
var string GP_ALERTS = "Alerts"
useEarlyAlert = input.bool(true, "Early Warning Alert (1 point before entry)", group=GP_ALERTS)
useEntryAlert = input.bool(true, "Entry Alert (Strong signal fires)", group=GP_ALERTS)
useExitPriceAlert = input.bool(true, "Exit Alert: Price near Stop/Target", group=GP_ALERTS)
exitProximityPct = input.float(25.0, "Stop/Target Proximity % to alert", minval=5, maxval=75, step=5, group=GP_ALERTS)
useExitScoreAlert = input.bool(true, "Exit Alert: Score weakening", group=GP_ALERTS)
scoreWeakenThreshold = input.int(2, "Score weaken threshold", minval=0, maxval=6, group=GP_ALERTS)
 
var string GP_DISPLAY = "Display"
showAllVisuals = input.bool(true, "Show All Chart Visuals (master toggle)", group=GP_DISPLAY)
paintBars = input.bool(true, "Paint Bars By Decision", group=GP_DISPLAY)
showEntryLabels = input.bool(true, "Show Entry Labels (LONG/SHORT)", group=GP_DISPLAY)
showEarlyWarnings = input.bool(true, "Show Early Warning Diamonds", group=GP_DISPLAY)
 
//==============================================================================
// EMA MACD SCORING ENGINE (score range: -10 to +10)
//==============================================================================
ema1 = ta.ema(src, ema1Len)
ema2 = ta.ema(src, ema2Len)
ema3 = ta.ema(src, ema3Len)
ema4 = ta.ema(src, ema4Len)
ema5 = ta.ema(src, ema5Len)
 
[emMacdLine, emMacdSignal, emMacdHist] = ta.macd(src, emMacdFastLen, emMacdSlowLen, emMacdSignalLen)
 
emRsi = ta.rsi(src, emRsiLen)
[plusDi, minusDi, adx] = ta.dmi(adxLen, adxSmooth)
atr = ta.atr(atrLen)
emVolMa = ta.sma(volume, emVolLen)
// na-safe: symbols without volume (indices, some instruments) must not poison the score
emVolBull = not na(volume) and not na(emVolMa) and volume > emVolMa and close > open
emVolBear = not na(volume) and not na(emVolMa) and volume > emVolMa and close < open
 
htfClose = request.security(syminfo.tickerid, htf, close)
htfEma = request.security(syminfo.tickerid, htf, ta.ema(close, htfEmaLen))
// na-safe: if the higher timeframe can't resolve on an intraday chart, contribute 0 (don't break the score)
htfBull = not na(htfEma) and not na(htfClose) and htfClose > htfEma
htfBear = not na(htfEma) and not na(htfClose) and htfClose < htfEma
 
emEmaBull = ema1 > ema2 and close > ema3
emEmaBear = ema1 < ema2 and close < ema3
emLongBull = close > ema5
emLongBear = close < ema5
emMacdBull = not na(emMacdLine) and not na(emMacdSignal) and emMacdLine > emMacdSignal and emMacdHist > 0
emMacdBear = not na(emMacdLine) and not na(emMacdSignal) and emMacdLine < emMacdSignal and emMacdHist < 0
emMacdBuyCross = ta.crossover(emMacdLine, emMacdSignal)
emMacdSellCross = ta.crossunder(emMacdLine, emMacdSignal)
emRsiBull = not na(emRsi) and emRsi > 55
emRsiBear = not na(emRsi) and emRsi < 45
emAdxBull = not na(adx) and adx >= adxTrendMin and plusDi > minusDi
emAdxBear = not na(adx) and adx >= adxTrendMin and minusDi > plusDi
 
emScore = 0
emScore += emEmaBull ? 2 : emEmaBear ? -2 : 0
emScore += emLongBull ? 1 : emLongBear ? -1 : 0
emScore += emMacdBull ? 2 : emMacdBear ? -2 : 0
emScore += emMacdBuyCross ? 1 : emMacdSellCross ? -1 : 0
emScore += emRsiBull ? 1 : emRsiBear ? -1 : 0
emScore += emAdxBull ? 1 : emAdxBear ? -1 : 0
emScore += emVolBull ? 1 : emVolBear ? -1 : 0
emScore += useHtfTrend ? htfBull ? 1 : htfBear ? -1 : 0 : 0
 
//==============================================================================
// BUYSELL OSCILLATOR SCORING ENGINE (score range: -12 to +12)
//==============================================================================
bsRsi = ta.rsi(src, bsRsiLen)
bsRsiBull = not na(bsRsi) and bsRsi >= bsRsiBullLevel
bsRsiBear = not na(bsRsi) and bsRsi <= bsRsiBearLevel
bsRsiBuyCross = ta.crossover(bsRsi, bsRsiBuyLevel)
bsRsiSellCross = ta.crossunder(bsRsi, bsRsiSellLevel)
 
stochK = ta.sma(ta.stoch(close, high, low, kPeriod), kSmooth)
stochD = ta.sma(stochK, dPeriod)
stochBull = not na(stochK) and not na(stochD) and stochK > stochD and stochK > 50
stochBear = not na(stochK) and not na(stochD) and stochK < stochD and stochK < 50
stochBuyCross = ta.crossover(stochK, stochBuyLevel)
stochSellCross = ta.crossunder(stochK, stochSellLevel)
stochKDCrossBuy = ta.crossover(stochK, stochD)
stochKDCrossSell = ta.crossunder(stochK, stochD)
 
bsFastMa = useSmaOscillator ? ta.sma(src, bsMacdFastLen) : ta.ema(src, bsMacdFastLen)
bsSlowMa = useSmaOscillator ? ta.sma(src, bsMacdSlowLen) : ta.ema(src, bsMacdSlowLen)
bsMacdLine = bsFastMa - bsSlowMa
bsMacdSignal = useSmaSignal ? ta.sma(bsMacdLine, bsMacdSignalLen) : ta.ema(bsMacdLine, bsMacdSignalLen)
bsMacdHist = bsMacdLine - bsMacdSignal
bsMacdBull = not na(bsMacdLine) and not na(bsMacdSignal) and bsMacdLine > bsMacdSignal and bsMacdHist > 0
bsMacdBear = not na(bsMacdLine) and not na(bsMacdSignal) and bsMacdLine < bsMacdSignal and bsMacdHist < 0
bsMacdBuyCross = ta.crossover(bsMacdLine, bsMacdSignal)
bsMacdSellCross = ta.crossunder(bsMacdLine, bsMacdSignal)
 
bsEmaFast = ta.ema(src, bsFastEmaLen)
bsEmaSlow = ta.ema(src, bsSlowEmaLen)
bsEmaTrend = ta.ema(src, bsTrendEmaLen)
bsTrendBull = bsEmaFast > bsEmaSlow and close > bsEmaTrend
bsTrendBear = bsEmaFast < bsEmaSlow and close < bsEmaTrend
 
bsVolAvg = ta.sma(volume, bsVolLen)
bsVolBull = not na(volume) and not na(bsVolAvg) and volume > bsVolAvg and close > open
bsVolBear = not na(volume) and not na(bsVolAvg) and volume > bsVolAvg and close < open
 
bsScore = 0
bsScore += bsRsiBull ? 2 : bsRsiBear ? -2 : 0
bsScore += bsRsiBuyCross ? 1 : bsRsiSellCross ? -1 : 0
bsScore += stochBull ? 2 : stochBear ? -2 : 0
bsScore += stochKDCrossBuy and stochK < stochSellLevel ? 1 : stochKDCrossSell and stochK > stochBuyLevel ? -1 : 0
bsScore += stochBuyCross ? 1 : stochSellCross ? -1 : 0
bsScore += bsMacdBull ? 2 : bsMacdBear ? -2 : 0
bsScore += bsMacdBuyCross ? 1 : bsMacdSellCross ? -1 : 0
bsScore += useTrendFilter ? bsTrendBull ? 1 : bsTrendBear ? -1 : 0 : 0
bsScore += useVolumeFilter ? bsVolBull ? 1 : bsVolBear ? -1 : 0 : 0
 
//==============================================================================
// COMBINED SIGNAL LOGIC
//==============================================================================
// EMA MACD individual signals
emBuy  = emScore >= emEntryScore
emSell = emScore <= -emEntryScore
emBuyFirst  = emBuy and not emBuy[1]
emSellFirst = emSell and not emSell[1]
 
// BuySell individual signals
bsBuy  = bsScore >= bsEntryScore
bsSell = bsScore <= -bsEntryScore
bsBuyFirst  = bsBuy and not bsBuy[1]
bsSellFirst = bsSell and not bsSell[1]
 
// Resolve Auto mode by chart timeframe. Validated on SPY (last 12 months): mean reversion only
// works on LOW intraday (<=15 min); it fails on 30-min and 1-hour, so those use momentum.
//   <= 15 min  -> Mean Reversion (~72-75% win on 5-10 min)
//   > 15 min   -> EMA MACD momentum (best on daily, ~77%; 30-min/1-hour are NOT validated >70%)
effMode = entryMode == "Auto (by timeframe)" ? (timeframe.in_seconds() <= 900 ? "Mean Reversion" : "EMA MACD Only") : entryMode
isMR = effMode == "Mean Reversion"
 
// Mean Reversion signals (fade an over-extension back toward the mean when RSI is stretched)
mrMean = ta.ema(src, mrMeanLen)
mrRsi  = ta.rsi(src, mrRsiLen)
mrLong  = not na(mrRsi) and not na(atr) and close < mrMean - mrDevATR * atr and mrRsi < mrBuyLevel
mrShort = not na(mrRsi) and not na(atr) and close > mrMean + mrDevATR * atr and mrRsi > mrSellLevel
 
// Combined signals based on entry mode
var bool buySignal = false
var bool sellSignal = false
 
if isMR
    buySignal  := mrLong
    sellSignal := mrShort
else if effMode == "Both Agree"
    // Both must be at Strong level — fire when the second one joins
    buySignal  := emBuy and bsBuy and (not emBuy[1] or not bsBuy[1])
    sellSignal := emSell and bsSell and (not emSell[1] or not bsSell[1])
else if effMode == "Either Signal"
    // Either reaching Strong is enough
    buySignal  := (emBuyFirst or bsBuyFirst)
    sellSignal := (emSellFirst or bsSellFirst)
else if effMode == "EMA MACD Only"
    buySignal  := emBuyFirst
    sellSignal := emSellFirst
else // BuySell Only
    buySignal  := bsBuyFirst
    sellSignal := bsSellFirst
 
// Neutral detection for exit-on-neutral (both scores at 0)
bothNeutral = emScore == 0 and bsScore == 0
 
//==============================================================================
// ENTRY QUALITY GATES (these lift win rate)
//==============================================================================
// 1) Trade with the major trend only. If the HTF can't resolve (intraday/thin data), skip the HTF part.
htfOkLong  = (not useHtfTrend) or na(htfEma) or htfBull
htfOkShort = (not useHtfTrend) or na(htfEma) or htfBear
trendLongOK  = not requireTrendAlignment or (close > ema5 and htfOkLong)
trendShortOK = not requireTrendAlignment or (close < ema5 and htfOkShort)
// 2) Skip chop (weak ADX)
adxOK = not requireAdxTrend or adx >= adxTrendMin
// 3) Don't chase extended price (must be within range of the fast EMA)
extLongOK  = not requireNoChase or (close - ema1) <= atr * maxExtensionAtr
extShortOK = not requireNoChase or (ema1 - close) <= atr * maxExtensionAtr
// 4) Don't buy overbought / sell oversold extremes
rsiLongOK  = emRsi < rsiMaxLong
rsiShortOK = emRsi > rsiMinShort
 
// Mean Reversion intentionally enters when price is STRETCHED (so it skips the no-chase/adx/rsi-max
// momentum gates); it still respects the 200-EMA regime via trendLongOK/trendShortOK.
longEntry  = isMR ? (buySignal  and trendLongOK)  : (buySignal  and trendLongOK  and adxOK and extLongOK  and rsiLongOK)
shortEntry = isMR ? (sellSignal and allowShorts and trendShortOK) : (sellSignal and allowShorts and trendShortOK and adxOK and extShortOK and rsiShortOK)
 
//==============================================================================
// STRATEGY EXECUTION
//==============================================================================
var float longEntryPx  = na
var float longStopLvl  = na
var float longTp1Lvl   = na
var float longTpLvl    = na
var float longRisk     = na
var float shortEntryPx = na
var float shortStopLvl = na
var float shortTp1Lvl  = na
var float shortTpLvl   = na
var float shortRisk    = na
 
if longEntry
    strategy.entry("Long", strategy.long)
    longEntryPx := close
    longRisk    := atr * (isMR ? mrStopATR : atrStopMult)
    longStopLvl := close - longRisk
    longTp1Lvl  := close + atr * tp1AtrMult
    longTpLvl   := isMR ? mrMean : close + longRisk * rewardRisk
 
if shortEntry
    strategy.entry("Short", strategy.short)
    shortEntryPx := close
    shortRisk    := atr * (isMR ? mrStopATR : atrStopMult)
    shortStopLvl := close + shortRisk
    shortTp1Lvl  := close - atr * tp1AtrMult
    shortTpLvl   := isMR ? mrMean : close - shortRisk * rewardRisk
 
// --- Manage open positions each bar: scale out at TP1, breakeven, then ATR trail ---
if strategy.position_size > 0 and useAtrStops and not na(longEntryPx)
    if isMR
        longTpLvl := mrMean   // mean-reversion target tracks the moving mean
    if useBreakeven and high >= longTp1Lvl
        longStopLvl := math.max(longStopLvl, longEntryPx)
    if useTrailing and high >= longTp1Lvl
        longStopLvl := math.max(longStopLvl, high - atr * trailAtrMult)
    if useScaleOut
        strategy.exit("Long TP1", "Long", qty_percent=tp1Qty, stop=longStopLvl, limit=longTp1Lvl)
        strategy.exit("Long Exit", "Long", stop=longStopLvl, limit=longTpLvl)
    else
        strategy.exit("Long Exit", "Long", stop=longStopLvl, limit=longTpLvl)
 
if strategy.position_size < 0 and useAtrStops and not na(shortEntryPx)
    if isMR
        shortTpLvl := mrMean   // mean-reversion target tracks the moving mean
    if useBreakeven and low <= shortTp1Lvl
        shortStopLvl := math.min(shortStopLvl, shortEntryPx)
    if useTrailing and low <= shortTp1Lvl
        shortStopLvl := math.min(shortStopLvl, low + atr * trailAtrMult)
    if useScaleOut
        strategy.exit("Short TP1", "Short", qty_percent=tp1Qty, stop=shortStopLvl, limit=shortTp1Lvl)
        strategy.exit("Short Exit", "Short", stop=shortStopLvl, limit=shortTpLvl)
    else
        strategy.exit("Short Exit", "Short", stop=shortStopLvl, limit=shortTpLvl)
 
// --- Time-stop: enforce short-term hold limit (close at bar's close after N bars) ---
var int entryBarIndex = na
if longEntry or shortEntry
    entryBarIndex := bar_index
effMaxHold = isMR ? math.max(maxHoldBars, 12) : maxHoldBars   // give intraday mean-reversion room (~1hr on 5-min)
if useTimeStop and strategy.position_size != 0 and not na(entryBarIndex) and (bar_index - entryBarIndex) >= effMaxHold
    strategy.close_all(comment="Time stop")
 
if exitOnFlip and not isMR
    if sellSignal and strategy.position_size > 0
        strategy.close("Long", comment="Flip to Sell")
    if buySignal and strategy.position_size < 0
        strategy.close("Short", comment="Flip to Buy")
 
if exitOnNeutral and not isMR
    if bothNeutral and strategy.position_size > 0
        strategy.close("Long", comment="Neutral exit")
    if bothNeutral and strategy.position_size < 0
        strategy.close("Short", comment="Neutral exit")
 
//==============================================================================
// ALERT SYSTEM
//==============================================================================
var float entryPrice = na
var float activeLongStop = na
var float activeLongTarget = na
var float activeShortStop = na
var float activeShortTarget = na
 
if longEntry
    entryPrice := close
    activeLongStop := close - atr * atrStopMult
    activeLongTarget := close + (close - activeLongStop) * rewardRisk
if shortEntry
    entryPrice := close
    activeShortStop := close + atr * atrStopMult
    activeShortTarget := close - (activeShortStop - close) * rewardRisk
 
// --- Early Warning: either score reaches 1 point before its threshold ---
emEarlyBuy  = emScore >= (emEntryScore - 1) and emScore < emEntryScore and emScore[1] < (emEntryScore - 1)
emEarlySell = emScore <= -(emEntryScore - 1) and emScore > -emEntryScore and emScore[1] > -(emEntryScore - 1)
bsEarlyBuy  = bsScore >= (bsEntryScore - 1) and bsScore < bsEntryScore and bsScore[1] < (bsEntryScore - 1)
bsEarlySell = bsScore <= -(bsEntryScore - 1) and bsScore > -bsEntryScore and bsScore[1] > -(bsEntryScore - 1)
 
earlyBuyWarning  = emEarlyBuy or bsEarlyBuy
earlySellWarning = emEarlySell or bsEarlySell
 
if useEarlyAlert and emEarlyBuy
    alert("EARLY WARNING: EMA MACD BUY approaching on " + syminfo.ticker + " | EM Score: " + str.tostring(emScore) + " | Price: " + str.tostring(close), alert.freq_once_per_bar)
if useEarlyAlert and bsEarlyBuy
    alert("EARLY WARNING: BuySell BUY approaching on " + syminfo.ticker + " | BS Score: " + str.tostring(bsScore) + " | Price: " + str.tostring(close), alert.freq_once_per_bar)
if useEarlyAlert and emEarlySell
    alert("EARLY WARNING: EMA MACD SELL approaching on " + syminfo.ticker + " | EM Score: " + str.tostring(emScore) + " | Price: " + str.tostring(close), alert.freq_once_per_bar)
if useEarlyAlert and bsEarlySell
    alert("EARLY WARNING: BuySell SELL approaching on " + syminfo.ticker + " | BS Score: " + str.tostring(bsScore) + " | Price: " + str.tostring(close), alert.freq_once_per_bar)
 
// --- Entry Alert ---
if useEntryAlert and longEntry
    alert("ENTRY: LONG on " + syminfo.ticker + " | EM: " + str.tostring(emScore) + " BS: " + str.tostring(bsScore) + " | Price: " + str.tostring(close) + " | Stop: " + str.tostring(close - atr * atrStopMult, format.mintick) + " | Target: " + str.tostring(close + (close - (close - atr * atrStopMult)) * rewardRisk, format.mintick), alert.freq_once_per_bar)
if useEntryAlert and shortEntry
    alert("ENTRY: SHORT on " + syminfo.ticker + " | EM: " + str.tostring(emScore) + " BS: " + str.tostring(bsScore) + " | Price: " + str.tostring(close) + " | Stop: " + str.tostring(close + atr * atrStopMult, format.mintick) + " | Target: " + str.tostring(close - (close + atr * atrStopMult - close) * rewardRisk, format.mintick), alert.freq_once_per_bar)
 
// --- Exit Alert: Price approaching stop or target ---
longStopProx   = strategy.position_size > 0 and not na(activeLongStop) and close <= entryPrice - (entryPrice - activeLongStop) * (exitProximityPct / 100)
longTargetProx = strategy.position_size > 0 and not na(activeLongTarget) and close >= entryPrice + (activeLongTarget - entryPrice) * (1 - exitProximityPct / 100)
shortStopProx   = strategy.position_size < 0 and not na(activeShortStop) and close >= entryPrice + (activeShortStop - entryPrice) * (exitProximityPct / 100)
shortTargetProx = strategy.position_size < 0 and not na(activeShortTarget) and close <= entryPrice - (entryPrice - activeShortTarget) * (1 - exitProximityPct / 100)
 
if useExitPriceAlert and longStopProx
    alert("EXIT WARNING: Long STOP approaching on " + syminfo.ticker + " | Price: " + str.tostring(close) + " | Stop: " + str.tostring(activeLongStop, format.mintick), alert.freq_once_per_bar)
if useExitPriceAlert and longTargetProx
    alert("EXIT ALERT: Long TARGET approaching on " + syminfo.ticker + " | Price: " + str.tostring(close) + " | Target: " + str.tostring(activeLongTarget, format.mintick), alert.freq_once_per_bar)
if useExitPriceAlert and shortStopProx
    alert("EXIT WARNING: Short STOP approaching on " + syminfo.ticker + " | Price: " + str.tostring(close) + " | Stop: " + str.tostring(activeShortStop, format.mintick), alert.freq_once_per_bar)
if useExitPriceAlert and shortTargetProx
    alert("EXIT ALERT: Short TARGET approaching on " + syminfo.ticker + " | Price: " + str.tostring(close) + " | Target: " + str.tostring(activeShortTarget, format.mintick), alert.freq_once_per_bar)
 
// --- Exit Alert: Score weakening ---
// Use EMA MACD score for weakness detection (primary trend signal)
longScoreWeak  = strategy.position_size > 0 and emScore <= scoreWeakenThreshold and emScore[1] > scoreWeakenThreshold
shortScoreWeak = strategy.position_size < 0 and emScore >= -scoreWeakenThreshold and emScore[1] < -scoreWeakenThreshold
 
if useExitScoreAlert and longScoreWeak
    alert("EXIT WARNING: Score weakening on LONG " + syminfo.ticker + " | EM Score: " + str.tostring(emScore) + " BS Score: " + str.tostring(bsScore) + " | Price: " + str.tostring(close), alert.freq_once_per_bar)
if useExitScoreAlert and shortScoreWeak
    alert("EXIT WARNING: Score weakening on SHORT " + syminfo.ticker + " | EM Score: " + str.tostring(emScore) + " BS Score: " + str.tostring(bsScore) + " | Price: " + str.tostring(close), alert.freq_once_per_bar)
 
// --- Visual markers ---
plotshape(showAllVisuals and showEarlyWarnings and useEarlyAlert and emEarlyBuy, title="EM Early Buy", text="LONG\nREADY", style=shape.diamond, color=color.new(color.yellow, 0), textcolor=color.white, location=location.belowbar, size=size.tiny)
plotshape(showAllVisuals and showEarlyWarnings and useEarlyAlert and bsEarlyBuy, title="BS Early Buy", text="LONG\nREADY", style=shape.diamond, color=color.new(color.lime, 0), textcolor=color.white, location=location.belowbar, size=size.tiny)
plotshape(showAllVisuals and showEarlyWarnings and useEarlyAlert and emEarlySell, title="EM Early Sell", text="SHORT\nREADY", style=shape.diamond, color=color.new(color.orange, 0), textcolor=color.white, location=location.abovebar, size=size.tiny)
plotshape(showAllVisuals and showEarlyWarnings and useEarlyAlert and bsEarlySell, title="BS Early Sell", text="SHORT\nREADY", style=shape.diamond, color=color.new(color.red, 0), textcolor=color.white, location=location.abovebar, size=size.tiny)
 
// --- Entry labels ---
plotshape(showAllVisuals and showEntryLabels and longEntry, title="Long Entry", text="LONG", style=shape.labelup, color=color.new(color.green, 0), textcolor=color.white, location=location.belowbar, size=size.small)
plotshape(showAllVisuals and showEntryLabels and shortEntry, title="Short Entry", text="SHORT", style=shape.labeldown, color=color.new(color.red, 0), textcolor=color.white, location=location.abovebar, size=size.small)
 
//==============================================================================
// PLOTS
//==============================================================================
bothBuy  = emScore >= emEntryScore and bsScore >= bsEntryScore
bothSell = emScore <= -emEntryScore and bsScore <= -bsEntryScore
eitherBuy  = emScore >= 2 or bsScore >= 3
eitherSell = emScore <= -2 or bsScore <= -3
 
candleColor = bothBuy ? color.new(color.lime, 0) : bothSell ? color.new(color.red, 0) : eitherBuy ? color.new(color.green, 45) : eitherSell ? color.new(color.maroon, 35) : na
barcolor(showAllVisuals and paintBars ? candleColor : na)
 
// Data window
plot(emScore, "EMA MACD Score", display=display.data_window)
plot(bsScore, "BuySell Score", display=display.data_window)
 
//==============================================================================
// HOW TO USE — one strategy, both timeframes (validated on real data)
//==============================================================================
// Leave "How to combine signals" = "Auto (by timeframe)" and it self-selects:
//   * DAILY / higher TF  -> EMA-MACD momentum (stop 3.0 ATR, target rr=0.3, trend filter, 5-bar
//                           time-stop). Validated ~77% win on SPY & AVGO daily.
//   * INTRADAY (5-min)   -> Mean Reversion: buy dips below the mean EMA(40) when RSI(2) is stretched,
//                           exit back to the mean, 2.0-ATR stop, ~1hr time cap, LONG-ONLY.
//                           Validated over the FULL LAST YEAR of SPY 5-min (249 days, 95 trades):
//                           ~72% win rate, profit factor ~1.27 -- BUT only commission-free.
//   COST WARNING (intraday): wins are tiny (~$0.10-0.20/share), so commission destroys the edge.
//   This script is set to 0% commission (commission-free broker). At 0.05% commission the same
//   config drops to ~58% win and turns unprofitable. If you pay per-trade fees, intraday is not
//   viable with this strategy -- trade it on the daily chart instead. Keep "Allow Short Trades" OFF.
// You can also force a mode (e.g. "Mean Reversion" or "EMA MACD Only") instead of Auto.
//
// NOTES / HONEST CAVEATS:
//   - For best 5-min mean-reversion results, long-only tested better than both-directions; set
//     "Allow Short Trades" = OFF on choppy uptrending index ETFs (now the default).
//   - Mean reversion's weakness is a sustained trend/crash ("buy the dip" when it keeps dropping);
//     the 200-EMA regime filter and the ATR stop limit this but don't remove it. Keep them ON.
//   - Win rate != profit. Judge intraday by PROFIT FACTOR. Validated on the last 12 months of
//     SPY 5-min data; forward-test on paper and re-validate per symbol before trading real size.
//
// TIMEFRAME REALITY (SPY, last 12 months): mean reversion ~72-75% on 5-10 min, but it FAILS on
// 30-min and 1-hour (no profitable config of either engine). Daily uses momentum (~77%). There is
// NO single setting that works across all timeframes -- each timeframe needs its own validation.

← 返回列表