Sage Trend (RichPole)

RichPole · study · 184 行 · 点赞 60 · TradingView 原页

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

Pine Script

//@version=6
// Sage Trend (RichPole) v2
// Tells you what kind of market you are in: up trend, down trend, or no trend.
// Direction: an ATR trend line on the price chart. It flips only when a candle closes through it.
// Strength: ADX (J. Welles Wilder). The line and the columns are coloured only while the trend is strong enough to count.
// Grey = no trend: the direction exists, but there is not enough strength behind it.
// One-word notes: Uptrend / Downtrend (once per trend leg, when strength confirms it) and Fading (once, when strength drops from its peak).
// A flip guard stops opposite notes printing within 20 bars of each other.
// A small map shows the same reading on higher timeframes.
// Notes, alerts and higher-timeframe values use closed candles only. Nothing is redrawn.
// Educational tool. It does not give buy or sell signals. It tells you which playbook fits the market.
indicator("Sage Trend (RichPole)", shorttitle = "Sage Trend", overlay = false, max_labels_count = 500)
 
// ---------- Inputs ----------
grpL = "Trend line (direction)"
lineMult = input.float(4.0, "Line distance (x ATR)", minval = 0.5, step = 0.5, group = grpL, display = display.none, tooltip = "Bigger = smoother line and fewer flips. Smaller = faster line, more flips.")
lineAtr  = input.int(10, "Line ATR length", minval = 1, group = grpL, display = display.none)
 
grpS = "Trend strength (ADX)"
adxLen   = input.int(14, "ADX length", minval = 2, group = grpS, display = display.none)
startLv  = input.float(25.0, "Trend starts when ADX rises above", minval = 5.0, step = 1.0, group = grpS, display = display.none, tooltip = "A trend counts when ADX is above this level and still rising.")
endLv    = input.float(20.0, "Trend ends when ADX falls below", minval = 1.0, step = 1.0, group = grpS, display = display.none, tooltip = "Keep this a few points under the start level so the trend does not switch on and off around one line.")
strongLv = input.float(40.0, "Strong trend above", minval = 10.0, step = 1.0, group = grpS, display = display.none)
minAge   = input.int(2, "Bars on the new side before a note", minval = 1, group = grpS, display = display.none, tooltip = "The line must stay on its new side for this many closed candles before a trend can be confirmed.")
flipGuard = input.int(20, "No opposite note within (bars)", minval = 0, group = grpS, display = display.none, tooltip = "Stops Downtrend and Uptrend notes printing on top of each other when the line flips back quickly. The line still changes colour. 0 = off.")
 
grpF = "Fading"
fadeMin   = input.float(35.0, "Only after ADX reached", minval = 10.0, step = 1.0, group = grpF, display = display.none)
fadeDrop  = input.float(8.0, "ADX drop from its peak", minval = 1.0, step = 1.0, group = grpF, display = display.none)
fadeBuild = input.float(5.0, "ADX must first rise by", minval = 0.0, step = 1.0, group = grpF, display = display.none, tooltip = "The trend must first build strength after its note before it can show Fading.")
 
grpN = "Notes"
showNotes = input.bool(true, "Notes on the price chart", group = grpN, display = display.none)
noteTrend = input.bool(true, "Uptrend / Downtrend", group = grpN, display = display.none)
noteFade  = input.bool(true, "Fading", group = grpN, display = display.none)
noteFlat  = input.bool(false, "No trend", group = grpN, display = display.none)
 
grpD = "Display"
showLine  = input.bool(true, "Trend line on the price chart", group = grpD, display = display.none)
showFill  = input.bool(true, "Soft fill while trending", group = grpD, display = display.none)
showLv    = input.bool(true, "Level lines in the pane", group = grpD, display = display.none)
colorBars = input.bool(false, "Colour candles by trend", group = grpD, display = display.none)
 
grpT = "Trend map"
showMap = input.bool(true, "Show trend map", group = grpT, display = display.none)
mapPos  = input.string("Top right", "Map position", options = ["Top right", "Bottom right", "Bottom left", "Top left"], group = grpT, display = display.none)
tf1     = input.timeframe("15", "Timeframe 1", group = grpT, display = display.none)
tf2     = input.timeframe("60", "Timeframe 2", group = grpT, display = display.none)
tf3     = input.timeframe("240", "Timeframe 3", group = grpT, display = display.none)
 
grpC = "Colours"
upColor   = input.color(#26a69a, "Up trend", group = grpC, display = display.none)
dnColor   = input.color(#ef5350, "Down trend", group = grpC, display = display.none)
flatColor = input.color(#5d6675, "No trend", group = grpC, display = display.none)
fadeColor = input.color(#f7c948, "Fading note", group = grpC, display = display.none)
 
// ---------- Core logic (one function, used for the chart and for the map) ----------
sageTrend() =>
    [stLine, stDir] = ta.supertrend(lineMult, lineAtr)
    [dp, dm, ax] = ta.dmi(adxLen, adxLen)
    int dirN = stDir < 0 ? 1 : -1                 // 1 = line below price (up), -1 = line above price (down)
    var bool  on     = false
    var bool  noted  = false
    var bool  faded  = false
    var float peak   = 0.0
    var float startA = 0.0
    var int   legStart = 0
    var int   lastNoteBar = na
    var int   lastNoteDir = 0
    bool startNow = false
    bool fadeNow  = false
    bool endNow   = false
    if dirN != nz(dirN[1], dirN)
        // the line flipped: a new leg starts, unconfirmed (grey) until strength agrees
        on    := false
        noted := false
        faded := false
        peak  := 0.0
        legStart := bar_index
    int  age     = bar_index - legStart + 1
    bool guardOk = flipGuard <= 0 or lastNoteDir != -dirN or na(lastNoteBar) or bar_index - lastNoteBar >= flipGuard
    if not on and ax >= startLv and ax > ax[1] and age >= minAge
        on := true
        if not noted
            noted  := true
            startA := ax
            peak   := ax
            if guardOk
                startNow    := true
                lastNoteBar := bar_index
                lastNoteDir := dirN
    else if on and ax < endLv
        on     := false
        endNow := true
    if on
        peak := math.max(peak, ax)
        if not faded and noted and peak >= fadeMin and peak - startA >= fadeBuild and ax <= peak - fadeDrop
            faded   := true
            fadeNow := true
    [stLine, dirN, on, ax, startNow, fadeNow, endNow]
 
[tLine, tDir, tOn, tAdx, tStart, tFade, tEnd] = sageTrend()
bool flipped = tDir != nz(tDir[1], tDir)
 
// ---------- Colours ----------
color stateCol  = tOn ? (tDir == 1 ? upColor : dnColor) : flatColor
int   lineAlpha = tOn ? (tAdx >= strongLv ? 0 : 20) : 35
 
// ---------- Price chart: trend line and soft fill ----------
pLine = plot(showLine ? tLine : na, "Trend line", color = flipped ? na : color.new(stateCol, lineAlpha), linewidth = 2, style = plot.style_linebr, force_overlay = true)
pMid  = plot(showLine and showFill ? hl2 : na, "Fill anchor", color = na, display = display.none, editable = false, force_overlay = true)
fill(pLine, pMid, color = showFill and tOn and not flipped ? color.new(stateCol, 94) : na, title = "Trend fill")
barcolor(colorBars ? stateCol : na, title = "Candle colour by trend")
 
// ---------- Pane: trend strength columns ----------
plot(tAdx, "Trend strength (ADX)", color = tOn ? color.new(tDir == 1 ? upColor : dnColor, tAdx >= strongLv ? 0 : 35) : color.new(flatColor, 30), style = plot.style_columns)
plot(showLv ? startLv : na, "Trend level", color = color.new(#8a96a8, 40), style = plot.style_circles, linewidth = 1, display = display.pane)
plot(showLv ? strongLv : na, "Strong level", color = color.new(#8a96a8, 70), style = plot.style_circles, linewidth = 1, display = display.pane)
 
// ---------- Notes (closed candles only) ----------
bool evUp   = barstate.isconfirmed and tStart and tDir == 1
bool evDown = barstate.isconfirmed and tStart and tDir == -1
bool evFade = barstate.isconfirmed and tFade
bool evFlat = barstate.isconfirmed and tEnd
bool evFlip = barstate.isconfirmed and flipped
 
if showNotes and barstate.isconfirmed
    string tip = "ADX " + str.tostring(tAdx, "#.0")
    if noteTrend and evUp
        label.new(bar_index, low, "Uptrend", style = label.style_label_up, color = upColor, textcolor = color.white, size = size.tiny, text_formatting = text.format_bold, tooltip = "Price is above the trend line and trend strength is rising above " + str.tostring(startLv) + ". " + tip, force_overlay = true)
    if noteTrend and evDown
        label.new(bar_index, high, "Downtrend", style = label.style_label_down, color = dnColor, textcolor = color.white, size = size.tiny, text_formatting = text.format_bold, tooltip = "Price is below the trend line and trend strength is rising above " + str.tostring(startLv) + ". " + tip, force_overlay = true)
    if noteFade and evFade
        label.new(bar_index, tDir == 1 ? high : low, "Fading", style = tDir == 1 ? label.style_label_down : label.style_label_up, color = fadeColor, textcolor = color.black, size = size.tiny, text_formatting = text.format_bold, tooltip = "Trend strength dropped " + str.tostring(fadeDrop) + " points from its peak. The move is losing power. It does not mean reverse. " + tip, force_overlay = true)
    if noteFlat and evFlat
        label.new(bar_index, tDir == 1 ? high : low, "No trend", style = tDir == 1 ? label.style_label_down : label.style_label_up, color = color.new(flatColor, 10), textcolor = color.white, size = size.tiny, text_formatting = text.format_bold, tooltip = "Trend strength fell below " + str.tostring(endLv) + ". Range rules apply. " + tip, force_overlay = true)
 
// ---------- Trend map (higher timeframes, last closed bar only) ----------
mapVals(string tf) =>
    request.security(syminfo.tickerid, tf, [tDir[1], tOn[1], tAdx[1], tAdx[2]], lookahead = barmerge.lookahead_on)
 
[d1, o1, a1, b1] = mapVals(tf1)
[d2, o2, a2, b2] = mapVals(tf2)
[d3, o3, a3, b3] = mapVals(tf3)
 
tfName(string tf) =>
    int sec = timeframe.in_seconds(tf)
    sec < 60 ? tf : sec < 3600 ? str.tostring(sec / 60) + "m" : sec < 86400 ? str.tostring(sec / 3600) + "h" : tf
 
stText(int d, bool o, float a) =>
    na(a) ? "-" : not o ? "No trend" : d == 1 ? (a >= strongLv ? "Strong up" : "Up") : (a >= strongLv ? "Strong down" : "Down")
 
stColor(int d, bool o, float a) =>
    na(a) or not o ? color.new(#8a96a8, 0) : d == 1 ? upColor : dnColor
 
strText(float a, float aPrev) =>
    na(a) ? "-" : str.tostring(a, "#") + (a > aPrev ? " ▲" : " ▼")
 
pos = mapPos == "Top right" ? position.top_right : mapPos == "Bottom left" ? position.bottom_left : mapPos == "Top left" ? position.top_left : position.bottom_right
var table trendMap = table.new(pos, 3, 5, bgcolor = color.new(#000000, 25), frame_color = color.new(#8a96a8, 70), frame_width = 1)
if showMap and barstate.islast
    color hc = color.new(#8a96a8, 0)
    table.cell(trendMap, 0, 0, "Sage Trend", text_color = color.white, text_size = size.small, text_formatting = text.format_bold, text_halign = text.align_left)
    table.cell(trendMap, 1, 0, "Trend", text_color = hc, text_size = size.small)
    table.cell(trendMap, 2, 0, "Strength", text_color = hc, text_size = size.small)
    table.cell(trendMap, 0, 1, "Chart", text_color = color.white, text_size = size.small, text_halign = text.align_left)
    table.cell(trendMap, 1, 1, stText(tDir[1], tOn[1], tAdx[1]), text_color = stColor(tDir[1], tOn[1], tAdx[1]), text_size = size.small, text_formatting = text.format_bold)
    table.cell(trendMap, 2, 1, strText(tAdx[1], tAdx[2]), text_color = color.white, text_size = size.small)
    table.cell(trendMap, 0, 2, tfName(tf1), text_color = color.white, text_size = size.small, text_halign = text.align_left)
    table.cell(trendMap, 1, 2, stText(d1, o1, a1), text_color = stColor(d1, o1, a1), text_size = size.small, text_formatting = text.format_bold)
    table.cell(trendMap, 2, 2, strText(a1, b1), text_color = color.white, text_size = size.small)
    table.cell(trendMap, 0, 3, tfName(tf2), text_color = color.white, text_size = size.small, text_halign = text.align_left)
    table.cell(trendMap, 1, 3, stText(d2, o2, a2), text_color = stColor(d2, o2, a2), text_size = size.small, text_formatting = text.format_bold)
    table.cell(trendMap, 2, 3, strText(a2, b2), text_color = color.white, text_size = size.small)
    table.cell(trendMap, 0, 4, tfName(tf3), text_color = color.white, text_size = size.small, text_halign = text.align_left)
    table.cell(trendMap, 1, 4, stText(d3, o3, a3), text_color = stColor(d3, o3, a3), text_size = size.small, text_formatting = text.format_bold)
    table.cell(trendMap, 2, 4, strText(a3, b3), text_color = color.white, text_size = size.small)
 
// ---------- Alerts (on candle close) ----------
alertcondition(evUp, "Uptrend", "Sage Trend: uptrend confirmed")
alertcondition(evDown, "Downtrend", "Sage Trend: downtrend confirmed")
alertcondition(evFade, "Fading", "Sage Trend: trend fading")
alertcondition(evFlat, "No trend", "Sage Trend: trend strength fell below the end level")
alertcondition(evFlip, "Trend line flip", "Sage Trend: price closed through the trend line")

← 返回列表