本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("MES/MNQ LONG Scalping Assistant v3.3", overlay=true, max_labels_count=500)
// =====================================================
// 1. SETTINGS
// =====================================================
g1 = "Trend and Entry Signals"
fastLen = input.int(9, "Fast EMA", minval=1, group=g1)
slowLen = input.int(21, "Slow EMA", minval=2, group=g1)
breakLen = input.int(5, "Breakout Lookback", minval=2, group=g1)
volMult = input.float(1.10, "Breakout Relative Volume", minval=0.5, step=0.05, group=g1)
requireHTF = input.bool(false, "Require Bullish Higher Timeframe for Trend Entries", group=g1)
allowReversal = input.bool(true, "Enable Reversal LONG Signals", group=g1)
showWatch = input.bool(true, "Show Early Momentum WATCH", group=g1)
showContinuation = input.bool(true, "Show Continuation Dots", group=g1)
showWeak = input.bool(true, "Show Weakening Dots", group=g1)
g2 = "ATR Risk Management"
atrLen = input.int(14, "ATR Length", minval=1, group=g2)
atrMult = input.float(1.5, "ATR Stop Multiplier", minval=0.25, step=0.25, group=g2)
tpAtrMult = input.float(2.5, "ATR Target Multiplier", minval=0.25, step=0.25, group=g2)
contracts = input.int(1, "Micro Contracts for Risk Estimate", minval=1, group=g2)
minRoomR = input.float(1.0, "Minimum Resistance Clearance (R)", minval=0.25, step=0.25, group=g2)
filterResistance = input.bool(true, "Filter Entries Near Resistance", group=g2)
g3 = "Momentum and Resistance"
momentumThreshold = input.int(3, "Bullish Momentum Threshold", minval=1, maxval=5, group=g3)
swingLookback = input.int(12, "Swing Resistance Lookback", minval=3, group=g3)
showMomentumBg = input.bool(false, "Highlight Strong Bullish Momentum", group=g3)
g4 = "Sessions and Display"
tz = "America/Chicago"
pmSession = input.session("0300-0830", "Premarket (Central)", group=g4)
tradeSession = input.session("0830-1500", "Signal Window (Central)", group=g4)
showLevels = input.bool(true, "Show PM / OR Levels", group=g4)
showDash = input.bool(true, "Show Dashboard", group=g4)
showRiskLines = input.bool(true, "Show Simulated Trade Lines", group=g4)
// =====================================================
// 2. AUTOMATIC TIMEFRAME DETECTION
// =====================================================
is3m = timeframe.isminutes and timeframe.multiplier == 3
is5m = timeframe.isminutes and timeframe.multiplier == 5
is15m = timeframe.isminutes and timeframe.multiplier == 15
supportedTF = is3m or is5m or is15m
htfTF = is15m ? "60" : "15"
htfLabel = is15m ? "1h Context" : "15m Context"
// =====================================================
// 3. CORE INDICATORS
// =====================================================
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
vw = ta.vwap(hlc3)
atr = ta.atr(atrLen)
avgVol = ta.sma(volume[1], 20)
rvol = not na(avgVol) and avgVol > 0 ? volume / avgVol : na
plot(fast, "9 EMA", color=color.orange, linewidth=2)
plot(slow, "21 EMA", color=color.purple, linewidth=2)
plot(vw, "VWAP", color=color.aqua, linewidth=2)
// Completed HTF candles only.
// On 3m/5m use 15m; on 15m use 1h.
[htfClose15, htfFast15, htfSlow15] = request.security(
syminfo.tickerid, "15",
[close[1], ta.ema(close, fastLen)[1],
ta.ema(close, slowLen)[1]],
lookahead=barmerge.lookahead_on)
[htfClose60, htfFast60, htfSlow60] = request.security(
syminfo.tickerid, "60",
[close[1], ta.ema(close, fastLen)[1],
ta.ema(close, slowLen)[1]],
lookahead=barmerge.lookahead_on)
htfBull15 = htfClose15 > htfFast15 and htfFast15 > htfSlow15
htfBull60 = htfClose60 > htfFast60 and htfFast60 > htfSlow60
htfBull = is15m ? htfBull60 : htfBull15
// =====================================================
// 4. TIMEFRAME-INDEPENDENT PM / OPENING RANGE LEVELS
// Calculate from 1-minute data.
// =====================================================
f_levels() =>
bool pm = not na(time("1", pmSession, tz))
bool or5 = not na(time("1", "0830-0835:1234567", tz))
bool or30 = not na(time("1", "0830-0900:1234567", tz))
bool pmStart = pm and not pm[1]
bool or5Start = or5 and not or5[1]
bool or30Start = or30 and not or30[1]
var float pH = na
var float pL = na
var float o5H = na
var float o5L = na
var float o30H = na
var float o30L = na
if pmStart
pH := high
pL := low
o5H := na
o5L := na
o30H := na
o30L := na
else if pm
pH := math.max(nz(pH, high), high)
pL := math.min(nz(pL, low), low)
if or5Start
o5H := high
o5L := low
else if or5
o5H := math.max(nz(o5H, high), high)
o5L := math.min(nz(o5L, low), low)
if or30Start
o30H := high
o30L := low
else if or30
o30H := math.max(nz(o30H, high), high)
o30L := math.min(nz(o30L, low), low)
[pH, pL, o5H, o5L, o30H, o30L, pm, or5, or30]
[pmH, pmL, or5H, or5L, or30H, or30L,
inPM1, inOR5_1, inOR30_1] = request.security(
syminfo.tickerid, "1", f_levels(),
lookahead=barmerge.lookahead_off)
inTrade = not na(time(timeframe.period, tradeSession, tz))
// Display levels only after their collection window.
// This avoids showing an unfinished OR as a final level.
plot(showLevels and not inPM1 ? pmH : na,
"PM High", color=color.blue, style=plot.style_linebr)
plot(showLevels and not inPM1 ? pmL : na,
"PM Low", color=color.blue, style=plot.style_linebr)
plot(showLevels and not inOR5_1 ? or5H : na,
"5m OR High", color=color.yellow, style=plot.style_linebr)
plot(showLevels and not inOR5_1 ? or5L : na,
"5m OR Low", color=color.yellow, style=plot.style_linebr)
plot(showLevels and not inOR30_1 ? or30H : na,
"30m OR High", color=color.lime, style=plot.style_linebr)
plot(showLevels and not inOR30_1 ? or30L : na,
"30m OR Low", color=color.lime, style=plot.style_linebr)
// =====================================================
// 5. TREND SCORE (0-7)
// =====================================================
aboveFast = close > fast
aboveSlow = close > slow
aboveVWAP = close > vw
emaAligned = fast > slow
fastRising = fast > fast[1]
slowRising = slow > slow[1]
int trendScore = 0
trendScore += aboveFast ? 1 : 0
trendScore += aboveSlow ? 1 : 0
trendScore += aboveVWAP ? 1 : 0
trendScore += emaAligned ? 1 : 0
trendScore += fastRising ? 1 : 0
trendScore += slowRising ? 1 : 0
trendScore += htfBull ? 1 : 0
strongBull = trendScore >= 6
bullTrend = trendScore >= 4 and emaAligned
trendBase = aboveFast and emaAligned and aboveVWAP
htfOK = not requireHTF or htfBull
trendText = strongBull ? "STRONG BULLISH" :
bullTrend ? "BULLISH" :
trendScore >= 3 ? "MIXED" : "WEAK"
trendColor = strongBull ? color.green :
bullTrend ? color.teal :
trendScore >= 3 ? color.orange : color.red
// =====================================================
// 6. MOMENTUM SCORE (0-5)
// =====================================================
barRange = math.max(high - low, syminfo.mintick)
body = math.abs(close - open)
bodyRatio = body / barRange
closeLocation = (close - low) / barRange
green = close > open
strongGreen = green and bodyRatio >= 0.55 and closeLocation >= 0.70
higherHigh = high > high[1]
higherLow = low > low[1]
higherClose = close > close[1]
volumeStrong = not na(rvol) and rvol >= 1.0
fastRecovery = close > fast and higherClose
int momentumScore = 0
momentumScore += strongGreen ? 1 : 0
momentumScore += higherHigh ? 1 : 0
momentumScore += higherLow ? 1 : 0
momentumScore += volumeStrong ? 1 : 0
momentumScore += fastRecovery ? 1 : 0
momentumBull = momentumScore >= momentumThreshold
momentumStrong = momentumScore >= 4
momentumText = momentumScore >= 4 ? "STRONG BULLISH" :
momentumScore >= momentumThreshold ? "BULLISH" :
momentumScore == 2 ? "DEVELOPING" : "WEAK"
momentumColor = momentumScore >= 4 ? color.green :
momentumBull ? color.teal :
momentumScore == 2 ? color.orange : color.red
bgcolor(showMomentumBg and momentumStrong ?
color.new(color.green, 92) : na)
// =====================================================
// 7. PRICE ACTION SIGNALS
// =====================================================
priorHigh = ta.highest(high, breakLen)[1]
breakout = trendBase and htfOK and
close > priorHigh and strongGreen and
not na(rvol) and rvol >= volMult
emaTouch = low <= fast or low <= slow
bounce = trendBase and htfOK and emaTouch and
green and higherClose and
closeLocation >= 0.65 and not breakout
reversal = allowReversal and not trendBase and
momentumStrong and strongGreen and
close > fast and close > high[1] and
not na(rvol) and rvol >= 1.0
earlyWatch = showWatch and momentumBull and
close > fast and
(not emaAligned or not aboveVWAP) and
not reversal
// =====================================================
// 8. NEARBY RESISTANCE
// =====================================================
float nearestR = na
// Only use completed PM / OR levels as resistance.
pmComplete = not inPM1
or5Complete = not inOR5_1
or30Complete = not inOR30_1
if pmComplete and not na(pmH) and pmH > close
nearestR := pmH
if or5Complete and not na(or5H) and or5H > close
nearestR := na(nearestR) ? or5H : math.min(nearestR, or5H)
if or30Complete and not na(or30H) and or30H > close
nearestR := na(nearestR) ? or30H : math.min(nearestR, or30H)
swingResistance = ta.highest(high, swingLookback)[1]
if not na(swingResistance) and swingResistance > close
nearestR := na(nearestR) ? swingResistance :
math.min(nearestR, swingResistance)
// =====================================================
// 9. ATR STOP / TARGET
// =====================================================
atrStopDistance = math.ceil(
atr * atrMult / syminfo.mintick) * syminfo.mintick
atrTargetDistance = math.ceil(
atr * tpAtrMult / syminfo.mintick) * syminfo.mintick
candidateStop = close - atrStopDistance
candidateDistance = atrStopDistance
dollarRisk = candidateDistance * syminfo.pointvalue * contracts
dollarReward = atrTargetDistance * syminfo.pointvalue * contracts
riskOK = not na(atrStopDistance) and atrStopDistance > 0
roomR = not na(nearestR) and candidateDistance > 0 ?
(nearestR - close) / candidateDistance : na
roomOK = not filterResistance or na(roomR) or roomR >= minRoomR
entryOK = supportedTF and inTrade and riskOK and roomOK
// =====================================================
// 10. SIMULATED TRADE TRACKING
// =====================================================
var bool active = false
var float entryPrice = na
var float stopPrice = na
var float targetPrice = na
var int entryBar = na
bool longSignal = false
bool reversalSignal = false
bool exitSignal = false
bool continuation = false
bool weakening = false
bool watchSignal = false
string exitReason = ""
if barstate.isconfirmed
if active
hitStop = bar_index > entryBar and low <= stopPrice
hitTarget = bar_index > entryBar and high >= targetPrice
lostTrend = close < slow and close < open
sessionEnd = not inTrade
if hitStop or hitTarget or lostTrend or sessionEnd
exitSignal := true
exitReason := hitStop ? "STOP" :
hitTarget ? "TARGET" :
sessionEnd ? "SESSION" : "MOMENTUM"
active := false
label.new(bar_index, high,
"EXIT: " + exitReason,
style=label.style_label_down,
color=color.red, textcolor=color.white)
else if entryOK and (breakout or bounce or reversal)
active := true
longSignal := true
reversalSignal := reversal
entryPrice := close
stopPrice := candidateStop
targetPrice := entryPrice + atrTargetDistance
entryBar := bar_index
label.new(bar_index, low,
reversal ? "REVERSAL LONG\nHIGHER RISK" :
breakout ? "LONG\nBREAKOUT" :
"LONG\nEMA BOUNCE",
style=label.style_label_up,
color=reversal ? color.orange : color.green,
textcolor=color.white)
continuation := inTrade and strongBull and
momentumBull and green and higherClose and
higherLow and not longSignal
weakening := inTrade and
momentumScore < momentumScore[1] and
(close < fast or close < open) and
not exitSignal
watchSignal := inTrade and earlyWatch and
not active and not longSignal and
not earlyWatch[1]
// =====================================================
// 11. CHART MARKERS
// =====================================================
plotshape(watchSignal, title="EARLY WATCH",
style=shape.diamond, location=location.belowbar,
color=color.yellow, size=size.small,
text="WATCH", textcolor=color.yellow)
plotshape(longSignal and not reversalSignal,
title="LONG ENTRY",
style=shape.triangleup, location=location.belowbar,
color=color.lime, size=size.small)
plotshape(reversalSignal, title="REVERSAL LONG",
style=shape.triangleup, location=location.belowbar,
color=color.orange, size=size.small)
plotshape(showContinuation and continuation,
title="BULLISH CONTINUATION",
style=shape.circle, location=location.belowbar,
color=color.lime, size=size.tiny)
plotshape(showWeak and weakening,
title="MOMENTUM WEAKENING",
style=shape.circle, location=location.abovebar,
color=color.orange, size=size.tiny)
plotshape(exitSignal, title="EXIT",
style=shape.triangledown, location=location.abovebar,
color=color.red, size=size.small)
// =====================================================
// 12. SIMULATED TRADE LINES
// =====================================================
plot(showRiskLines and active ? entryPrice : na,
"Simulated Entry", color=color.white,
style=plot.style_linebr)
plot(showRiskLines and active ? stopPrice : na,
"Simulated Stop", color=color.red,
linewidth=2, style=plot.style_linebr)
plot(showRiskLines and active ? targetPrice : na,
"Simulated Target", color=color.green,
linewidth=2, style=plot.style_linebr)
// =====================================================
// 13. DASHBOARD
// =====================================================
var table dash = table.new(position.top_right, 2, 15,
border_width=1)
if barstate.islast
bg = color.new(color.black, 15)
if showDash
table.cell(dash, 0, 0, "LONG ASSISTANT",
bgcolor=color.black, text_color=color.white)
table.cell(dash, 1, 0, "v3.3",
bgcolor=color.black, text_color=color.white)
table.cell(dash, 0, 1, "Chart TF",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 1,
supportedTF ? timeframe.period : "UNSUPPORTED",
bgcolor=bg, text_color=color.white)
table.cell(dash, 0, 2, "Trend",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 2, trendText,
bgcolor=trendColor, text_color=color.white)
table.cell(dash, 0, 3, "Trend Score",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 3,
str.tostring(trendScore) + "/7",
bgcolor=bg, text_color=color.white)
table.cell(dash, 0, 4, "Momentum",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 4, momentumText,
bgcolor=momentumColor, text_color=color.white)
table.cell(dash, 0, 5, "Momentum Score",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 5,
str.tostring(momentumScore) + "/5",
bgcolor=bg, text_color=color.white)
table.cell(dash, 0, 6, htfLabel,
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 6,
htfBull ? "BULLISH" : "NOT BULLISH",
bgcolor=bg,
text_color=htfBull ? color.lime : color.orange)
table.cell(dash, 0, 7, "VWAP",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 7,
aboveVWAP ? "ABOVE" : "BELOW",
bgcolor=bg,
text_color=aboveVWAP ? color.lime : color.orange)
volText = na(rvol) ? "N/A" :
str.tostring(rvol, "#.00") + "x"
table.cell(dash, 0, 8, "Relative Volume",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 8, volText,
bgcolor=bg, text_color=color.white)
table.cell(dash, 0, 9, "ATR",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 9,
str.tostring(atr, "#.00"),
bgcolor=bg, text_color=color.white)
state = not supportedTF ? "USE 3/5/15m" :
active ? "HOLD LONG" :
reversal ? "REVERSAL WATCH" :
earlyWatch ? "EARLY WATCH" :
momentumStrong ? "BULLISH MOMENTUM" :
bullTrend ? "WATCH LONG" : "WAIT"
table.cell(dash, 0, 10, "Trade Status",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 10, state,
bgcolor=active ? color.green : bg,
text_color=color.white)
table.cell(dash, 0, 11, "Nearest Resistance",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 11,
na(nearestR) ? "NONE ABOVE" :
str.tostring(nearestR, format.mintick),
bgcolor=bg, text_color=color.white)
table.cell(dash, 0, 12, "Est. Risk",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 12,
"$" + str.tostring(dollarRisk, "#.00"),
bgcolor=bg, text_color=color.orange)
table.cell(dash, 0, 13, "Est. Reward",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 13,
"$" + str.tostring(dollarReward, "#.00"),
bgcolor=bg, text_color=color.lime)
table.cell(dash, 0, 14, "Entry Filters",
bgcolor=bg, text_color=color.white)
table.cell(dash, 1, 14,
not supportedTF ? "UNSUPPORTED TF" :
not riskOK ? "ATR UNAVAILABLE" :
not roomOK ? "RESISTANCE" :
not inTrade ? "OUTSIDE HOURS" :
"READY IF SIGNAL",
bgcolor=bg, text_color=color.white)
else
table.clear(dash, 0, 0, 1, 14)
// =====================================================
// 14. OPTIONAL ALERTS
// =====================================================
alertcondition(watchSignal, "Early Bullish WATCH",
"Early bullish momentum on {{ticker}}")
alertcondition(longSignal, "LONG Entry",
"LONG setup on {{ticker}} at {{close}}")
alertcondition(continuation, "Bullish Continuation",
"Bullish continuation on {{ticker}}")
alertcondition(weakening, "Momentum Weakening",
"Momentum weakening on {{ticker}}")
alertcondition(exitSignal, "Simulated EXIT",
"Simulated exit condition on {{ticker}}")