Highest and Low %
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Naruephat

//@version=5
indicator("Highest and Low %", overlay=true)
//input
len_lowes_ = input.int(25,'len lowest',step= 25)
size_is =  input.string('auto','size table right', options = ['auto','tiny','small','normal'] , inline = 'Lnets') 

size_s = size_is == 'auto' ? size.auto :
          size_is == 'tiny' ?  size.tiny :    
           size_is == 'small' ? size.small : 
             size_is == 'small' ? size.normal :  size.auto

toDate          = time 
date_start_entry    =  time
var begin           = date_start_entry
days                = (toDate - begin) / (24 * 60 * 60 * 1000) 


//  find the value high 
var h = 0.
if h < high
    h := high
//find the value low
var l = 999999999999999999.0
if l > low
    l := low

//latest low
var lowest = 0.
lowest := ta.lowest(low,len_lowes_)

//calcul percentage
price = ((close -close[1]  ) /close[1] ) * 100

lowes = label.new(bar_index, high, text= 'lowest: '+str.tostring(lowest,'#,###.##') +"$|"+ str.tostring(lowest*34,'#,###.##') + 'บ.\n Price Last: ' +str.tostring(close,'#,###.##') +"$|"+ str.tostring(close*34,'#,###.##') + 'บ.\n(' +str.tostring(price,'#.##'  )+')',yloc = yloc.abovebar ,style=label.style_none ,textcolor = close > close[1] ? color.green :color.white , size =size.normal )
label.delete(lowes[1])


plot( h, color = color.new(color.aqua,0) , title = '' )
plot( l, color = color.new(color.aqua,0) , title = '' )
plot( lowest , color = color.new(color.orange,0) , title = '' )
mess1s = str.tostring(days, "#,### days |") + str.tostring(days/30, "#,### month  | ") + str.tostring(days/365, "#,###") +' year'
mess1 =  'Highest : ' +str.tostring((h - l) / l * 100 ,'#,###.##') + ' %'
mess1_2 = 'Low last : '+str.tostring((lowest - h) / h * 100   ,'#,###.##') + ' %'

//table
table table_countbar = table.new(position.top_right, 1,50 ,border_color = color.orange)
table.cell(table_countbar,0, 0, mess1s,text_size = size_s , bgcolor =color.new(color.blue,80),text_color =color.new(color.white,0) ) 
table.cell(table_countbar,0, 1, mess1,text_size = size_s , bgcolor =color.new(color.green,80),text_color =color.new(color.white,0) ) 
table.cell(table_countbar,0, 2, mess1_2,text_size = size_s , bgcolor =color.new(color.red,80),text_color =color.new(color.white,0) ) 
