Revenue Acceleration (TTM)

oxanol74 · study · 94 行 · 点赞 5 · TradingView 原页

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

Pine Script

//@version=6
// Revenue Acceleration (TTM) — open source
// Measures the CHANGE in a company's revenue growth rate (the second derivative),
// not the growth itself. Data: TradingView fundamentals, quarterly revenue.
//   growth now  g0 = TTM(q0..q3) / TTM(q4..q7)  - 1
//   growth prev g1 = TTM(q4..q7) / TTM(q8..q11) - 1
//   acceleration   = g0 - g1, in percentage points (pp)
// Quarter basis: g0 = q0 / q4 - 1, g1 = q1 / q5 - 1.
// Needs 12 reported quarters (Quarter basis: 6).
indicator("Revenue Acceleration (TTM)", shorttitle = "Rev Accel", overlay = false, precision = 1)
 
basis     = input.string("TTM", "Basis", options = ["TTM", "Quarter"],
     tooltip = "TTM — trailing twelve months against the prior twelve: smooth, seasonality removed.\nQuarter — latest quarter against the same quarter a year earlier: faster, noisier.")
flatPP    = input.float(1.0, "Flat band, pp", minval = 0.0, step = 0.5,
     tooltip = "Acceleration inside ±this band is treated as FLAT.")
capPct    = input.float(1000.0, "Growth sanity cap, %", minval = 100.0, step = 100.0,
     tooltip = "A growth rate beyond this (absolute) comes from a near-zero base and is shown as n/a.")
showGrow  = input.bool(true, "Show revenue growth line")
showTable = input.bool(true, "Show summary table")
 
// Quarterly revenue arrives once per report (gaps on), newest first in the array.
qRev = request.financial(syminfo.tickerid, "TOTAL_REVENUE", "FQ", gaps = barmerge.gaps_on, ignore_invalid_symbol = true)
var revQ = array.new_float(0)
if not na(qRev)
    array.unshift(revQ, qRev)
    if array.size(revQ) > 12
        array.pop(revQ)
 
qAt(int i) =>
    array.size(revQ) > i ? array.get(revQ, i) : float(na)
 
sum4(int from) =>
    float s = 0.0
    bool ok = true
    for i = from to from + 3
        v = qAt(i)
        if na(v)
            ok := false
        else
            s += v
    ok ? s : float(na)
 
growPct(float cur, float base) =>
    float r = na
    if not na(cur) and not na(base) and base > 0
        v = (cur / base - 1.0) * 100.0
        r := math.abs(v) > capPct ? float(na) : v
    r
 
float g0 = na
float g1 = na
if basis == "TTM"
    g0 := growPct(sum4(0), sum4(4))
    g1 := growPct(sum4(4), sum4(8))
else
    g0 := growPct(qAt(0), qAt(4))
    g1 := growPct(qAt(1), qAt(5))
acc = na(g0) or na(g1) ? float(na) : g0 - g1
 
cUp   = color.new(#26a69a, 0)
cDown = color.new(#ef5350, 0)
cFlat = color.new(#8d99ab, 0)
accCol = na(acc) ? cFlat : acc > flatPP ? cUp : acc < -flatPP ? cDown : cFlat
 
plot(acc, "Revenue acceleration, pp", color = color.new(accCol, 25), style = plot.style_columns)
plot(showGrow ? g0 : na, "Revenue growth YoY, %", color = color.new(#f2cc60, 0), linewidth = 2, style = plot.style_stepline)
hline(0, "Zero", color = color.new(#8d99ab, 50), linestyle = hline.style_dotted)
 
// Alerts fire on the bar where a new report changes the reading.
turnUp   = not na(acc) and not na(acc[1]) and acc > flatPP and acc[1] <= flatPP
turnDown = not na(acc) and not na(acc[1]) and acc < -flatPP and acc[1] >= -flatPP
alertcondition(turnUp, "Revenue starts accelerating", "Revenue Acceleration: {{ticker}} — revenue growth turned up")
alertcondition(turnDown, "Revenue starts decelerating", "Revenue Acceleration: {{ticker}} — revenue growth turned down")
 
fmt(float v, string unit) =>
    na(v) ? "n/a" : (v > 0 ? "+" : "") + str.tostring(v, "0.0") + unit
 
var table t = table.new(position.top_right, 2, 5, border_width = 1)
if showTable and barstate.islast
    bg  = color.new(#131722, 0)
    dim = color.new(#8d99ab, 0)
    txt = color.new(#e6ecf5, 0)
    need = basis == "TTM" ? 12 : 6
    verdict = na(acc) ? "needs " + str.tostring(need) + " quarters" : acc > flatPP ? "ACCELERATING ▲" : acc < -flatPP ? "DECELERATING ▼" : "FLAT"
    table.cell(t, 0, 0, "Revenue · " + basis, text_color = dim, bgcolor = bg, text_size = size.small)
    table.cell(t, 1, 0, str.tostring(array.size(revQ)) + "Q loaded", text_color = dim, bgcolor = bg, text_size = size.small)
    table.cell(t, 0, 1, "Growth now", text_color = dim, bgcolor = bg, text_size = size.small)
    table.cell(t, 1, 1, fmt(g0, "%"), text_color = txt, bgcolor = bg, text_size = size.small)
    table.cell(t, 0, 2, "Growth prev", text_color = dim, bgcolor = bg, text_size = size.small)
    table.cell(t, 1, 2, fmt(g1, "%"), text_color = txt, bgcolor = bg, text_size = size.small)
    table.cell(t, 0, 3, "Acceleration", text_color = dim, bgcolor = bg, text_size = size.small)
    table.cell(t, 1, 3, fmt(acc, " pp"), text_color = accCol, bgcolor = bg, text_size = size.small)
    table.cell(t, 0, 4, "Reading", text_color = dim, bgcolor = bg, text_size = size.small)
    table.cell(t, 1, 4, verdict, text_color = accCol, bgcolor = bg, text_size = size.small)

← 返回列表