kDashboard - Fundamentals & Technicals

ravip_ag · study · 2038 行 · 点赞 3 · TradingView 原页

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

Pine Script

//@version=6
 
// kDashboard - Fundamentals & Technicals
// Version 1.0.0
//
// A compact equity dashboard combining four analytical dimensions:
//
// GROWTH
// Five quarterly observations and TTM sales/EPS growth.
//
// VALUATION
// Current P/E and P/S compared with historical valuation percentiles.
//
// TECHNICAL
// Liquidity, volatility, relative performance, up/down pressure,
// RSI, normalized extension, relative volume, earnings proximity,
// EMA21 interaction and multi-horizon moving-average alignment.
//
// QUALITY
// ROE, ROIC, gross margin and free cash flow margin.
//
// Financial data is sourced through TradingView's financial and
// earnings requests. Technical data is primarily calculated daily.
//
// Designed for equity analysis, not automated trading signals.
// Missing data and historical coverage vary by instrument.
//
// Release: v1.0.0
// License: MPL 2.0 (for an open-source publication)
 
 
 
 
 
indicator(
    "kDashboard - Fundamentals & Technicals",
    shorttitle="kDashboard",
    overlay=true,
    precision=2,
    max_bars_back=4000
)
 
// TABLE LAYOUT
// 7 columns x 14 rows
//
// ROWS 0-7   : GROWTH (cols 0-4), TECHNICAL (cols 5-6)
// ROWS 8-11  : VALUATION (cols 0-4), TECHNICAL (cols 5-6)
// ROW 12     : QUALITY header (merged cols 0-6)
// ROW 13     : QUALITY values (merged cols 0-6)
//
// QUALITY: ROE, ROIC, GM, FCF Margin
 
 
 
// ============================================================================
 
// INPUTS
 
// ============================================================================
 
 
showTable = input.bool(true, "Show Dashboard")
 
tablePosition = input.string(
    position.bottom_left,
    "Table Position",
    options=[
        position.top_left,
        position.top_center,
        position.top_right,
        position.middle_left,
        position.middle_center,
        position.middle_right,
        position.bottom_left,
        position.bottom_center,
        position.bottom_right
    ]
)
 
 
sizeOpt = input.string(
 
    "Normal",
 
    "Table Size",
 
    options=["Tiny", "Small", "Normal", "Large"]
 
)
 
txtSize = sizeOpt == "Tiny" ? size.tiny :
 
     sizeOpt == "Small" ? size.small :
 
     sizeOpt == "Large" ? size.large :
 
     size.normal
 
 
// ============================================================================
// TECHNICAL INPUTS
// ============================================================================
benchmark = input.symbol("AMEX:SPY", "RP Benchmark")
liqLength = input.int(21, "Liquidity Length", minval=1)
volLength = input.int(21, "Volatility Length", minval=1)
rsiLength = input.int(14, "RSI Length", minval=1)
udTimeframe = input.timeframe("30", "UD Timeframe")
udLookback = input.int(500, "UD Lookback Bars", minval=10)
rvLength = input.int(10, "Relative Volume Average Length", minval=2)
 
rvA = input.float(2.00, "RV Grade A >=", step=0.05)
rvB = input.float(1.50, "RV Grade B >=", step=0.05)
rvC = input.float(1.00, "RV Grade C >=", step=0.05)
rvD = input.float(0.75, "RV Grade D >=", step=0.05)
 
// ============================================================================
 
// COLORS
 
// ============================================================================
 
sectionBg = color.rgb(20, 83, 45)
 
sectionText = color.white
 
subHeaderBg = color.rgb(30, 64, 175)
 
subHeaderText = color.white
 
labelBg = color.rgb(226, 232, 240)
 
labelText = color.rgb(15, 23, 42)
 
whiteBg = color.rgb(219, 234, 254)
 
blackText = color.black
 
neutralBg = color.rgb(254, 249, 195)
 
neutralText = color.rgb(113, 63, 18)
 
greenBg = color.rgb(187, 247, 208)
 
greenText = color.rgb(22, 101, 52)
 
strongGreenBg = color.rgb(134, 239, 172)
 
strongGreenText = color.rgb(20, 83, 45)
 
orangeBg = color.rgb(254, 215, 170)
 
orangeText = color.rgb(124, 45, 18)
 
redBg = color.rgb(254, 202, 202)
 
redText = color.rgb(127, 29, 29)
 
separatorBg = color.rgb(100, 116, 139)
 
// ============================================================================
 
// FORMAT HELPERS
 
// ============================================================================
 
f_num1(value) =>
 
    na(value) ? "-" : str.tostring(value, "0.0")
 
f_integer(value) =>
 
    na(value) ? "-" : str.tostring(value, "0")
 
f_compact(value) =>
 
    if na(value)
 
        "-"
 
    else
 
        absValue = math.abs(value)
 
        if absValue >= 1000000000000
 
            str.tostring(value / 1000000000000.0, "0.00") + "T"
 
        else if absValue >= 1000000000
 
            str.tostring(value / 1000000000.0, "0.00") + "B"
 
        else if absValue >= 1000000
 
            str.tostring(value / 1000000.0, "0.00") + "M"
 
        else if absValue >= 1000
 
            str.tostring(value / 1000.0, "0.00") + "K"
 
        else
 
            str.tostring(value, "0.00")
 
f_pct(value) =>
 
    if na(value)
 
        "-"
 
    else
 
        prefix = value > 0 ? "+" : ""
 
        prefix + str.tostring(value, "0") + "%"
 
f_yoy(currentValue, priorValue) =>
 
    if na(currentValue) or na(priorValue) or priorValue == 0
 
        na
 
    else
 
        (currentValue - priorValue) / math.abs(priorValue) * 100
 
// ============================================================================
 
// GROWTH COLORS
 
// ============================================================================
 
f_growthBg(value) =>
 
    if na(value)
 
        neutralBg
 
    else if value >= 25
 
        strongGreenBg
 
    else if value >= 10
 
        greenBg
 
    else if value >= 0
 
        neutralBg
 
    else if value >= -10
 
        orangeBg
 
    else
 
        redBg
 
f_growthText(value) =>
 
    if na(value)
 
        neutralText
 
    else if value >= 25
 
        strongGreenText
 
    else if value >= 10
 
        greenText
 
    else if value >= 0
 
        neutralText
 
    else if value >= -10
 
        orangeText
 
    else
 
        redText
 
// ============================================================================
 
// DAILY P/E FOR HISTORICAL VALUATION RANGE
 
// ============================================================================
 
f_dailyPe() =>
 
    dailyEpsTtm = request.financial(
 
        syminfo.tickerid,
 
        "EARNINGS_PER_SHARE_DILUTED",
 
        "TTM",
 
        ignore_invalid_symbol=true
 
    )
 
    validEpsTtm = ta.valuewhen(
 
        not na(dailyEpsTtm),
 
        dailyEpsTtm,
 
        0
 
    )
 
    na(validEpsTtm) or validEpsTtm <= 0 ? na : close / validEpsTtm
 
// ============================================================================
 
// DAILY P/S FOR HISTORICAL VALUATION RANGE
 
// ============================================================================
 
f_dailyPs() =>
 
    dailyRevenueTtm = request.financial(
 
        syminfo.tickerid,
 
        "TOTAL_REVENUE",
 
        "TTM",
 
        ignore_invalid_symbol=true
 
    )
 
    dailyShares = request.financial(
 
        syminfo.tickerid,
 
        "TOTAL_SHARES_OUTSTANDING",
 
        "FQ",
 
        ignore_invalid_symbol=true
 
    )
 
    validRevenueTtm = ta.valuewhen(
 
        not na(dailyRevenueTtm),
 
        dailyRevenueTtm,
 
        0
 
    )
 
    validShares = ta.valuewhen(
 
        not na(dailyShares),
 
        dailyShares,
 
        0
 
    )
 
    dailyMarketCap = na(validShares) ? na : close * validShares
 
    na(dailyMarketCap) or na(validRevenueTtm) or validRevenueTtm <= 0 ?
 
         na :
 
         dailyMarketCap / validRevenueTtm
 
// ============================================================================
 
// VALID PERCENTILE RANGE
 
//
 
// Uses only positive, valid valuation observations.
 
//
 
// Returns:
 
// 99.9st percentile
 
// 0.1st percentile
 
// ============================================================================
 
f_validPercentileRange(source, requestedBars) =>
 
    var validValues = array.new_float()
 
    var validBarIndexes = array.new_int()
 
    if not na(source) and source > 0
 
        array.push(validValues, source)
 
        array.push(validBarIndexes, bar_index)
 
    cutoffBar = bar_index - requestedBars + 1
 
    while array.size(validBarIndexes) > 0
 
        oldestBarIndex = array.get(validBarIndexes, 0)
 
        if oldestBarIndex < cutoffBar
 
            array.shift(validBarIndexes)
 
            array.shift(validValues)
 
        else
 
            break
 
    validCount = array.size(validValues)
 
    float percentile999 = na
 
    float percentile01 = na
 
    hasRequestedHistory = bar_index >= requestedBars - 1
 
    if validCount >= 2
 
        sortedValues = array.copy(validValues)
 
        array.sort(
 
            sortedValues,
 
            order.ascending
 
        )
 
        percentile999 := array.percentile_linear_interpolation(
 
            sortedValues,
 
            99.9
 
        )
 
        percentile01 := array.percentile_linear_interpolation(
 
            sortedValues,
 
            0.1
 
        )
 
    [percentile999, percentile01]
 
// ============================================================================
 
// QUARTERLY FUNDAMENTALS
 
// ============================================================================
 
epsActual = request.earnings(
 
    syminfo.tickerid,
 
    earnings.actual,
 
    ignore_invalid_symbol=true,
 
    lookahead=barmerge.lookahead_off
 
)
 
epsStandardized = request.earnings(
 
    syminfo.tickerid,
 
    earnings.standardized,
 
    ignore_invalid_symbol=true,
 
    lookahead=barmerge.lookahead_off
 
)
 
quarterlySales = request.financial(
 
    syminfo.tickerid,
 
    "TOTAL_REVENUE",
 
    "FQ",
 
    ignore_invalid_symbol=true
 
)
 
salesGrowthRaw = request.financial(
 
    syminfo.tickerid,
 
    "REVENUE_ONE_YEAR_GROWTH",
 
    "FQ",
 
    ignore_invalid_symbol=true
 
)
 
// ============================================================================
 
// DETECT QUARTERLY EARNINGS EVENTS
 
// ============================================================================
 
isEpsEvent = ta.barssince(
 
    epsActual != epsActual[1] or
 
    epsStandardized != epsStandardized[1]
 
) == 0
 
f_pick(source, occurrence) =>
 
    pickedValue = ta.valuewhen(
 
        isEpsEvent,
 
        source,
 
        occurrence
 
    )
 
    firstValue = ta.valuewhen(
 
        bar_index == 0,
 
        source,
 
        0
 
    )
 
    na(pickedValue) ? firstValue : pickedValue
 
// ============================================================================
 
// QUARTER DATES
 
// ============================================================================
 
qDate0 = ta.valuewhen(isEpsEvent, time, 0)
 
qDate1 = ta.valuewhen(isEpsEvent, time, 1)
 
qDate2 = ta.valuewhen(isEpsEvent, time, 2)
 
qDate3 = ta.valuewhen(isEpsEvent, time, 3)
 
qDate4 = ta.valuewhen(isEpsEvent, time, 4)
 
f_quarterLabel(eventTime) =>
 
    na(eventTime) ? "-" : str.format("{0,date,MMM-yy}", eventTime)
 
// ============================================================================
 
// EPS HISTORY
 
// ============================================================================
 
a0 = f_pick(epsActual, 0)
 
a1 = f_pick(epsActual, 1)
 
a2 = f_pick(epsActual, 2)
 
a3 = f_pick(epsActual, 3)
 
a4 = f_pick(epsActual, 4)
 
a5 = f_pick(epsActual, 5)
 
a6 = f_pick(epsActual, 6)
 
a7 = f_pick(epsActual, 7)
 
a8 = f_pick(epsActual, 8)
 
e0 = na(a0) ? f_pick(epsStandardized, 0) : a0
 
e1 = na(a1) ? f_pick(epsStandardized, 1) : a1
 
e2 = na(a2) ? f_pick(epsStandardized, 2) : a2
 
e3 = na(a3) ? f_pick(epsStandardized, 3) : a3
 
e4 = na(a4) ? f_pick(epsStandardized, 4) : a4
 
e5 = na(a5) ? f_pick(epsStandardized, 5) : a5
 
e6 = na(a6) ? f_pick(epsStandardized, 6) : a6
 
e7 = na(a7) ? f_pick(epsStandardized, 7) : a7
 
e8 = na(a8) ? f_pick(epsStandardized, 8) : a8
 
// ============================================================================
 
// SALES HISTORY
 
// ============================================================================
 
s0 = f_pick(quarterlySales, 0)
 
s1 = f_pick(quarterlySales, 1)
 
s2 = f_pick(quarterlySales, 2)
 
s3 = f_pick(quarterlySales, 3)
 
s4 = f_pick(quarterlySales, 4)
 
s5 = f_pick(quarterlySales, 5)
 
s6 = f_pick(quarterlySales, 6)
 
s7 = f_pick(quarterlySales, 7)
 
s8 = f_pick(quarterlySales, 8)
 
// ============================================================================
 
// REPORTED SALES-GROWTH DATA
 
// ============================================================================
 
sg0Raw = f_pick(salesGrowthRaw, 0)
 
sg1Raw = f_pick(salesGrowthRaw, 1)
 
sg2Raw = f_pick(salesGrowthRaw, 2)
 
sg3Raw = f_pick(salesGrowthRaw, 3)
 
sg4Raw = f_pick(salesGrowthRaw, 4)
 
// ============================================================================
 
// QUARTERLY YOY GROWTH
 
// ============================================================================
 
salesYoY0 = na(sg0Raw) ? f_yoy(s0, s4) : sg0Raw
 
salesYoY1 = na(sg1Raw) ? f_yoy(s1, s5) : sg1Raw
 
salesYoY2 = na(sg2Raw) ? f_yoy(s2, s6) : sg2Raw
 
salesYoY3 = na(sg3Raw) ? f_yoy(s3, s7) : sg3Raw
 
salesYoY4 = na(sg4Raw) ? f_yoy(s4, s8) : sg4Raw
 
epsYoY0 = f_yoy(e0, e4)
 
epsYoY1 = f_yoy(e1, e5)
 
epsYoY2 = f_yoy(e2, e6)
 
epsYoY3 = f_yoy(e3, e7)
 
epsYoY4 = f_yoy(e4, e8)
 
// ============================================================================
 
// TTM GROWTH
 
// ============================================================================
 
ttmSalesNow = na(s0) or na(s1) or na(s2) or na(s3) ? na : s0 + s1 + s2 + s3
 
ttmSalesPrior = na(s4) or na(s5) or na(s6) or na(s7) ? na : s4 + s5 + s6 + s7
 
ttmEpsNow = na(e0) or na(e1) or na(e2) or na(e3) ? na : e0 + e1 + e2 + e3
 
ttmEpsPrior = na(e4) or na(e5) or na(e6) or na(e7) ? na : e4 + e5 + e6 + e7
 
ttmSalesGrowth = f_yoy(
 
    ttmSalesNow,
 
    ttmSalesPrior
 
)
 
ttmEpsGrowth = f_yoy(
 
    ttmEpsNow,
 
    ttmEpsPrior
 
)
 
// ============================================================================
 
// CURRENT VALUATION
 
// ============================================================================
 
epsDilutedTtm = request.financial(
 
    syminfo.tickerid,
 
    "EARNINGS_PER_SHARE_DILUTED",
 
    "TTM",
 
    ignore_invalid_symbol=true
 
)
 
revenueTtm = request.financial(
 
    syminfo.tickerid,
 
    "TOTAL_REVENUE",
 
    "TTM",
 
    ignore_invalid_symbol=true
 
)
 
sharesOutstanding = request.financial(
 
    syminfo.tickerid,
 
    "TOTAL_SHARES_OUTSTANDING",
 
    "FQ",
 
    ignore_invalid_symbol=true
 
)
 
validEpsDilutedTtm = ta.valuewhen(
 
    not na(epsDilutedTtm),
 
    epsDilutedTtm,
 
    0
 
)
 
validRevenueTtm = ta.valuewhen(
 
    not na(revenueTtm),
 
    revenueTtm,
 
    0
 
)
 
validSharesOutstanding = ta.valuewhen(
 
    not na(sharesOutstanding),
 
    sharesOutstanding,
 
    0
 
)
 
peRatio = na(validEpsDilutedTtm) or validEpsDilutedTtm <= 0 ?
 
     na :
 
     close / validEpsDilutedTtm
 
marketCapitalization = na(validSharesOutstanding) ?
 
     na :
 
     close * validSharesOutstanding
 
psRatio = na(marketCapitalization) or
 
     na(validRevenueTtm) or
 
     validRevenueTtm <= 0 ?
 
     na :
 
     marketCapitalization / validRevenueTtm
 
// ============================================================================
 
// HISTORICAL P/S RANGES
 
// ============================================================================
 
[ps10Y999, ps10Y01] = request.security(
 
    syminfo.tickerid,
 
    "D",
 
    f_validPercentileRange(f_dailyPs(), 2520),
 
    calc_bars_count=4000
 
)
 
// ============================================================================
 
// HISTORICAL P/E RANGES
 
// ============================================================================
 
[pe10Y999, pe10Y01] = request.security(
 
    syminfo.tickerid,
 
    "D",
 
    f_validPercentileRange(f_dailyPe(), 2520),
 
    calc_bars_count=4000
 
)
 
// ============================================================================
// QUALITY FUNDAMENTALS
// ============================================================================
 
qualityROE = request.financial(
    syminfo.tickerid,
    "RETURN_ON_EQUITY",
    "FY",
    ignore_invalid_symbol=true
)
 
qualityROIC = request.financial(
    syminfo.tickerid,
    "RETURN_ON_INVESTED_CAPITAL",
    "FY",
    ignore_invalid_symbol=true
)
 
qualityGM = request.financial(
    syminfo.tickerid,
    "GROSS_MARGIN",
    "TTM",
    ignore_invalid_symbol=true
)
 
qualityFCFM = request.financial(
    syminfo.tickerid,
    "FREE_CASH_FLOW_MARGIN",
    "FY",
    ignore_invalid_symbol=true
)
 
 
 
// ============================================================================
// TECHNICAL HELPERS
// ============================================================================
f_compactMoney(value) =>
    if na(value)
        "-"
    else if math.abs(value) >= 1000000000
        str.tostring(value / 1000000000, "0.0") + "B"
    else if math.abs(value) >= 1000000
        str.tostring(value / 1000000, "0.0") + "M"
    else if math.abs(value) >= 1000
        str.tostring(value / 1000, "0.0") + "K"
    else
        str.tostring(value, "0")
 
f_pct2(value) =>
    na(value) ? "-" : str.tostring(value, "0.00") + "%"
 
f_signedPct2(value) =>
    if na(value)
        "-"
    else
        prefix = value > 0 ? "+" : ""
        prefix + str.tostring(value, "0.00") + "%"
 
f_num2(value) =>
    na(value) ? "-" : str.tostring(value, "0.00")
 
f_grade(fastMa, middleMa, slowMa) =>
    if na(fastMa) or na(middleMa) or na(slowMa)
        "-"
    else if fastMa > middleMa and middleMa > slowMa
        "A"
    else if fastMa < middleMa and middleMa < slowMa
        "C"
    else
        "B"
 
f_rvGrade(value) =>
    if na(value)
        "-"
    else if value >= rvA
        "A"
    else if value >= rvB
        "B"
    else if value >= rvC
        "C"
    else if value >= rvD
        "D"
    else
        "E"
 
// ============================================================================
// DAILY TECHNICAL BUNDLE
// ============================================================================
f_dailyBundle() =>
    dailyLiquidity = ta.sma(close * volume, liqLength)
 
    avgPrice = ta.sma(close, volLength)
    dailyVolatility = na(avgPrice) or avgPrice == 0 ? na : ta.atr(volLength) / avgPrice * 100
 
    dailyReturn21 = na(close[21]) ? na : close / close[21]
    dailyRsi = ta.rsi(close, rsiLength)
 
    dEma9 = ta.ema(hlc3, 9)
    dEma21 = ta.ema(close, 21)
    dEma34 = ta.ema(close, 34)
 
    ext9 = dEma9 == 0 ? na : (close - dEma9) / dEma9 * 100
    ext21 = dEma21 == 0 ? na : (close - dEma21) / dEma21 * 100
    ext34 = dEma34 == 0 ? na : (close - dEma34) / dEma34 * 100
    avgExtension = na(ext9) or na(ext21) or na(ext34) ? na : (ext9 + ext21 + ext34) / 3.0
    dailyExtension = na(dailyVolatility) or dailyVolatility == 0 ? na : avgExtension / dailyVolatility
 
    touchesEma21 = low <= dEma21 and high >= dEma21
    changesSide = not na(dEma21[1]) and (
         (close > dEma21 and close[1] < dEma21[1]) or
         (close < dEma21 and close[1] > dEma21[1])
    )
    onEma21 = close == dEma21
    ema21Interaction = touchesEma21 or changesSide or onEma21
    dailyDte21 = ta.barssince(ema21Interaction)
 
    priorAvgVolume = ta.sma(volume[1], rvLength)
    dailyRelativeVolume = na(priorAvgVolume) or priorAvgVolume == 0 ? na : volume / priorAvgVolume
    rvOldest = dailyRelativeVolume[3]
    rvMiddle = dailyRelativeVolume[2]
    rvNewest = dailyRelativeVolume[1]
 
    dSma50 = ta.sma(close, 50)
    dSma100 = ta.sma(close, 100)
    dSma200 = ta.sma(close, 200)
    dSma500 = ta.sma(close, 500)
    dSma1000 = ta.sma(close, 1000)
 
    dEma5 = ta.ema(close, 5)
    dEma13 = ta.ema(close, 13)
 
    gradeLong = f_grade(dSma200, dSma500, dSma1000)
    gradeMedium = f_grade(dSma50, dSma100, dSma200)
    gradeShort = f_grade(dEma9, dEma21, dEma34)
    gradeFast = f_grade(dEma5, dEma9, dEma13)
 
    dailyTrend = gradeLong + gradeMedium + gradeShort + gradeFast
 
    [dailyLiquidity, dailyVolatility, dailyReturn21, dailyRsi, dailyExtension, dailyDte21, rvOldest, rvMiddle, rvNewest, dailyTrend]
 
// ============================================================================
// REQUEST DAILY TECHNICAL DATA
// ============================================================================
[liquidity, volatility, stockReturn21, rsiValue, extension, barsSinceEma21, rvDay3, rvDay2, rvDay1, trendCode] = request.security(
    syminfo.tickerid,
    "D",
    f_dailyBundle(),
    gaps=barmerge.gaps_off,
    lookahead=barmerge.lookahead_off,
    calc_bars_count=1500
)
 
benchmarkReturn21 = request.security(
    benchmark,
    "D",
    na(close[21]) ? na : close / close[21],
    gaps=barmerge.gaps_off,
    lookahead=barmerge.lookahead_off
)
 
relativePerformance = na(stockReturn21) or na(benchmarkReturn21) or benchmarkReturn21 == 0 ? na : (stockReturn21 / benchmarkReturn21 - 1) * 100
 
// ============================================================================
// UD PRESSURE RATIO
// ============================================================================
f_udRatio() =>
    candleRange = high - low
    closeLocation = candleRange > 0 ? (close - low) / candleRange : 0.5
    upPressure = volume * closeLocation
    downPressure = volume * (1.0 - closeLocation)
    avgUp = ta.sma(upPressure, udLookback)
    avgDown = ta.sma(downPressure, udLookback)
    na(avgDown) or avgDown == 0 ? na : avgUp / avgDown
 
udRatio = request.security(
    syminfo.tickerid,
    udTimeframe,
    f_udRatio(),
    gaps=barmerge.gaps_off,
    lookahead=barmerge.lookahead_off,
    calc_bars_count=udLookback + 100
)
 
daysToEarnings = na(earnings.future_time) ? na : math.floor((earnings.future_time - timenow) / 86400000)
rvCode = f_rvGrade(rvDay3) + f_rvGrade(rvDay2) + f_rvGrade(rvDay1)
 
rpTextColor = na(relativePerformance) ? blackText : relativePerformance > 0 ? greenText : relativePerformance < 0 ? redText : blackText
udTextColor = na(udRatio) ? blackText : udRatio > 1 ? greenText : udRatio < 1 ? redText : blackText
rsiTextColor = na(rsiValue) ? blackText : rsiValue >= 70 ? redText : rsiValue <= 30 ? greenText : blackText
extensionTextColor = na(extension) ? blackText : extension >= 2 ? redText : extension <= -2 ? greenText : blackText
 
trend1 = str.substring(trendCode, 0, 1)
trend2 = str.substring(trendCode, 1, 2)
trend3 = str.substring(trendCode, 2, 3)
trend4 = str.substring(trendCode, 3, 4)
 
bullishTrendCount = (trend1 == "A" ? 1 : 0) + (trend2 == "A" ? 1 : 0) + (trend3 == "A" ? 1 : 0) + (trend4 == "A" ? 1 : 0)
bearishTrendCount = (trend1 == "C" ? 1 : 0) + (trend2 == "C" ? 1 : 0) + (trend3 == "C" ? 1 : 0) + (trend4 == "C" ? 1 : 0)
trendTextColor = bullishTrendCount > bearishTrendCount ? greenText : bearishTrendCount > bullishTrendCount ? redText : neutralText
 
// ============================================================================
 
// TABLE
 
//
 
// Growth uses 5 columns:
 
// 0 Period
 
// 1 Sales
 
// 2 SG
 
// 3 EPS
 
// 4 EG
 
//
 
// Valuation:
 
// 0 Metric
 
// 1 Current
 
// 2 10Y 99.9/0.1
 
//
 
// Rows:
 
// 0  Growth header
 
// 1  Growth subheaders
 
// 2  Q1
 
// 3  Q2
 
// 4  Q3
 
// 5  Q4
 
// 6  Q5
 
// 7  TTM
 
// 8  Valuation header
 
// 9  Valuation subheaders
 
// 10 P/E
 
// 11 P/S
 
// ============================================================================
 
 
var table dashboard = table.new(
    tablePosition,
    7,
    14,
    frame_color=color.rgb(100, 116, 139),
    frame_width=2,
    border_width=0
)
 
 
// ============================================================================
 
// TABLE HELPERS
 
// ============================================================================
 
f_cell(column, row, cellText, bg, textColor) =>
 
    table.cell(
 
        dashboard,
 
        column,
 
        row,
 
        cellText,
 
        bgcolor=bg,
 
        text_color=textColor,
 
        text_size=txtSize
 
    )
 
f_label(column, row, cellText) =>
 
    f_cell(
 
        column,
 
        row,
 
        cellText,
 
        labelBg,
 
        labelText
 
    )
 
f_value(column, row, cellText) =>
 
    f_cell(
 
        column,
 
        row,
 
        cellText,
 
        whiteBg,
 
        blackText
 
    )
 
f_growthCell(column, row, value) =>
 
    f_cell(
 
        column,
 
        row,
 
        f_pct(value),
 
        f_growthBg(value),
 
        f_growthText(value)
 
    )
 
 
f_techValue(column, row, cellText, textColor) =>
    f_cell(
        column,
        row,
        cellText,
        whiteBg,
        textColor
    )
 
// ============================================================================
 
// BUILD TABLE
 
// ============================================================================
 
if barstate.islast
 
    table.clear(
        dashboard,
        0,
        0,
        6,
        13
    )
 
    if showTable
 
        // ====================================================================
 
        // GROWTH HEADER
 
        // ====================================================================
 
        f_cell(
 
            0,
 
            0,
 
            "GROWTH",
 
            sectionBg,
 
            sectionText
 
        )
 
        table.merge_cells(
 
            dashboard,
 
            0,
 
            0,
 
            4,
 
            0
 
        )
 
 
        // ====================================================================
        // TECHNICAL HEADER
        // ====================================================================
        f_cell(
            5,
            0,
            "TECHNICAL",
            sectionBg,
            sectionText
        )
        table.merge_cells(dashboard, 5, 0, 6, 0)
 
 
        // ====================================================================
 
        // GROWTH SUBHEADERS
 
        // ====================================================================
 
        f_cell(
 
            0,
 
            1,
 
            "Period",
 
            subHeaderBg,
 
            subHeaderText
 
        )
 
        f_cell(
 
            1,
 
            1,
 
            "Sales",
 
            subHeaderBg,
 
            subHeaderText
 
        )
 
        f_cell(
 
            2,
 
            1,
 
            "EPS",
 
            subHeaderBg,
 
            subHeaderText
 
        )
 
        f_cell(
 
            3,
 
            1,
 
            "SG",
 
            subHeaderBg,
 
            subHeaderText
 
        )
 
 
 
        f_cell(
 
            4,
 
            1,
 
            "EG",
 
            subHeaderBg,
 
            subHeaderText
 
        )
 
 
        f_cell(5, 1, "Metric", subHeaderBg, subHeaderText)
        f_cell(6, 1, "Value", subHeaderBg, subHeaderText)
 
 
        // ====================================================================
 
        // Q1 — OLDEST
 
        // ====================================================================
 
        f_label(
 
            0,
 
            2,
 
            f_quarterLabel(qDate4)
 
        )
 
        f_value(
 
            1,
 
            2,
 
            f_compact(s4)
 
        )
 
        f_value(
 
            2,
 
            2,
 
            f_num1(e4)
 
        )
 
        f_growthCell(
 
            3,
 
            2,
 
            salesYoY4
 
        )
 
 
 
        f_growthCell(
 
            4,
 
            2,
 
            epsYoY4
 
        )
 
        // ====================================================================
 
        // Q2
 
        // ====================================================================
 
        f_label(
 
            0,
 
            3,
 
            f_quarterLabel(qDate3)
 
        )
 
        f_value(
 
            1,
 
            3,
 
            f_compact(s3)
 
        )
 
        f_value(
 
            2,
 
            3,
 
            f_num1(e3)
 
        )
 
        f_growthCell(
 
            3,
 
            3,
 
            salesYoY3
 
        )
 
 
 
        f_growthCell(
 
            4,
 
            3,
 
            epsYoY3
 
        )
 
        // ====================================================================
 
        // Q3
 
        // ====================================================================
 
        f_label(
 
            0,
 
            4,
 
            f_quarterLabel(qDate2)
 
        )
 
        f_value(
 
            1,
 
            4,
 
            f_compact(s2)
 
        )
 
        f_value(
 
            2,
 
            4,
 
            f_num1(e2)
 
        )
 
        f_growthCell(
 
            3,
 
            4,
 
            salesYoY2
 
        )
 
 
 
        f_growthCell(
 
            4,
 
            4,
 
            epsYoY2
 
        )
 
        // ====================================================================
 
        // Q4
 
        // ====================================================================
 
        f_label(
 
            0,
 
            5,
 
            f_quarterLabel(qDate1)
 
        )
 
        f_value(
 
            1,
 
            5,
 
            f_compact(s1)
 
        )
 
        f_value(
 
            2,
 
            5,
 
            f_num1(e1)
 
        )
 
        f_growthCell(
 
            3,
 
            5,
 
            salesYoY1
 
        )
 
 
 
        f_growthCell(
 
            4,
 
            5,
 
            epsYoY1
 
        )
 
        // ====================================================================
 
        // Q5 — LATEST
 
        // ====================================================================
 
        f_label(
 
            0,
 
            6,
 
            f_quarterLabel(qDate0)
 
        )
 
        f_value(
 
            1,
 
            6,
 
            f_compact(s0)
 
        )
 
 
 
        f_value(
 
            2,
 
            6,
 
            f_num1(e0)
 
        )
 
        f_growthCell(
 
            3,
 
            6,
 
            salesYoY0
 
        )
 
        f_growthCell(
 
            4,
 
            6,
 
            epsYoY0
 
        )
 
        // ====================================================================
 
        // TTM
 
        // ====================================================================
 
        f_label(
 
            0,
 
            7,
 
            "TTM"
 
        )
 
        f_value(
 
            1,
 
            7,
 
            f_compact(ttmSalesNow)
 
        )
 
        f_value(
 
            2,
 
            7,
 
            f_num1(ttmEpsNow)
 
        )
 
        f_growthCell(
 
            3,
 
            7,
 
            ttmSalesGrowth
 
        )
 
        f_growthCell(
 
            4,
 
            7,
 
            ttmEpsGrowth
 
        )
 
        // ====================================================================
 
        // VALUATION HEADER
 
        // ====================================================================
 
        f_cell(
 
            0,
 
            8,
 
            "VALUATION",
 
            sectionBg,
 
            sectionText
 
        )
 
        table.merge_cells(
 
            dashboard,
 
            0,
 
            8,
 
            4,
 
            8
 
        )
 
        // ====================================================================
 
        // VALUATION SUBHEADERS
 
        // ====================================================================
 
        f_cell(
 
            0,
 
            9,
 
            "Metric",
 
            subHeaderBg,
 
            subHeaderText
 
        )
 
        f_cell(
 
            1,
 
            9,
 
            "Current",
 
            subHeaderBg,
 
            subHeaderText
 
        )
 
        f_cell(
 
            2,
 
            9,
 
            "Hist 99.9/0.1",
 
            subHeaderBg,
 
            subHeaderText
 
        )
 
        table.merge_cells(dashboard, 2, 9, 4, 9)
 
        // ====================================================================
 
        // P/E
 
        // ====================================================================
 
        f_label(
 
            0,
 
            10,
 
            "P/E"
 
        )
 
        f_value(
 
            1,
 
            10,
 
            f_integer(peRatio)
 
        )
 
        f_value(
 
            2,
 
            10,
 
            f_integer(pe10Y999) + "/" + f_integer(pe10Y01)
 
        )
 
        table.merge_cells(dashboard, 2, 10, 4, 10)
 
        // ====================================================================
 
        // P/S
 
        // ====================================================================
 
        f_label(
 
            0,
 
            11,
 
            "P/S"
 
        )
 
        f_value(
 
            1,
 
            11,
 
            f_num1(psRatio)
 
        )
 
        f_value(
 
            2,
 
            11,
 
            f_num1(ps10Y999) + "/" + f_num1(ps10Y01)
 
        )
 
        table.merge_cells(dashboard, 2, 11, 4, 11)
 
 
        // ====================================================================
        // TECHNICAL VALUES
        // ====================================================================
        f_label(5, 2, "LIQ")
        f_techValue(6, 2, f_compactMoney(liquidity), blackText)
 
        f_label(5, 3, "VOL")
        f_techValue(6, 3, f_pct2(volatility), blackText)
 
        f_label(5, 4, "PF")
        f_techValue(6, 4, f_signedPct2(relativePerformance), rpTextColor)
 
        f_label(5, 5, "UD")
        f_techValue(6, 5, f_num2(udRatio), udTextColor)
 
        f_label(5, 6, "RSI")
        f_techValue(6, 6, f_integer(rsiValue), rsiTextColor)
 
        f_label(5, 7, "EXT")
        f_techValue(6, 7, f_num2(extension), extensionTextColor)
 
        f_label(5, 8, "RV3")
        f_techValue(6, 8, rvCode, blackText)
 
        f_label(5, 9, "DTE")
        f_techValue(6, 9, f_integer(daysToEarnings), blackText)
 
        f_label(5, 10, "DTE21")
        f_techValue(6, 10, f_integer(barsSinceEma21), blackText)
 
        f_label(5, 11, "TREND")
        f_techValue(6, 11, trendCode, trendTextColor)
 
 
        // ====================================================================
        // QUALITY HEADER - ROW 12
        // ====================================================================
 
        f_cell(0, 12, "QUALITY", sectionBg, sectionText)
        table.merge_cells(dashboard, 0, 12, 6, 12)
 
 
        // ====================================================================
        // QUALITY VALUES - ROW 13 (SINGLE MERGED CELL)
        // ====================================================================
 
        qualityText = "ROE " + (na(qualityROE) ? "-" : str.tostring(qualityROE, "0") + "%") +
             "      ROIC " + (na(qualityROIC) ? "-" : str.tostring(qualityROIC, "0") + "%") +
             "      GM " + (na(qualityGM) ? "-" : str.tostring(qualityGM, "0") + "%") +
             "      FCF " + (na(qualityFCFM) ? "-" : str.tostring(qualityFCFM, "0") + "%")
 
        f_value(0, 13, qualityText)
        table.merge_cells(dashboard, 0, 13, 6, 13)
 
        table.cell_set_text_halign(
            dashboard,
            0,
            13,
            text.align_center
        )
 
 
        // ====================================================================
 
        // ALIGNMENT
 
        // ====================================================================
 
        for row = 2 to 7
 
            for col = 1 to 4
 
                table.cell_set_text_halign(
 
                    dashboard,
 
                    col,
 
                    row,
 
                    text.align_right
 
                )
 
        for row = 10 to 11
 
            for col = 1 to 2
 
                table.cell_set_text_halign(
 
                    dashboard,
 
                    col,
 
                    row,
 
                    text.align_right
 
                )
 
        for row = 2 to 11
            table.cell_set_text_halign(
                dashboard,
                6,
                row,
                text.align_right
            )
 
// ============================================================================
 
// DATA WINDOW
 
// ============================================================================
 
plot(peRatio, "P/E Current", display=display.data_window)
 
plot(psRatio, "P/S Current", display=display.data_window)
 
plot(pe10Y999, "P/E 10Y 99.9 percentile", display=display.data_window)
 
plot(pe10Y01, "P/E 10Y 0.1 percentile", display=display.data_window)
 
plot(ps10Y999, "P/S 10Y 99.9 percentile", display=display.data_window)
 
plot(ps10Y01, "P/S 10Y 0.1 percentile", display=display.data_window)
 
plot(s4, "Q1 Sales", display=display.data_window)
 
plot(s3, "Q2 Sales", display=display.data_window)
 
plot(s2, "Q3 Sales", display=display.data_window)
 
plot(s1, "Q4 Sales", display=display.data_window)
 
plot(s0, "Q5 Sales Latest", display=display.data_window)
 
plot(ttmSalesNow, "TTM Sales", display=display.data_window)
 
plot(e4, "Q1 EPS", display=display.data_window)
 
plot(e3, "Q2 EPS", display=display.data_window)
 
plot(e2, "Q3 EPS", display=display.data_window)
 
plot(e1, "Q4 EPS", display=display.data_window)
 
plot(e0, "Q5 EPS Latest", display=display.data_window)
 
plot(ttmEpsNow, "TTM EPS", display=display.data_window)
 
plot(salesYoY4, "Q1 Sales YoY", display=display.data_window)
 
plot(salesYoY3, "Q2 Sales YoY", display=display.data_window)
 
plot(salesYoY2, "Q3 Sales YoY", display=display.data_window)
 
plot(salesYoY1, "Q4 Sales YoY", display=display.data_window)
 
plot(salesYoY0, "Q5 Sales YoY Latest", display=display.data_window)
 
plot(ttmSalesGrowth, "TTM Sales Growth", display=display.data_window)
 
plot(epsYoY4, "Q1 EPS YoY", display=display.data_window)
 
plot(epsYoY3, "Q2 EPS YoY", display=display.data_window)
 
plot(epsYoY2, "Q3 EPS YoY", display=display.data_window)
 
plot(epsYoY1, "Q4 EPS YoY", display=display.data_window)
 
plot(epsYoY0, "Q5 EPS YoY Latest", display=display.data_window)
 
plot(ttmEpsGrowth, "TTM EPS Growth", display=display.data_window)
 
 
plot(liquidity, "Liquidity 21D", display=display.data_window)
plot(volatility, "Volatility 21D", display=display.data_window)
plot(relativePerformance, "PF 21D vs SPY", display=display.data_window)
plot(udRatio, "UD 500", display=display.data_window)
plot(rsiValue, "RSI", display=display.data_window)
plot(extension, "Extension", display=display.data_window)
plot(rvDay3, "RV Day -3", display=display.data_window)
plot(rvDay2, "RV Day -2", display=display.data_window)
plot(rvDay1, "RV Day -1", display=display.data_window)
plot(float(daysToEarnings), "DTE", display=display.data_window)
plot(float(barsSinceEma21), "DTE21", display=display.data_window)
plot(qualityROE, "ROE %", display=display.data_window)
plot(qualityGM, "Gross Margin %", display=display.data_window)
plot(qualityFCFM, "FCF Margin %", display=display.data_window)

← 返回列表