本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © NJ_Algo
//@version=6
indicator("Reversal Probability ATR Bands [NJ]", "RPAB", overlay = true, behind_chart = false)
// ███╗ ██╗ ██╗ █████╗ ██╗ ██████╗ ██████╗
// ████╗ ██║ ██║ ██╔══██╗██║ ██╔════╝ ██╔═══██╗
// ██╔██╗ ██║ ██║ ███████║██║ ██║ ███╗██║ ██║
// ██║╚██╗██║ ██║ ██╔══██║██║ ██║ ██║██║ ██║
// ██║ ╚████║ ██╗ ██║ ██║ ██║██║ ██║ ██║██║ ██║
// ██║ ╚███║ ╚████╔╝ ██║ ██║███████╗╚██████╔╝╚██████╔╝
// ╚═╝ ╚══╝ ╚═══╝ ╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝
// ✧═════════════════✧
// ║ INPUTS ║
// ✧═════════════════✧
const string g1 = "✧ DISPLAY ✧"
colorMode = input.string("Colored", "Color mode", options = ["Colored", "Neutral"], group = g1)
dispCandles = input.bool(true, "Color candles", group = g1)
const string g2 = "✧ CALCULATION ✧"
maType = input.string("SMA", "MA type", ["SMA", "EMA", "WMA", "VWMA", "RMA", "HMA"], group = g2)
maLen = input.int(70, "MA length", group = g2)
atrMulti = input.float(2, "Bands multiplier", step = 0.1, group = g2)
const string g3 = "✧ PROBABILITY TABLE ✧"
showStats = input.bool(true, "Show statistics table", group = g3)
statsPos = input.string("Bottom right", "Table position", options = ["Top left", "Middle left", "Bottom left", "Top right", "Middle right", "Bottom right", "Top center", "Middle center", "Bottom center"], group = g3)
horizon = input.int(30, "Bars to reach MA", group = g3, tooltip = "A signal counts as a win if price reaches the MA within this many bars.")
// ✧══════════════════════✧
// ║ CALCULATION ║
// ✧══════════════════════✧
ma = switch maType
"SMA" => ta.sma(close, maLen)
"EMA" => ta.ema(close, maLen)
"WMA" => ta.wma(close, maLen)
"VWMA" => ta.vwma(close, maLen)
"RMA" => ta.rma(close, maLen)
"HMA" => ta.hma(close, maLen)
atr = ta.atr(50)
ub1 = ma + atr * 1 * atrMulti
ub2 = ma + atr * 2 * atrMulti
ub3 = ma + atr * 3 * atrMulti
lb1 = ma - atr * 1 * atrMulti
lb2 = ma - atr * 2 * atrMulti
lb3 = ma - atr * 3 * atrMulti
// ✧════════════════✧
// ║ PLOTS ║
// ✧════════════════✧
// Colors
longCol = colorMode == "Colored" ? color.rgb(0, 136, 100) : colorMode == "Neutral" ? color.rgb(179, 179, 179) : na
shortCol = colorMode == "Colored" ? color.rgb(120, 0, 32) : colorMode == "Neutral" ? color.rgb(60, 60, 60) : na
neutCol = color.rgb(0, 43, 136)
sigCol = close >= open ? longCol : close < open ? shortCol : na
// Display bands, MA and fills
uf1 = plot(ub1, "Upper band 1", longCol)
uf2 = plot(ub2, "Upper band 2", longCol)
uf3 = plot(ub3, "Upper band 3", longCol, 2)
mid = plot(ma, "MA", neutCol)
lf1 = plot(lb1, "Lower band 1", shortCol)
lf2 = plot(lb2, "Lower band 2", shortCol)
lf3 = plot(lb3, "Lower band 3", shortCol, 2)
fill(mid, uf1, color.new(longCol, 90))
fill(uf1, uf2, color.new(longCol, 80))
fill(uf2, uf3, color.new(longCol, 70))
fill(mid, lf1, color.new(shortCol, 90))
fill(lf1, lf2, color.new(shortCol, 80))
fill(lf2, lf3, color.new(shortCol, 70))
// Color candles
plotcandle(
open, high, low, close,
"Colored candles",
dispCandles ? sigCol : na,
dispCandles ? sigCol : na,
bordercolor = dispCandles ? sigCol : na,
display = display.all - display.status_line - display.price_scale)
// Arrow signals
longSig = low < lb2 and close > lb2 and barstate.isconfirmed
shortSig = high > ub2 and close < ub2 and barstate.isconfirmed
plotshape(longSig, "Long", shape.triangleup, location.belowbar, longCol, size = size.small)
plotshape(shortSig, "Short", shape.triangledown, location.abovebar, shortCol, size = size.small)
// ✧═══════════════════════════✧
// ║ SIGNAL STATISTICS ║
// ✧═══════════════════════════✧
// Band index: 0 = Upper 3, 1 = Upper 2, 2 = Upper 1, 3 = Lower 1, 4 = Lower 2, 5 = Lower 3
bandLevel(int b) =>
switch b
0 => ub3
1 => ub2
2 => ub1
3 => lb1
4 => lb2
=> lb3
var array<int> openBar = array.new<int>()
var array<int> openBand = array.new<int>()
var array<int> bTotal = array.new<int>(6, 0)
var array<int> bWins = array.new<int>(6, 0)
var array<int> bBars = array.new<int>(6, 0)
if barstate.isconfirmed
// Check open signals: did price reach the MA, or did time run out?
if openBar.size() > 0
for i = openBar.size() - 1 to 0
int b = openBand.get(i)
int age = bar_index - openBar.get(i)
bool hit = b < 3 ? low <= ma : high >= ma
if hit or age >= horizon
bTotal.set(b, bTotal.get(b) + 1)
if hit
bWins.set(b, bWins.get(b) + 1)
bBars.set(b, bBars.get(b) + age)
openBar.remove(i)
openBand.remove(i)
// Start tracking new signals at every band
for b = 0 to 5
float lvl = bandLevel(b)
bool sig = b < 3 ? high > lvl and close < lvl : low < lvl and close > lvl
if sig
openBar.push(bar_index)
openBand.push(b)
// ✧══════════════════════════✧
// ║ STATISTICS TABLE ║
// ✧══════════════════════════✧
posOf(string p) =>
switch p
"Top left" => position.top_left
"Middle left" => position.middle_left
"Bottom left" => position.bottom_left
"Top right" => position.top_right
"Middle right" => position.middle_right
"Top center" => position.top_center
"Middle center" => position.middle_center
"Bottom center" => position.bottom_center
=> position.bottom_right
rateText(int wins, int total) => total > 0 ? str.tostring(math.round(float(wins) / total * 100, 2), "0.00") + " %" : "-"
barsText(int bars, int wins) => wins > 0 ? str.tostring(math.round(float(bars) / wins, 2), "0.00") : "-"
var stTbl = table.new(posOf(statsPos), 4, 7, bgcolor = #131722, frame_color = #363A45, frame_width = 1, border_color = #2A2E39, border_width = 1)
stHead(int col, string txt) =>
table.cell(stTbl, col, 0, txt, text_color = color.white, bgcolor = neutCol, text_size = size.normal, text_formatting = text.format_bold)
stRow(int row, string name, int b) =>
bg = row % 2 == 0 ? #1C2030 : #131722
total = bTotal.get(b)
wins = bWins.get(b)
bars = bBars.get(b)
table.cell(stTbl, 0, row, name, text_color = #B2B5BE, bgcolor = bg, text_halign = text.align_left, text_size = size.normal)
table.cell(stTbl, 1, row, str.tostring(total), text_color = color.white, bgcolor = bg, text_halign = text.align_right, text_size = size.normal, text_font_family = font.family_monospace)
table.cell(stTbl, 2, row, rateText(wins, total), text_color = color.white, bgcolor = bg, text_halign = text.align_right, text_size = size.normal, text_font_family = font.family_monospace)
table.cell(stTbl, 3, row, barsText(bars, wins), text_color = color.white, bgcolor = bg, text_halign = text.align_right, text_size = size.normal, text_font_family = font.family_monospace)
if barstate.islast and showStats
stHead(0, "Band")
stHead(1, "Count")
stHead(2, "Reached MA")
stHead(3, "Avg bars")
for b = 0 to 5
stRow(b + 1, b < 3 ? "Upper band " + str.tostring(3 - b) : "Lower band " + str.tostring(b - 2), b)