Search code examples
pine-scriptpine-script-v4

Pine script: Cannot get the upper (nor the lower) value of the Ichimoku leading lines


looking to find what am I doing wrong. The plots that i get above the candles are not the values of the upper leading line ( i tried it with the lower --> same results) here is my code:

//@version=4     
study(title="Ich1", overlay=true )
var upper_val = 0.1
var lower_val = 1.0

// ichimoku calculations 

conversionPeriods = input(9, minval=1, title="Conversion Line Periods"), 
basePeriods = input(26, minval=1, title="Base Line Periods") 
laggingSpan2Periods` = input(52, minval=1, title="Lagging Span 2 Periods"), 
displacement = input(26, minval=1, title="Displacement")

donchian(len) => avg(lowest(len), highest(len))

conversionLine = donchian(conversionPeriods) 
baseLine = donchian(basePeriods) 
leadLine1 = avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)

// plot    
plot(conversionLine,color=color.orange,linewidth=4,title="Conversion Line") 
plot(baseLine, color=color.purple , linewidth=3, title="Base Line") 
plot(close, offset = -displacement + 1, color=#459915, title="Lagging Span") 
p1=plot(leadLine1,offset=displacement - 1,color=color.green,  title="Lead 1") 
p2=plot(leadLine2,offset=displacement - 1,color=color.red, title="Lead 2")

// end plot ichimoku

 
// rules 
if (leadLine1 >= leadLine2)
    upper_val := leadLine1
    lower_val := leadLine2 
else
    upper_val := leadLine2
    lower_val := leadLine1

target = tostring(upper_val) // can be replaced with: 
                            // target = tostring(upper_val[0])

//print the upper_val

label.new(bar_index, high , yloc = yloc.abovebar, textcolor = color.red, style = label.style_none,
           text = target)

Solution

  • Offset of your upper_line is equal to displacement - 1

    So when you call the string of the value on the current bar it is showing the value without the offset.

    change line 38 of your code to show actual value with the offset:

    target = tostring(upper_val[displacement - 1])