Probability of Candle Close Higher Than Open
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © theDOGEguy1

//@version=4
study("Probability of Candle Close Higher Than Open", overlay=true)

// Define input variables
lookback_period = input(title="Lookback Period", type=input.integer, defval=2, minval=1, maxval=10)
prob_threshold = input(title="Probability Threshold", type=input.float, defval=0.5, minval=0, maxval=1, step=0.01)

// Define variables
prev_high = highest(lookback_period)[1]
prev_low = lowest(lookback_period)[1]
prev_body = abs(open[1] - close[1])

// Assess probability of current candle closing higher than open
var probability = 0.0
if open > prev_high
    probability := 0.1
else if open < prev_low
    probability := 0.9
else
    if close[1] > open[1]
        probability := 0.5 + (prev_body / 2)
    else if close[1] < open[1]
        probability := 0.1 + (prev_body / 2)
    else
        probability := 0.1

// Display probability label on chart
if barstate.islast
    label.new(bar_index, high, text=tostring(probability, "#.##"), color=color.white, textcolor=color.black, style=label.style_label_left, size=size.normal)

// Plot horizontal line at probability threshold
hline(prob_threshold, title="Probability Threshold", color=color.gray, linestyle=hline.style_dotted, linewidth=2)

