本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
//@version=6
indicator("Chandelier Exit - Flip Statistics [Dots3Red]",
shorttitle = "Chandelier Stats [D3R]",
overlay = true,
max_labels_count = 500)
// Inputs
C_TXT = color.new(#f8fafc, 0)
C_DIM = color.new(#94a3b8, 0)
C_BG = color.new(#131722, 5)
C_BORD = color.new(#334155, 0)
C_GOOD = color.new(#00c896, 0)
C_BAD = color.new(#ff4466, 0)
C_UP = color.new(#00f0ff, 0)
C_DN = color.new(#ff00aa, 0)
C_AMBER = color.new(#ffb700, 0)
C_BAR = color.new(#b2b5be,0)
GRP_CE = "🕯️ Chandelier Exit"
GRP_ENG = "🎯 Flip Statistics"
GRP_VIS = "🎨 Visuals"
GRP_HUD = "🖥️ Dashboard"
ce_len = input.int(22, "ATR Period", minval=2, group=GRP_CE)
ce_mult = input.float(3.0, "ATR Multiplier", minval=0.5, step=0.1, group=GRP_CE)
use_close = input.bool(true, "Use Close for Extremes", group=GRP_CE,
tooltip="On: the stop trails the highest/lowest close. Off: it trails the highest high / lowest low.")
min_n = input.int(10, "Min Trades to Show Stats", minval=3, group=GRP_ENG,
tooltip="Each flip opens a hypothetical trade that closes at the next opposite flip. 1R is the distance from the entry close to the Chandelier stop at entry.")
col_long = input.color(#00f0ff, "Long Stop Color", group=GRP_VIS)
col_short = input.color(#ff00aa, "Short Stop Color", group=GRP_VIS)
w_stop = input.int(2, "Stop Line Thickness", minval=1, maxval=6, group=GRP_VIS)
show_fill = input.bool(true, "Fill Between Price and Stop", group=GRP_VIS)
fill_tr = input.int(90, "Fill Transparency", minval=50, maxval=100, group=GRP_VIS)
show_lbl = input.bool(true, "Flip Labels", group=GRP_VIS)
use_bars = input.bool(false, "Use Gray Bars", group=GRP_VIS)
lbl_long = input.color(#00f0ff, "Long Label Color", group=GRP_VIS)
lbl_short = input.color(#ff00aa, "Short Label Color", group=GRP_VIS)
show_hud = input.bool(true, "Show Dashboard", group=GRP_HUD)
hud_pos = input.string("Top Right", "Position",
options=["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group=GRP_HUD)
float atr_m = ce_mult * ta.atr(ce_len)
float long_stop = (use_close ? ta.highest(close, ce_len) : ta.highest(high, ce_len)) - atr_m
float long_prev = nz(long_stop[1], long_stop)
long_stop := close[1] > long_prev ? math.max(long_stop, long_prev) : long_stop
float short_stop = (use_close ? ta.lowest(close, ce_len) : ta.lowest(low, ce_len)) + atr_m
float short_prev = nz(short_stop[1], short_stop)
short_stop := close[1] < short_prev ? math.min(short_stop, short_prev) : short_stop
var int dir = 1
dir := close > short_prev ? 1 : close < long_prev ? -1 : dir
bool flip_long = dir == 1 and dir[1] == -1
bool flip_short = dir == -1 and dir[1] == 1
var int t_dir = 0
var float t_entry = na
var float t_risk = na
var float t_mfe = 0.0
var float[] st_n = array.new<float>(2, 0.0)
var float[] st_win = array.new<float>(2, 0.0)
var float[] st_r = array.new<float>(2, 0.0)
var float[] st_mfe = array.new<float>(2, 0.0)
var float[] st_1r = array.new<float>(2, 0.0)
var float[] st_2r = array.new<float>(2, 0.0)
bool new_long = false
bool new_short = false
if barstate.isconfirmed and not na(atr_m)
if t_dir != 0 and t_risk > 0
float fav = t_dir > 0 ? high - t_entry : t_entry - low
t_mfe := math.max(t_mfe, fav / t_risk)
if (flip_long or flip_short) and t_dir != 0 and t_risk > 0
int side = t_dir > 0 ? 0 : 1
float res = (close - t_entry) * t_dir / t_risk
array.set(st_n, side, array.get(st_n, side) + 1.0)
array.set(st_r, side, array.get(st_r, side) + res)
array.set(st_mfe, side, array.get(st_mfe, side) + t_mfe)
if res > 0
array.set(st_win, side, array.get(st_win, side) + 1.0)
if t_mfe >= 1.0
array.set(st_1r, side, array.get(st_1r, side) + 1.0)
if t_mfe >= 2.0
array.set(st_2r, side, array.get(st_2r, side) + 1.0)
if flip_long or flip_short
t_dir := flip_long ? 1 : -1
t_entry := close
float stp_at = flip_long ? long_stop : short_stop
t_risk := (close - stp_at) * (flip_long ? 1 : -1) > 0 ? math.abs(close - stp_at) : na
t_mfe := 0.0
new_long := flip_long
new_short := flip_short
f_pct(int side, float[] arr) =>
float n = array.get(st_n, side)
n >= min_n ? str.tostring(math.round(array.get(arr, side) / n * 100.0)) + "%" : "…"
f_avg_r(int side, float[] arr) =>
float n = array.get(st_n, side)
n >= min_n ? str.tostring(array.get(arr, side) / n, "#.##") + "R" : "…"
// Dashboard
f_hud_pos(string s) =>
switch s
"Top Right" => position.top_right
"Top Left" => position.top_left
"Bottom Right" => position.bottom_right
=> position.bottom_left
var table hud = table.new(f_hud_pos(hud_pos), 7, 6,
bgcolor=C_BG, border_color=C_BORD,
border_width=1, frame_color=C_BORD, frame_width=2)
f_side_row(int row, int side, color bg) =>
float n = array.get(st_n, side)
float avg = n >= min_n ? array.get(st_r, side) / n : na
table.cell(hud, 0, row, side == 0 ? "▲ Long" : "▼ Short", text_color = side == 0 ? C_UP : C_DN, bgcolor=bg, text_size=size.small)
table.cell(hud, 1, row, str.tostring(int(n)), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 2, row, f_pct(side, st_win), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 3, row, f_avg_r(side, st_r), text_color = na(avg) ? C_DIM : avg > 0 ? C_GOOD : C_BAD, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 4, row, f_avg_r(side, st_mfe), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 5, row, f_pct(side, st_1r), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 6, row, f_pct(side, st_2r), text_color=C_TXT, bgcolor=bg, text_size=size.small, text_halign=text.align_center)
if show_hud and barstate.islast
color bgA = color.new(#1e293b, 40)
color bgB = color.new(#0f172a, 40)
table.cell(hud, 0, 0, "CHANDELIER STATS [D3R]", text_color=C_TXT,
bgcolor=color.new(#1e293b, 0), text_size=size.small, text_halign=text.align_center)
table.merge_cells(hud, 0, 0, 6, 0)
table.cell(hud, 0, 1, "Side", text_color=C_DIM, bgcolor=bgB, text_size=size.small)
table.cell(hud, 1, 1, "Trades", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
table.cell(hud, 2, 1, "Win %", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
tooltip="Share of flip-to-flip trades that closed in profit at the next opposite flip.")
table.cell(hud, 3, 1, "Avg R", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
tooltip="Average result per trade, in multiples of the initial risk (entry to Chandelier stop at entry).")
table.cell(hud, 4, 1, "Avg MFE", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
tooltip="Average maximum favorable excursion: the best open profit each trade reached, in R.")
table.cell(hud, 5, 1, "≥ 1R", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
tooltip="Share of trades that were at least 1R in profit at some point.")
table.cell(hud, 6, 1, "≥ 2R", text_color=C_DIM, bgcolor=bgB, text_size=size.small, text_halign=text.align_center,
tooltip="Share of trades that were at least 2R in profit at some point.")
f_side_row(2, 0, bgA)
f_side_row(3, 1, bgB)
float open_r = t_dir != 0 and t_risk > 0 ? (close - t_entry) * t_dir / t_risk : na
table.cell(hud, 0, 4, "Open Trade", text_color=C_DIM, bgcolor=bgA, text_size=size.small)
table.cell(hud, 1, 4, t_dir == 0 ? "—" : (t_dir > 0 ? "Long · " : "Short · ") + (na(open_r) ? "—" : str.tostring(open_r, "#.##")) + "R now · best " + str.tostring(t_mfe, "#.##") + "R",
text_color = t_dir > 0 ? C_UP : t_dir < 0 ? C_DN : C_DIM, bgcolor=bgA, text_size=size.small, text_halign=text.align_center)
table.merge_cells(hud, 1, 4, 6, 4)
table.cell(hud, 0, 5, "Settings", text_color=C_DIM, bgcolor=bgB, text_size=size.small)
table.cell(hud, 1, 5, str.tostring(ce_len) + " × " + str.tostring(ce_mult) + " ATR · " + (use_close ? "close" : "high/low"),
text_color=C_TXT, bgcolor=bgB, text_size=size.small, text_halign=text.align_center)
table.merge_cells(hud, 1, 5, 6, 5)
// Visual
p_mid = plot(ohlc4, "Mid", display=display.none)
p_long = plot(dir == 1 ? long_stop : na, "Long Stop", color=col_long, linewidth=w_stop, style=plot.style_linebr)
p_short = plot(dir == -1 ? short_stop : na, "Short Stop", color=col_short, linewidth=w_stop, style=plot.style_linebr)
fill(p_mid, p_long, color = show_fill and dir == 1 ? color.new(col_long, fill_tr) : na, title="Long Fill")
fill(p_mid, p_short, color = show_fill and dir == -1 ? color.new(col_short, fill_tr) : na, title="Short Fill")
f_lbl_txt(int side, string base) =>
float n = array.get(st_n, side)
n >= min_n ? base + " · win " + str.tostring(math.round(array.get(st_win, side) / n * 100.0)) + "%" : base
if show_lbl and new_long
label.new(bar_index+9, low*0.996, f_lbl_txt(0, "▲ Long"),
style=label.style_label_up, color=color.new(#000000, 100), textcolor=lbl_long, size=size.small)
if show_lbl and new_short
label.new(bar_index+9, high *1.004, f_lbl_txt(1, "▼ Short"),
style=label.style_label_down, color=color.new(#000000, 100), textcolor=lbl_short, size=size.small)
barcolor( use_bars ? C_BAR: na)
// Alert
alertcondition(flip_long and barstate.isconfirmed, "Chandelier Long Flip", "D3R Chandelier Stats: flipped long on {{ticker}}")
alertcondition(flip_short and barstate.isconfirmed, "Chandelier Short Flip", "D3R Chandelier Stats: flipped short on {{ticker}}")