本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("Daily Trend Breakout + R-Ladder Trail", overlay = true,
initial_capital = 10000, currency = currency.USDT,
commission_type = strategy.commission.percent, commission_value = 0.1,
process_orders_on_close = true, pyramiding = 0, max_labels_count = 500)
// ---------- Inputs ----------
emaLen = input.int(50, "Breakout EMA")
trendLen = input.int(200, "Trend EMA")
exitLen = input.int(20, "Exit EMA")
lookback = input.int(5, "Breakout lookback (candles)")
atrLen = input.int(14, "ATR length")
atrMult = input.float(1.5, "Stop = ATR x", step = 0.1)
trailStartR = input.float(1.0, "Trail start (R)", step = 0.5)
trailStepR = input.float(1.0, "Trail step (R)", step = 0.5)
trailLockR = input.float(1.0, "Trail lock (R behind)", step = 0.5)
riskPct = input.float(1.0, "Risk % per trade", step = 0.25)
showTable = input.bool(true, "Show rules table")
// ---------- Indicators ----------
emaB = ta.ema(close, emaLen)
emaT = ta.ema(close, trendLen)
emaX = ta.ema(close, exitLen)
atr = ta.atr(atrLen)
prevH = ta.highest(high, lookback)[1] // highest high of previous candles, current excluded
uptrend = emaB > emaT
longSig = close > emaB and close > prevH and uptrend
stopDist = atr * atrMult
// ---------- Trade state ----------
var float entryPx = na
var float riskR = na
var float stopPx = na
var float bestHigh = na
if longSig and strategy.position_size == 0
qty = math.min(strategy.equity * riskPct / 100 / stopDist, strategy.equity / close)
strategy.entry("Long", strategy.long, qty = qty)
entryPx := close
riskR := stopDist
stopPx := close - stopDist
bestHigh := close
strategy.exit("Stop", "Long", stop = stopPx)
label.new(bar_index, low, "LONG", style = label.style_label_up,
color = color.new(color.green, 0), textcolor = color.white, size = size.small)
else if strategy.position_size > 0
bestHigh := math.max(bestHigh, high)
gainR = (bestHigh - entryPx) / riskR
if gainR >= trailStartR
k = math.floor((gainR - trailStartR) / trailStepR) * trailStepR + trailStartR
stopPx := math.max(stopPx, entryPx + (k - trailLockR) * riskR)
strategy.exit("Stop", "Long", stop = stopPx)
if close < emaX
strategy.close("Long", comment = "Close < EMA " + str.tostring(exitLen))
// ---------- Drawing ----------
inPos = strategy.position_size > 0
plot(emaX, "Exit EMA 20", color = color.orange, linewidth = 1)
plot(emaB, "Breakout EMA 50", color = color.blue, linewidth = 2)
plot(emaT, "Trend EMA 200", color = color.gray, linewidth = 2)
plot(inPos ? entryPx : na, "Entry", color = color.gray, style = plot.style_linebr)
plot(inPos ? stopPx : na, "Stop / trail", color = color.red, style = plot.style_linebr, linewidth = 2)
bgcolor(uptrend ? color.new(color.green, 94) : na, title = "Uptrend zone")
var table t = table.new(position.top_right, 1, 5, bgcolor = color.new(color.black, 20))
if showTable and barstate.islast
table.cell(t, 0, 0, "RULES (1D)", text_color = color.white, text_size = size.small)
table.cell(t, 0, 1, "Trend: EMA 50 > EMA 200", text_color = color.white, text_size = size.small)
table.cell(t, 0, 2, "Entry: close > EMA 50 + 5-candle high", text_color = color.white, text_size = size.small)
table.cell(t, 0, 3, "Stop: 1.5 x ATR, trail 1R ladder", text_color = color.white, text_size = size.small)
table.cell(t, 0, 4, "Exit: close < EMA 20", text_color = color.white, text_size = size.small)