Active Target (RichPole)

RichPole · study · 194 行 · 点赞 186 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=6
// Active Target (RichPole)
// Maps the recent price range, a switch zone in the middle, and two targets beyond the range.
// When a candle CLOSES above the upper switch line, the up side becomes active and targets are set above the range.
// When a candle CLOSES below the lower switch line, the down side becomes active and targets are set below the range.
// Targets are fixed at the switch. A panel shows how often the targets were reached on this chart.
// Closed candles only. Past switches and targets are never redrawn.
// Educational tool. It does not give buy or sell signals. Targets are a map, not a promise.
indicator("Active Target (RichPole)", shorttitle = "Active Target", overlay = true, max_lines_count = 500, max_labels_count = 500)
 
// ---------- Inputs ----------
grpR = "Range"
rangeMode = input.string("Custom bars", "Range length", options = ["Custom bars", "About 1 day", "About 1 week"], group = grpR, display = display.none, tooltip = "About 1 day / 1 week use the chart timeframe (24-hour days). For markets with short sessions, use Custom bars. Example: 1-minute NIFTY = 375 bars for one session.")
rangeBars = input.int(100, "Custom bars", minval = 10, maxval = 5000, group = grpR, display = display.none)
zonePct   = input.float(61.8, "Switch line (% of range)", minval = 50.1, maxval = 95.0, step = 0.1, group = grpR, display = display.none, tooltip = "Upper switch line = this % of the range. Lower switch line = the mirror (100 minus this). 61.8 gives a 38.2-61.8 middle zone.")
 
grpT = "Targets"
t1Pct   = input.float(27.2, "Target 1: beyond the range (% of range)", minval = 1.0, step = 0.1, group = grpT, display = display.none, tooltip = "27.2 = the 127.2% extension.")
showT2  = input.bool(true, "Show Target 2", group = grpT, display = display.none)
t2Pct   = input.float(61.8, "Target 2: beyond the range (% of range)", minval = 1.0, step = 0.1, group = grpT, display = display.none, tooltip = "61.8 = the 161.8% extension.")
keepPast = input.int(5, "Past switches to keep on the chart", minval = 0, maxval = 100, group = grpT, display = display.none)
 
grpD = "Display"
showRange = input.bool(true, "Range high and low", group = grpD, display = display.none)
showZone  = input.bool(true, "Switch zone", group = grpD, display = display.none)
showNotes = input.bool(true, "Notes on the chart", group = grpD, display = display.none)
showStats = input.bool(true, "Hit-rate panel", group = grpD, display = display.none)
statsPos  = input.string("Bottom right", "Panel position", options = ["Top right", "Bottom right", "Bottom left", "Top left"], group = grpD, display = display.none)
 
grpC = "Colours"
upColor   = input.color(#26a69a, "Up side", group = grpC, display = display.none)
dnColor   = input.color(#ef5350, "Down side", group = grpC, display = display.none)
zoneColor = input.color(color.new(#8a96a8, 88), "Switch zone fill", group = grpC, display = display.none)
rangeCol  = input.color(color.new(#8a96a8, 40), "Range lines", group = grpC, display = display.none)
noteGrey  = input.color(#8a96a8, "Back-in-zone note", group = grpC, display = display.none)
 
// ---------- Range ----------
int   tfSec   = timeframe.in_seconds(timeframe.period)
int   dayBars = math.max(10, math.min(5000, math.round(86400.0 / tfSec)))
int   N       = rangeMode == "About 1 day" ? dayBars : rangeMode == "About 1 week" ? math.min(5000, dayBars * 5) : rangeBars
float H       = ta.highest(high, N)
float L       = ta.lowest(low, N)
float R       = H - L
float zf      = zonePct / 100.0
float upLine  = L + zf * R
float dnLine  = L + (1.0 - zf) * R
bool  ready   = bar_index >= N and R > 0
 
// ---------- Helpers ----------
fmtPrice(float p) =>
    str.tostring(p, format.mintick)
 
pct(int a, int b) =>
    b > 0 ? str.tostring(100.0 * a / b, "#") + "% (" + str.tostring(a) + "/" + str.tostring(b) + ")" : "-"
 
// ---------- State ----------
var int   side     = 0
var int   flipBar  = na
var float t1       = na
var float t2       = na
var bool  t1Hit    = false
var bool  t2Hit    = false
var bool  backDone = false
var int   flips    = 0
var int   t1Hits   = 0
var int   t1Miss   = 0
var int   t2Hits   = 0
var int   t2Miss   = 0
var float barsToT1 = 0.0
var line  t1Line   = na
var line  t2Line   = na
var label t1Lab    = na
var label t2Lab    = na
var array<line>  oldLines  = array.new<line>()
var array<label> oldLabels = array.new<label>()
 
bool evUp   = false
bool evDown = false
bool evT1   = false
bool evT2   = false
bool evBack = false
 
note(int x, float y, string txt, color bg, color fg, bool below, string tip) =>
    if showNotes
        label.new(x, y, txt, xloc = xloc.bar_index, style = below ? label.style_label_up : label.style_label_down, color = bg, textcolor = fg, size = size.tiny, tooltip = tip, text_formatting = text.format_bold)
 
archive(line ln1, line ln2, label lb1, label lb2) =>
    // Move the finished switch's lines into history and keep only the newest few
    for ln in array.from(ln1, ln2)
        if not na(ln)
            array.push(oldLines, ln)
    for lb in array.from(lb1, lb2)
        if not na(lb)
            array.push(oldLabels, lb)
    while array.size(oldLines) > keepPast * 2
        line.delete(array.shift(oldLines))
    while array.size(oldLabels) > keepPast * 2
        label.delete(array.shift(oldLabels))
 
if barstate.isconfirmed and ready
    // ----- Targets reached? (only on candles after the switch) -----
    if side != 0 and bar_index > flipBar
        if not t1Hit and (side == 1 ? high >= t1 : low <= t1)
            t1Hit := true
            t1Hits += 1
            barsToT1 += bar_index - flipBar
            evT1 := true
            line.set_style(t1Line, line.style_solid)
            line.set_x2(t1Line, bar_index)
            label.set_text(t1Lab, "T1 ✓ " + fmtPrice(t1))
            note(bar_index, side == 1 ? high : low, "T1 hit", side == 1 ? upColor : dnColor, color.white, side == -1, "Target 1 reached " + str.tostring(bar_index - flipBar) + " bars after the switch.")
        if showT2 and not t2Hit and (side == 1 ? high >= t2 : low <= t2)
            t2Hit := true
            t2Hits += 1
            evT2 := true
            line.set_style(t2Line, line.style_solid)
            line.set_x2(t2Line, bar_index)
            label.set_text(t2Lab, "T2 ✓ " + fmtPrice(t2))
            note(bar_index, side == 1 ? high : low, "T2 hit", side == 1 ? upColor : dnColor, color.white, side == -1, "Target 2 reached " + str.tostring(bar_index - flipBar) + " bars after the switch.")
        // ----- Back in the zone (first time only) -----
        if not backDone and close < upLine and close > dnLine
            backDone := true
            evBack := true
            note(bar_index, side == 1 ? high : low, "Back in zone", color.new(noteGrey, 20), color.white, side == -1, "Price closed back inside the switch zone. The move is pausing or failing.")
 
    // ----- Switch to the up side or the down side -----
    bool goUp   = close > upLine and side != 1
    bool goDown = close < dnLine and side != -1
    if goUp or goDown
        if side != 0
            if not t1Hit
                t1Miss += 1
            if showT2 and not t2Hit
                t2Miss += 1
            archive(t1Line, t2Line, t1Lab, t2Lab)
        side     := goUp ? 1 : -1
        flipBar  := bar_index
        t1       := goUp ? H + t1Pct / 100.0 * R : L - t1Pct / 100.0 * R
        t2       := goUp ? H + t2Pct / 100.0 * R : L - t2Pct / 100.0 * R
        t1Hit    := false
        t2Hit    := false
        backDone := false
        flips    += 1
        evUp     := goUp
        evDown   := goDown
        color sc = goUp ? upColor : dnColor
        t1Line := line.new(bar_index, t1, bar_index + 1, t1, xloc = xloc.bar_index, color = sc, style = line.style_dashed, width = 1)
        t1Lab  := label.new(bar_index + 1, t1, "T1 " + fmtPrice(t1), xloc = xloc.bar_index, style = label.style_label_left, color = color.new(sc, 85), textcolor = sc, size = size.tiny, text_formatting = text.format_bold)
        if showT2
            t2Line := line.new(bar_index, t2, bar_index + 1, t2, xloc = xloc.bar_index, color = color.new(sc, 30), style = line.style_dotted, width = 1)
            t2Lab  := label.new(bar_index + 1, t2, "T2 " + fmtPrice(t2), xloc = xloc.bar_index, style = label.style_label_left, color = color.new(sc, 85), textcolor = color.new(sc, 20), size = size.tiny, text_formatting = text.format_bold)
        else
            t2Line := na
            t2Lab  := na
        note(bar_index, goUp ? low : high, goUp ? "Up side" : "Down side", sc, color.white, goUp, (goUp ? "Closed above the upper switch line. Targets set above the range: T1 " : "Closed below the lower switch line. Targets set below the range: T1 ") + fmtPrice(t1) + (showT2 ? ", T2 " + fmtPrice(t2) : "") + ".")
 
    // ----- Stretch the open target lines to the newest candle -----
    if side != 0
        if not t1Hit
            line.set_x2(t1Line, bar_index + 1)
            label.set_x(t1Lab, bar_index + 1)
        if showT2 and not t2Hit
            line.set_x2(t2Line, bar_index + 1)
            label.set_x(t2Lab, bar_index + 1)
 
// ---------- Plots ----------
plot(showRange and ready ? H : na, "Range high", color = rangeCol, style = plot.style_stepline, linewidth = 1)
plot(showRange and ready ? L : na, "Range low", color = rangeCol, style = plot.style_stepline, linewidth = 1)
pUp = plot(showZone and ready ? upLine : na, "Upper switch line", color = color.new(upColor, 50), style = plot.style_stepline, linewidth = 1)
pDn = plot(showZone and ready ? dnLine : na, "Lower switch line", color = color.new(dnColor, 50), style = plot.style_stepline, linewidth = 1)
fill(pUp, pDn, color = zoneColor, title = "Switch zone")
 
// ---------- Hit-rate panel ----------
pos = statsPos == "Top right" ? position.top_right : statsPos == "Bottom left" ? position.bottom_left : statsPos == "Top left" ? position.top_left : position.bottom_right
var table panel = table.new(pos, 2, 5, bgcolor = color.new(#000000, 25), border_width = 0, frame_color = color.new(#8a96a8, 70), frame_width = 1)
if showStats and barstate.islast
    color tc = color.new(color.white, 10)
    table.cell(panel, 0, 0, "Active Target", text_color = color.white, text_size = size.small, text_formatting = text.format_bold)
    table.cell(panel, 1, 0, "this chart", text_color = color.new(#8a96a8, 0), text_size = size.small)
    table.cell(panel, 0, 1, "Switches", text_color = tc, text_size = size.small, text_halign = text.align_left)
    table.cell(panel, 1, 1, str.tostring(flips), text_color = tc, text_size = size.small, text_halign = text.align_right)
    table.cell(panel, 0, 2, "T1 reached", text_color = tc, text_size = size.small, text_halign = text.align_left)
    table.cell(panel, 1, 2, pct(t1Hits, t1Hits + t1Miss), text_color = upColor, text_size = size.small, text_halign = text.align_right)
    table.cell(panel, 0, 3, "T2 reached", text_color = tc, text_size = size.small, text_halign = text.align_left)
    table.cell(panel, 1, 3, showT2 ? pct(t2Hits, t2Hits + t2Miss) : "off", text_color = upColor, text_size = size.small, text_halign = text.align_right)
    table.cell(panel, 0, 4, "Avg bars to T1", text_color = tc, text_size = size.small, text_halign = text.align_left)
    table.cell(panel, 1, 4, t1Hits > 0 ? str.tostring(barsToT1 / t1Hits, "#") : "-", text_color = tc, text_size = size.small, text_halign = text.align_right)
 
// ---------- Alerts (fire once the candle closes) ----------
alertcondition(evUp, "Up side", "Active Target: closed above the upper switch line, up targets set")
alertcondition(evDown, "Down side", "Active Target: closed below the lower switch line, down targets set")
alertcondition(evT1, "Target 1 hit", "Active Target: Target 1 reached")
alertcondition(evT2, "Target 2 hit", "Active Target: Target 2 reached")
alertcondition(evBack, "Back in zone", "Active Target: price closed back inside the switch zone")

← 返回列表