pine-scriptpine-script-v5

Get high from specified date as starting point, not all time


Trying to modify code that gets high from all time for multiple tickers. Want to add user input for specified date to start from, this year, etc.

I can't figure out how to specify the starting bar.

I tried switching out startIndex but it doesn't take date as it is now and I'm not sure how to rewrite it.

// Want to add user input start time to check for high
i_startTime = input(defval = timestamp("01 Jan 2024 01:00 +0000"), title = "Start Time")


use01 = input.bool(true,title="",inline = "u1", group = 'Asset')
t01 = input.symbol('BINANCE:BTCUSDT',title="", inline = "u1", group='Asset')
//Multiple lines of this for many tickers

//CALCULATIONS
startIndex = bar_index == 1

strip(asset)=>
    assArr = str.split(asset, ":")
    array.get(assArr,1)

allTimeHigh()=>
    value=0.0
    value:= startIndex ? high : high > value[1] ? high : value[1]


[currVal01, ath01] = request.security(t01, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
//Multiple lines of this for many tickers

dist01 = (currVal01 - ath01) / ath01

arrDist = array.new_float(0)
arrName = array.new_string(0)

if use01
    array.push(arrName, strip(t01))
    array.push(arrDist, dist01)

// Function to swap elements in the arrName and arrDist arrays
swap(arrName, arrDist, i, j) =>
    tempDist = array.get(arrDist, i)
    tempName = array.get(arrName, i)
    array.set(arrDist, i, array.get(arrDist, j))
    array.set(arrName, i, array.get(arrName, j))
    array.set(arrDist, j, tempDist)
    array.set(arrName, j, tempName)

// Simple bubble sort to sort both arrays based on arrDist
for i = 0 to array.size(arrDist) - 2
    for j = 0 to array.size(arrDist) - i - 2
        if array.get(arrDist, j) < array.get(arrDist, j + 1)
            swap(arrName, arrDist, j, j + 1)

Here is the full code:

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/

//@version=5
indicator("ATH Distance Heatmap", overlay = true) 

//INPUTS
i_startTime = input.time(defval = timestamp("01 Jan 2024 01:00 +0000"), title = "Start Time")
dashSize = input.string("Normal", title = "Dashboard Size:", options = ["Small","Normal","Large"])
dashColSize = input.int(5,minval=1, title="Column(s) in a Row:")

use01 = input.bool(true,title="",inline = "u1", group = 'Asset')
t01 = input.symbol('BINANCE:BTCUSDT',title="", inline = "u1", group='Asset')

use02 = input.bool(true,title="",inline = "u2", group = 'Asset')
t02 = input.symbol('BINANCE:ETHUSDT',title="", inline = "u2", group='Asset')

use03 = input.bool(true,title="",inline = "u3", group = 'Asset')
t03 = input.symbol('BINANCE:SOLUSDT',title="", inline = "u3",group='Asset')

use04 = input.bool(true,title="",inline = "u4", group = 'Asset')
t04 = input.symbol('BINANCE:BNBUSDT',title="", inline = "u4",group='Asset')

use05 = input.bool(true,title="",inline = "u5", group = 'Asset')
t05 = input.symbol('BINANCE:ARBUSDT',title="", inline = "u5",group='Asset')

use06 = input.bool(true,title="",inline = "u6", group = 'Asset')
t06 = input.symbol('BINANCE:FTMUSDT',title="", inline = "u6",group='Asset')

use07 = input.bool(true,title="",inline = "u7", group = 'Asset')
t07 = input.symbol('BINANCE:NEARUSDT',title="", inline = "u7",group='Asset')

use08 = input.bool(true,title="",inline = "u8", group = 'Asset')
t08 = input.symbol('BINANCE:OPUSDT',title="", inline = "u8",group='Asset')

use09 = input.bool(true,title="",inline = "u9", group = 'Asset')
t09 = input.symbol('BINANCE:INJUSDT',title="", inline = "u9",group='Asset')

use10 = input.bool(true,title="",inline = "u10", group = 'Asset')
t10 = input.symbol('BINANCE:LINKUSDT',title="", inline = "u10",group='Asset')

use11 = input.bool(true,title="",inline = "u11", group = 'Asset')
t11 = input.symbol('BINANCE:DOTUSDT',title="", inline = "u11",group='Asset')

use12 = input.bool(true,title="",inline = "u12", group = 'Asset')
t12 = input.symbol('BINANCE:AVAXUSDT',title="", inline = "u12",group='Asset')

use13 = input.bool(true,title="",inline = "u13", group = 'Asset')
t13 = input.symbol('BINANCE:BCHUSDT',title="", inline = "u13",group='Asset')

use14 = input.bool(true,title="",inline = "u14", group = 'Asset')
t14 = input.symbol('BINANCE:LTCUSDT',title="", inline = "u14",group='Asset')

use15 = input.bool(true,title="",inline = "u15", group = 'Asset')
t15 = input.symbol('BINANCE:LDOUSDT',title="", inline = "u15",group='Asset')

use16 = input.bool(true,title="",inline = "u16", group = 'Asset')
t16 = input.symbol('BINANCE:AAVEUSDT',title="", inline = "u16",group='Asset')

use17 = input.bool(true,title="",inline = "u17", group = 'Asset')
t17 = input.symbol('BINANCE:COMPUSDT',title="", inline = "u17",group='Asset')

use18 = input.bool(true,title="",inline = "u18", group = 'Asset')
t18 = input.symbol('BINANCE:STXUSDT',title="", inline = "u18",group='Asset')

use19 = input.bool(true,title="",inline = "u19", group = 'Asset')
t19 = input.symbol('BINANCE:THETAUSDT',title="", inline = "u19",group='Asset')

use20 = input.bool(true,title="",inline = "u20", group = 'Asset')
t20 = input.symbol('BINANCE:RUNEUSDT',title="", inline = "u20",group='Asset')

use21 = input.bool(true,title="",inline = "u21", group = 'Asset')
t21 = input.symbol('BINANCE:UNIUSDT',title="", inline = "u21",group='Asset')

use22 = input.bool(true,title="",inline = "u22", group = 'Asset')
t22 = input.symbol('BINANCE:SUSHIUSDT',title="", inline = "u22",group='Asset')

use23 = input.bool(true,title="",inline = "u23", group = 'Asset')
t23 = input.symbol('BINANCE:1INCHUSDT',title="", inline = "u23",group='Asset')

use24 = input.bool(true,title="",inline = "u24", group = 'Asset')
t24 = input.symbol('BINANCE:DYDXUSDT',title="", inline = "u24",group='Asset')

use25 = input.bool(true,title="",inline = "u25", group = 'Asset')
t25 = input.symbol('BINANCE:TONUSDT',title="", inline = "u25",group='Asset')

use26 = input.bool(true,title="",inline = "u26", group = 'Asset')
t26 = input.symbol('BINANCE:SUIUSDT',title="", inline = "u26",group='Asset')

use27 = input.bool(true,title="",inline = "u27", group = 'Asset')
t27 = input.symbol('BINANCE:SEIUSDT',title="", inline = "u27",group='Asset')

use28 = input.bool(true,title="",inline = "u28", group = 'Asset')
t28 = input.symbol('BINANCE:BTCUSDT',title="", inline = "u28",group='Asset')

use29 = input.bool(true,title="",inline = "u29", group = 'Asset')
t29 = input.symbol('BINANCE:JTOUSDT',title="", inline = "u29",group='Asset')

use30 = input.bool(true,title="",inline = "u30", group = 'Asset')
t30 = input.symbol('BINANCE:TAOUSDT',title="", inline = "u30",group='Asset')

use31 = input.bool(true,title="",inline = "u31", group = 'Asset')
t31 = input.symbol('BINANCE:TIAUSDT',title="", inline = "u31",group='Asset')  

use32 = input.bool(true,title="",inline = "u32", group = 'Asset')
t32 = input.symbol('BINANCE:ICPUSDT',title="", inline = "u32",group='Asset')

use33 = input.bool(true,title="",inline = "u33", group = 'Asset')
t33 = input.symbol('BINANCE:PYTHUSDT',title="", inline = "u33",group='Asset')

use34 = input.bool(true,title="",inline = "u34", group = 'Asset')
t34 = input.symbol('BINANCE:CHZUSDT',title="", inline = "u34",group='Asset')

use35 = input.bool(true,title="",inline = "u35", group = 'Asset')
t35 = input.symbol('BINANCE:MANAUSDT',title="", inline = "u35",group='Asset')

use36 = input.bool(true,title="",inline = "u36", group = 'Asset')
t36 = input.symbol('BINANCE:SANDUSDT',title="", inline = "u36",group='Asset')

use37 = input.bool(true,title="",inline = "u37", group = 'Asset')
t37 = input.symbol('BINANCE:WIFUSDT',title="", inline = "u37",group='Asset')

use38 = input.bool(true,title="",inline = "u38", group = 'Asset')
t38 = input.symbol('BINANCE:PEPEUSDT',title="", inline = "u38",group='Asset')

use39 = input.bool(true,title="",inline = "u39", group = 'Asset')
t39 = input.symbol('BINANCE:SHIBUSDT',title="", inline = "u39",group='Asset')

use40 = input.bool(true,title="",inline = "u40", group = 'Asset')
t40 = input.symbol('BINANCE:DOGEUSDT',title="", inline = "u40", group='Asset')


// EDIT
//i_startTime = input.time(defval = timestamp("01 Jan 2024 01:00 +0000"), title = "Start Time")
//i_startTime = input(defval = timestamp("01 Jan 2024 01:00 +0000"), title = "Start Time")
//i_endTime = input.time(title = "starttime", defval=0)
//i_daysBack=input.int(10)

//highestHigh = ta.highest(high, 20)
//lowestLows = ta.lowest(low, 20)

//i_date = input.time(timestamp("01 Jan 2024 00:00 +0300"), "Start Date", confirm=true)
//var int idx_date = na
//idx_date := (time == i_date) ? bar_index : idx_date
//myStartIndex = str.tostring(bar_index-idx_date)

//anchorTime = input.time(timestamp("01 Jan 2024 00:00 -0500"), "Date")
//anchorBarIndex = (time - anchorTime) / (1000 * timeframe.in_seconds(timeframe.period))
//anchorBarsBack = bar_index - anchorBarIndex 

//specificDate = input.time(timestamp("01 Jan 2024 00:00 -0500"), "Date")
//var float lowOnDate= na
//if specificDate and not specificDate[1]
//    lowOnDate := low


//CALCULATIONS
//startIndex = bar_index == 1
//startIndex = time >= i_startTime
startIndex = time >= i_startTime

strip(asset)=>
    assArr = str.split(asset, ":")
    array.get(assArr,1)

allTimeHigh()=>
    value=0.0
//    value:= startIndex ? high : high > value[1] ? high : value[1]
    value:= startIndex ? high : high > value[1] ? high : value[1]

//Show the timespan checked for High in grey
bgcolor(startIndex ? color.new(color.gray, 90) : na)


[currVal01, ath01] = request.security(t01, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal02, ath02] = request.security(t02, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal03, ath03] = request.security(t03, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal04, ath04] = request.security(t04, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal05, ath05] = request.security(t05, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal06, ath06] = request.security(t06, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal07, ath07] = request.security(t07, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal08, ath08] = request.security(t08, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal09, ath09] = request.security(t09, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal10, ath10] = request.security(t10, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal11, ath11] = request.security(t11, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal12, ath12] = request.security(t12, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal13, ath13] = request.security(t13, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal14, ath14] = request.security(t14, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal15, ath15] = request.security(t15, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal16, ath16] = request.security(t16, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal17, ath17] = request.security(t17, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal18, ath18] = request.security(t18, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal19, ath19] = request.security(t19, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal20, ath20] = request.security(t20, 'M', [close, allTimeHigh()],lookahead=barmerge.lookahead_on)
[currVal21, ath21] = request.security(t21, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal22, ath22] = request.security(t22, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal23, ath23] = request.security(t23, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal24, ath24] = request.security(t24, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal25, ath25] = request.security(t25, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal26, ath26] = request.security(t26, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal27, ath27] = request.security(t27, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal28, ath28] = request.security(t28, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal29, ath29] = request.security(t29, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal30, ath30] = request.security(t30, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal31, ath31] = request.security(t31, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal32, ath32] = request.security(t32, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal33, ath33] = request.security(t33, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal34, ath34] = request.security(t34, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal35, ath35] = request.security(t35, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal36, ath36] = request.security(t36, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal37, ath37] = request.security(t37, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal38, ath38] = request.security(t38, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal39, ath39] = request.security(t39, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)
[currVal40, ath40] = request.security(t40, 'M', [close, allTimeHigh()], lookahead=barmerge.lookahead_on)

dist01 = (currVal01 - ath01) / ath01
dist02 = (currVal02 - ath02) / ath02
dist03 = (currVal03 - ath03) / ath03
dist04 = (currVal04 - ath04) / ath04
dist05 = (currVal05 - ath05) / ath05
dist06 = (currVal06 - ath06) / ath06
dist07 = (currVal07 - ath07) / ath07
dist08 = (currVal08 - ath08) / ath08
dist09 = (currVal09 - ath09) / ath09
dist10 = (currVal10 - ath10) / ath10
dist11 = (currVal11 - ath11) / ath11
dist12 = (currVal12 - ath12) / ath12
dist13 = (currVal13 - ath13) / ath13
dist14 = (currVal14 - ath14) / ath14
dist15 = (currVal15 - ath15) / ath15
dist16 = (currVal16 - ath16) / ath16
dist17 = (currVal17 - ath17) / ath17
dist18 = (currVal18 - ath18) / ath18
dist19 = (currVal19 - ath19) / ath19
dist20 = (currVal20 - ath20) / ath20
dist21 = (currVal21 - ath21) / ath21
dist22 = (currVal22 - ath22) / ath22
dist23 = (currVal23 - ath23) / ath23
dist24 = (currVal24 - ath24) / ath24
dist25 = (currVal25 - ath25) / ath25
dist26 = (currVal26 - ath26) / ath26
dist27 = (currVal27 - ath27) / ath27
dist28 = (currVal28 - ath28) / ath28
dist29 = (currVal29 - ath29) / ath29
dist30 = (currVal30 - ath30) / ath30
dist31 = (currVal31 - ath31) / ath31
dist32 = (currVal32 - ath32) / ath32
dist33 = (currVal33 - ath33) / ath33
dist34 = (currVal34 - ath34) / ath34
dist35 = (currVal35 - ath35) / ath35
dist36 = (currVal36 - ath36) / ath36
dist37 = (currVal37 - ath37) / ath37
dist38 = (currVal38 - ath38) / ath38
dist39 = (currVal39 - ath39) / ath39
dist40 = (currVal40 - ath40) / ath40

arrDist = array.new_float(0)
arrName = array.new_string(0)

if use01
    array.push(arrName, strip(t01))
    array.push(arrDist, dist01)
if use02
    array.push(arrName, strip(t02))
    array.push(arrDist, dist02)
if use03
    array.push(arrName, strip(t03))
    array.push(arrDist, dist03)
if use04
    array.push(arrName, strip(t04))
    array.push(arrDist, dist04)
if use05
    array.push(arrName, strip(t05))
    array.push(arrDist, dist05)
if use06
    array.push(arrName, strip(t06))
    array.push(arrDist, dist06)
if use07
    array.push(arrName, strip(t07))
    array.push(arrDist, dist07)
if use08
    array.push(arrName, strip(t08))
    array.push(arrDist, dist08)
if use09
    array.push(arrName, strip(t09))
    array.push(arrDist, dist09)
if use10
    array.push(arrName, strip(t10))
    array.push(arrDist, dist10)
if use11
    array.push(arrName, strip(t11))
    array.push(arrDist, dist11)
if use12
    array.push(arrName, strip(t12))
    array.push(arrDist, dist12)
if use13
    array.push(arrName, strip(t13))
    array.push(arrDist, dist13)
if use14
    array.push(arrName, strip(t14))
    array.push(arrDist, dist14)
if use15
    array.push(arrName, strip(t15))
    array.push(arrDist, dist15)
if use16
    array.push(arrName, strip(t16))
    array.push(arrDist, dist16)
if use17
    array.push(arrName, strip(t17))
    array.push(arrDist, dist17)
if use18
    array.push(arrName, strip(t18))
    array.push(arrDist, dist18)
if use19
    array.push(arrName, strip(t19))
    array.push(arrDist, dist19)
if use20
    array.push(arrName, strip(t20))
    array.push(arrDist, dist20)
if use21
    array.push(arrName, strip(t21))
    array.push(arrDist, dist21)
if use22
    array.push(arrName, strip(t22))
    array.push(arrDist, dist22)
if use23
    array.push(arrName, strip(t23))
    array.push(arrDist, dist23)
if use24
    array.push(arrName, strip(t24))
    array.push(arrDist, dist24)
if use25
    array.push(arrName, strip(t25))
    array.push(arrDist, dist25)
if use26
    array.push(arrName, strip(t26))
    array.push(arrDist, dist26)
if use27
    array.push(arrName, strip(t27))
    array.push(arrDist, dist27)
if use28
    array.push(arrName, strip(t28))
    array.push(arrDist, dist28)
if use29
    array.push(arrName, strip(t29))
    array.push(arrDist, dist29)
if use30
    array.push(arrName, strip(t30))
    array.push(arrDist, dist30)
if use31
    array.push(arrName, strip(t31))
    array.push(arrDist, dist31)
if use32
    array.push(arrName, strip(t32))
    array.push(arrDist, dist32)
if use33
    array.push(arrName, strip(t33))
    array.push(arrDist, dist33)
if use34
    array.push(arrName, strip(t34))
    array.push(arrDist, dist34)
if use35
    array.push(arrName, strip(t35))
    array.push(arrDist, dist35)
if use36
    array.push(arrName, strip(t36))
    array.push(arrDist, dist36)
if use37
    array.push(arrName, strip(t37))
    array.push(arrDist, dist37)
if use38
    array.push(arrName, strip(t38))
    array.push(arrDist, dist38)
if use39
    array.push(arrName, strip(t39))
    array.push(arrDist, dist39)
if use40
    array.push(arrName, strip(t40))
    array.push(arrDist, dist40)

// Function to swap elements in the arrName and arrDist arrays
swap(arrName, arrDist, i, j) =>
    tempDist = array.get(arrDist, i)
    tempName = array.get(arrName, i)
    array.set(arrDist, i, array.get(arrDist, j))
    array.set(arrName, i, array.get(arrName, j))
    array.set(arrDist, j, tempDist)
    array.set(arrName, j, tempName)

// Simple bubble sort to sort both arrays based on arrDist
for i = 0 to array.size(arrDist) - 2
    for j = 0 to array.size(arrDist) - i - 2
        if array.get(arrDist, j) < array.get(arrDist, j + 1)
            swap(arrName, arrDist, j, j + 1)

//Set heatmeap colors
bearClr = color.red
bullClr = color.green

getColor(dist)=>
    color.from_gradient(dist,-1,0,bearClr,bullClr) 

//PLOTS
//Set table text size
textSize = dashSize == "Small" ? size.small : dashSize == "Normal" ? size.normal : size.large

//Create Table
var table nameDisplay = table.new(position.top_right, 50, 50, bgcolor = color.new(color.white,100), frame_width = 1, frame_color = color.gray, border_width = 1, border_color = color.gray)
if barstate.islast
    table.cell(nameDisplay, 0, 0, "ATH DISTANCE HEATMAP", bgcolor = color.black, text_color = color.white, text_size = textSize)
    table.merge_cells(nameDisplay,0,0,dashColSize-1,0)
    rowVal = 1
    for i = 0 to array.size(arrDist) - 1
        distValue = array.get(arrDist, i)
        colorValue = getColor(distValue)
        assetName = array.get(arrName,i)
        colVal = i % dashColSize 
        if colVal == 0
            rowVal := rowVal + 1
        table.cell(nameDisplay, colVal, rowVal, assetName+"\n"+str.format("{0,number,percent}", distValue), bgcolor = colorValue, text_color = color.white, text_size = textSize)

Solution

  • First, change bar_index to time

    For a time input, use input.time

    and for debug, use bgcolor

    i_startTime = input.time(defval = timestamp("01 Jan 2024 01:00 +0000"), title = "Start Time")
    
    startIndex = time >= i_startTime
    bgcolor(startIndex ? color.new(color.gray, 90) : na)