Bar Count for Backtesting
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KioseffTrading

//@version=5
indicator("Bar Count for Backtesting", overlay = true)

//  ________________________________________________
// |                                               |  
// |       ---------------------------------       | 
// |      | K̲ i̲ o̲ s̲ e̲ f̲ f̲     T̲ r̲ a̲ d̲ i̲ n̲ g |      | 
// |      |                                 |      |  
// |      | ƃ u ᴉ p ɐ ɹ ꓕ     ⅎ ⅎ ǝ s o ᴉ ꓘ |      |  
// |       --------------------------------        |  
// |                                               | 
// |_______________________________________________|


barType                                      = input.string(defval = "Time", options = ["Time", "Bars Back"], title = "Identify Bars Tested Using: ")
start                                        = input.time(defval = timestamp("May 02 2022"), title = "Test Start Date",group = "If Date & Time", 
 tooltip = "Clicking the Table on the Chart Causes Two Vertical Lines to Appear. One Line Appears at the Testing Start Date; One Line Appears at the Testing End Date. The Lines Can Be Dragged to Narrow/Widen the Start and End Date Without Making Changes in the User Input Tab. Use This Feature to Quickly Isolate Portions of the Data Set.")
end                                          = input.time(defval = timestamp("Jan 02 2023"), title = "Test End Date",group = "If Date & Time" ,    
 tooltip = "Clicking the Table on the Chart Causes Two Vertical Lines to Appear. One Line Appears at the Testing Start Date; One Line Appears at the Testing End Date. The Lines Can Be Dragged to Narrow/Widen the Start and End Date Without Making Changes in the User Input Tab. Use This Feature to Quickly Isolate Portions of the Data Set for Testing.")
barsBack                                     = input.int(defval = 0, title = "Bars Back", group = "If Bars Back", minval = 0, step = 50)
dataDisplay                                  = input.string(defval = "Table and Label", title = "Display Bars Tested: ", options = ["Label", "Table", "Table and Label"])

HH = ta.highest(high, 200)

var int   [] barTrack                        = array.new_int(2)
var float [] timeTrack                       = array.new_float(1)
var table [] tab                             = array.new_table()


date() => 
    start <= end ? time >= start and time <= end : 
     time <= start and time >= end



if barstate.isfirst
    array.set(timeTrack, 0, timestamp(year, month, dayofmonth, hour, minute, second))
    array.push(tab, table.new(position.bottom_right, 2, 2, bgcolor = color.new(color.white, 50), frame_color = #000000, border_color = #000000))
if barType == "Time"

    if start > array.get(timeTrack, 0) 
        if date()[1] == false and date() == true
            array.set(barTrack, 0, bar_index)
            line.new(bar_index, close, bar_index, close + 1, extend = extend.both, width = 2, color = color.white)
   
    else if start <= array.get(timeTrack, 0) 
        if barstate.isfirst
            array.set(barTrack, 0, bar_index)
            line.new(bar_index, close, bar_index, close + 1, extend = extend.both, width = 2, color = color.white)
   
    if end < timenow
        if date()[1] == true and date() == false
            array.set(barTrack, 1, bar_index)
            line.new(bar_index, close, bar_index, close + 1, extend = extend.both, width = 2, color = color.white)
   
    else 
        if barstate.islast
            array.set(barTrack, 1, bar_index)
            line.new(bar_index, close, bar_index, close + 1, extend = extend.both, width = 2, color = color.white)

    
    if dataDisplay != "Table"

        if bar_index == array.get(barTrack, 1)
            label.new(bar_index + 20, 
                             HH, 
                             text = str.tostring(array.get(barTrack, 1) >= array.get(barTrack, 0) ? 
                             array.get(barTrack, 1) - array.get(barTrack, 0) : 
                             array.get(barTrack, 0) - array.get(barTrack, 1), "##") + "\n Bars Tested", 
                             textcolor = #000000, 
                             color = color.new(color.white, 50), 
                             style = label.style_label_left
                             
                             )
    
    if dataDisplay != "Label"

        if bar_index == array.get(barTrack, 1)  
            table.cell(
                             array.get(tab, 0), 
                             0,
                             0,
                             text = str.tostring(array.get(barTrack, 1) >= array.get(barTrack, 0) ? 
                             array.get(barTrack, 1) - array.get(barTrack, 0) : 
                             array.get(barTrack, 0) - array.get(barTrack, 1), "##") + "\n Bars Tested", 
                             text_color = #000000

                             )
    
if barType == "Bars Back"
    
    if bar_index == last_bar_index - barsBack and barsBack <= last_bar_index or barsBack > last_bar_index and bar_index == 0
        
        if dataDisplay != "Table"
        
            label.new(barsBack <= last_bar_index ? bar_index - 20 : bar_index + 1, close , text = 
            
               str.tostring(year, "##")       + "/" 
             + str.tostring(month, "##")      + "/" 
             + str.tostring(dayofmonth, "##") + "\n" 
             + str.tostring(hour, "##")       + ":" 
             + str.tostring(minute, "##")     + ":" 
             + str.tostring(second, "##")     + "\n" 
             + str.tostring(barsBack <= last_bar_index ? barsBack : last_bar_index, "##") + 
             " Bars Tested",
             color = color.new(color.white, 50), 
             textcolor = #000000, 
             style = label.style_label_right
             
             )
    
        line.new(bar_index, close, bar_index, close + 1, color = color.white, width = 2, extend = extend.both)
        
        if dataDisplay != "Label"

            table.cell(
                             array.get(tab, 0), 
                             0,
                             0,
                             text = 
            
                               str.tostring(year, "##")       + "/" 
                             + str.tostring(month, "##")      + "/" 
                             + str.tostring(dayofmonth, "##") + "\n" 
                             + str.tostring(hour, "##")       + ":" 
                             + str.tostring(minute, "##")     + ":" 
                             + str.tostring(second, "##")     + "\n" 
                             + str.tostring(barsBack <= last_bar_index ? barsBack : last_bar_index, "##") + 
                             " Bars Tested", 
                             text_color = #000000

                             )

bgcolor(color = barType == "Time" and start <= end and time >= start and time <= end ? 
     color.new(color.white, 90) : 
      barType == "Time" and end < start and time >= end and time <= start ? 
     color.new(color.white, 90) : 
      barType == "Bars Back" and bar_index >= last_bar_index - barsBack ? 
      color.new(color.white, 90) : 
      na
      )
     


