Created
July 29, 2026 11:52
-
-
Save pedr0-fr/3429922319d6364155738a8c9141ba66 to your computer and use it in GitHub Desktop.
Rainbow indicator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //@version=6 | |
| indicator("BTC Rainbow Intercept", shorttitle="Rainbow c", overlay=false, max_lines_count=500) | |
| // --- Inputs --- | |
| slope = input.float(5.7, "Slope", step=0.0001, tooltip="Exponent of the power law: price = 10^(slope·log10(days) + intercept)") | |
| src = input.source(close, "Price source") | |
| projMonths = input.int(4, "Projection horizon (months)", minval=1, maxval=24) | |
| showFlat = input.bool(true, "Show flat-price projection") | |
| showShift = input.bool(true, "Show shifted-price projection") | |
| shiftPct = input.float(-10.0, "Price shift %", step=0.5) | |
| showPower = input.bool(true, "Draw power line on price chart") | |
| showBands = input.bool(true, "Draw intercept bands on price chart") | |
| bandWidth = input.float(0.05, "Band width (intercept units)", minval=0.01, step=0.01, tooltip="0.1 ≈ ×1.26 above / ×0.79 below the power line") | |
| useManualC = input.bool(false, "Manual intercept", tooltip="Off: power line anchors to the current derived intercept (passes through the latest close). On: uses the value below.") | |
| manualC = input.float(-16.6773, "Manual intercept value", step=0.01) | |
| // --- Days since genesis block (Jan 3, 2009) --- | |
| genesisTime = timestamp("GMT", 2009, 1, 3, 0, 0) | |
| daysSinceGenesis = (time - genesisTime) / 86400000.0 | |
| // --- First bar time (full chart anchor) --- | |
| var float firstBarTime = na | |
| if barstate.isfirst | |
| firstBarTime := time | |
| // --- Implied intercept for the current price --- | |
| intercept = math.log10(src) - slope * math.log10(daysSinceGenesis) | |
| plot(intercept, "Implied intercept", color=color.orange, linewidth=2) | |
| hline(-16.6773, "Fitted intercept (-16.6773)", color=color.gray, linestyle=hline.style_dashed) | |
| // --- Projections (in the pane) --- | |
| var array<line> projLines = array.new<line>() | |
| drawProjection(float p0, float p1, color col) => | |
| int nSegs = 16 | |
| float horizonMs = projMonths * 30.4375 * 86400000.0 | |
| float stepMs = horizonMs / nSegs | |
| for i = 0 to nSegs - 1 | |
| t1 = time + i * stepMs | |
| t2 = time + (i + 1) * stepMs | |
| d1 = (t1 - genesisTime) / 86400000.0 | |
| d2 = (t2 - genesisTime) / 86400000.0 | |
| pA = p0 + (p1 - p0) * i / nSegs | |
| pB = p0 + (p1 - p0) * (i + 1) / nSegs | |
| c1 = math.log10(pA) - slope * math.log10(d1) | |
| c2 = math.log10(pB) - slope * math.log10(d2) | |
| array.push(projLines, line.new(int(t1), c1, int(t2), c2, | |
| xloc = xloc.bar_time, color = col, | |
| style = line.style_dashed, width = 1)) | |
| // --- Power curves drawn on the MAIN price chart, spanning the full chart --- | |
| var array<line> powerLines = array.new<line>() | |
| drawPowerCurve(float c, color col, string sty) => | |
| float dStart = (firstBarTime - genesisTime) / 86400000.0 | |
| float dEnd = daysSinceGenesis + projMonths * 30.4375 | |
| int nSegs = 120 | |
| float ratio = math.pow(dEnd / dStart, 1.0 / nSegs) | |
| for i = 0 to nSegs - 1 | |
| d1 = dStart * math.pow(ratio, i) | |
| d2 = dStart * math.pow(ratio, i + 1) | |
| t1 = genesisTime + d1 * 86400000.0 | |
| t2 = genesisTime + d2 * 86400000.0 | |
| p1 = math.pow(10, slope * math.log10(d1) + c) | |
| p2 = math.pow(10, slope * math.log10(d2) + c) | |
| array.push(powerLines, line.new(int(t1), p1, int(t2), p2, | |
| xloc = xloc.bar_time, color = col, | |
| style = sty, width = 1, | |
| force_overlay = true)) | |
| if barstate.islast | |
| while array.size(projLines) > 0 | |
| line.delete(array.pop(projLines)) | |
| while array.size(powerLines) > 0 | |
| line.delete(array.pop(powerLines)) | |
| if showFlat | |
| drawProjection(src, src, color.new(color.orange, 30)) | |
| if showShift | |
| pShift = src * (1 + shiftPct / 100.0) | |
| drawProjection(pShift, pShift, color.new(color.red, 30)) | |
| // choose intercept for the power line: auto (current derived) or manual | |
| float powerC = useManualC ? manualC : intercept | |
| if showPower | |
| drawPowerCurve(powerC, color.new(color.blue, 20), line.style_solid) | |
| if showBands | |
| drawPowerCurve(powerC + bandWidth, color.new(color.blue, 60), line.style_dotted) | |
| drawPowerCurve(powerC - bandWidth, color.new(color.blue, 60), line.style_dotted) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment