Search code examples
pine-scriptpine-script-v4

Plotting value and char over the last bar


I'm stuck with the problem below.

What I would like to achieve:

  • Indicator which shows the diff. from 200 EMA in percentages. [solved]
  • Add a char (arrow pointing to the bar) over/under the last bar (depending on whether it is a positive or negative value)
  • Add a text over/under the last bar with the actual value of the bar

I tried with plotchar and I managed to add the arrows to all bar, but I failed to filter them to show only at the last bar (show_last=1 was not accepted as argument).

I tried to add "text=variable" but Pine accepts only literal string there. I tried tostring(value) but was not accepted.

My code so far which works fine:

//
study("Percent diff. from 200 day EMA", shorttitle="% From EMA", overlay=true, precision=2)

dif200 = (close - (ema(close,200))) * 100 / ema(close,200)

plot(dif200, "Diff. from 200", color=#2196f399, linewidth=2, title="From 200", style=columns)
plot(dif200, "Actual value", color=#2196f399, trackprice=true, linewidth=1, transp=10, offset=-9999)

hline(0, color=red, title="Zero line", linestyle=solid, linewidth=2)

What should be my next step?

Thank you for your kind help! Regards, Ivan


Solution

    1. Not possible to plot something on the chart pane if the script is on a separate pane. The single option is to use barcolor function.
    2. Not possible in pine v1. You should use the latest version of the pinescript //@version=4. To plot a dynamic value you have to use label.new + tostring functions as in the example below. To be able to plot the same dynamic label on the chart pane you have to create a separate script and load it on the chart.

    //@version=4
    study("Percent diff. from 200 day EMA", shorttitle="% From EMA", overlay=false, precision=2)
    
    dif200 = (close - (ema(close,200))) * 100 / ema(close,200)
    
    plot(dif200, "Diff. from 200", color=#2196f399, linewidth=2, title="From 200", style=plot.style_columns)
    plot(dif200, "Actual value", color=#2196f399, trackprice=true, linewidth=1, transp=10, offset=-9999)
    
    hline(0, color=color.red, title="Zero line", linestyle=hline.style_solid, linewidth=2)
    
    if barstate.islast
        label.new(bar_index, dif200, tostring(dif200), xloc.bar_index, yloc.price, #2196f399, label.style_label_left, color.white)