Exponential Regression Channel

muratkbesiroglu · study · 105 行 · 点赞 41 · TradingView 原页

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

Pine Script

//@version=6
indicator("Exponential Regression Channel", shorttitle = "ExpReg Channel", overlay = true, max_bars_back = 2000, max_polylines_count = 4)
 
// Model: ln(price) = a + b*x; fitted price = exp(a + b*x).
// Sigma is the RMS deviation of log-price residuals in the same window.
length = input.int(365, "Regression Length (Bars)", minval = 3, maxval = 2000)
src = input.source(close, "Price Source")
mult = input.float(2.0, "Standard Deviation Multiplier", minval = 0.1, step = 0.1)
showFill = input.bool(true, "Fill Channel")
showTable = input.bool(true, "Show Information Table")
midColor = input.color(color.orange, "Regression Line Color")
bandColor = input.color(color.aqua, "Channel Color")
 
var polyline middle = na
var polyline upper = na
var polyline lower = na
var polyline shade = na
var table info = table.new(position.top_right, 2, 6, bgcolor = color.new(color.black, 15))
 
if barstate.islast
    polyline.delete(middle)
    polyline.delete(upper)
    polyline.delete(lower)
    polyline.delete(shade)
    table.clear(info, 0, 0, 1, 5)
 
    if bar_index < length - 1
        table.cell(info, 0, 0, "Not enough bars. Reduce the regression length.", text_color = color.orange)
    else
        float sumY = 0.0
        float sumXY = 0.0
        bool valid = true
        // x = 0 is the oldest bar; x = length - 1 is the newest bar.
        for i = 0 to length - 1
            float price = src[i]
            if na(price) or price <= 0
                valid := false
            else
                float x = length - 1 - i
                float y = math.log(price)
                sumY += y
                sumXY += x * y
 
        if not valid
            table.cell(info, 0, 0, "The source must contain positive, non-missing values.", text_color = color.red)
        else
            float meanX = (length - 1) / 2.0
            float meanY = sumY / length
            float sxx = length * (length * length - 1.0) / 12.0
            float b = (sumXY - meanX * sumY) / sxx
            float a = meanY - b * meanX
            float sse = 0.0
            for i = 0 to length - 1
                float fitted = a + b * (length - 1 - i)
                float residual = math.log(src[i]) - fitted
                sse += residual * residual
            float sigma = math.sqrt(sse / length)
 
            midPoints = array.new<chart.point>()
            upPoints = array.new<chart.point>()
            lowPoints = array.new<chart.point>()
            fillPoints = array.new<chart.point>()
            // Evaluate the exponential fit at each bar for a straight channel on a log chart.
            for x = 0 to length - 1
                int idx = bar_index - length + 1 + x
                float fitted = a + b * x
                midPoints.push(chart.point.from_index(idx, math.exp(fitted)))
                upPoints.push(chart.point.from_index(idx, math.exp(fitted + mult * sigma)))
                lowPoints.push(chart.point.from_index(idx, math.exp(fitted - mult * sigma)))
                fillPoints.push(chart.point.from_index(idx, math.exp(fitted + mult * sigma)))
            for j = 0 to length - 1
                int x = length - 1 - j
                int idx = bar_index - length + 1 + x
                fillPoints.push(chart.point.from_index(idx, math.exp(a + b * x - mult * sigma)))
 
            if showFill
                shade := polyline.new(fillPoints, closed = true, line_color = color.new(bandColor, 100), fill_color = color.new(bandColor, 92))
            middle := polyline.new(midPoints, line_color = midColor, line_width = 2)
            upper := polyline.new(upPoints, line_color = bandColor, line_width = 1)
            lower := polyline.new(lowPoints, line_color = bandColor, line_width = 1)
 
            if showTable
                float current = math.exp(a + b * (length - 1))
                // Annualize using elapsed calendar time, not a fixed number of bars.
                // The open realtime bar uses the current observation time.
                int endTime = barstate.isrealtime ? timenow : time_close
                int startTime = time_close[length - 1]
                float yearMs = 365.2425 * 24.0 * 60.0 * 60.0 * 1000.0
                float years = (endTime - startTime) / yearMs
                float annualGrowth = years > 0 ? 100.0 * (math.exp(b * (length - 1) / years) - 1.0) : na
                float priceCagr = years > 0 ? 100.0 * (math.exp((math.log(src) - math.log(src[length - 1])) / years) - 1.0) : na
                string annualText = na(annualGrowth) ? "N/A" : str.tostring(annualGrowth, "#.##") + "%"
                string cagrText = na(priceCagr) ? "N/A" : str.tostring(priceCagr, "#.##") + "%"
                table.cell(info, 0, 0, "Exponential Regression", text_color = color.white)
                table.cell(info, 1, 0, str.tostring(length) + " bars", text_color = color.white)
                table.cell(info, 0, 1, "Upper Band", text_color = bandColor)
                table.cell(info, 1, 1, str.tostring(current * math.exp(mult * sigma), format.mintick), text_color = bandColor)
                table.cell(info, 0, 2, "Regression", text_color = midColor)
                table.cell(info, 1, 2, str.tostring(current, format.mintick), text_color = midColor)
                table.cell(info, 0, 3, "Lower Band", text_color = bandColor)
                table.cell(info, 1, 3, str.tostring(current * math.exp(-mult * sigma), format.mintick), text_color = bandColor)
                table.cell(info, 0, 4, "Annualized Regression Growth", text_color = color.white)
                table.cell(info, 1, 4, annualText, text_color = color.white)
                table.cell(info, 0, 5, "Price CAGR (Selected Period)", text_color = color.white)
                table.cell(info, 1, 5, cagrText, text_color = color.white)

← 返回列表