Inside Day Probability
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © omararturo

//@version=5
indicator("Inside Day Probability", overlay = true)

// We use `var` to only initialize the table on the first bar.
var table dashboard = table.new(position.top_right, 3, 3, bgcolor = color.white, frame_color = color.gray, border_width = 1, border_color = color.gray)

var IDFCount = 0
var IDFBullishCount = 0
var IDFBullishSuccessCount = 0
var IDFBearishCount = 0
var IDFBearishSuccessCount = 0

IDFBullishSuccess = false
IDFBearishSuccess = false

insideDay = (high[3] > high[2]) and (low[3] < low[2])
bullish = (low[2] > low[1] and high[2] > high[1]) and (low[2] < close[1] and high[2] > close[1]) and (high[2] > open[1] and low[2] < open[1])
if (insideDay and bullish)
    IDFBullishCount += 1
    if (low[1] <= low and high[1] <= high)
        IDFBullishSuccess := true
        IDFBullishSuccessCount += 1        

barcolor(IDFBullishSuccess ? color.lime : na, 0)

bearish = (high[2] < high[1] and low[2] < low[1]) and (high[2] > close[1] and low[2] < close[1]) and (high[2] > open[1] and low[2] < open[1])
if (insideDay and bearish)
    IDFBearishCount += 1
    if (high[1] >= high and low[1] >= low)
        IDFBearishSuccess := true
        IDFBearishSuccessCount += 1

barcolor(IDFBearishSuccess ? color.white : na, 0)

IDF = (insideDay and (bullish or bearish))
if (IDF)
    IDFCount += 1
    
barcolor(IDF ? color.yellow : na, -2)

if barstate.islast
    // We only populate the table on the last bar.
    table.cell(dashboard, 0, 0, "Inside Day")
    table.cell(dashboard, 1, 0, str.tostring(IDFCount))
    table.cell(dashboard, 2, 0, "Success %")

    table.cell(dashboard, 0, 1, "Inside Day Bullish")
    table.cell(dashboard, 1, 1, str.tostring(IDFBullishCount))
    table.cell(dashboard, 2, 1, str.tostring(IDFBullishSuccessCount * 100 / IDFBullishCount))

    table.cell(dashboard, 0, 2, "Inside Day Bearish")
    table.cell(dashboard, 1, 2, str.tostring(IDFBearishCount))
    table.cell(dashboard, 2, 2, str.tostring(IDFBearishSuccessCount * 100 / IDFBearishCount))
