KeitoFX Dynamic Indicator Free vers.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © uniuxfu

//@version=5
indicator("KeitoFX Dynamic Indicator Free vers.", overlay=true)

// Check if the current timeframe is between 1m and 5m
isAllowedTimeframe() =>
    timeframe.multiplier >= 1 and timeframe.multiplier <= 5 and timeframe.isintraday


    
// DYNAMIC FVG
var int maruBodyHeight = 10

bool dynamic = false
bool bullDynamic = close == high and (dynamic ? open == low : true)
bool bearDynamic = close == low and (dynamic ? open == high : true)

// Engulfing Candles
// --------------- INPUTS ---------------
var bool show_H1 = true
var bool show_H4 = false
var bool show_CUR = false
var bool filter_liqudity = true
var bool filter_close = true

// --------------- function ---------------
drawing_lec() =>
    bull_engulf = false
    bear_engulf = false
    if barstate.isnew
        prior_open = open[1]
        prior_close = close[1]
        current_open = open
        current_close = close

        bull_engulf := (current_open <= prior_close) and (current_open < prior_open) and (current_close > prior_open)
        bear_engulf := (current_open >= prior_close) and (current_open > prior_open) and (current_close < prior_open)

        if filter_liqudity
            bull_engulf := bull_engulf and low <= low[1]
            bear_engulf := bear_engulf and high >= high[1]

        if filter_close
            bull_engulf := bull_engulf and close >= high[1]
            bear_engulf := bear_engulf and close <= low[1]
    [bull_engulf, bear_engulf]

[bull_engulf_H1, bear_engulf_H1] = request.security(syminfo.tickerid, "60", drawing_lec())
[bull_engulf_H4, bear_engulf_H4] = request.security(syminfo.tickerid, "240", drawing_lec())
[bull_engulf_CUR, bear_engulf_CUR] = request.security(syminfo.tickerid, timeframe.period, drawing_lec())

// Displacement settings
var bool require_fvg = true
var string disp_type = "Open to Close"
var int std_len = 100
var float std_x = 1.5
var color disp_color = input.color(#e5e0ff, "Bar Color")

float candle_range = disp_type == "Open to Close" ? math.abs(open - close) : high - low
float std = ta.stdev(candle_range, std_len) * std_x
bool fvg = close[1] > open[1] ? high[2] < low[0] : low[2] > high[0]
bool displacement = require_fvg ? candle_range[1] > std[1] and fvg : candle_range > std
barcolor(isAllowedTimeframe() and displacement ? disp_color : na, offset = require_fvg ? -1 : na)