Candle Counter [theEccentricTrader]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © theEccentricTrader
//@version=5


indicator('Candle Counter [theEccentricTrader]', overlay = true)


//////////// sample period filter ////////////


showSamplePeriod = input(defval = true, title = 'Show Sample Period', group = 'Sample Period')

start = input.time(timestamp('2 Jan 1800 00:00'), title = 'Start Date', group = 'Sample Period')
end = input.time(timestamp('1 Jan 3000 00:00'), title = 'End Date', group = 'Sample Period')
samplePeriodFilter = time >= start and time <= end

var barOneDate = time

f_timeToString(_t) =>
    str.tostring(dayofmonth(_t), '00') + '.' + str.tostring(month(_t), '00') + '.' + str.tostring(year(_t), '0000')

startDateText = barOneDate > start ? f_timeToString(barOneDate) : f_timeToString(start)
endDateText = time >= end ? '-' + f_timeToString(end) : '-' + f_timeToString(time)


//////////// candle counters ////////////


candle = close and samplePeriodFilter and barstate.isconfirmed 
higherHigh = high > high[1] and samplePeriodFilter and barstate.isconfirmed
higherLow = low > low[1] and samplePeriodFilter and barstate.isconfirmed
lowerHigh = high < high[1] and samplePeriodFilter and barstate.isconfirmed
lowerLow = low < low[1] and samplePeriodFilter and barstate.isconfirmed
doubleTop = high == high[1] and samplePeriodFilter and barstate.isconfirmed
doubleBottom = low == low[1] and samplePeriodFilter and barstate.isconfirmed

greenCandle = close >= open and samplePeriodFilter and barstate.isconfirmed
higherHighGreen = high > high[1] and close >= open and samplePeriodFilter and barstate.isconfirmed
higherLowGreen = low > low[1] and close >= open and samplePeriodFilter and barstate.isconfirmed
lowerHighGreen = high < high[1] and close >= open and samplePeriodFilter and barstate.isconfirmed
lowerLowGreen = low < low[1] and close >= open and samplePeriodFilter and barstate.isconfirmed
doubleTopGreen = high == high[1] and close >= open and samplePeriodFilter and barstate.isconfirmed
doubleBottomGreen = low == low[1] and close >= open and samplePeriodFilter and barstate.isconfirmed

redCandle = close < open and samplePeriodFilter and barstate.isconfirmed
higherHighRed = high > high[1] and close < open and samplePeriodFilter and barstate.isconfirmed
higherLowRed = low > low[1] and close < open and samplePeriodFilter and barstate.isconfirmed
lowerHighRed = high < high[1] and close < open and samplePeriodFilter and barstate.isconfirmed
lowerLowRed = low < low[1] and close < open and samplePeriodFilter and barstate.isconfirmed
doubleTopRed = high == high[1] and close < open and samplePeriodFilter and barstate.isconfirmed
doubleBottomRed = low == low[1] and close < open and samplePeriodFilter and barstate.isconfirmed

var candleCounter = 0
var higherHighCounter = 0
var higherLowCounter = 0
var lowerHighCounter = 0
var lowerLowCounter = 0
var doubleTopCounter = 0
var doubleBottomCounter = 0

var greenCounter = 0
var higherHighGreenCounter = 0
var higherLowGreenCounter = 0
var lowerHighGreenCounter = 0
var lowerLowGreenCounter = 0
var doubleTopGreenCounter = 0
var doubleBottomGreenCounter = 0

var redCounter = 0
var higherHighRedCounter = 0
var higherLowRedCounter = 0
var lowerHighRedCounter = 0
var lowerLowRedCounter = 0
var doubleTopRedCounter = 0
var doubleBottomRedCounter = 0

if candle
    candleCounter += 1
if higherHigh 
    higherHighCounter += 1
if higherLow 
    higherLowCounter += 1    
if lowerHigh 
    lowerHighCounter += 1
if lowerLow 
    lowerLowCounter += 1
if doubleTop 
    doubleTopCounter += 1
if doubleBottom 
    doubleBottomCounter += 1

if greenCandle 
    greenCounter += 1
if higherHighGreen 
    higherHighGreenCounter += 1
if higherLowGreen 
    higherLowGreenCounter += 1   
if lowerHighGreen 
    lowerHighGreenCounter += 1
if lowerLowGreen 
    lowerLowGreenCounter += 1
if doubleTopGreen 
    doubleTopGreenCounter += 1   
if doubleBottomGreen 
    doubleBottomGreenCounter += 1
    
if redCandle 
    redCounter += 1
if higherHighRed 
    higherHighRedCounter += 1
if higherLowRed 
    higherLowRedCounter += 1   
if lowerHighRed 
    lowerHighRedCounter += 1
if lowerLowRed 
    lowerLowRedCounter += 1
if doubleTopRed 
    doubleTopRedCounter += 1  
if doubleBottomRed 
    doubleBottomRedCounter += 1
    

//////////// table //////////// 


candleTablePositionInput = input.string(title = 'Position', defval = 'Top Right', options = ['Top Right', 'Top Center', 'Top Left', 'Bottom Right', 'Bottom Center', 'Bottom Left', 
 'Middle Right', 'Middle Center', 'Middle Left'], group = 'Table Positioning')
candleTablePosition = candleTablePositionInput == 'Top Right' ? position.top_right : candleTablePositionInput == 'Top Center' ? position.top_center : 
 candleTablePositionInput == 'Top Left' ? position.top_left : candleTablePositionInput == 'Bottom Right' ? position.bottom_right : 
 candleTablePositionInput == 'Bottom Center' ? position.bottom_center : candleTablePositionInput == 'Bottom Left' ? position.bottom_left : 
 candleTablePositionInput == 'Middle Right' ? position.middle_right : candleTablePositionInput == 'Middle Center' ? position.middle_center : 
 candleTablePositionInput == 'Middle Left' ? position.middle_left : na

textSizeInput = input.string(title = 'Text Size', defval = 'Normal', options = ['Tiny', 'Small', 'Normal', 'Large'], group = 'Table Text Sizing')
textSize = textSizeInput == 'Tiny' ? size.tiny : textSizeInput == 'Small' ? size.small : textSizeInput == 'Normal' ? size.normal : textSizeInput == 'Large' ? size.large : na

var candleTable = table.new(candleTablePosition, 100, 100, border_width = 1)

table.cell(candleTable, 0, 1, text = 'Candles', bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 2, text = 'Green Candles', bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 3, text = 'Red Candles', bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 4, text = 'Higher High Candles', bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 5, text = 'Higher Low Candles', bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 6, text = 'Lower High Candles', bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 7, text = 'Lower Low Candles', bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 8, text = 'Double Top Candles', bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 9, text = 'Double Bottom Candles', bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 10, text = 'Higher High Green Candles', bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 11, text = 'Higher Low Green Candles', bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 12, text = 'Lower High Green Candles', bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 13, text = 'Lower Low Green Candles', bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 14, text = 'Double Top Green Candles', bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 15, text = 'Double Bottom Green Candles', bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 16, text = 'Higher High Red Candles', bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 17, text = 'Higher Low Red Candles', bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 18, text = 'Lower High Red Candles', bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 19, text = 'Lower Low Red Candles', bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 20, text = 'Double Top Red Candles', bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 0, 21, text = 'Double Bottom Red Candles', bgcolor = color.red, text_color = color.white, text_size = textSize)

table.cell(candleTable, 1, 1, text = str.tostring(candleCounter), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 2, text = str.tostring(greenCounter), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 3, text = str.tostring(redCounter), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 4, text = str.tostring(higherHighCounter), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 5, text = str.tostring(higherLowCounter), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 6, text = str.tostring(lowerHighCounter), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 7, text = str.tostring(lowerLowCounter), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 8, text = str.tostring(doubleTopCounter), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 9, text = str.tostring(doubleBottomCounter), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 10, text = str.tostring(higherHighGreenCounter), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 11, text = str.tostring(higherLowGreenCounter), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 12, text = str.tostring(lowerHighGreenCounter), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 13, text = str.tostring(lowerLowGreenCounter), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 14, text = str.tostring(doubleTopGreenCounter), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 15, text = str.tostring(doubleBottomGreenCounter), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 16, text = str.tostring(higherHighRedCounter), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 17, text = str.tostring(higherLowRedCounter), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 18, text = str.tostring(lowerHighRedCounter), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 19, text = str.tostring(lowerLowRedCounter), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 20, text = str.tostring(doubleTopRedCounter), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 1, 21, text = str.tostring(doubleBottomRedCounter), bgcolor = color.red, text_color = color.white, text_size = textSize)

table.cell(candleTable, 2, 1, text = '%', bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 2, text = str.tostring(math.round(greenCounter / candleCounter * 100, 2)), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 3, text = str.tostring(math.round(redCounter / candleCounter * 100, 2)), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 4, text = str.tostring(math.round(higherHighCounter / candleCounter * 100, 2)), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 5, text = str.tostring(math.round(higherLowCounter / candleCounter * 100, 2)), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 6, text = str.tostring(math.round(lowerHighCounter / candleCounter * 100, 2)), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 7, text = str.tostring(math.round(lowerLowCounter / candleCounter * 100, 2)), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 8, text = str.tostring(math.round(doubleTopCounter / candleCounter * 100, 2)), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 9, text = str.tostring(math.round(doubleBottomCounter / candleCounter * 100, 2)), bgcolor = color.blue, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 10, text = str.tostring(math.round(higherHighGreenCounter / greenCounter * 100, 2)), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 11, text = str.tostring(math.round(higherLowGreenCounter / greenCounter * 100, 2)), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 12, text = str.tostring(math.round(lowerHighGreenCounter / greenCounter * 100, 2)), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 13, text = str.tostring(math.round(lowerLowGreenCounter / greenCounter * 100, 2)), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 14, text = str.tostring(math.round(doubleTopGreenCounter / greenCounter * 100, 2)), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 15, text = str.tostring(math.round(doubleBottomGreenCounter / greenCounter * 100, 2)), bgcolor = color.green, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 16, text = str.tostring(math.round(higherHighRedCounter / redCounter * 100, 2)), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 17, text = str.tostring(math.round(higherLowRedCounter / redCounter * 100, 2)), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 18, text = str.tostring(math.round(lowerHighRedCounter / redCounter * 100, 2)), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 19, text = str.tostring(math.round(lowerLowRedCounter / redCounter * 100, 2)), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 20, text = str.tostring(math.round(doubleTopRedCounter / redCounter * 100, 2)), bgcolor = color.red, text_color = color.white, text_size = textSize)
table.cell(candleTable, 2, 21, text = str.tostring(math.round(doubleBottomRedCounter / redCounter * 100, 2)), bgcolor = color.red, text_color = color.white, text_size = textSize)

if showSamplePeriod 
    table.cell(candleTable, 0, 22, text = startDateText + endDateText, bgcolor = color.black, text_color = color.white, text_size = textSize)


//////////// plots ////////////


showPlots = input(defval = true, title = "Show Plots", group = 'Plots')

plotshape(high > high[1] and close >= open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.triangleup, color = color.green)
plotshape(low > low[1] and close >= open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.triangleup, color = color.green, location = location.belowbar)
plotshape(high < high[1] and close >= open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.triangledown, color = color.red)
plotshape(low < low[1] and close >= open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.triangledown, color = color.red, location = location.belowbar)
plotshape(high == high[1] and close >= open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.diamond, color = color.blue, textcolor = color.blue)
plotshape(low == low[1] and close >= open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.diamond, color = color.blue, textcolor = color.blue, location = location.belowbar)

plotshape(high > high[1] and close < open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.triangleup, color = color.green)
plotshape(low > low[1] and close < open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.triangleup, color = color.green, location = location.belowbar)
plotshape(high < high[1] and close < open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.triangledown, color = color.red)
plotshape(low < low[1] and close < open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.triangledown, color = color.red, location = location.belowbar)
plotshape(high == high[1] and close < open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.diamond, color = color.blue)
plotshape(low == low[1] and close < open and samplePeriodFilter and barstate.isconfirmed and showPlots, style = shape.diamond, color = color.blue, location = location.belowbar)



