Kakashi Hatake
Kakashi Hatake

Reputation: 15

how to use plot() when value to be plotted is in the local block of if() in pinescript tradingview

I am a beginner in coding world and i am learning pine script.

I am having trouble plotting a variable which in the if() function and not in the global scope of the script.

I am getting this compiling error

line 73: Undeclared identifier 'tbf'

how do I plot "tbf"?

i have blockquoted the tbf variable line near the end

here is the script

//@version=5
indicator("My script")
// getting user inputs -----{

//Input Volume ("D" from Levine’s formula)
Vol_D     = input.int(title = "Vol_D", defval = 1, tooltip = "input Vol_D that is total volume")
//Bar Price to use; suggest "Low" for TopFinder and "High" for BottomFinder
TBF_Price = input.source(title = "Price type", defval = low)
//Bar Volume information to use; suggest "volume", or "1" if no volume information
MyVolume  = na(volume) ? 1 : volume
//StartBar is where the topfinder or bottomefinder will start from
StartBar  = input.int(title = 'Start Bar', minval = 1, maxval = 100000, defval = 10000)

//}----- end of user inputs

// setting up a start point -----{

start = bar_index == StartBar - 1 
    
//}----- end of setting up a start point

// assiging variables for calcualtion  -----{

//add current bar’s price * volume to cumulative total
pv = 0.0
pv := pv + TBF_Price * MyVolume
//running total of volume begin calculation of TBF price
vol = 0.0
vol := vol + MyVolume
//}------ end assiging variables for calcualtion

// calcualtion begins -----{

//begin calculation of tfbf
if (Vol_D  != 0)
    //store copy of input volume
    _D = Vol_D 
    //calculate "e" per Levine’s formula 
    e = vol * (1 - vol / _D) 
    //if "e" greater than zero, continue to calculate TBF price otherwise, TBF is completed 
    if (e > 0)
        //temporary copy of "e"
        eT = e
        //used for iteration iterate backwards until the cumulative displaced volume is greater than or equal to "e"
        j = -1
            
        while (eT > 0)
            j = j + 1
            eT = eT - MyVolume[j]
            
            
        //If displaced volume is greater than "e" (nearly always), an interpolated pv amount is calculated for "j" bars ago using only that part of
        //"j" bar’s volume needed to make cumulative displaced volume equal to "e". Note that at this point, "eT" is negative and contains the partial volume
        //of "j" bars ago that should be excluded.
        if (eT < 0) 
            pvInt = TBF_Price[j] * (MyVolume[j] + eT) 
        else 
            pvInt = 0
            
            //calculate TBF curve price for this bar
           

>           tbf = (pv - pv[j] + pvInt) / e

            //calculate percent TBF completion
            pct_D = vol / _D * 100
            
     
//}------ end of calculation

// plotting tbf line -----{

//plotting tbf
plot(tbf == 0 ? na : tbf, style=plot.style_line, linewidth=2)

//}----- end of plotting

Upvotes: 0

Views: 540

Answers (1)

Dave
Dave

Reputation: 865

The error says "tbf" is not declared, which is true based on the code Where can I see it?

Upvotes: 1

Related Questions