Trap Trading - SwaG
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Swag

//@version=5
indicator("Trap Trading - SwaG", overlay = true)
//Inputs
bool red_zone_traps = input.bool(defval = false, title = "Red Zone Traps")
bool avoid_traps_at_support_or_resistance = input.bool(defval = true, title = " Avoid Traps At Support/Resistance Line")
bool support_resistance_levels = input.bool(defval = true, title = "Support Resistance Levels")
color resistance_color = input.color(color.purple, title="Previous Day Resistance", inline = "colors")
color support_color = input.color(color.green, title="Previous Day Support", inline = "colors")

//Static Variables
timeframe = timeframe.period
var float dh = 0.0
var float dl  = 0.0
var bool is_support_confirmed = false
var bool is_resistance_confirmed = false
var float highest_green_open = na
var float lowest_red_open = na

var int pre_support_bar = na
var int pre_resistance_bar = na
var int cur_support_bar = na
var int cur_resistance_bar = na
var int total_intraday_bar = 0
if timeframe.isminutes
    if timeframe.multiplier == 1
        total_intraday_bar := 374
    else if timeframe.multiplier == 3
        total_intraday_bar := 124
    else if timeframe.multiplier == 5
        total_intraday_bar := 74
    else if timeframe.multiplier == 10
        total_intraday_bar := 37
    else if timeframe.multiplier == 15
        total_intraday_bar := 24

newDayStart = dayofmonth != dayofmonth[1] and timeframe.isintraday

// Set day high day low
if newDayStart
    dh := high
    dl  := low
    highest_green_open := na
    lowest_red_open := na
else
    dh := math.max(dh, high)
    dl  := math.min(dl, low)

//support & resistance
is_first_bar = request.security(syminfo.tickerid,"D",barstate.isfirst)
if timeframe.isminutes and timeframe.multiplier <= 15 and support_resistance_levels
    if newDayStart and not is_first_bar
        pre_resistance_bar := cur_resistance_bar
        pre_support_bar := cur_support_bar
        box.new(left=bar_index, top=high[bar_index-pre_resistance_bar],right= bar_index+total_intraday_bar, bottom=low[bar_index-pre_resistance_bar],border_color = color.new(resistance_color,50),bgcolor = color.new(resistance_color,90))
        box.new(left=bar_index, top=high[bar_index-pre_support_bar],right= bar_index+total_intraday_bar, bottom=low[bar_index-pre_support_bar],border_color = color.new(support_color,50),bgcolor = color.new(support_color,90))

    if newDayStart
        cur_support_bar := na
        cur_resistance_bar := na

    if volume > volume[1]
        if open > close
            if na(cur_resistance_bar)
                cur_resistance_bar := bar_index
            else if high >= high[bar_index-cur_resistance_bar]
                cur_resistance_bar := bar_index
        if open < close
            if na(cur_support_bar)
                cur_support_bar := bar_index
            else if low <= low[bar_index-cur_support_bar]
                cur_support_bar := bar_index

//Valid trap range find
if open < close
    if na(highest_green_open)
        highest_green_open := open
    else
        highest_green_open := math.max(highest_green_open,open)

if open > close
    if na(lowest_red_open)
        lowest_red_open := open
    else
        lowest_red_open := math.min(lowest_red_open,open)

if not timeframe.isintraday
    highest_green_open := na
    lowest_red_open := na

// Confirmation of support and resistance
if high >= dh
    is_resistance_confirmed := false
if low <= dl
    is_support_confirmed := false

support_confirm_condition = close > open and volume > volume[1] and low > dl
resistance_confirmed_condition = open > close and volume > volume[1] and high < dh

if support_confirm_condition
    is_support_confirmed := true
if resistance_confirmed_condition
    is_resistance_confirmed := true

//finding Trapping candles
sell_trap_condition = open > close and volume > volume[1]
buy_trap_condition = close > open and volume > volume[1]

if timeframe.isintraday and not red_zone_traps
    sell_trap_condition := sell_trap_condition and is_support_confirmed and is_resistance_confirmed
    buy_trap_condition := buy_trap_condition and is_support_confirmed and is_resistance_confirmed
    if avoid_traps_at_support_or_resistance
        sell_trap_condition := sell_trap_condition and low > lowest_red_open
        buy_trap_condition := buy_trap_condition and high < highest_green_open

sell_trap = request.security(symbol = syminfo.tickerid ,timeframe = timeframe,expression = sell_trap_condition)
buy_trap = request.security(symbol = syminfo.tickerid ,timeframe = timeframe,expression = buy_trap_condition)

//plotting objects
plotshape(sell_trap, color = color.green, style = shape.triangleup, title = "Sellers Trap")
plotshape(buy_trap, color = color.red, style = shape.triangledown,location = location.belowbar, title = "Buyers Trap")
bgcolor( not (is_support_confirmed and is_resistance_confirmed) and timeframe.isintraday? color.new(color.red,90) : na)
plot(highest_green_open, color = color.red, style = plot.style_stepline, title = "Sell below", linewidth = 2)
plot(lowest_red_open, color = color.green, style = plot.style_stepline, title = "Buy Above", linewidth = 2)