Tradingview Status Box

Tradingview’s proprietary Pine script has clearly its limitation, but in script version 4 Tradingview has added a couple of useful features.

One of them is the option to add labels to the chart, which can used to render a status box, which might show important position parameters. The code below shows a simple example for how to add a status box to the last bar, which shows current position (long, short, flat), the current profit and the current price. It renders the box in green if profit is positive, red if negative and blue if neutral.

The status box can also be seen in action here on Tradingview.

posColor = color.new(color.green, 75)
negColor = color.new(color.red, 75)
dftColor = color.new(color.blue, 75)
posProfit= (strategy.position_size != 0) ? (close * 100 / strategy.position_avg_price - 100) : 0.0
posDir   = (strategy.position_size  > 0) ? "long" : strategy.position_size < 0 ? "short" : "flat"
posCol   = (posProfit > 0) ? posColor : (posProfit < 0) ? negColor : dftColor

var label lb = na
label.delete(lb)
lb := label.new(bar_index, max(high, highest(5)[1]),
   color=posCol,
   text="Pos: "+ posDir +
      "\nPnL: "+tostring(posProfit, "#.##")+"%" +
      "\nClose: "+tostring(close, "#.##"))
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *