本页源码来自 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/
// © orbitonetv
//@version=6
indicator("Trend Targets [OrbitOne]", "Trend Targets [OrbitOne]", overlay = true, max_lines_count = 500, max_labels_count = 500)
// ════════ TREND ════════
gT = "Trend"
len = input.int(10, "Trend length", minval = 2, maxval = 200, group = gT, tooltip = "Length of the SMA of highs/lows that forms the channel. 5–10: short term, more signals. Higher values: fewer but stronger signals.")
atrLen = input.int(200, "ATR length", minval = 1, group = gT)
bandMult = input.float(0.8, "Band width (ATR multiplier)", minval = 0, step = 0.1, group = gT, tooltip = "Distance added outside the SMA channel. Higher values require a stronger move to flip the trend and reduce false signals.")
confirm = input.bool(true, "Wait for bar close to confirm signals", group = gT, tooltip = "When enabled, signals only appear on closed bars and never repaint.")
sideOpt = input.string("Both", "Signal direction", options = ["Both", "Long only", "Short only"], group = gT)
// ════════ TARGETS ════════
gH = "Targets & Stop"
tMode = input.string("ATR", "Target / stop unit", options = ["ATR", "Price"], group = gH, tooltip = "ATR: distances adapt to volatility.\nPrice: fixed price distance (e.g. 0.20).")
nT = input.int(3, "Number of targets", minval = 1, maxval = 3, group = gH)
tStep = input.float(1.0, "Target spacing", minval = 0.01, step = 0.05, group = gH, tooltip = "In ATR units, 1.0 = each target is 1 ATR further away.\nIn price units, 0.20 = TP1 +0.20, TP2 +0.40, TP3 +0.60.")
slMode = input.string("Fixed distance", "Stop placement", options = ["Trend line", "Fixed distance"], group = gH)
slDist = input.float(1.0, "Fixed stop distance", minval = 0.01, step = 0.05, group = gH, tooltip = "Used only when stop placement is 'Fixed distance'. Same unit as targets.")
lineLen = input.int(20, "Line length (bars)", minval = 3, maxval = 300, group = gH)
onlyLast = input.bool(true, "Show lines of the latest signal only", group = gH)
costPct = input.float(0.05, "Round-trip trading cost %", minval = 0, step = 0.01, group = gH, tooltip = "Fees + slippage for opening and closing one trade, used in the statistics table. Set it to match your broker.")
// ════════ STYLE ════════
gG = "Style"
upCol = input.color(#00C853, "Bullish / long color", group = gG)
dnCol = input.color(#FF1744, "Bearish / short color", group = gG)
entCol = input.color(#2962FF, "Entry line color", group = gG)
tpCol = input.color(#00C853, "Target (TP) color", group = gG)
slCol = input.color(#FF1744, "Stop (SL) color", group = gG)
lnW = input.int(2, "Line width", minval = 1, maxval = 6, group = gG)
showTL = input.bool(true, "Show trend line", group = gG)
showFill = input.bool(true, "Trend fill", group = gG)
showSig = input.bool(true, "Long / short labels", group = gG)
barCol = input.bool(false, "Color bars by trend", group = gG)
ctrOn = input.bool(true, "Highlight counter-trend bars", group = gG, tooltip = "Bars that close against the current trend (up bars in a downtrend, down bars in an uptrend) get their own color.")
ctrCol = input.color(#00B0FF, "Counter-trend bar color", group = gG)
showTbl = input.bool(true, "Statistics table", group = gG)
tblPos = input.string("Bottom left", "Table position", options = ["Top right", "Bottom right", "Top left", "Bottom left"], group = gG)
tblSize = input.string("Tiny", "Table text size", options = ["Tiny", "Small", "Normal"], group = gG)
// ════════ CALCULATION ════════
atrRaw = ta.atr(atrLen)
band = atrRaw * bandMult
upper = ta.sma(high, len) + band
lower = ta.sma(low, len) - band
unit = tMode == "ATR" ? atrRaw : 1.0
okBar = not confirm or barstate.isconfirmed
upX = ta.crossover(close, upper)
dnX = ta.crossunder(close, lower)
var int trend = 0
buySig = false
sellSig = false
if upX and okBar and trend != 1
trend := 1
buySig := sideOpt != "Short only"
if dnX and okBar and trend != -1
trend := -1
sellSig := sideOpt != "Long only"
trendLine = trend == 1 ? lower : trend == -1 ? upper : na
// ════════ SIGNAL TRACKING ════════
var array<line> lns = array.new<line>()
var array<label> lbs = array.new<label>()
var int dir = 0
var int entBar = na
var float ent = na
var float slP = na
var float tp1 = na
var float tp2 = na
var float tp3 = na
var bool h1 = false
var bool h2 = false
var bool h3 = false
var bool done = true
var label lbT1 = na
var label lbT2 = na
var label lbT3 = na
var label lbS = na
var int nSig = 0
var int c1 = 0
var int c2 = 0
var int c3 = 0
var int cSl = 0
var bool open1 = false
var int w1 = 0
var int l1 = 0
var float pnlPct = 0.0
hit1Now = false
slNow = false
// check the open signal against its targets and stop
if not done and bar_index > entBar
if dir == 1
if low <= slP
done := true
slNow := true
if not h1
cSl += 1
label.set_text(lbS, label.get_text(lbS) + " ✗")
else
if not h1 and high >= tp1
h1 := true
c1 += 1
hit1Now := true
label.set_text(lbT1, label.get_text(lbT1) + " ✓")
if nT >= 2 and not h2 and high >= tp2
h2 := true
c2 += 1
label.set_text(lbT2, label.get_text(lbT2) + " ✓")
if nT >= 3 and not h3 and high >= tp3
h3 := true
c3 += 1
label.set_text(lbT3, label.get_text(lbT3) + " ✓")
else
if high >= slP
done := true
slNow := true
if not h1
cSl += 1
label.set_text(lbS, label.get_text(lbS) + " ✗")
else
if not h1 and low <= tp1
h1 := true
c1 += 1
hit1Now := true
label.set_text(lbT1, label.get_text(lbT1) + " ✓")
if nT >= 2 and not h2 and low <= tp2
h2 := true
c2 += 1
label.set_text(lbT2, label.get_text(lbT2) + " ✓")
if nT >= 3 and not h3 and low <= tp3
h3 := true
c3 += 1
label.set_text(lbT3, label.get_text(lbT3) + " ✓")
if (nT == 1 and h1) or (nT == 2 and h2) or (nT == 3 and h3)
done := true
// "exit at TP1" result, in percent of entry price
if open1 and slNow and not h1
pnlPct += dir * (slP - ent) / ent * 100 - costPct
l1 += 1
open1 := false
else if open1 and hit1Now
pnlPct += dir * (tp1 - ent) / ent * 100 - costPct
w1 += 1
open1 := false
f_level(float y, color col, string sty, string txt) =>
s = sty == "solid" ? line.style_solid : sty == "dashed" ? line.style_dashed : line.style_dotted
ln = line.new(bar_index, y, bar_index + lineLen, y, color = col, width = sty == "solid" ? lnW + 1 : lnW, style = s)
lb = label.new(bar_index + lineLen, y, txt, style = label.style_label_left, color = col, textcolor = color.white, size = size.small)
array.push(lns, ln)
array.push(lbs, lb)
lb
// new signal
if buySig or sellSig
// previous signal reached neither TP1 nor SL before the opposite signal: close it here
if open1
r = dir * (close - ent) / ent * 100 - costPct
pnlPct += r
if r > 0
w1 += 1
else
l1 += 1
open1 := true
if onlyLast
while array.size(lns) > 0
line.delete(array.shift(lns))
while array.size(lbs) > 0
label.delete(array.shift(lbs))
dir := buySig ? 1 : -1
ent := close
entBar := bar_index
slP := slMode == "Trend line" ? (dir == 1 ? lower : upper) : ent - dir * slDist * unit
tp1 := ent + dir * tStep * unit
tp2 := ent + dir * 2 * tStep * unit
tp3 := ent + dir * 3 * tStep * unit
h1 := false
h2 := false
h3 := false
done := false
nSig += 1
sideTxt = dir == 1 ? "LONG " : "SHORT "
f_level(ent, entCol, "solid", sideTxt + str.tostring(ent, format.mintick))
lbS := f_level(slP, slCol, "dashed", "SL " + str.tostring(slP, format.mintick) + " (" + str.tostring(slP - ent, format.mintick) + ")")
lbT1 := f_level(tp1, tpCol, "dotted", "TP1 " + str.tostring(tp1, format.mintick) + " (" + str.tostring(tp1 - ent, format.mintick) + ")")
lbT2 := nT >= 2 ? f_level(tp2, tpCol, "dotted", "TP2 " + str.tostring(tp2, format.mintick) + " (" + str.tostring(tp2 - ent, format.mintick) + ")") : na
lbT3 := nT >= 3 ? f_level(tp3, tpCol, "dotted", "TP3 " + str.tostring(tp3, format.mintick) + " (" + str.tostring(tp3 - ent, format.mintick) + ")") : na
if showSig
if dir == 1
label.new(bar_index, low, "LONG", yloc = yloc.belowbar, style = label.style_label_up, color = upCol, textcolor = color.white, size = size.normal)
else
label.new(bar_index, high, "SHORT", yloc = yloc.abovebar, style = label.style_label_down, color = dnCol, textcolor = color.white, size = size.normal)
while array.size(lns) > 450
line.delete(array.shift(lns))
while array.size(lbs) > 400
label.delete(array.shift(lbs))
// ════════ PLOTS ════════
tCol = trend == 1 ? upCol : trend == -1 ? dnCol : color.gray
pTL = plot(showTL ? trendLine : na, "Trend line", color = tCol, linewidth = 2, style = plot.style_linebr)
pMid = plot(hl2, "Mid", display = display.none)
fill(pTL, pMid, color = showFill and showTL ? color.new(tCol, 88) : na, title = "Trend fill")
ctrBar = (trend == -1 and close > open) or (trend == 1 and close < open)
barcolor(ctrOn and ctrBar ? ctrCol : barCol ? tCol : na)
// ════════ STATISTICS TABLE ════════
var table tb = table.new(tblPos == "Top right" ? position.top_right : tblPos == "Bottom right" ? position.bottom_right : tblPos == "Top left" ? position.top_left : position.bottom_left, 2, 9, border_width = 1)
tSz = tblSize == "Tiny" ? size.tiny : tblSize == "Small" ? size.small : size.normal
f_pct(int c) =>
nSig > 0 ? str.tostring(float(c) / float(nSig) * 100, "#") + "% (" + str.tostring(c) + ")" : "—"
if showTbl and barstate.islast
bgC = color.new(color.gray, 35)
table.cell(tb, 0, 0, "Trend (" + str.tostring(len) + ")", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 0, trend == 1 ? "UP ▲" : trend == -1 ? "DOWN ▼" : "—", bgcolor = tCol, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 1, "Last signal", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 1, dir == 0 ? "—" : (dir == 1 ? "LONG " : "SHORT ") + str.tostring(ent, format.mintick) + (done ? "" : " (open)"), bgcolor = dir == 1 ? upCol : dir == -1 ? dnCol : bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 2, "Signals on chart", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 2, str.tostring(nSig), bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 3, "Reached TP1", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 3, f_pct(c1), bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 4, "Reached TP2", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 4, nT >= 2 ? f_pct(c2) : "—", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 5, "Reached TP3", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 5, nT >= 3 ? f_pct(c3) : "—", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 6, "SL before any target", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 6, f_pct(cSl), bgcolor = dnCol, text_color = color.white, text_size = tSz)
tot1 = w1 + l1
table.cell(tb, 0, 7, "Exit at TP1: wins / losses", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 7, str.tostring(w1) + " / " + str.tostring(l1) + (tot1 > 0 ? " " + str.tostring(float(w1) / float(tot1) * 100, "#") + "%" : ""), bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 0, 8, "Exit at TP1: net % (after costs)", bgcolor = bgC, text_color = color.white, text_size = tSz)
table.cell(tb, 1, 8, (pnlPct > 0 ? "+" : "") + str.tostring(pnlPct, "#.##") + "%", bgcolor = pnlPct >= 0 ? upCol : dnCol, text_color = color.white, text_size = tSz)
// ════════ ALERTS ════════
alertcondition(buySig, "Long signal", "LONG | {{ticker}} | {{interval}} | {{close}}")
alertcondition(sellSig, "Short signal", "SHORT | {{ticker}} | {{interval}} | {{close}}")
alertcondition(hit1Now, "TP1 hit", "TP1 | {{ticker}} | {{interval}} | {{close}}")
alertcondition(slNow, "SL hit", "SL | {{ticker}} | {{interval}} | {{close}}")