AH Beta v1

AlpHay · study · 220 行 · 点赞 1 · TradingView 原页

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

Pine Script

//@version=6
// This source code is subject to the terms of the Mozilla Public License 2.0: https://mozilla.org/MPL/2.0/
// © AlpHay
 
const string indVersion = "1.0.8"
const string indType = "Beta"
const string indName = "AH " + indType + " v" + indVersion
 
indicator(indName, indType, overlay = true, max_bars_back = 1500)
 
// Period labels use a fixed model: 21 daily bars per month, 252 per year.
const int len1M = 21
const int len3M = 63
const int len6M = 126
const int len9M = 189
const int len1Y = 252
const int len2Y = 504
const int len3Y = 756
const int len4Y = 1008
const int len5Y = 1260
 
const color cxx = color.rgb(255, 255, 255, 100)
 
const color cred = color.rgb(242, 53, 69)
const color cgrn = color.rgb(10, 153, 129)
const color cblu = color.rgb(40, 98, 255)
const color ccrm = color.rgb(248, 236, 202)
const color lbc1 = color.rgb(128, 128, 128)
 
method toText(float this, string format= "#.##", bool isFormatAlt= false, float target= 0.0, int multiplier= 1) =>
    string out = ""
    if not na(this)
        if format == format.percent
            float pct = this * multiplier
            out := str.tostring(pct, "#.##") + "%"
            if isFormatAlt and pct >= target
                out := "+" + out
        else
            out := isFormatAlt ? this <= target ? str.tostring(this, format) : "+" + str.tostring(this, format) : str.tostring(this, format)
    out
 
method toColor(float this, float target= 0.0, bool isFlipped= false) => isFlipped ? (this > target ? cred : this < target ? cgrn : cblu) : (this > target ? cgrn : this < target ? cred : cblu)
 
method addCell(table this, int columnId, int rowId, string title, color txtColor, string horAlign= na, int txtSize= na, string tooltip= na, color bgColor= na, string verAlign= na, bool isBold= false, bool isItalic= false) =>
    table.cell(this, columnId, rowId, title, 0, 0, txtColor, na(horAlign)?text.align_right:horAlign, na(verAlign)?text.align_center:verAlign, na(txtSize)?10:txtSize, na(bgColor)?cxx:bgColor, tooltip, font.family_monospace)
    if isBold and isItalic
        table.cell_set_text_formatting(this, columnId, rowId, text.format_bold+text.format_italic)
    else if isBold
        table.cell_set_text_formatting(this, columnId, rowId, text.format_bold)
    else if isItalic
        table.cell_set_text_formatting(this, columnId, rowId, text.format_italic)
    this
 
enum TablePosition
	TopLeft
	TopCenter
	TopRight
	MidLeft
	MidCenter
	MidRight
	BotLeft
	BotCenter
	BotRight
 
method getPosition(TablePosition this) =>
    switch this
        TablePosition.TopLeft => position.top_left
        TablePosition.TopCenter => position.top_center
        TablePosition.TopRight => position.top_right
        TablePosition.MidLeft => position.middle_left
        TablePosition.MidCenter => position.middle_center
        TablePosition.MidRight => position.middle_right
        TablePosition.BotLeft => position.bottom_left
        TablePosition.BotCenter => position.bottom_center
        TablePosition.BotRight => position.bottom_right
        => position.middle_center
 
// Statistics are evaluated inside the asset's daily context.
// Reference prices use TradingView's normal bar mapping, without a separate date join.
dailyStats(float referenceClose, int length) =>
    bool valid = not na(close) and not na(close[1]) and close > 0 and close[1] > 0 and not na(referenceClose) and not na(referenceClose[1]) and referenceClose > 0 and referenceClose[1] > 0
    float assetReturn = valid ? close / close[1] - 1.0 : na
    float referenceReturn = valid ? referenceClose / referenceClose[1] - 1.0 : na
    float runningCount = ta.cum(valid ? 1.0 : 0.0)
    int count = int(runningCount - nz(runningCount[length]))
    float assetDeviation = ta.stdev(assetReturn, length)
    float referenceDeviation = ta.stdev(referenceReturn, length)
    float correlation = ta.correlation(assetReturn, referenceReturn, length)
    float beta = na
    if count == length and referenceDeviation > 0.0
        // Covariance / reference variance = correlation * asset deviation / reference deviation.
        beta := assetDeviation == 0.0 ? 0.0 : correlation * assetDeviation / referenceDeviation
        correlation := assetDeviation > 0.0 ? math.max(-1.0, math.min(1.0, correlation)) : na
    else
        correlation := na
    [beta, correlation, count]
 
dailyReport(string referenceTicker) =>
    [referenceClose, description] = request.security(referenceTicker, "1D", [close, syminfo.description], gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true)
    [b1M, c1M, n1M] = dailyStats(referenceClose, len1M)
    [b3M, c3M, n3M] = dailyStats(referenceClose, len3M)
    [b6M, c6M, n6M] = dailyStats(referenceClose, len6M)
    [b9M, c9M, n9M] = dailyStats(referenceClose, len9M)
    [b1Y, c1Y, n1Y] = dailyStats(referenceClose, len1Y)
    [b2Y, c2Y, n2Y] = dailyStats(referenceClose, len2Y)
    [b3Y, c3Y, n3Y] = dailyStats(referenceClose, len3Y)
    [b4Y, c4Y, n4Y] = dailyStats(referenceClose, len4Y)
    [b5Y, c5Y, n5Y] = dailyStats(referenceClose, len5Y)
    [b1M, c1M, n1M, time_tradingday[len1M], b3M, c3M, n3M, time_tradingday[len3M], b6M, c6M, n6M, time_tradingday[len6M], b9M, c9M, n9M, time_tradingday[len9M], b1Y, c1Y, n1Y, time_tradingday[len1Y], b2Y, c2Y, n2Y, time_tradingday[len2Y], b3Y, c3Y, n3Y, time_tradingday[len3Y], b4Y, c4Y, n4Y, time_tradingday[len4Y], b5Y, c5Y, n5Y, time_tradingday[len5Y], time_tradingday, description]
 
type BetaWindow
    string Title
    int Length
    int Start
    int End
    int Count
    float Beta
    float Correlation
 
formatDate(int stamp) => na(stamp) ? "—" : str.format_time(stamp, "yyyy-MM-dd", "UTC")
 
method toTooltip(BetaWindow this) =>
    string out = str.tostring(this.Length) + " daily bars"
    out += "\n" + formatDate(this.Start) + " to " + formatDate(this.End)
    out += "\nObservations: " + str.tostring(this.Count)
    if this.Count < this.Length
        out += "\nNot enough data for this period."
    else if na(this.Beta)
        out += "\nReference returns do not vary."
    else if na(this.Correlation)
        out += "\nAsset returns do not vary; correlation is unavailable."
    out
 
// Settings: one user-selected reference, report appearance and visible periods.
var string targetSymbol = input.symbol("US500", "Reference", group = "Data", tooltip = "Symbol to compare with the chart asset.")
 
var TablePosition tablePos = input.enum(TablePosition.TopCenter, "Position", group = "Report")
var int lsz = input.int(12, "Text size", minval = 8, maxval = 20, group = "Report")
var color labelColor = input.color(lbc1, "Label color", group = "Report", tooltip = "Text color of the B and C row labels.")
var color titleColor = input.color(lbc1, "Title color", group = "Report")
var color backgroundColor = input.color(cxx, "Background color", group = "Report")
var color highlightColor = input.color(ccrm, "Highlight color", group = "Report", tooltip = "Background and transparency of the reference, period headings and row labels.")
 
var color borderColor = input.color(color.new(lbc1, 75), "Border color", group = "Report", tooltip = "Color and transparency of the internal cell borders.")
var int borderWidth = input.int(1, "Border width", minval = 0, maxval = 5, group = "Report", tooltip = "Internal cell border width in pixels. Set to 0 to hide the borders.")
var color frameColor = input.color(color.new(lbc1, 75), "Frame color", group = "Report", tooltip = "Color and transparency of the outer table frame.")
var int frameWidth = input.int(1, "Frame width", minval = 0, maxval = 5, group = "Report", tooltip = "Outer table frame width in pixels. Set to 0 to hide the frame.")
 
// Both sides use standard, regular-session, split-adjusted prices in their native currencies.
var string mainTicker = ticker.new(syminfo.prefix, syminfo.ticker, session.regular, adjustment.splits)
var string targetPrefix = syminfo.prefix(targetSymbol)
var string targetTick = syminfo.ticker(targetSymbol)
var string targetTicker = ticker.new(targetPrefix, targetTick, session.regular, adjustment.splits)
var bool has1M = input.bool(true, "1M", group = "Periods", inline = "short", tooltip = "21 daily bars.")
var bool has3M = input.bool(true, "3M", group = "Periods", inline = "short")
var bool has6M = input.bool(true, "6M", group = "Periods", inline = "short")
var bool has9M = input.bool(true, "9M", group = "Periods", inline = "short")
var bool has1Y = input.bool(true, "1Y", group = "Periods", inline = "long")
var bool has2Y = input.bool(true, "2Y", group = "Periods", inline = "long")
var bool has3Y = input.bool(true, "3Y", group = "Periods", inline = "long")
var bool has4Y = input.bool(true, "4Y", group = "Periods", inline = "long")
var bool has5Y = input.bool(true, "5Y", group = "Periods", inline = "long")
 
var string betaTooltip = "Beta: historical sensitivity of the asset's returns to the reference."
var string corrTooltip = "Correlation: strength and direction of the linear relationship between returns, from -1 to +1."
var string pos = tablePos.getPosition()
var table tbl = na
 
// Calculate on daily bars first, then display the results on the chart interval.
[b1M, c1M, n1M, s1M, b3M, c3M, n3M, s3M, b6M, c6M, n6M, s6M, b9M, c9M, n9M, s9M, b1Y, c1Y, n1Y, s1Y, b2Y, c2Y, n2Y, s2Y, b3Y, c3Y, n3Y, s3Y, b4Y, c4Y, n4Y, s4Y, b5Y, c5Y, n5Y, s5Y, lastDay, targetDescription] = request.security(mainTicker, "1D", dailyReport(targetTicker), gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_off, ignore_invalid_symbol = true, calc_bars_count = 1500)
 
if barstate.islast
    array<BetaWindow> windows = array.new<BetaWindow>()
    if has1M
        windows.push(BetaWindow.new("1M", len1M, s1M, lastDay, n1M, b1M, c1M))
    if has3M
        windows.push(BetaWindow.new("3M", len3M, s3M, lastDay, n3M, b3M, c3M))
    if has6M
        windows.push(BetaWindow.new("6M", len6M, s6M, lastDay, n6M, b6M, c6M))
    if has9M
        windows.push(BetaWindow.new("9M", len9M, s9M, lastDay, n9M, b9M, c9M))
    if has1Y
        windows.push(BetaWindow.new("1Y", len1Y, s1Y, lastDay, n1Y, b1Y, c1Y))
    if has2Y
        windows.push(BetaWindow.new("2Y", len2Y, s2Y, lastDay, n2Y, b2Y, c2Y))
    if has3Y
        windows.push(BetaWindow.new("3Y", len3Y, s3Y, lastDay, n3Y, b3Y, c3Y))
    if has4Y
        windows.push(BetaWindow.new("4Y", len4Y, s4Y, lastDay, n4Y, b4Y, c4Y))
    if has5Y
        windows.push(BetaWindow.new("5Y", len5Y, s5Y, lastDay, n5Y, b5Y, c5Y))
 
    // Keep room for a settings hint when all period columns are hidden.
    int lastCol = math.max(1, windows.size())
    if not na(tbl)
        table.delete(tbl)
    tbl := table.new(pos, lastCol + 1, 4, bgcolor = backgroundColor, frame_color = frameColor, frame_width = frameWidth, border_color = borderColor, border_width = borderWidth)
    table.merge_cells(tbl, 0, 0, lastCol, 0)
    string referenceName = na(targetDescription) ? "" : targetDescription
    string referenceTitle = targetTick + (referenceName == "" or referenceName == targetTick ? "" : " · " + referenceName)
    string referenceTip = "Asset: " + syminfo.prefix + ":" + syminfo.ticker
    referenceTip += "\nReference: " + targetSymbol
    referenceTip += "\nAs of: " + formatDate(lastDay)
    string analysisTitle = " (Beta & Correlation)"
    int titleLimit = math.max(20, math.min(64, lastCol * 8) - str.length(analysisTitle))
    string title = (str.length(referenceTitle) > titleLimit ? str.substring(referenceTitle, 0, titleLimit - 1) + "…" : referenceTitle) + analysisTitle
    tbl.addCell(0, 0, title, titleColor, text.align_left, lsz, referenceTip, highlightColor, isBold = true)
    tbl.addCell(0, 1, "", titleColor, text.align_center, lsz, bgColor = highlightColor)
    tbl.addCell(0, 2, "B", labelColor, text.align_center, lsz, betaTooltip, highlightColor, isBold = true)
    tbl.addCell(0, 3, "C", labelColor, text.align_center, lsz, corrTooltip, highlightColor, isBold = true)
    if windows.size() == 0
        tbl.addCell(1, 1, "Select a period in Settings", labelColor, text.align_left, lsz, bgColor = backgroundColor)
    for i = 0 to (windows.size() == 0 ? na : windows.size() - 1)
        BetaWindow window = windows.get(i)
        string details = window.toTooltip()
        string betaText = na(window.Beta) ? "—" : window.Beta.toText("0.00", true)
        string corrText = na(window.Correlation) ? "—" : window.Correlation.toText("0.00", true)
        tbl.addCell(i + 1, 1, " " + window.Title + " ", titleColor, text.align_center, lsz, details, highlightColor, isBold = true)
        tbl.addCell(i + 1, 2, " " + betaText + " ", na(window.Beta) ? labelColor : window.Beta.toColor(), text.align_right, lsz, "Beta\n" + details, backgroundColor)
        tbl.addCell(i + 1, 3, " " + corrText + " ", na(window.Correlation) ? labelColor : window.Correlation.toColor(), text.align_right, lsz, "Correlation\n" + details, backgroundColor)

← 返回列表