本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("MES 5m 4-to-1 Trading Alerts", overlay=true)
// SETTINGS
emaLen = input.int(9, "EMA Length")
breakoutBars = input.int(6, "Breakout Lookback", minval=2)
atrLen = input.int(14, "ATR Length")
swingBars = input.int(4, "Stop Lookback", minval=2)
riskLimit = input.float(50.0, "Maximum Risk USD", minval=1)
rr = input.float(4.0, "Reward to Risk Ratio", minval=1)
minBodyATR = input.float(0.5, "Minimum Breakout Body ATR", minval=0.1)
minStopATR = input.float(0.5, "Minimum Stop ATR", minval=0.1)
cooldownBars = input.int(5, "Alert Cooldown Bars", minval=1)
// INDICATORS
ema9 = ta.ema(close, emaLen)
vwap = ta.vwap(hlc3)
atr = ta.atr(atrLen)
plot(ema9, "9 EMA", color=color.lime, linewidth=2)
plot(vwap, "VWAP", color=color.orange, linewidth=2)
// MES 5-MINUTE ONLY
validChart = syminfo.root == "MES" and timeframe.period == "5"
// BREAKOUT CANDLE (PREVIOUS CLOSED BAR)
prevHigh = ta.highest(high, breakoutBars)[2]
prevLow = ta.lowest(low, breakoutBars)[2]
prevBody = math.abs(close[1] - open[1])
bullBreak = close[1] > prevHigh and close[1] > open[1] and prevBody >= atr[1] * minBodyATR and close[1] > ema9[1] and close[1] > vwap[1]
bearBreak = close[1] < prevLow and close[1] < open[1] and prevBody >= atr[1] * minBodyATR and close[1] < ema9[1] and close[1] < vwap[1]
// CONFIRMING CANDLE
bullConfirm = close > prevHigh and close >= close[1] and close > ema9 and close > vwap and ema9 > ema9[1]
bearConfirm = close < prevLow and close <= close[1] and close < ema9 and close < vwap and ema9 < ema9[1]
// STOP LOSS FROM RECENT PRICE STRUCTURE
longSL = math.round_to_mintick(ta.lowest(low, swingBars) - syminfo.mintick)
shortSL = math.round_to_mintick(ta.highest(high, swingBars) + syminfo.mintick)
longRiskPts = close - longSL
shortRiskPts = shortSL - close
longRiskUSD = longRiskPts * syminfo.pointvalue
shortRiskUSD = shortRiskPts * syminfo.pointvalue
// RISK FILTER
longRiskOK = longRiskPts >= atr * minStopATR and longRiskUSD > 0 and longRiskUSD <= riskLimit
shortRiskOK = shortRiskPts >= atr * minStopATR and shortRiskUSD > 0 and shortRiskUSD <= riskLimit
// PROFIT TARGETS
longTP = math.round_to_mintick(close + longRiskPts * rr)
shortTP = math.round_to_mintick(close - shortRiskPts * rr)
// ALERT COOLDOWN
var int lastAlertBar = na
canAlert = na(lastAlertBar) or bar_index - lastAlertBar >= cooldownBars
// BUY AND SELL SIGNALS
buySignal = validChart and barstate.isconfirmed and canAlert and bullBreak and bullConfirm and longRiskOK and not na(vwap)
sellSignal = validChart and barstate.isconfirmed and canAlert and bearBreak and bearConfirm and shortRiskOK and not na(vwap)
// SHOW BUY / SELL ON CHART
plotshape(buySignal, title="BUY", style=shape.labelup, location=location.belowbar, color=color.green, text="BUY", textcolor=color.white)
plotshape(sellSignal, title="SELL", style=shape.labeldown, location=location.abovebar, color=color.red, text="SELL", textcolor=color.white)
// PHONE NOTIFICATIONS
if buySignal
lastAlertBar := bar_index
message = "MES BUY 4:1 SETUP | Entry: " + str.tostring(close, format.mintick) + " | SL: " + str.tostring(longSL, format.mintick) + " | TP: " + str.tostring(longTP, format.mintick) + " | Risk: $" + str.tostring(longRiskUSD, "#.##")
alert(message, alert.freq_once_per_bar_close)
if sellSignal
lastAlertBar := bar_index
message = "MES SELL 4:1 SETUP | Entry: " + str.tostring(close, format.mintick) + " | SL: " + str.tostring(shortSL, format.mintick) + " | TP: " + str.tostring(shortTP, format.mintick) + " | Risk: $" + str.tostring(shortRiskUSD, "#.##")
alert(message, alert.freq_once_per_bar_close)