Big Moves Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © bhnmzm

//@version=5
indicator("Big Moves Indicator", overlay=true)

// Input variables
default_percentage = input.float(5, minval=0, maxval=100, title="Default Control Percentage (%)", tooltip="Will be applied only if specific control percentages for the timeframe are not set.", display = display.none)
max_displayed_moves = input.int(10, "Maximum number of moves to display", minval = 0, maxval = 100, display = display.none)

var group="Use Timeframe-Specific Control Percentage"
bool tf_1_enabled = input.bool(false, title = "", inline="01", group=group, display = display.none)
string tf_1 = input.timeframe(defval = "", title = "Timeframe: ", inline="01", group=group, display = display.none)
float tf_1_per = input.float(0, minval=0, maxval=100, title="Percentage: ", inline="01", group=group, display = display.none)

bool tf_2_enabled = input.bool(false, title = "", inline="02", group=group, display = display.none)
string tf_2 = input.timeframe(defval = "", title = "Timeframe: ", inline="02", group=group, display = display.none)
float tf_2_per = input.float(0, minval=0, maxval=100, title="Percentage: ", inline="02", group=group, display = display.none)

bool tf_3_enabled = input.bool(false, title = "", inline="03", group=group, display = display.none)
string tf_3 = input.timeframe(defval = "", title = "Timeframe: ", inline="03", group=group, display = display.none)
float tf_3_per = input.float(0, minval=0, maxval=100, title="Percentage: ", inline="03", group=group, display = display.none)

bool tf_4_enabled = input.bool(false, title = "", inline="04", group=group, display = display.none)
string tf_4 = input.timeframe(defval = "", title = "Timeframe: ", inline="04", group=group, display = display.none)
float tf_4_per = input.float(0, minval=0, maxval=100, title="Percentage: ", inline="04", group=group, display = display.none)

bool tf_5_enabled = input.bool(false, title = "", inline="05", group=group, display = display.none)
string tf_5 = input.timeframe(defval = "", title = "Timeframe: ", inline="05", group=group, display = display.none)
float tf_5_per = input.float(0, minval=0, maxval=100, title="Percentage: ", inline="05", group=group, display = display.none)

var label_ids = array.new_label(max_displayed_moves)

// Timeframe-specific control percentages
float current_tf_in_mins = timeframe.in_seconds() / 60
float tf_1_mins = not na(tf_1) ? timeframe.in_seconds(tf_1) / 60 : na
float tf_2_mins = not na(tf_2) ? timeframe.in_seconds(tf_2) / 60 : na
float tf_3_mins = not na(tf_3) ? timeframe.in_seconds(tf_3) / 60 : na
float tf_4_mins = not na(tf_4) ? timeframe.in_seconds(tf_4) / 60 : na
float tf_5_mins = not na(tf_5) ? timeframe.in_seconds(tf_5) / 60 : na

// Get the selected control percentage based on the timeframe
getPercentage() =>
    if tf_1_enabled and tf_1_mins == current_tf_in_mins
        tf_1_per
    else if tf_2_enabled and tf_2_mins == current_tf_in_mins
        tf_2_per
    else if tf_3_enabled and tf_3_mins == current_tf_in_mins
        tf_3_per
    else if tf_4_enabled and tf_4_mins == current_tf_in_mins
        tf_4_per
    else if tf_5_enabled and tf_5_mins == current_tf_in_mins
        tf_5_per
    else 
        default_percentage

// Indicator calculations
pre_close = close[1]
is_red = close <= open
height = high - low
move = height / pre_close * 100

active_percentage = getPercentage()
is_big_move = move > active_percentage

// Plotting
if is_big_move
    lbl = label.new(is_big_move ? bar_index : na, is_red ? high : low, style=is_red ? label.style_label_down : label.style_label_up, text = str.tostring(move, "##.##") + "%")
    // add new label to the queue
    array.push(label_ids, lbl)
    // remove oldest label
    label.delete(array.shift(label_ids))

// Alerts
alertcondition(is_big_move and is_red, "Big Falling Move", "Big falling move")
alertcondition(is_big_move and not is_red, "Big Rising Move", "Big rising move")
