RVOL Follow-Through [ITA]

itamardrori_ · study · 251 行 · 点赞 14 · TradingView 原页

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

Pine Script

// © ITA Trading Tools - itamardrori_
//@version=6
// =============================================================================
// RVOL Follow-Through [ITA]
//
// Every volume tool marks the day that traded twice its normal volume. The
// implied claim is that heavy volume means the move is real. A search for
// "volume follow through" on this platform returns one script.
//
// So this marks the day and then checks it. Each spike is followed for a week
// and scored on two things: did price keep going in the direction the spike
// day moved, and did it hold the spike day's range without closing back
// through the far side of it.
//
// Three decisions worth stating, because they are where this kind of count
// quietly flatters itself:
//
//   the average   is taken over the days BEFORE the spike. Including the
//                 spike day in its own average shrinks every ratio, and a
//                 3x day reads as 2.5x.
//
//   the chart     is daily or higher. Intraday volume is shaped by the clock,
//                 so the first bar of every session is a "spike" against the
//                 bars before it, and the count fills up with opening bells.
//
//   the window    a spike whose week has not finished yet is left out of the
//                 rates, not scored as a failure and not as a success, and
//                 every row says how many spikes it is built on.
// =============================================================================
 
indicator("RVOL Follow-Through [ITA]", overlay = true,
     max_boxes_count = 500, max_labels_count = 500)
 
// ─── INPUTS ──────────────────────────────────────────────────────────────────
grpCalc = "Calculation"
rvLen   = input.int(20, "Days In The Volume Average", minval = 5, maxval = 200, group = grpCalc,
     tooltip = "The spike day is compared against the average of this many days before it, never including itself.")
rvMin   = input.float(2.0, "Spike Threshold (x Average)", minval = 1.2, maxval = 10, step = 0.1, group = grpCalc,
     tooltip = "2.0 means at least twice the normal volume.")
fwdLen  = input.int(5, "Sessions To Follow It", minval = 1, maxval = 60, group = grpCalc,
     tooltip = "How long each spike is followed before it is scored. Five is one trading week.")
 
grpShow   = "Display"
showBox   = input.bool(true, "Draw The Spike Range", group = grpShow,
     tooltip = "The spike day's high and low, carried forward for the follow window.")
showRatio = input.bool(false, "Label The Ratio", group = grpShow)
showStats = input.bool(true, "Show The Count", group = grpShow)
maxDraw   = input.int(40, "Most Recent Spikes To Draw", minval = 1, maxval = 200, group = grpShow,
     tooltip = "Applies to the drawings only. Every spike is counted whatever this is set to.")
statPos   = input.string("Top Right", "Count Position",
     options = ["Top Right", "Top Left", "Bottom Right", "Bottom Left"], group = grpShow)
statSizeStr = input.string("Normal", "Count Text Size",
     options = ["Tiny", "Small", "Normal", "Large", "Huge"], group = grpShow)
 
grpCol = "Colors"
upCol  = input.color(#089981, "Up Day Spike", group = grpCol)
dnCol  = input.color(#f23645, "Down Day Spike", group = grpCol)
 
sizeOf(string s) =>
    r = size.normal
    if s == "Tiny"
        r := size.tiny
    else if s == "Small"
        r := size.small
    else if s == "Large"
        r := size.large
    else if s == "Huge"
        r := size.huge
    r
 
posOf(string s) =>
    r = position.top_right
    if s == "Top Left"
        r := position.top_left
    else if s == "Bottom Right"
        r := position.bottom_right
    else if s == "Bottom Left"
        r := position.bottom_left
    r
 
statSize = sizeOf(statSizeStr)
 
// ─── THE SPIKE ───────────────────────────────────────────────────────────────
wrongTf = timeframe.isintraday
avgVol  = ta.sma(volume, rvLen)[1]       // the days before, not including today
rvol    = avgVol > 0 ? volume / avgVol : na
isSpike = not wrongTf and not na(rvol) and rvol >= rvMin and bar_index > rvLen
dir     = close >= close[1] ? 1 : -1
 
var bool anyVolume = false
if volume > 0
    anyVolume := true
 
// ─── FOLLOWING EACH SPIKE ────────────────────────────────────────────────────
// Spikes can come on consecutive days, so each is followed on its own rather
// than one at a time. A later spike does not cut short the one before it.
var array<int>   pBar   = array.new<int>()
var array<int>   pDir   = array.new<int>()
var array<float> pClose = array.new<float>()
var array<float> pHi    = array.new<float>()
var array<float> pLo    = array.new<float>()
var array<bool>  pHeld  = array.new<bool>()
var array<box>   pBox   = array.new<box>()
var array<box>   drawn  = array.new<box>()
 
var int spikes  = 0
var int upN     = 0
var int upKept  = 0
var int dnN     = 0
var int dnKept  = 0
var int heldN   = 0
var int held    = 0
 
// The baseline. Every day, spike or not, is asked the same question five
// sessions later. Without it the spike rate means nothing: on a stock that
// has risen for twenty years almost any day is higher a week later more often
// than not, and a spike rate of 57% can be no edge at all. The only claim
// volume can make is that it does better than this.
var int baseN  = 0
var int baseUp = 0
if not wrongTf and bar_index > rvLen + fwdLen
    baseN  += 1
    baseUp += close > close[fwdLen] ? 1 : 0
 
// Score what is already open before adding today, so a spike is never judged
// on its own bar.
if array.size(pBar) > 0
    for i = array.size(pBar) - 1 to 0
        age = bar_index - array.get(pBar, i)
        d   = array.get(pDir, i)
 
        // Held means no close back through the far side of the spike day. An
        // up day that closes below its own low has been fully given back.
        if d == 1 and close < array.get(pLo, i)
            array.set(pHeld, i, false)
        if d == -1 and close > array.get(pHi, i)
            array.set(pHeld, i, false)
 
        if age >= fwdLen
            kept = d == 1 ? close > array.get(pClose, i) : close < array.get(pClose, i)
            if d == 1
                upN += 1
                upKept += kept ? 1 : 0
            else
                dnN += 1
                dnKept += kept ? 1 : 0
            heldN += 1
            held  += array.get(pHeld, i) ? 1 : 0
 
            // A miss stays on the chart, fainter and dotted. Erasing it would
            // leave only the hits visible, which is the flattering version.
            bx = array.get(pBox, i)
            if not na(bx) and not kept
                c = d == 1 ? upCol : dnCol
                box.set_bgcolor(bx, color.new(c, 93))
                box.set_border_style(bx, line.style_dotted)
                box.set_border_color(bx, color.new(c, 55))
 
            array.remove(pBar, i)
            array.remove(pDir, i)
            array.remove(pClose, i)
            array.remove(pHi, i)
            array.remove(pLo, i)
            array.remove(pHeld, i)
            array.remove(pBox, i)
 
if isSpike
    spikes += 1
    box bx = na
    if showBox
        c = dir == 1 ? upCol : dnCol
        // Starts the session after the spike. Drawn from the spike bar itself,
        // the box sat on top of the one candle the whole script is about and
        // hid it. The window is the sessions that follow, so that is where
        // the box belongs, and the spike candle stays visible at its edge.
        bx := box.new(bar_index + 1, high, bar_index + fwdLen, low,
             bgcolor = color.new(c, 72), border_color = color.new(c, 15), border_width = 2)
        array.push(drawn, bx)
        while array.size(drawn) > maxDraw
            box.delete(array.shift(drawn))
    array.push(pBar, bar_index)
    array.push(pDir, dir)
    array.push(pClose, close)
    array.push(pHi, high)
    array.push(pLo, low)
    array.push(pHeld, true)
    array.push(pBox, bx)
 
    if showRatio
        label.new(bar_index, na, str.tostring(rvol, "#.#") + "x",
             yloc = dir == 1 ? yloc.belowbar : yloc.abovebar,
             style = dir == 1 ? label.style_label_up : label.style_label_down,
             color = #e5c56b, textcolor = #0b0e13, size = size.small)
 
// ─── THE COUNT ───────────────────────────────────────────────────────────────
upRate   = upN   > 0 ? upKept / upN   * 100.0 : 0.0
dnRate   = dnN   > 0 ? dnKept / dnN   * 100.0 : 0.0
heldRate = heldN > 0 ? held   / heldN * 100.0 : 0.0
baseRate = baseN > 0 ? baseUp / baseN * 100.0 : 0.0
 
pctOf(float pct, int n) =>
    n > 0 ? str.tostring(pct, "#.#") + "%  of " + str.tostring(n) : "no data"
 
var table stats = na
if showStats and barstate.islast
    if na(stats)
        stats := table.new(posOf(statPos), 2, 6, border_width = 1)
    table.cell(stats, 0, 0, "RVOL FOLLOW-THROUGH", text_color = #e5c56b, text_size = statSize)
    table.cell(stats, 1, 0, "", text_size = statSize)
 
    if wrongTf or not anyVolume
        // Four rows of "no data" read as a broken script. These two cases have
        // a reason, and the reason is what gets printed.
        table.cell(stats, 0, 1, wrongTf ? "Chart timeframe" : "This symbol",
             text_color = color.new(color.white, 25), text_size = statSize)
        table.cell(stats, 1, 1, wrongTf ? "needs daily or higher" : "has no volume data",
             text_color = color.white, text_size = statSize)
        table.cell(stats, 0, 2, "", text_size = statSize)
        table.cell(stats, 1, 2, wrongTf ? "intraday volume follows the clock" : "try a stock or an ETF",
             text_color = color.new(color.white, 25), text_size = statSize)
        table.cell(stats, 0, 3, "", text_size = statSize)
        table.cell(stats, 1, 3, "", text_size = statSize)
        table.cell(stats, 0, 4, "", text_size = statSize)
        table.cell(stats, 1, 4, "", text_size = statSize)
        table.cell(stats, 0, 5, "", text_size = statSize)
        table.cell(stats, 1, 5, "", text_size = statSize)
    else
        table.cell(stats, 0, 1, "Spikes at " + str.tostring(rvMin, "#.#") + "x",
             text_color = color.new(color.white, 25), text_size = statSize)
        table.cell(stats, 1, 1, str.tostring(spikes), text_color = color.white, text_size = statSize)
        // Each spike rate sits directly above the number it has to beat. A
        // down spike "keeps going" when the stock falls, so its baseline is
        // the share of ordinary days that fell, which is 100 minus the up rate.
        table.cell(stats, 0, 2, "Up days kept going", text_color = color.new(color.white, 25), text_size = statSize)
        table.cell(stats, 1, 2, pctOf(upRate, upN),
             text_color = upRate > baseRate ? upCol : dnCol, text_size = statSize)
        table.cell(stats, 0, 3, "Any day, higher " + str.tostring(fwdLen) + " later",
             text_color = color.new(color.white, 45), text_size = statSize)
        table.cell(stats, 1, 3, baseN > 0 ? str.tostring(baseRate, "#.#") + "%" : "no data",
             text_color = color.new(color.white, 45), text_size = statSize)
        table.cell(stats, 0, 4, "Down days kept going", text_color = color.new(color.white, 25), text_size = statSize)
        table.cell(stats, 1, 4, pctOf(dnRate, dnN),
             text_color = dnRate > 100 - baseRate ? upCol : dnCol, text_size = statSize)
        table.cell(stats, 0, 5, "Held the spike range", text_color = color.new(color.white, 25), text_size = statSize)
        table.cell(stats, 1, 5, pctOf(heldRate, heldN), text_color = color.white, text_size = statSize)
 
// ─── ALERTS ──────────────────────────────────────────────────────────────────
alertcondition(isSpike and dir == 1, "Up day volume spike",
     "Volume at or above the spike threshold on an up day.")
alertcondition(isSpike and dir == -1, "Down day volume spike",
     "Volume at or above the spike threshold on a down day.")

← 返回列表