Down since Initial Price
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © lirre8
//
// =========================================
//
// Don't hesitate to send me a message if you have questions, noticed any bugs or have suggestions on improvements.
//
// =========================================

//@version=5
indicator(title="Down since Initial Price", shorttitle="DIP", overlay=true)

DisplayCurrent = input(true, "Display from current price")
DisplayLowest = input(true, "Display from lowest price")
LabelColor = input(color.blue, "Label color")
TextColor = input(color.black, "Text color")

open_price = 0.0
open_price := barstate.isfirst ? open : open_price[1]

low_price = 0.0
low_price := barstate.isfirst ? low
           : low < low_price[1] ? low
           : low_price[1]

low_index = 0
low_index := barstate.isfirst ? 0
           : low < low_price[1] ? bar_index
           : low_index[1]

if DisplayLowest and barstate.islast and low_price < open_price
    txt = str.format("{0,number,percent} since initial price", (low_price - open_price) / open_price)
    label.new(low_index, 0, yloc=yloc.belowbar, text=txt, style=label.style_label_up, color=LabelColor, textcolor=TextColor)
    
if DisplayCurrent and barstate.islast
    txt = str.format("{0,number,percent} since initial price", (close - open_price) / open_price)
    label.new(bar_index+1, close, text=txt, style=label.style_label_left)
