Zendog Bar Percentage
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © zendog123

//@version=4
study("Zendog Bar Percentage", overlay=true, max_labels_count=500)

is_green = false
is_red = false
cfg_decimals = input(type=input.integer, title="Decimals", defval=1, step=1)


get_month_string(month_number)=>
    if month_number == 1
        _string = "Jan"
    else if month_number == 2
        _string = "Feb"
    else if month_number == 3
        _string = "Mar"
    else if month_number == 4
        _string = "Apr"
    else if month_number == 5
        _string = "May"
    else if month_number == 6
        _string = "Jun"
    else if month_number == 7
        _string = "Jul"
    else if month_number == 8
        _string = "Aug"
    else if month_number == 9
        _string = "Sep"
    else if month_number == 10
        _string = "Oct"
    else if month_number == 11
        _string = "Nov"
    else if month_number == 12
        _string = "Dec"


time_to_date_string(timeinms)=>
    if timeinms > 0
        _string = tostring(dayofmonth(timeinms), "00/") + get_month_string(month(timeinms)) +"/"+ tostring(year(timeinms), "0000") +
          " " + tostring(hour(timeinms), "00:") + tostring(minute(timeinms), "00:") + tostring(second(timeinms), "00")
    else
        _string = ""


//strategy timeframe limitation (run just between specific dates)
bool limit_date_range = input(title="Limit Date Range", type=input.bool, defval=false, group="Date range")

//Input options that configure backtest date range
start_time = input(defval = timestamp("01 Aug 2021 00:00 +0000"), title = "Start Time",
  type = input.time, group="Backtest date range")
end_time = input(defval = timestamp("31 Dec 2021 00:00 +0000"), title = "End Time",
  type = input.time, group="Backtest date range")

bool in_date_range = false
if limit_date_range
    in_date_range := time >= start_time and time <= end_time
else
    in_date_range := true


var float max_percentage_open_close_green       = 0
var float max_percentage_high_low_green         = 0
var string max_percentage_open_close_green_date = ""
var string max_percentage_high_low_green_date   = ""

var float max_absolute_open_close_green         = 0
var float max_absolute_high_low_green           = 0
var string max_absolute_open_close_green_date   = ""
var string max_absolute_high_low_green_date     = ""

var float max_percentage_open_close_red         = 0
var float max_percentage_high_low_red           = 0
var string max_percentage_open_close_red_date   = ""
var string max_percentage_high_low_red_date     = ""

var float max_absolute_open_close_red           = 0
var float max_absolute_high_low_red             = 0
var string max_absolute_open_close_red_date     = ""
var string max_absolute_high_low_red_date       = ""


if in_date_range
    //green
    if close >= open
        percentage_high_low     = round(((high*100/low)-100), cfg_decimals)
        percentage_open_close   = round(((close*100/open)-100), cfg_decimals)
        
        absolute_high_low       = high - low
        absolute_open_close     = close - open
        
        if max_percentage_open_close_green < percentage_open_close
            max_percentage_open_close_green         := percentage_open_close
            max_percentage_open_close_green_date  := time_to_date_string(time)
        
        if max_percentage_high_low_green < percentage_high_low
            max_percentage_high_low_green           := percentage_high_low
            max_percentage_high_low_green_date      := time_to_date_string(time)
        
        
        if max_absolute_open_close_green < absolute_open_close
            max_absolute_open_close_green         := absolute_open_close
            max_absolute_open_close_green_date    := time_to_date_string(time)
        
        if max_absolute_high_low_green < absolute_high_low
            max_absolute_high_low_green           := absolute_high_low
            max_absolute_high_low_green_date      := time_to_date_string(time)
        
        
        label.new(bar_index, high, text=tostring(percentage_high_low)+"\n"+tostring(percentage_open_close), 
          yloc=yloc.abovebar, size=size.small, style=label.style_none, textcolor=color.green)
    //red
    else 
        percentage_high_low = round(((low*100/high)-100), cfg_decimals)
        percentage_open_close = round(((close*100/open)-100), cfg_decimals)
        
        absolute_high_low       = low - high
        absolute_open_close     = close - open
        
        if max_percentage_open_close_red > percentage_open_close
            max_percentage_open_close_red           := percentage_open_close
            max_percentage_open_close_red_date      := time_to_date_string(time)
        
        if max_percentage_high_low_red > percentage_high_low
            max_percentage_high_low_red             := percentage_high_low
            max_percentage_high_low_red_date        := time_to_date_string(time)
        
        if max_absolute_open_close_red > absolute_open_close
            max_absolute_open_close_red         := absolute_open_close
            max_absolute_open_close_red_date    := time_to_date_string(time)
        
        if max_absolute_high_low_red > absolute_high_low
            max_absolute_high_low_red           := absolute_high_low
            max_absolute_high_low_red_date      := time_to_date_string(time)
        
        label.new(bar_index, low, text=""+tostring(percentage_high_low)+"\n"+tostring(percentage_open_close), 
          yloc=yloc.belowbar, size=size.small, style=label.style_none, textcolor=color.red)
        
        

if barstate.islastconfirmedhistory
    table stats_table = table.new(position.top_right, columns=2, rows=10,
          frame_width=1, frame_color=color.black)
    _row = 0
    table.cell(stats_table, column=0, row=0,
      text="Green Candles", text_halign=text.align_left, text_size=size.normal, text_color=color.black, bgcolor=color.green)
    table.cell(stats_table, column=1, row=0,
      text="", bgcolor=color.green)
       
    table.cell(stats_table, column=0, row=1,
          text="Max % Open/Close", text_halign=text.align_left, text_valign=text.align_top, text_color=color.black, text_size=size.normal, bgcolor=color.green)
    table.cell(stats_table, column=1, row=1,
          text=tostring(max_percentage_open_close_green)+"%\n( "+tostring(max_percentage_open_close_green_date)+" )", text_halign=text.align_left, text_color=color.black, text_size=size.normal, bgcolor=color.green)
          
    table.cell(stats_table, column=0, row=2,
          text="Max "+tostring(syminfo.currency)+" Open/Close", text_halign=text.align_left, text_valign=text.align_top, text_color=color.black, text_size=size.normal, bgcolor=color.green)
    table.cell(stats_table, column=1, row=2,
          text=tostring(max_absolute_open_close_green)+" "+tostring(syminfo.currency)+"\n( "+tostring(max_absolute_open_close_green_date)+" )", text_halign=text.align_left, text_color=color.black, text_size=size.normal, bgcolor=color.green)          

    table.cell(stats_table, column=0, row=3,
          text="Max % Low/High", text_halign=text.align_left, text_valign=text.align_top, text_color=color.black, text_size=size.normal, bgcolor=color.green)
    table.cell(stats_table, column=1, row=3,
          text=tostring(max_percentage_high_low_green)+"%\n( "+tostring(max_percentage_high_low_green_date)+" )", text_halign=text.align_left, text_color=color.black, text_size=size.normal, bgcolor=color.green)
    
    table.cell(stats_table, column=0, row=4,
          text="Max "+tostring(syminfo.currency)+" Low/High", text_halign=text.align_left, text_valign=text.align_top, text_color=color.black, text_size=size.normal, bgcolor=color.green)
    table.cell(stats_table, column=1, row=4,
          text=tostring(max_absolute_high_low_green)+" "+tostring(syminfo.currency)+"\n( "+tostring(max_absolute_high_low_green_date)+" )", text_halign=text.align_left, text_color=color.black, text_size=size.normal, bgcolor=color.green)
    
          
    table.cell(stats_table, column=0, row=5,
      text="Red Candles", text_halign=text.align_left, text_size=size.normal, text_color=color.black, bgcolor=#FF9494)
    table.cell(stats_table, column=1, row=5, text="", bgcolor=#FF9494)
       
    table.cell(stats_table, column=0, row=6,
          text="Max % Open/Close", text_halign=text.align_left, text_valign=text.align_top, text_color=color.black, text_size=size.normal, bgcolor=#FF9494)
    table.cell(stats_table, column=1, row=6,
          text=tostring(max_percentage_open_close_red)+"%\n( "+tostring(max_percentage_open_close_red_date)+" )", text_halign=text.align_left, text_color=color.black, text_size=size.normal, bgcolor=#FF9494)
          
    table.cell(stats_table, column=0, row=7,
          text="Max "+tostring(syminfo.currency)+" Open/Close", text_halign=text.align_left, text_valign=text.align_top, text_color=color.black, text_size=size.normal, bgcolor=#FF9494)
    table.cell(stats_table, column=1, row=7,
          text=tostring(max_absolute_open_close_red)+" "+tostring(syminfo.currency)+"\n( "+tostring(max_absolute_open_close_red_date)+" )", text_halign=text.align_left, text_color=color.black, text_size=size.normal, bgcolor=#FF9494)

    table.cell(stats_table, column=0, row=8,
          text="Max % High/Low", text_halign=text.align_left, text_valign=text.align_top, text_color=color.black, text_size=size.normal, bgcolor=#FF9494)
    table.cell(stats_table, column=1, row=8,
          text=tostring(max_percentage_high_low_red)+"%\n( "+tostring(max_percentage_open_close_red_date)+" )", text_halign=text.align_left, text_color=color.black, text_size=size.normal, bgcolor=#FF9494)

    table.cell(stats_table, column=0, row=9,
          text="Max "+tostring(syminfo.currency)+" High/Low", text_halign=text.align_left, text_valign=text.align_top, text_color=color.black, text_size=size.normal, bgcolor=#FF9494)
    table.cell(stats_table, column=1, row=9,
          text=tostring(max_absolute_high_low_red)+" "+tostring(syminfo.currency)+"\n( "+tostring(max_absolute_high_low_red_date)+" )", text_halign=text.align_left, text_color=color.black, text_size=size.normal, bgcolor=#FF9494)
    
