本页源码来自 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/
// © MAlphaCore
//@version=6
// M·Alpha·Trend Targets — a trend flip with a stop and three targets (1R, 2R, 3R), and next to every target the share of
// past flips on THIS chart that actually reached it. The trend is an ATR trailing band (the classic Supertrend logic).
// A signal ends when its stop is hit or the trend flips again; if stop and target sit in the same bar, the stop counts.
// The stop label shows how often a signal ended (stop or new flip) before even 1R was reached.
// Everything is decided on closed bars, so nothing repaints.
indicator("M·Alpha·Trend Targets", shorttitle = "M·Alpha·Targets", overlay = true, max_lines_count = 50, max_labels_count = 500)
// ───────────────────────────── Inputs ─────────────────────────────
grTr = "Trend"
atrLen = input.int(10, "ATR length", minval = 1, maxval = 200, group = grTr, display = display.none)
factor = input.float(3.0, "ATR factor", minval = 0.5, maxval = 20, step = 0.1, group = grTr, display = display.none)
grSt = "Statistics"
sample = input.int(100, "Past signals counted", minval = 10, maxval = 1000, group = grSt, display = display.none)
grShow = "Display"
showBand = input.bool(true, "Show trend band", group = grShow, display = display.none)
showHits = input.bool(true, "Mark target hits on past signals", group = grShow, display = display.none)
lineLen = input.int(15, "Line length to the right (bars)", minval = 1, maxval = 100, group = grShow, display = display.none)
grStyle = "Style"
colLong = input.color(#ffeb3b, "Long", group = grStyle, display = display.none)
colShort = input.color(#b2b5be, "Short", group = grStyle, display = display.none)
colStop = input.color(#787b86, "Stop", group = grStyle, display = display.none)
colText = input.color(#e8eaf0, "Text", group = grStyle, display = display.none)
textSizeIn = input.string("Small", "Text size", options = ["Tiny", "Small", "Normal"], group = grStyle, display = display.none)
// ───────────────────────────── Trend ─────────────────────────────
[band, dir] = ta.supertrend(factor, atrLen) // dir < 0: up trend, dir > 0: down trend
flipUp = barstate.isconfirmed and dir < 0 and dir[1] > 0
flipDown = barstate.isconfirmed and dir > 0 and dir[1] < 0
textSize = textSizeIn == "Tiny" ? size.tiny : textSizeIn == "Normal" ? size.normal : size.small
// ───────────────────────────── Signal state ─────────────────────────────
var int side = 0 // 1 long, -1 short, 0 none
var float entry = na
var float stop = na
var float risk = na
var int reached = 0 // highest target reached so far (0..3)
var bool stopped = false
var array<int> results = array.new<int>() // per finished signal: highest target reached, -1 = stopped before 1R
var array<line> curLines = array.new<line>()
var array<label> curLabels = array.new<label>()
hit1 = false
hit2 = false
hit3 = false
hitStop = false
// follow the open signal on every closed bar after its flip bar
if barstate.isconfirmed and side != 0 and not stopped and not flipUp and not flipDown
hitS = side == 1 ? low <= stop : high >= stop
if hitS
stopped := true
hitStop := true
else if reached < 3
for k = reached + 1 to 3
tgt = entry + side * k * risk
if side == 1 ? high >= tgt : low <= tgt
reached := k
hit1 := hit1 or k == 1
hit2 := hit2 or k == 2
hit3 := hit3 or k == 3
finish() =>
results.push(stopped and reached == 0 ? -1 : reached)
if results.size() > sample
results.shift()
rate(int k) =>
n = results.size()
c = 0
for r in results
if k == 0 ? r == -1 : r >= k
c += 1
n > 0 ? 100.0 * c / n : na
pct(float v) => na(v) ? "–" : str.tostring(v, "#") + " %"
drawSignal() =>
for l in curLines
l.delete()
for lb in curLabels
lb.delete()
curLines.clear()
curLabels.clear()
col = side == 1 ? colLong : colShort
n = results.size()
x2 = bar_index + lineLen
curLines.push(line.new(bar_index, entry, x2, entry, color = col, width = 2))
curLabels.push(label.new(x2, entry, "Entry", style = label.style_label_left, color = color.new(col, 85), textcolor = colText, size = textSize))
curLines.push(line.new(bar_index, stop, x2, stop, color = colStop, width = 2, style = line.style_dashed))
curLabels.push(label.new(x2, stop, "Stop · ended before 1R " + pct(na(rate(1)) ? na : 100 - rate(1)), style = label.style_label_left, color = color.new(colStop, 85), textcolor = colText, size = textSize))
for k = 1 to 3
tgt = entry + side * k * risk
curLines.push(line.new(bar_index, tgt, x2, tgt, color = color.new(col, 30), width = 1, style = line.style_dotted))
curLabels.push(label.new(x2, tgt, str.tostring(k) + "R · reached " + pct(rate(k)) + (k == 3 ? " (last " + str.tostring(n) + " signals)" : ""), style = label.style_label_left, color = color.new(col, 85), textcolor = colText, size = textSize))
// a new flip closes the old signal and opens a new one
if flipUp or flipDown
if side != 0
finish()
side := flipUp ? 1 : -1
entry := close
stop := band
risk := math.abs(close - band)
reached := 0
stopped := false
drawSignal()
// keep the line ends and labels moving with the newest bar
if barstate.islast and curLines.size() > 0
for l in curLines
l.set_x2(bar_index + lineLen)
for lb in curLabels
lb.set_x(bar_index + lineLen)
// ───────────────────────────── Plots ─────────────────────────────
plot(showBand ? band : na, "Trend band", color = dir < 0 ? color.new(colLong, 40) : color.new(colShort, 40), style = plot.style_linebr, linewidth = 2)
plotshape(flipUp, "Long flip", shape.triangleup, location.belowbar, colLong, size = size.small)
plotshape(flipDown, "Short flip", shape.triangledown, location.abovebar, colShort, size = size.small)
hitY = side == 1 ? high : low
stopY = side == 1 ? low : high
hitCol = side == 1 ? colLong : colShort
plotchar(showHits and hit1 ? hitY : na, "1R reached", "1", location.absolute, hitCol, size = size.tiny)
plotchar(showHits and hit2 ? hitY : na, "2R reached", "2", location.absolute, hitCol, size = size.tiny)
plotchar(showHits and hit3 ? hitY : na, "3R reached", "3", location.absolute, hitCol, size = size.tiny)
plotshape(showHits and hitStop ? stopY : na, "Stop hit", shape.xcross, location.absolute, colStop, size = size.tiny)
alertcondition(flipUp, "Long flip", "M·Alpha·Trend Targets: trend flipped up")
alertcondition(flipDown, "Short flip", "M·Alpha·Trend Targets: trend flipped down")