本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("V2.3 Indicator (Keep last 5): M15 Trend + M5 EMA20 Reclaim + RSI + ATR SL/TP + Trailing Runner",
overlay=true, max_labels_count=500, max_lines_count=500)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// INPUTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
tfTrend = input.timeframe("15", "Trend TF (M15)")
emaFastLen = input.int(50, "Trend EMA Fast")
emaSlowLen = input.int(200, "Trend EMA Slow")
emaEntryLen = input.int(20, "Entry EMA (chart TF)")
rsiLen = input.int(14, "RSI Length")
rsiLongMin = input.int(55, "RSI Long ≥")
rsiShortMax = input.int(45, "RSI Short ≤")
adxLen = input.int(14, "ADX Length")
adxMin = input.float(18.0, "ADX Min")
atrLen = input.int(14, "ATR Length")
slAtrMult = input.float(1.3, "SL = ATR x", step=0.1, minval=0.1)
tp1R = input.float(1.0, "TP1 (R)", step=0.1, minval=0.1)
tp2R = input.float(1.6, "TP2 (R) (fixed)", step=0.1, minval=0.1)
showRunnerTrail = input.bool(true, "Show runner trailing stop (ATR trail)")
trailAtrMult = input.float(1.2, "Runner Trail = ATR x", step=0.1, minval=0.1)
trailFromBE = input.bool(true, "Start trailing only after BE triggers?")
cooldownBars = input.int(15, "Cooldown bars after signal", minval=0)
showLevels = input.bool(true, "Draw SL/TP lines")
extendBars = input.int(200, "Extend lines (bars)", minval=10)
showPriceLabels = input.bool(true, "Show Entry/SL/TP price labels")
keepSignals = input.int(5, "Keep last N signals", minval=1, maxval=20)
showHitLabels = input.bool(true, "Show SL/TP hit labels")
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// HELPERS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
f_adx() =>
[_, _, adx] = ta.dmi(adxLen, adxLen)
adx
f_del_line(_ln) =>
if not na(_ln)
line.delete(_ln)
f_del_label(_lb) =>
if not na(_lb)
label.delete(_lb)
f_set_line_y(_ln, _y) =>
if not na(_ln)
line.set_y1(_ln, _y)
line.set_y2(_ln, _y)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// M15 TREND FILTER
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
m15Close = request.security(syminfo.tickerid, tfTrend, close)
m15EmaFast = request.security(syminfo.tickerid, tfTrend, ta.ema(close, emaFastLen))
m15EmaSlow = request.security(syminfo.tickerid, tfTrend, ta.ema(close, emaSlowLen))
m15Adx = request.security(syminfo.tickerid, tfTrend, f_adx())
ema200Up = m15EmaSlow > m15EmaSlow[1]
ema200Down = m15EmaSlow < m15EmaSlow[1]
trendLong = m15Close > m15EmaSlow and m15EmaFast > m15EmaSlow and ema200Up and m15Adx >= adxMin
trendShort = m15Close < m15EmaSlow and m15EmaFast < m15EmaSlow and ema200Down and m15Adx >= adxMin
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ENTRY LOGIC (chart TF intended = M5)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ema20 = ta.ema(close, emaEntryLen)
rsi = ta.rsi(close, rsiLen)
atr = ta.atr(atrLen)
touchedLong = low <= ema20
touchedShort = high >= ema20
reclaimLong = ta.crossover(close, ema20)
reclaimShort = ta.crossunder(close, ema20)
bull = close > open
bear = close < open
rawBuy = trendLong and reclaimLong and touchedLong and rsi >= rsiLongMin and bull
rawSell = trendShort and reclaimShort and touchedShort and rsi <= rsiShortMax and bear
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// COOLDOWN
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var int cd = 0
cd := math.max(cd - 1, 0)
buySignal = rawBuy and cd == 0
sellSignal = rawSell and cd == 0
if buySignal or sellSignal
cd := cooldownBars
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ARRAYS (store last N signals)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
var line[] aEntryLine = array.new_line()
var line[] aSLLine = array.new_line()
var line[] aTP1Line = array.new_line()
var line[] aTP2Line = array.new_line()
var line[] aRunLine = array.new_line()
var label[] aEntryLab = array.new_label()
var label[] aSLLab = array.new_label()
var label[] aTP1Lab = array.new_label()
var label[] aTP2Lab = array.new_label()
var int[] aDir = array.new_int()
var float[] aEntry = array.new_float()
var float[] aRisk = array.new_float()
var float[] aTP1 = array.new_float()
var float[] aTP2 = array.new_float()
var float[] aRunnerSL = array.new_float()
var bool[] aBE = array.new_bool()
var bool[] aActive = array.new_bool()
var bool[] aTP1Done = array.new_bool()
f_delete_signal_at(idx) =>
f_del_line(array.get(aEntryLine, idx))
f_del_line(array.get(aSLLine, idx))
f_del_line(array.get(aTP1Line, idx))
f_del_line(array.get(aTP2Line, idx))
f_del_line(array.get(aRunLine, idx))
f_del_label(array.get(aEntryLab, idx))
f_del_label(array.get(aSLLab, idx))
f_del_label(array.get(aTP1Lab, idx))
f_del_label(array.get(aTP2Lab, idx))
array.remove(aEntryLine, idx)
array.remove(aSLLine, idx)
array.remove(aTP1Line, idx)
array.remove(aTP2Line, idx)
array.remove(aRunLine, idx)
array.remove(aEntryLab, idx)
array.remove(aSLLab, idx)
array.remove(aTP1Lab, idx)
array.remove(aTP2Lab, idx)
array.remove(aDir, idx)
array.remove(aEntry, idx)
array.remove(aRisk, idx)
array.remove(aTP1, idx)
array.remove(aTP2, idx)
array.remove(aRunnerSL, idx)
array.remove(aBE, idx)
array.remove(aActive, idx)
array.remove(aTP1Done, idx)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ADD NEW SIGNAL
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
if buySignal or sellSignal
if array.size(aDir) >= keepSignals
f_delete_signal_at(0)
int dir = buySignal ? 1 : -1
float entryPrice = close
float riskPts = atr * slAtrMult
float sl = dir == 1 ? entryPrice - riskPts : entryPrice + riskPts
float tp1 = dir == 1 ? entryPrice + tp1R * riskPts : entryPrice - tp1R * riskPts
float tp2 = dir == 1 ? entryPrice + tp2R * riskPts : entryPrice - tp2R * riskPts
// ✅ Typed declarations (fixes your error)
line lnEntry = na
line lnSL = na
line lnTP1 = na
line lnTP2 = na
line lnRun = na
label lbEntry = na
label lbSL = na
label lbTP1 = na
label lbTP2 = na
if showLevels
int x1 = bar_index
int x2 = bar_index + extendBars
lnEntry := line.new(x1, entryPrice, x2, entryPrice)
lnSL := line.new(x1, sl, x2, sl)
lnTP1 := line.new(x1, tp1, x2, tp1)
lnTP2 := line.new(x1, tp2, x2, tp2)
if showRunnerTrail
lnRun := line.new(bar_index, sl, bar_index + extendBars, sl)
if showPriceLabels
lbEntry := label.new(bar_index, entryPrice, "Entry: " + str.tostring(entryPrice, format.mintick), style=label.style_label_left)
lbSL := label.new(bar_index, sl, "SL: " + str.tostring(sl, format.mintick), style=label.style_label_left)
lbTP1 := label.new(bar_index, tp1, "TP1: " + str.tostring(tp1, format.mintick), style=label.style_label_left)
lbTP2 := label.new(bar_index, tp2, "TP2: " + str.tostring(tp2, format.mintick), style=label.style_label_left)
array.push(aEntryLine, lnEntry)
array.push(aSLLine, lnSL)
array.push(aTP1Line, lnTP1)
array.push(aTP2Line, lnTP2)
array.push(aRunLine, lnRun)
array.push(aEntryLab, lbEntry)
array.push(aSLLab, lbSL)
array.push(aTP1Lab, lbTP1)
array.push(aTP2Lab, lbTP2)
array.push(aDir, dir)
array.push(aEntry, entryPrice)
array.push(aRisk, riskPts)
array.push(aTP1, tp1)
array.push(aTP2, tp2)
array.push(aRunnerSL, sl)
array.push(aBE, false)
array.push(aActive, true)
array.push(aTP1Done, false)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// UPDATE RUNNER + HIT LABELS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
int n = array.size(aDir)
if n > 0
for i = 0 to n - 1
if array.get(aActive, i)
int dir = array.get(aDir, i)
float entry = array.get(aEntry, i)
float riskPts = array.get(aRisk, i)
float tp1 = array.get(aTP1, i)
float tp2 = array.get(aTP2, i)
float runSL = array.get(aRunnerSL, i)
bool beAct = array.get(aBE, i)
bool tp1Done = array.get(aTP1Done, i)
// TP1 hit (label once)
tp1HitNow = dir == 1 ? (high >= tp1) : (low <= tp1)
if showHitLabels and (not tp1Done) and tp1HitNow
label.new(bar_index, tp1, "TP1 HIT", style=label.style_label_down)
tp1Done := true
// Activate BE after +1R
if not beAct
beTrigger = dir == 1 ? (high >= entry + riskPts) : (low <= entry - riskPts)
if beTrigger
beAct := true
runSL := entry
// Trail
trailCandidate = dir == 1 ? (close - atr * trailAtrMult) : (close + atr * trailAtrMult)
if (not trailFromBE) or beAct
runSL := dir == 1 ? math.max(runSL, trailCandidate) : math.min(runSL, trailCandidate)
// Update runner line
if showRunnerTrail
f_set_line_y(array.get(aRunLine, i), runSL)
// Exit hits
slHitNow = dir == 1 ? (low <= runSL) : (high >= runSL)
tp2HitNow = dir == 1 ? (high >= tp2) : (low <= tp2)
if showHitLabels and slHitNow
label.new(bar_index, runSL, "SL HIT", style=label.style_label_up)
if showHitLabels and tp2HitNow
label.new(bar_index, tp2, "TP2 HIT", style=label.style_label_down)
if slHitNow or tp2HitNow
array.set(aActive, i, false)
// Save updates
array.set(aBE, i, beAct)
array.set(aRunnerSL, i, runSL)
array.set(aTP1Done, i, tp1Done)
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// PLOTS + SIGNAL MARKERS + ALERTS
//━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
plot(ema20, "EMA20 (Entry TF)", linewidth=2)
plot(m15EmaSlow, "M15 EMA200", linewidth=2)
plotshape(buySignal, title="BUY", style=shape.triangleup, location=location.belowbar, size=size.small, text="BUY")
plotshape(sellSignal, title="SELL", style=shape.triangledown, location=location.abovebar, size=size.small, text="SELL")
alertcondition(buySignal, title="Buy Alert", message="BUY signal: M15 trend + EMA20 reclaim + RSI confirm")
alertcondition(sellSignal, title="Sell Alert", message="SELL signal: M15 trend + EMA20 reclaim + RSI confirm")