pine-script-v5

Indicator keep on dissapearing


I wrote a Long Short Position Indicator, The indicator works 100%, but it keeps on dissapearing. If it dissapear I need to "Hide" en then "Unhide" it to get it back on my screen

I tried to change my If statements and even tried varip but without success.

My Code:

//@version=5
indicator("Long & Short Position", "L & S",overlay=true,precision = 4)

Price = input.price(defval = 0,confirm = true, title = "Entry Price")
sl    = input.price(defval = 0,confirm = true, title = "Stop Loss") 
LotSize    = input(defval = 1000.000000, title = "Lot Size") 
rr = input.float(title="Risk Reward Ratio", defval=1.5, minval = 1.00, step=0.10)
Close = close

//Take Profit Calculation
take_profit = (Price - sl)  * rr + Price // Take Profit Value @ 1.5 Risk Reward Ratio 
Buy  = take_profit > Price

//Pips Calculation for Realized Profit
pips = if Buy //take_profit > Price
    (take_profit - Price)  
else
    (Price - take_profit)

PL = (pips * LotSize) 

//Pips Calculations Unrealized Profit
Pips = if Buy // Price > close
    (close - Price)
else 
    (Price - close)

Un_PL = (Pips * LotSize) 

Un_PL_Col = if Un_PL > 0
    color.green
else
    color.red

//Drawing Profit Value on chart
tblPos = "Top Right" 
tblposition = tblPos == 'Top Left' ? position.top_left : tblPos == 'Top Right' ?                 position.top_right : tblPos == 'Bottom Left' ? position.bottom_left : tblPos == 'Bottom Right' ? position.bottom_right : tblPos == 'Middle Left' ? position.middle_left : position.middle_right

tblBorderColor = color.black 
celllBgColor = color.white 
cellTextColor = color.black 

var resultsTable = table.new(position=tblposition, columns=3, rows=3, bgcolor=#ffffff,     border_width=1, frame_color=tblBorderColor, frame_width=4)
table.clear(resultsTable, 0, 0)
table.cell(resultsTable, column=0, row=1, text='Potential P&L : ' + str.tostring(PL,"$ ###.##"), text_color=cellTextColor, text_halign=text.align_right, text_valign=text.align_center, bgcolor=celllBgColor)
table.cell(resultsTable, column=0, row=2, text='Unrealized P&L : ' + str.tostring(Un_PL,"$ ###.##"), text_color=Un_PL_Col, text_halign=text.align_right, text_valign=text.align_center, bgcolor=celllBgColor)

// Plot box on chart
a = box.new(bar_index[1]+ 6, ta.highest(sl, 1), bar_index[1]+ 12, ta.lowest(Price, 1), color.white, 1, line.style_solid, extend.none, xloc.bar_index, color.new(color.red, 60))
b = box.new(bar_index[1]+ 6, ta.highest(Price, 1), bar_index[1]+ 12, ta.lowest(take_profit, 1), color.white, 1, line.style_solid, extend.none, xloc.bar_index, color.new(color.lime, 60))
c = box.new(bar_index[2]+ 9, ta.highest(Price, 2), bar_index[1]+ 10, ta.lowest(Close, 1), color.white, 1, line.style_solid, extend.none, xloc.bar_index, Un_PL_Col)

box.delete(a[1])
box.delete(b[1])      
box.delete(c[1])      
    

Solution

  • I've solved the issue by making use of force_overlay = true

    a = box.new(bar_index[1]+ 6, ta.highest(sl, 1), bar_index[1]+ 12, ta.lowest(Price, 1), color.white, 1, line.style_solid, extend.none, xloc.bar_index, color.new(color.red, 60))
    b = box.new(bar_index[1]+ 6, ta.highest(Price, 1), bar_index[1]+ 12, ta.lowest(take_profit, 1), color.white, 1, line.style_solid, extend.none, xloc.bar_index, color.new(color.lime, 60))
    

    to

    a = box.new(bar_index[1]+ 6, ta.highest(sl, 1), bar_index[1]+ 12, ta.lowest(Price, 1),force_overlay = true, border_color = color.white, border_style = line.style_solid, bgcolor = color.new(color.red, 60))
    b = box.new(bar_index[1]+ 6, ta.highest(Price, 1), bar_index[1]+ 12, ta.lowest(take_profit, 1),force_overlay = true, border_color = color.white, border_style = line.style_solid, bgcolor = color.new(color.lime, 60))