Search code examples
pine-scriptpine-script-v5trading

PineScript to draw box only on current candle


I want to draw a box with the following details:

left = current candle - 5
right = current candle
top = high for the selected 5 candles
bottom = low for the selected 5 candles

I wrote the following code:

highestClose = ta.highest(close, 5)
lowestClose = ta.lowest(close, 5)
box.new(bar_index-5, highestClose, bar_index, lowestClose, border_color=color.green, border_width=1,bgcolor=na)

But the code makes boxes for every candle

Image for reference - https://i.imgur.com/F27c5ky.png

I just want the box on the last 5 candles. Also, is there any way to fill the box with some transparency?


Solution

  • You can assign the box created to a variable, which will save it as a series, and on each bar delete the previous box created:

    highestClose = ta.highest(close, 5)
    lowestClose = ta.lowest(close, 5)
    myBox = box.new(bar_index-5, highestClose, bar_index, lowestClose, border_color=color.green, border_width=1,bgcolor=na)
    box.delete(myBox[1])