本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
strategy("Channel Breakout 100/50 + Daily ATR Stop + R-Ladder Trail (4H)", 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 ----------
entryLen = input.int(100, "Entry channel (candles)")
exitLen = input.int(50, "Exit channel (candles)")
atrLen = input.int(14, "Daily ATR length")
atrMult = input.float(2.0, "Stop = daily ATR x", step = 0.1)
tpR = input.float(4.0, "Take profit (R)", step = 0.5)
trailStartR = input.float(2.0, "Trail start (R)", step = 0.5)
trailStepR = input.float(1.0, "Trail step (R)", step = 0.5)
riskPct = input.float(1.0, "Risk % per trade", step = 0.25)
useLong = input.bool(true, "Allow longs")
useShort = input.bool(true, "Allow shorts")
showTable = input.bool(true, "Show rules table")
// ---------- Daily close + daily ATR (last closed day, no repaint) ----------
[dClose, dAtr] = request.security(syminfo.tickerid, "D", [close[1], ta.atr(atrLen)[1]],
lookahead = barmerge.lookahead_on)
// ---------- Channels (current candle excluded) ----------
hiEntry = ta.highest(high, entryLen)[1]
loEntry = ta.lowest(low, entryLen)[1]
hiExit = ta.highest(high, exitLen)[1]
loExit = ta.lowest(low, exitLen)[1]
longStop = dClose - atrMult * dAtr
shortStop = dClose + atrMult * dAtr
longRisk = close - longStop
shortRisk = shortStop - close
longSig = useLong and close > hiEntry and longRisk > 0
shortSig = useShort and close < loEntry and shortRisk > 0
// ---------- Trade state ----------
var float entryPx = na
var float riskR = na
var float stopPx = na
var float tpPx = na
var float bestPx = na
qtyFor(r) =>
math.min(strategy.equity * riskPct / 100 / r, strategy.equity / close)
if longSig and strategy.position_size <= 0
entryPx := close
riskR := longRisk
stopPx := longStop
tpPx := close + tpR * longRisk
bestPx := close
strategy.entry("Long", strategy.long, qty = qtyFor(longRisk))
strategy.exit("Long exit", "Long", stop = stopPx, limit = tpPx)
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 shortSig and strategy.position_size >= 0
entryPx := close
riskR := shortRisk
stopPx := shortStop
tpPx := close - tpR * shortRisk
bestPx := close
strategy.entry("Short", strategy.short, qty = qtyFor(shortRisk))
strategy.exit("Short exit", "Short", stop = stopPx, limit = tpPx)
label.new(bar_index, high, "SHORT", style = label.style_label_down,
color = color.new(color.red, 0), textcolor = color.white, size = size.small)
else if strategy.position_size > 0
bestPx := math.max(bestPx, high)
gainR = (bestPx - entryPx) / riskR
if gainR >= trailStartR
steps = math.floor((gainR - trailStartR) / trailStepR)
stopPx := math.max(stopPx, entryPx + steps * trailStepR * riskR)
strategy.exit("Long exit", "Long", stop = stopPx, limit = tpPx)
if close < loExit
strategy.close("Long", comment = "Close < " + str.tostring(exitLen) + "-low")
else if strategy.position_size < 0
bestPx := math.min(bestPx, low)
gainR = (entryPx - bestPx) / riskR
if gainR >= trailStartR
steps = math.floor((gainR - trailStartR) / trailStepR)
stopPx := math.min(stopPx, entryPx - steps * trailStepR * riskR)
strategy.exit("Short exit", "Short", stop = stopPx, limit = tpPx)
if close > hiExit
strategy.close("Short", comment = "Close > " + str.tostring(exitLen) + "-high")
// ---------- Drawing ----------
inLong = strategy.position_size > 0
inShort = strategy.position_size < 0
plot(hiEntry, "Entry channel high (long trigger)", color = color.new(color.green, 0), linewidth = 2)
plot(loEntry, "Entry channel low (short trigger)", color = color.new(color.red, 0), linewidth = 2)
plot(inLong ? loExit : inShort ? hiExit : na, "Exit channel level",
color = color.orange, style = plot.style_linebr, linewidth = 2)
plot(inLong or inShort ? stopPx : na, "Stop / trailing stop", color = color.red, style = plot.style_linebr, linewidth = 2)
plot(inLong or inShort ? tpPx : na, "Take profit", color = color.lime, style = plot.style_linebr)
var table t = table.new(position.top_right, 1, 6, bgcolor = color.new(color.black, 20))
if showTable and barstate.islast
table.cell(t, 0, 0, "RULES (4H)", text_color = color.white, text_size = size.small)
table.cell(t, 0, 1, "Long: close > " + str.tostring(entryLen) + "-candle high", text_color = color.white, text_size = size.small)
table.cell(t, 0, 2, "Short: close < " + str.tostring(entryLen) + "-candle low", text_color = color.white, text_size = size.small)
table.cell(t, 0, 3, "Stop: " + str.tostring(atrMult) + " x daily ATR | TP: " + str.tostring(tpR) + "R", text_color = color.white, text_size = size.small)
table.cell(t, 0, 4, "Trail: break-even at " + str.tostring(trailStartR) + "R, then +1R steps", text_color = color.white, text_size = size.small)
table.cell(t, 0, 5, "Exit: close beyond " + str.tostring(exitLen) + "-candle level", text_color = color.white, text_size = size.small)