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

//@version=5
indicator("IC breakout", overlay=true, max_labels_count = 500)

// Variables
breakoutText = ""

// Functions
updateBreakOutText(ICBreakLow, ICBreakHigh, previousHighLowRange, previousOpenCloseRange) =>
    updatedText = ""
    if ICBreakLow
        updatedText := "IC Beakout Low\nPrev h/l %: " + str.tostring(previousHighLowRange * 100) + "\nPrev o/c %: " + str.tostring(previousOpenCloseRange * 100) 
    else if ICBreakHigh
        updatedText := "IC breakout high\nPrev h/l %: " + str.tostring(previousHighLowRange * 100) + "\nPrev o/c %: " + str.tostring(previousOpenCloseRange * 100)
    updatedText

// Inputs
maxHighLowPriceRange = input.float(2, title='Maximum high/low price range', minval=0, step=0.1, group="Price Range") / 100
maxOpenClosePriceRange = input.float(0.1, title='Maximum open/close price range', minval=0, step=0.01, group="Price Range") / 100

// Calculate the previous high/low price range
previousHighLowRange = (high[1] - low[1]) / low[1]

// Calculate the previous open/close price range
previousOpenCloseRange = 0.0
if (close[1] > open[1])
    previousOpenCloseRange := (close[1] - open[1]) / open[1]
else
    previousOpenCloseRange := (open[1] - close[1]) / close[1]

// Calculate if current price breaks low
priceBreaksDown = close < low[1]

// Calculate if current price breaks high
priceBreaksUp = close > high[1]

// Calculate if previous block was an indecision candle
indecisionCandle = (previousHighLowRange <= maxHighLowPriceRange) and (previousOpenCloseRange <= maxOpenClosePriceRange)

// Calculate if indecision candle and breakout to low
ICBreakLow = indecisionCandle and priceBreaksDown

// Calculate if indecision candle and breakout to high
ICBreakHigh = indecisionCandle and priceBreaksUp

// Update the label
breakoutText := updateBreakOutText(ICBreakLow, ICBreakHigh, previousHighLowRange, previousOpenCloseRange)

if ICBreakLow or ICBreakHigh
    breakoutLabel = label.new(bar_index, low, text=breakoutText, style=label.style_label_up, color=color.new(color.orange, 0))
    label.set_yloc(breakoutLabel, yloc.belowbar)

// Alerts
alertcondition(ICBreakLow, title="2. IC breakout low")
alertcondition(ICBreakHigh, title="3. IC breakout high")
alertcondition(ICBreakLow or ICBreakHigh, title="1. IC breakout high/low")