WpProbabilisticLib
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Wpkenpachi

//@version=5

// @description Library that contains functions to calculate probabilistic based on historical candle analysis
library("WpProbabilisticLib")

int g = 1
int r = -1
int d = 0

// Doji meaning table
opts() => ["NO", "AS_RED", "AS_GREEN"]
[opt_no, opt_as_red, opt_as_green] = opts() 

// @function        This function check what type of candle is, based on its close and open prices
// @param open      series float (open price)
// @param close     series float (close price)
// @returns         This function return the candle type (1 for Bullish, -1 Bearish, 0 as Doji candle)
export CandleType(float open, float close) =>
    if (close > open)
        g
    else if (close < open)
        r
    else
        d

// @function                    This function calculates the percentage difference between Bullish and Bearish in a candlestick range back in time and which is the type with the least occurrences
// @param open                  series float (open price series)
// @param close                 series float (close price series)
// @param qtd_candles_before    simple int (Number of candles before to calculate)
// @param consider_dojis        simple string (How to consider dojis (no consider "NO", as bearish "AS_RED", as bullish "AS_GREEN"))
// @returns                     tuple(float, int) (Returns the percentage difference between Bullish and Bearish candles and which type of candle has the least occurrences)
export CandleTypePercentDiff(series float open, series float close, simple int qtd_candles_before, simple string consider_dojis = "NO") =>
    int qtd_red     = int(0)
    int qtd_green   = int(0)
    
    float red_p     = float(0)
    float green_p   = float(0)
    float diff_p    = float(0)
    int lower_p     = int(0)
    for int i = 1 to qtd_candles_before
        if CandleType(open[i], close[i]) == r
            qtd_red += 1
        else if CandleType(open[i], close[i]) == g
            qtd_green += 1
        else
            if consider_dojis == opt_as_red
                qtd_red += 1
            else if consider_dojis == opt_as_green
                qtd_green += 1
            
    red_p := (qtd_red * 100) / qtd_candles_before
    green_p := (qtd_green * 100) / qtd_candles_before
    
    diff_p := math.abs(red_p - green_p)
    lower_p := (math.min(red_p, green_p) == red_p) and (red_p != green_p) ? r : g
    [math.round(diff_p, 2), lower_p]


[diff, lower] = CandleTypePercentDiff(open, close, 89, "NO")
default_max = 16
default_min = -16
float default_exit_up   = float(9.0)
float default_exit_down = float(-9.0)

max_diff_bullish = plot(default_max, "Max Bullish Diff", color=color.red)
current_diff = plot( diff * (lower * -1) , "Current Diff")
max_diff_bearish = plot(default_min, "Max Bearish Diff", color=color.green)

hline(default_exit_up, title="Exit", color=color.yellow)
hline(default_exit_down, title="Exit", color=color.yellow)


