Bitcoin Power Law (Santostasi, Burger, Rainbow, Trolololo)

brzcap · study · 276 行 · 点赞 14 · TradingView 原页

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

Pine Script

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © brzcap
 
//@version=6
// =============================================================================
//  Bitcoin Power Law
// -----------------------------------------------------------------------------
//  What it does
//  ------------
//  Draws a Bitcoin power-law model on a BTC/USD (or USD-stablecoin) chart: the
//  model's center line and bands, a dashed projection into the future, and
//  where price sits against the model. Choose the model in the "Model"
//  dropdown.
//
//  How it works
//  ------------
//  Every line is a power law of time, i.e. a straight line on a log-log chart:
//        log10(price) = a + b * log10(days since the model's day zero)
//  The lines depend only on time, so they never repaint. They also look the
//  same on any exchange's chart, however much history that chart has.
//
//  Models (constants as published; sources checked Nov 2026)
//  ------
//  Santostasi & Perrenod (2026): a = -16.509, b = 5.690, days since 3 Jan 2009.
//      Bands are ±2σ of the fit's residuals (σ = 0.302 dex, from the paper).
//      Source: G. Santostasi & S. Perrenod, "A Mechanistic Derivation of the
//      Bitcoin Price Power Law" (2026). The intercept is from Perrenod's 2026
//      write-up (log P = -1.928 + 5.690 log Age, Age in years), converted to
//      days.
//  Burger corridor (Capriole): Burger's 2019 "natural long-term power-law
//      corridor of growth", as implemented in Capriole Investments'
//      TradingView script, with days since 1 Jan 2009:
//        center      a = -16.98212206   b = 5.83430649
//        support     a = -17.43212206   b = 5.83430649
//        resistance  a = -13.36632341   b = 5.02927337
//      Burger's article gives a center fit of
//      a = -17.01593313, b = 5.84509376 and the top line's slope 5.02927337,
//      but no band intercepts. Capriole's script is the complete corridor.
//  Rainbow Chart V1 (Blockchaincenter): ten lines,
//        price = 10^(k * ln(i + c) - 19.463), i = days since 1 Jan 2012,
//      i.e. a = -19.463, b = k * ln(10), day zero = 1 Jan 2012 minus c days.
//  Trolololo 2014: 10^(2.9065 * ln(days since 9 Jan 2009) - 19.493), the
//      log regression by bitcointalk user trolololo that started the field.
//      Center line only.
//  Trolololo 2017: 10^(2.66167155005961 * ln(days since 9 Jan 2009)
//      - 17.9183761889864), his 2017 recalibration. Center line only.
//  brzcap fit (2026): a = -16.2471, b = 5.6128, days since 3 Jan 2009. OLS on
//      CRYPTO:BTCUSD weekly closes, Jul 2010 to Nov 2026 (R^2 0.961). Bands
//      are multiples of the center line.
//  Custom: your own exponent and intercept, with bands as multiples.
//
//  Use a logarithmic price scale (right-click the price axis -> Logarithmic).
//  On a linear scale the early years flatten to zero.
// =============================================================================
 
indicator("Bitcoin Power Law", shorttitle = "BTC PL", overlay = true, max_polylines_count = 13)
 
// ----------------------------------------------------------------------------- Inputs
const string M_SP  = "Santostasi & Perrenod (2026)"
const string M_BUR = "Burger corridor (Capriole)"
const string M_RB  = "Rainbow Chart V1"
const string M_T14 = "Trolololo 2014"
const string M_T17 = "Trolololo 2017"
const string M_BRZ = "brzcap fit (2026)"
const string M_CUS = "Custom"
 
grpM = "Model"
model     = input.string(M_SP, "Model", options = [M_SP, M_BUR, M_RB, M_T14, M_T17, M_BRZ, M_CUS], group = grpM, tooltip = "Santostasi & Perrenod: the original power law's current published fit, with ±2σ bands.\nBurger corridor: support and resistance lines (Capriole's TradingView version).\nRainbow Chart V1: Blockchaincenter's original ten-line rainbow.\nTrolololo 2014 / 2017: the bitcointalk log regression that started it all, and its 2017 recalibration (center line only).\nbrzcap fit: OLS on weekly closes, 2010-2026.\nThe script header lists every constant and its source.")
exponent  = input.float(5.6128, "Custom exponent", step = 0.01, display = display.none, group = grpM, tooltip = "Used only when Model = Custom. Slope of log10(price) against log10(days since 3 Jan 2009).")
intercept = input.float(-16.2471, "Custom intercept (log10)", step = 0.01, display = display.none, group = grpM, tooltip = "Used only when Model = Custom. log10 of the model price on day 1 after genesis.")
 
grpB = "Bands"
showBands = input.bool(true, "Show bands", group = grpB)
floorMult = input.float(0.40, "Lower band (× center)", minval = 0.01, step = 0.05, display = display.none, group = grpB, tooltip = "Used only by the brzcap fit and Custom models; the others use their published bands. For the brzcap fit, the lowest weekly close relative to the center line in each cycle was: 2015 0.39×, 2018 0.43×, 2022 0.45×, Nov 2026 0.45×.")
upperMult = input.float(3.5, "Upper band (× center)", minval = 1.0, step = 0.25, display = display.none, group = grpB, tooltip = "Used only by the brzcap fit and Custom models. For the brzcap fit, cycle peaks relative to the center line were: 2013 9.9×, 2017 6.4×, 2021 3.5×, 2025 1.4×. Peaks have shrunk every cycle, so treat this as a reference level, not a ceiling.")
 
grpP = "Projection"
projDays = input.int(730, "Project forward (days)", minval = 0, maxval = 3650, step = 30, display = display.none, group = grpP, tooltip = "Extends the lines into the future as dashed lines. Set to 0 to turn it off.")
 
grpD = "Display"
showTable = input.bool(true, "Show info table", group = grpD)
anySymbol = input.bool(false, "Draw on any symbol", group = grpD, tooltip = "The models are BTC priced in USD. On other symbols the lines are hidden by default, because they would wreck the price scale.")
 
grpS = "Style"
midColor   = input.color(#f7931a, "Center line", group = grpS)
lowColor   = input.color(#26a69a, "Lower band",  group = grpS)
highColor  = input.color(#ef5350, "Upper band",  group = grpS)
shadeColor = input.color(color.new(#f7931a, 94), "Band shading", group = grpS, tooltip = "Fill between the lower and upper bands. Turn it off under Style → Band shading. The Rainbow model uses its own fixed colors.")
 
// ----------------------------------------------------------------------------- Model
const int   GENESIS_MS = 1230940800000          // 2009-01-03 00:00 UTC, the genesis block's day
const int   JAN01_MS   = 1230768000000          // 2009-01-01 00:00 UTC (Burger/Capriole day zero)
const int   JAN09_MS   = 1231459200000          // 2009-01-09 00:00 UTC (Trolololo day zero)
const int   RB_BASE_MS = 1325376000000          // 2012-01-01 00:00 UTC (Rainbow day index origin)
const float DAY_MS     = 86400000.0
const float LN10       = 2.302585092994046      // converts ln-based slopes to log10-based
 
// One power-law line: log10(price) = a + b * log10(days since dayZero).
type PLine
    float a
    float b
    int   dayZero
 
// Price on line `l` at time `t` (ms). na if the line is na or t is on/before its day zero.
f_lineAt(PLine l, float t) =>
    float v = na
    if not na(l)
        float d = (t - l.dayZero) / DAY_MS
        if d > 0
            v := math.pow(10, l.a + l.b * math.log10(d))
    v
 
bool isRainbow = model == M_RB
bool hasBands  = not isRainbow and model != M_T14 and model != M_T17   // Trolololo models are center-line only
 
var PLine  midL     = na
var PLine  lowL     = na
var PLine  highL    = na
var string lowName  = "Lower band"
var string highName = "Upper band"
var array<PLine> rainbow = array.new<PLine>()
 
if barstate.isfirst
    switch model
        M_SP =>
            midL     := PLine.new(-16.509, 5.690, GENESIS_MS)
            lowL     := PLine.new(-16.509 - 2 * 0.302, 5.690, GENESIS_MS)
            highL    := PLine.new(-16.509 + 2 * 0.302, 5.690, GENESIS_MS)
            lowName  := "−2σ band"
            highName := "+2σ band"
        M_BUR =>
            midL     := PLine.new(-16.98212206, 5.83430649, JAN01_MS)
            lowL     := PLine.new(-17.43212206, 5.83430649, JAN01_MS)
            highL    := PLine.new(-13.36632341, 5.02927337, JAN01_MS)
            lowName  := "Support"
            highName := "Resistance"
        M_RB =>
            array<float> ks = array.from(2.90, 2.886, 2.872, 2.859, 2.845, 2.8295, 2.815, 2.801, 2.788, 2.775)
            array<int>   cs = array.from(1400, 1375, 1350, 1320, 1293, 1275, 1250, 1225, 1200, 1175)
            for i = 0 to 9
                rainbow.push(PLine.new(-19.463, ks.get(i) * LN10, RB_BASE_MS - cs.get(i) * 86400000))
        M_T14 =>
            midL := PLine.new(-19.493, 2.9065 * LN10, JAN09_MS)
        M_T17 =>
            midL := PLine.new(-17.9183761889864, 2.66167155005961 * LN10, JAN09_MS)
        M_BRZ =>
            midL  := PLine.new(-16.2471, 5.6128, GENESIS_MS)
            lowL  := PLine.new(-16.2471 + math.log10(floorMult), 5.6128, GENESIS_MS)
            highL := PLine.new(-16.2471 + math.log10(upperMult), 5.6128, GENESIS_MS)
        =>
            midL  := PLine.new(intercept, exponent, GENESIS_MS)
            lowL  := PLine.new(intercept + math.log10(floorMult), exponent, GENESIS_MS)
            highL := PLine.new(intercept + math.log10(upperMult), exponent, GENESIS_MS)
 
// "$123,456" for large values, "$1.23" for small ones.
f_usd(float v) =>
    "$" + str.tostring(v, v >= 100 ? "#,###" : "#,##0.00")
 
// ----------------------------------------------------------------------------- Symbol check
// Only draw on BTC quoted in USD or a USD stablecoin, unless the user overrides it.
string cur    = syminfo.currency
bool   isBtc  = syminfo.basecurrency == "BTC" or str.contains(syminfo.ticker, "BTC")
bool   isUsd  = str.startswith(cur, "USD") or cur == "DAI" or cur == "FDUSD" or cur == "TUSD" or cur == "BUSD" or cur == "PYUSD"
bool   active = anySymbol or (isBtc and isUsd)
 
// ----------------------------------------------------------------------------- Center line and bands (every model except Rainbow)
float midV  = active ? f_lineAt(midL, time) : na
float lowV  = active and showBands ? f_lineAt(lowL, time) : na
float highV = active and showBands ? f_lineAt(highL, time) : na
 
pLow  = plot(lowV,  "Lower band",  color = lowColor,  linewidth = 1, display = hasBands and showBands ? display.all : display.none)
pMid  = plot(midV,  "Center line", color = midColor,  linewidth = 2, display = isRainbow ? display.none : display.all)
pHigh = plot(highV, "Upper band",  color = highColor, linewidth = 1, display = hasBands and showBands ? display.all : display.none)
fill(pLow, pHigh, color = shadeColor, title = "Band shading")
 
// ----------------------------------------------------------------------------- Rainbow (Rainbow model only)
// Line colors, top (red) to bottom (indigo). Band n is the fill between lines n and n+1.
var array<color> RB_COLS = array.from(#b71c1c, #e53935, #fb8c00, #ffb300, #fdd835, #9ccc65, #43a047, #1e88e5, #5e35b1, #5e35b1)
 
f_rb(int i) =>
    float v = na
    if active and isRainbow
        v := f_lineAt(rainbow.get(i), time)
    v
 
pR0 = plot(f_rb(0), "Rainbow line 1",  color = color.new(#b71c1c, 40), display = isRainbow ? display.all : display.none)
pR1 = plot(f_rb(1), "Rainbow line 2",  color = color.new(#e53935, 40), display = isRainbow ? display.all : display.none)
pR2 = plot(f_rb(2), "Rainbow line 3",  color = color.new(#fb8c00, 40), display = isRainbow ? display.all : display.none)
pR3 = plot(f_rb(3), "Rainbow line 4",  color = color.new(#ffb300, 40), display = isRainbow ? display.all : display.none)
pR4 = plot(f_rb(4), "Rainbow line 5",  color = color.new(#fdd835, 40), display = isRainbow ? display.all : display.none)
pR5 = plot(f_rb(5), "Rainbow line 6",  color = color.new(#9ccc65, 40), display = isRainbow ? display.all : display.none)
pR6 = plot(f_rb(6), "Rainbow line 7",  color = color.new(#43a047, 40), display = isRainbow ? display.all : display.none)
pR7 = plot(f_rb(7), "Rainbow line 8",  color = color.new(#1e88e5, 40), display = isRainbow ? display.all : display.none)
pR8 = plot(f_rb(8), "Rainbow line 9",  color = color.new(#5e35b1, 40), display = isRainbow ? display.all : display.none)
pR9 = plot(f_rb(9), "Rainbow line 10", color = color.new(#5e35b1, 40), display = isRainbow ? display.all : display.none)
fill(pR0, pR1, color = color.new(#b71c1c, 75), title = "Rainbow band 1")
fill(pR1, pR2, color = color.new(#e53935, 75), title = "Rainbow band 2")
fill(pR2, pR3, color = color.new(#fb8c00, 75), title = "Rainbow band 3")
fill(pR3, pR4, color = color.new(#ffb300, 75), title = "Rainbow band 4")
fill(pR4, pR5, color = color.new(#fdd835, 75), title = "Rainbow band 5")
fill(pR5, pR6, color = color.new(#9ccc65, 75), title = "Rainbow band 6")
fill(pR6, pR7, color = color.new(#43a047, 75), title = "Rainbow band 7")
fill(pR7, pR8, color = color.new(#1e88e5, 75), title = "Rainbow band 8")
fill(pR8, pR9, color = color.new(#5e35b1, 75), title = "Rainbow band 9")
 
// ----------------------------------------------------------------------------- Projection (dashed, into the future)
if barstate.islast
    for pl in polyline.all
        pl.delete()
    if active and projDays > 0
        array<PLine> lines  = array.new<PLine>()
        array<color> cols   = array.new<color>()
        array<int>   widths = array.new<int>()
        if isRainbow
            for i = 0 to 9
                lines.push(rainbow.get(i))
                cols.push(RB_COLS.get(i))
                widths.push(1)
        else
            if showBands and not na(highL)
                lines.push(highL)
                cols.push(highColor)
                widths.push(1)
            lines.push(midL)
            cols.push(midColor)
            widths.push(2)
            if showBands and not na(lowL)
                lines.push(lowL)
                cols.push(lowColor)
                widths.push(1)
        int steps = math.max(2, math.min(200, int(math.floor(projDays / 7.0))))   // ~weekly points
        for [j, ln] in lines
            array<chart.point> pts = array.new<chart.point>()
            for i = 0 to steps
                int t = time + int(math.round(i * projDays * DAY_MS / steps))
                pts.push(chart.point.from_time(t, f_lineAt(ln, t)))
            polyline.new(pts, xloc = xloc.bar_time, line_color = cols.get(j), line_style = line.style_dashed, line_width = widths.get(j))
 
// ----------------------------------------------------------------------------- Info table
// Neutral grays read on both light and dark chart themes.
var table info = showTable ? table.new(position.top_right, 2, 8, bgcolor = color.new(color.gray, 85), frame_color = color.new(color.gray, 60), frame_width = 1) : na
 
f_row(int row, string label, string value) =>
    table.cell(info, 0, row, label, text_color = color.gray, text_halign = text.align_left,  text_size = size.small)
    table.cell(info, 1, row, value, text_color = color.gray, text_halign = text.align_right, text_size = size.small)
 
if barstate.islast and showTable
    table.clear(info, 0, 0, 1, 7)
    if not active
        f_row(0, "Bitcoin Power Law", "needs a BTC/USD chart")
        f_row(1, "This chart", syminfo.basecurrency + " / " + cur)
    else
        f_row(0, "Model", model)
        f_row(1, "Days since genesis", str.tostring(math.floor((time - GENESIS_MS) / DAY_MS), "#,###"))
        if isRainbow
            // Count the lines above price: 0 = above the rainbow, 10 = below it, n = in band n.
            int linesAbove = 0
            for i = 0 to 9
                if close < f_lineAt(rainbow.get(i), time)
                    linesAbove += 1
            string pos = linesAbove == 0 ? "Above the rainbow" : linesAbove == 10 ? "Below the rainbow" : "Band " + str.tostring(linesAbove) + " of 9 (1 = top)"
            f_row(2, "Rainbow position", pos)
            f_row(3, "Top line",    f_usd(f_lineAt(rainbow.get(0), time)))
            f_row(4, "Bottom line", f_usd(f_lineAt(rainbow.get(9), time)))
        else
            float midNow = f_lineAt(midL, time)
            f_row(2, "Center line",    f_usd(midNow))
            f_row(3, "Price ÷ center", str.tostring(close / midNow, "0.00") + "×")
            int r = 4
            if showBands and not na(lowL)
                f_row(r, lowName, f_usd(f_lineAt(lowL, time)))
                r += 1
            if showBands and not na(highL)
                f_row(r, highName, f_usd(f_lineAt(highL, time)))
                r += 1
            f_row(r, "Center in 1 year", f_usd(f_lineAt(midL, time + 365 * DAY_MS)))

← 返回列表