ATR Levels
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © angelh584
// Method by AdamSilverTrade

//@version=5
indicator('ATR Levels', shorttitle='ATR Levels', overlay=true)

// === Timeframe Selection ===
timeframe_choice = input.string(title="Select Timeframe", defval="Daily", options=["Daily", "Weekly"], tooltip="Choose the timeframe for ATR calculations.")

// === General Inputs ===
group_general = "General"
atr_length = input.int(14, title='ATR Length', group=group_general)
trigger_percentage = input.float(0.236, title='Trigger Percentage', group=group_general)
level_size = input.int(2, title='Level Size', group=group_general)

// === Trigger Level Colors ===
group_trigger_levels = "Trigger Levels"
lower_trigger_level_color = input.color(color.red, title='Lower Trigger Level Color', group=group_trigger_levels)
upper_trigger_level_color = input.color(color.green, title='Upper Trigger Level Color', group=group_trigger_levels)

// === ATR Target Colors ===
group_atr_targets = "ATR Targets"
atr_target_color = input.color(color.purple, title='ATR Target Color', group=group_atr_targets)

// === ATR Target Multipliers ===
atr_target_mult1 = input.float(0.5, title='ATR Target 1 Multiplier', group=group_atr_targets)
atr_target_mult2 = input.float(0.723, title='ATR Target 2 Multiplier', group=group_atr_targets)

// === Table Settings ===
group_table = "Table Settings"
string  tableYposInput = input.string("top", title="Panel position", options = ["top", "middle", "bottom"], group=group_table, tooltip="Select the vertical position of the table.")
string  tableXposInput = input.string("right", title="Horizontal position", options = ["left", "center", "right"], group=group_table, tooltip="Select the horizontal position of the table.")
color   bgColorInput = input.color(color.new(color.gray, 30), title="Background Color", group=group_table, tooltip="Choose the background color for the table.")

// Set the appropriate timeframe based on user's choice
timeframe_func() =>
    timeframe_choice == 'Daily' ? 'D' : 'W'
// Offset for label position
offset = 10

// Data
period_index = 1
ticker = ticker.new(syminfo.prefix, syminfo.ticker, session=session.extended)
previous_close = request.security(ticker, timeframe_func(), close[period_index], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)
atr = request.security(ticker, timeframe_func(), ta.atr(atr_length)[period_index], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on)

// === Trigger Levels ===
lower_trigger = previous_close - trigger_percentage * atr
upper_trigger = previous_close + trigger_percentage * atr

// Plot trigger levels as lines
plot(lower_trigger, color=color.new(lower_trigger_level_color, 40), linewidth=level_size, title='Put Trigger', style=plot.style_stepline)
plot(upper_trigger, color=color.new(upper_trigger_level_color, 40), linewidth=level_size, title='Call Trigger', style=plot.style_stepline)

// Add text labels for trigger levels
var label label1 = na
var label label2 = na

if (na(label1))
    label1 := label.new(x=bar_index + offset, y=lower_trigger, text='Puts', color=color.new(lower_trigger_level_color, 0), style=label.style_label_center)
else
    label.set_xy(label1, x=bar_index + offset, y=lower_trigger)

if (na(label2))
    label2 := label.new(x=bar_index + offset, y=upper_trigger, text='Calls', color=color.new(upper_trigger_level_color, 0), style=label.style_label_center)
else
    label.set_xy(label2, x=bar_index + offset, y=upper_trigger)

// === ATR Target Levels ===
atr_target1_bullish = previous_close + atr * atr_target_mult1
atr_target2_bullish = previous_close + atr * atr_target_mult2
atr_target1_bearish = previous_close - atr * atr_target_mult1
atr_target2_bearish = previous_close - atr * atr_target_mult2

// Plot ATR target levels as lines
plot(atr_target1_bullish, color=color.new(atr_target_color, 40), linewidth=level_size, title='Bullish Target 1', style=plot.style_stepline)
plot(atr_target2_bullish, color=color.new(atr_target_color, 40), linewidth=level_size, title='Bullish Target 2', style=plot.style_stepline)
plot(atr_target1_bearish, color=color.new(atr_target_color, 40), linewidth=level_size, title='Bearish Target 1', style=plot.style_stepline)
plot(atr_target2_bearish, color=color.new(atr_target_color, 40), linewidth=level_size, title='Bearish Target 2', style=plot.style_stepline)


// Add text labels for ATR target levels
var label label3 = na
var label label4 = na
var label label5 = na
var label label6 = na

// Create an array to hold label variables
var label[] labels = array.new_label(4)

if (na(label3))
    label3 := label.new(x=bar_index + offset, y=atr_target1_bullish, text='Bullish Target 1', color=color.new(atr_target_color, 0), style=label.style_label_center)
else
    label.set_xy(label3, x=bar_index + offset, y=atr_target1_bullish)

if (na(label4))
    label4 := label.new(x=bar_index + offset, y=atr_target2_bullish, text='Bullish Target 2', color=color.new(atr_target_color, 0), style=label.style_label_center)
else
    label.set_xy(label4, x=bar_index + offset, y=atr_target2_bullish)

if (na(label5))
    label5 := label.new(x=bar_index + offset, y=atr_target1_bearish, text='Bearish Target 1', color=color.new(atr_target_color, 0), style=label.style_label_center)
else
    label.set_xy(label5, x=bar_index + offset, y=atr_target1_bearish)

if (na(label6))
    label6 := label.new(x=bar_index + offset, y=atr_target2_bearish, text='Bearish Target 2', color=color.new(atr_target_color, 0), style=label.style_label_center)
else
    label.set_xy(label6, x=bar_index + offset, y=atr_target2_bearish)


// === Enhanced Table to Display Call Trigger, Put Trigger, and Current ATR ===
var table panel = table.new(tableYposInput + "_" + tableXposInput, 2, 4)  // 2 columns, 4 rows

// Define some colors and emojis
headerColor = color.new(color.blue, 20)
bullColor = color.new(color.green, 40)
bearColor = color.new(color.red, 40)
neutralColor = color.new(color.gray, 40)
upArrow = "🔼"
downArrow = "🔽"

if barstate.islast
    // Table header
    table.cell(panel, 0, 0, "Parameter", bgcolor = headerColor, text_color = color.white)
    table.cell(panel, 1, 0, "Value", bgcolor = headerColor, text_color = color.white)

    // Call Trigger
    callTriggerColor = upper_trigger > close ? bullColor : bearColor
    table.cell(panel, 0, 1, upArrow + " Call Trigger", bgcolor = neutralColor)
    table.cell(panel, 1, 1, str.tostring(upper_trigger, format.mintick), text_color = color.black, bgcolor = callTriggerColor)

    // Put Trigger
    putTriggerColor = lower_trigger < close ? bearColor : bullColor
    table.cell(panel, 0, 2, downArrow + " Put Trigger", bgcolor = neutralColor)
    table.cell(panel, 1, 2, str.tostring(lower_trigger, format.mintick), text_color = color.black, bgcolor = putTriggerColor)

    // Current ATR
    table.cell(panel, 0, 3, "Current ATR", bgcolor = neutralColor)
    table.cell(panel, 1, 3, str.tostring(atr, format.mintick), text_color = color.black, bgcolor = neutralColor)

// Alert Condition 1: Bullish Trigger Cross
alertcondition(upper_trigger > close, title="Bullish Trigger Cross", message="Bullish Trigger Cross")

// Alert Condition 2: Bearish Trigger Cross
alertcondition(lower_trigger < close, title="Bearish Trigger Cross", message="Bearish Trigger Cross")

// Alert Condition 3: ATR Target 1 Bullish
alertcondition(close > atr_target1_bullish, title="ATR Target 1 Bullish", message="Price above ATR Target 1 (Bullish)")

// Alert Condition 4: ATR Target 2 Bullish
alertcondition(close > atr_target2_bullish, title="ATR Target 2 Bullish", message="Price above ATR Target 2 (Bullish)")

// Alert Condition 5: ATR Target 1 Bearish
alertcondition(close < atr_target1_bearish, title="ATR Target 1 Bearish", message="Price below ATR Target 1 (Bearish)")

// Alert Condition 6: ATR Target 2 Bearish
alertcondition(close < atr_target2_bearish, title="ATR Target 2 Bearish", message="Price below ATR Target 2 (Bearish)")

