本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This work is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
// https://creativecommons.org/licenses/by-nc-sa/4.0/
// © BigBeluga
//@version=6
indicator("Step Range Breakout & Trailing Stop [BigBeluga]", overlay=true, precision=2, max_lines_count=500, max_boxes_count=500)
// INPUTS ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
len = input.int(20, "Structure Length", minval=1, tooltip="Lookback period to calculate the highest high and lowest low for structural boundaries.")
len1 = input.int(5, "Consolidation Bars / Length", minval=1, tooltip="Number of consecutive bars the range average must remain unchanged to confirm a valid consolidation zone.")
// Range Display Mode Input
dspMd = input.string("Price", "Range Display Format", options=["Price", "Ticks", "Both"], group="Display Settings", tooltip="Choose whether to show the range value in Price, Ticks/Pips, or Both.")
aLen = input.int(14, "Trailing Stop ATR Length", minval=1, tooltip="Average True Range (ATR) length used for calculating the trailing stop distance.")
aMul = input.float(3.0, "Trailing Stop Multiplier", minval=0.1, step=0.1, tooltip="Multiplier applied to the ATR to set how far the trailing stop trails behind the price.")
wLen = input.int(100, "Win Rate Lookback Trades", minval=1, tooltip="Number of recent closed trades to calculate the win rate percentage.")
// Custom Color Inputs
cBul = input.color(color.rgb(10, 167, 151), "Bullish Color", group="Color Settings", tooltip="Color used for bullish breakouts, fills, and trailing stops.")
cBar = input.color(color.red, "Bearish Color", group="Color Settings", tooltip="Color used for bearish breakouts, fills, and trailing stops.")
cMid = input.color(color.rgb(190, 193, 204), "Mid Line Color", group="Color Settings", tooltip="Color used for the center line of the range.")
// }
// CALCULATIONS――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
hi = ta.highest(high, len)
lo = ta.lowest(low, len)
rAvg = (hi + lo) / 2
// Check if the range average remains completely unchanged over the specified length bars
stbl = ta.change(rAvg, len1) == 0
// Helper function to format the range text based on settings
f_txt(float pUp, float pLo) =>
float pDif = pUp - pLo
float tDif = pDif / syminfo.mintick
string txt = ""
if dspMd == "Price"
txt := "Range: " + str.tostring(pDif, "#.##")
else if dspMd == "Ticks"
txt := "Range: " + str.tostring(tDif, "#.##") + " Ticks"
else
txt := "Range: " + str.tostring(pDif, "#.##") + " (" + str.tostring(tDif, "#.##") + " Ticks)"
txt
// State variables
var float mLVal = na
var float uLVal = na
var float lLVal = na
var float tStop = na
var string stat = "SEARCHING" // "SEARCHING", "ZONE_ACTIVE", "TRAILING"
var bool isBul = true
// Line and Label references for dynamic drawing
var line mLin = na
var label rLab = na
var int zBar = na
// Box array for current active range gradient fill (historical boxes are preserved)
var box[] aBox = array.new_box(0)
int nSli = 16
cAtr = ta.atr(aLen)
bool jstTr = false
// Dashboard tracking variables
var string lDir = "None"
var float lPrc = na
var float eCls = na
var int[] trRes = array.new_int(0)
// State Machine Logic
if stat == "SEARCHING"
if stbl
mLVal := rAvg
uLVal := hi
lLVal := lo
zBar := bar_index - len1
// Create permanent historical lines for the new zone using user input color
mLin := line.new(zBar, mLVal, bar_index, mLVal, width=1, color = cMid)
// Create range label positioned at the horizontal middle of the range length
int midBar = math.round(zBar + (bar_index - zBar) / 2.0)
rLab := label.new(midBar, uLVal, text=f_txt(uLVal, lLVal), color=color.new(cMid, 100), textcolor=chart.fg_color, style=label.style_label_down, size=size.small)
// Initialize new active range boxes without deleting historical past boxes
aBox := array.new_box(0)
sSiz = (uLVal - lLVal) / nSli
for i = 0 to nSli - 1
bBot = lLVal + (i * sSiz)
bTop = lLVal + ((i + 1) * sSiz)
// Calculate gradient transparency for active consolidation state using neutral mid color
dCtr = math.abs(i - (nSli - 1) / 2.0)
mDst = (nSli - 1) / 2.0
trns = math.round(110 - (90 - 50) * (dCtr / mDst))
b = box.new(zBar, bTop, bar_index, bBot, border_color=na, bgcolor=color.new(cMid, int(trns)))
array.push(aBox, b)
stat := "ZONE_ACTIVE"
else if stat == "ZONE_ACTIVE"
// Extend active lines, current gradient boxes, and keep label centered horizontally as time moves on
line.set_x2(mLin, bar_index)
int midBar = math.round(zBar + (bar_index - zBar) / 2.0)
label.set_x(rLab, midBar)
label.set_y(rLab, uLVal)
label.set_text(rLab, f_txt(uLVal, lLVal))
if array.size(aBox) > 0
for b in aBox
box.set_right(b, bar_index)
if close > uLVal and barstate.isconfirmed
isBul := true
tStop := low - (cAtr * aMul)
// Record dashboard metrics for breakout
lDir := "BULLISH"
lPrc := close
eCls := close
// Keep label centered horizontally and pinned to lower line pointing up on bullish breakout
label.set_x(rLab, math.round(zBar + (bar_index - zBar) / 2.0))
label.set_y(rLab, lLVal)
label.set_style(rLab, label.style_label_up)
label.set_textcolor(rLab, chart.fg_color)
// Color gradient boxes pale green on upward breakout
if array.size(aBox) > 0
for i = 0 to array.size(aBox) - 1
b = array.get(aBox, i)
dCtr = math.abs(i - (nSli - 1) / 2.0)
mDst = (nSli - 1) / 2.0
trns = math.round(110 - (90 - 50) * (dCtr / mDst))
box.set_bgcolor(b, color.new(cBul, int(trns)))
stat := "TRAILING"
jstTr := true
else if close < lLVal and barstate.isconfirmed
isBul := false
tStop := high + (cAtr * aMul)
// Record dashboard metrics for breakout
lDir := "BEARISH"
lPrc := close
eCls := close
// Keep label centered horizontally and pinned to upper line pointing down on bearish breakout
label.set_x(rLab, math.round(zBar + (bar_index - zBar) / 2.0))
label.set_y(rLab, uLVal)
label.set_style(rLab, label.style_label_down)
label.set_textcolor(rLab, chart.fg_color)
// Color gradient boxes pale red on downward breakout
if array.size(aBox) > 0
for i = 0 to array.size(aBox) - 1
b = array.get(aBox, i)
dCtr = math.abs(i - (nSli - 1) / 2.0)
mDst = (nSli - 1) / 2.0
trns = math.round(110 - (90 - 50) * (dCtr / mDst))
box.set_bgcolor(b, color.new(cBar, int(trns)))
stat := "TRAILING"
jstTr := true
else if stat == "TRAILING"
if isBul
tStop := math.max(tStop, low - (cAtr * aMul))
if close < tStop and barstate.isconfirmed
bool isWin = isBul ? (close > eCls) : (close < eCls)
array.push(trRes, isWin ? 1 : 0)
if array.size(trRes) > wLen
array.shift(trRes)
stat := "SEARCHING"
else
tStop := math.min(tStop, high + (cAtr * aMul))
if close > tStop and barstate.isconfirmed
bool isWin = isBul ? (close > eCls) : (close < eCls)
array.push(trRes, isWin ? 1 : 0)
if array.size(trRes) > wLen
array.shift(trRes)
stat := "SEARCHING"
// }
// PLOT ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――{
tSPlt = stat == "TRAILING" ? tStop : na
hPlt = stat == "TRAILING" ? hl2 : na
p1 = plot(tSPlt, title="Trailing Stop", color=isBul ? cBul : cBar, linewidth=1, style=plot.style_linebr)
p2 = plot(tSPlt + (isBul ? +cAtr : -cAtr), title="Trailing Stop Reference", color=color.new(color.white, 100), style=plot.style_linebr)
fill(p1, p2, color=color.new(isBul ? cBul : cBar, 90), title="Trailing Stop Fill")
// Circle marker at the exact starting point of the trailing stop
plotshape(jstTr ? tSPlt : na, title="Trailing Start Circle", style=shape.circle, location=location.absolute, color= color.new(isBul ? cBul : cBar, 50), size=size.small)
plotshape(jstTr ? tSPlt : na, title="Trailing Start Circle", style=shape.circle, location=location.absolute, color= color.new(isBul ? cBul : cBar, 0), size=size.tiny)
// Dashboard Table Generation
var table dash = table.new(position = position.top_right, columns = 2, rows = 3, bgcolor = color.new(color.black, 50), border_color = chart.bg_color, border_width = 2)
if barstate.islast
// Calculate Win Rate statistics
int tTrd = array.size(trRes)
int wins = tTrd > 0 ? array.sum(trRes) : 0
float wRat = tTrd > 0 ? (float(wins) / float(tTrd)) * 100.0 : 0.0
// Populate Table Cells
table.cell(dash, 0, 0, "Last Breakout", text_color=chart.fg_color, text_size=size.normal)
table.cell(dash, 1, 0, lDir + "/" + str.tostring(lPrc, "#.##"), text_color=lDir == "BULLISH" ? cBul : cBar, text_size=size.normal)
table.cell(dash, 0, 1, "Closed Trades", text_color=chart.fg_color, text_size=size.normal)
table.cell(dash, 1, 1, str.tostring(tTrd), text_color=chart.fg_color, text_size=size.normal)
table.cell(dash, 0, 2, "Win Rate", text_color=chart.fg_color, text_size=size.normal)
table.cell(dash, 1, 2, str.tostring(wins) + "/" + str.tostring(tTrd) + " (" + str.tostring(wRat, "#.##") + "%)", text_color=wRat >= 50 ? cBul : cBar, text_size=size.normal)
// }