#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © tradeforopp
#indicator("Visualizing Displacement [TFO]", "Displacement [TFO]", true)
#Converted and mod by Sam4Cok@Samer800 - 12/2022
input BarColor = yes;
input ShowLines = no;
input require_fvg = yes; # "Require FVG"
input disp_type = {Default "Open to Close", "High to Low"}; # "Displacement Type"
input DisplacementLength = 100; # "Displacement Length"
input DisplacementStrength = 4.0; # "Displacement Strength"
def na =Double.NaN;
def range = if disp_type == disp_type."Open to Close" then 1 else 0;
def candle_range = if range then AbsValue(open - close) else high - low;
def std = stdev(candle_range, DisplacementLength) * DisplacementStrength;
def fvg = if close[1] > open[1] then high[2] < low[0] else low[2] > high[0];
def disp = if require_fvg then candle_range[1] > std[1] and fvg else candle_range > std;
def displacement = if require_fvg then disp[-1] else disp;
def raising = if displacement then if close>open then 1 else 0 else raising[1];
def disHi = if displacement then high else disHi[1];
def disLo = if displacement then low else disLo[1];
def mid = (disHi + disLo) / 2;
plot disUpper = if !ShowLines or disHi==0 then na else disHi;
disUpper.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
disUpper.AssignValueColor(if raising then Color.GREEN else Color.RED);
plot disLower = if !ShowLines or disLo==0 then na else disLo;
disLower.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
disLower.AssignValueColor(if raising then Color.GREEN else Color.RED);
plot midLine = if !ShowLines or mid==0 then na else mid;
midLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
midLine.SetDefaultColor(Color.GRAY);
midLine.SetStyle(Curve.SHORT_DASH);
AssignPriceColor(if (displacement and BarColor) then if raising then Color.CYAN else Color.MAGENTA else Color.CURRENT);
#--- END CODE