Repaints FVG Fair Value Gaps Bearish & Bullish Levels For ThinkOrSwim

Repaints
Trade for Opportunity has a script for an indicator in TV for Balanced Price Range. This would be an awesome indicator for TOS if anybody could adapt it.

Here's a link to the page: https://www.tradingview.com/script/856oabwc-Balanced-Price-Range-BPR/


Pinescript:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tradeforopp
//@version=5
indicator("BPR", overlay = true, max_bars_back = 500)
fvg_threshold = input.float(0, "FVG Threshold", tooltip = "Valid FVG's must have a gap greater than this number")
bpr_threshold = input.float(0, "BPR Threshold", tooltip = "Valid BPR's must have a range greater than this number")
bars_since = input(10, "Bars to Look for BPR", tooltip = "Only look for BPR's when a sequence of bearish and bullish FVG's are within this many bars of each other")
only_clean_bpr = input(false, "Only Clean BPR", tooltip = "Only show BPR's when price does not interfere with the range prior to its completion")
bearish_bpr_color = input.color(color.new(color.red, 70))
bullish_bpr_color = input.color(color.new(color.green, 70))
float box_high = na
float box_low = na
int box_left = 0
int box_right = 0
var box box_bearish = na
var box box_bullish = na
new_fvg_bearish = low[2] - high >= fvg_threshold
new_fvg_bullish = low - high[2] >= fvg_threshold

// Bullish BPR
bull_num_since = ta.barssince(new_fvg_bearish)
bull_bpr_cond_1 = new_fvg_bullish and bull_num_since <= bars_since
bull_bpr_cond_2 = bull_bpr_cond_1 ? high[bull_num_since] + low[bull_num_since + 2] + high[2] + low > math.max(low[bull_num_since + 2], low) - math.min(high[bull_num_since], high[2]) : na
bull_combined_low = bull_bpr_cond_2 ? math.max(high[bull_num_since], high[2]) : na
bull_combined_high = bull_bpr_cond_2 ? math.min(low[bull_num_since + 2], low) : na
bull_bpr_cond_3 = true
if only_clean_bpr
for h = 2 to (bull_num_since)
if high[h] > bull_combined_low
bull_bpr_cond_3 := false
bull_result = bull_bpr_cond_1 and bull_bpr_cond_2 and bull_bpr_cond_3 and (bull_combined_high - bull_combined_low >= bpr_threshold)
if bull_result
box_bullish := box.new(bar_index - bull_num_since - 1, bull_combined_high, bar_index + 1, bull_combined_low, border_color = bullish_bpr_color, border_width = 1, bgcolor = bullish_bpr_color)
if not na(box_bullish) and low > box.get_top(box_bullish)
box.set_right(box_bullish, bar_index + 1)
else if not na(box_bullish) and low < box.get_top(box_bullish)
box_bullish := na

// Bearish BPR
bear_num_since = ta.barssince(new_fvg_bullish)
bear_bpr_cond_1 = new_fvg_bearish and bear_num_since <= bars_since
bear_bpr_cond_2 = bear_bpr_cond_1 ? high[bear_num_since] + low[bear_num_since + 2] + high[2] + low > math.max(low[bear_num_since + 2], low) - math.min(high[bear_num_since], high[2]) : na
bear_combined_low = bear_bpr_cond_2 ? math.max(high[bear_num_since + 2], high) : na
bear_combined_high = bear_bpr_cond_2 ? math.min(low[bear_num_since], low[2]) : na
bear_bpr_cond_3 = true
if only_clean_bpr
for h = 2 to (bear_num_since)
if low[h] < bear_combined_high
bear_bpr_cond_3 := false
bear_result = bear_bpr_cond_1 and bear_bpr_cond_2 and bear_bpr_cond_3 and (bear_combined_high - bear_combined_low >= bpr_threshold)
if bear_result
box_bearish := box.new(bar_index - bear_num_since - 1, bear_combined_high, bar_index + 1, bear_combined_low, border_color = bearish_bpr_color, border_width = 1, bgcolor = bearish_bpr_color)
if not na(box_bearish) and high < box.get_top(box_bearish)
box.set_right(box_bearish, bar_index + 1)
else if not na(box_bearish) and high > box.get_top(box_bearish)
box_bearish := na
You may have missed it, but he made one for TOS. Here is the code: http://tos.mx/Uv4ImK8
Found Here:


#BPR
input fvg_threshold = 1;
input bpr_threshold = 1;
input bars_since = 10;
input only_clean_bpr = no;

def new_fvg_bearish = if low[2] - high > fvg_threshold then yes else no;
def new_fvg_bullish = if low - high[2] > fvg_threshold then yes else no;


# Bullish BPR
def new_bear = if new_fvg_bearish then 1 else new_bear[1] + 1;
def bullish_bear_fvg_high = if new_bear == 1 then low[2] else bullish_bear_fvg_high[1];
def bullish_bear_fvg_low = if new_bear == 1 then high else bullish_bear_fvg_low[1];

def bull_cond_1 = new_fvg_bullish and new_fvg_bearish within bars_since bars;

def bull_fvg_high = if bull_cond_1 then low else bull_fvg_high[1];
def bull_fvg_low = if bull_cond_1 then high[2] else bull_fvg_low[1];

def bull_cond_2 = if bull_cond_1 then if bullish_bear_fvg_low + bullish_bear_fvg_high + bull_fvg_low + bull_fvg_high > Max(bullish_bear_fvg_low, bull_fvg_low) - Min(bullish_bear_fvg_high, bull_fvg_high) then yes else no else no;

def bull_combined_low = if bull_cond_1 and bull_cond_2 then Max(bullish_bear_fvg_low, bull_fvg_low) else bull_combined_low[1];
def bull_combined_high = if bull_cond_1 and bull_cond_2 then Min(bullish_bear_fvg_high, bull_fvg_high) else bull_combined_high[1];

def bull_cond_3 = if only_clean_bpr then fold i = 2 to bars_since with j = yes do if i < new_bear and high > bull_combined_low then j and no else j and yes else yes;

def bull_result = if bull_cond_1 and bull_cond_2 and (bull_combined_high - bull_combined_low > bpr_threshold) then yes else if low < bull_combined_low then double.nan else bull_result[1];

def bull_result_high = if bull_result and bull_combined_low < bull_combined_high then bull_combined_high else bull_result_high[1];
def bull_result_low = if bull_result and bull_combined_low < bull_combined_high then bull_combined_low else bull_result_low[1];


# Bearish BPR
def new_bull = if new_fvg_bullish then 1 else new_bull[1] + 1;
def bearish_bull_fvg_high = if new_bull == 1 then low else bearish_bull_fvg_high[1];
def bearish_bull_fvg_low = if new_bull == 1 then high[2] else bearish_bull_fvg_low[1];

def bear_cond_1 = new_fvg_bearish and new_fvg_bullish within bars_since bars;

def bear_fvg_high = if bear_cond_1 then low[2] else bear_fvg_high[1];
def bear_fvg_low = if bear_cond_1 then high else bear_fvg_low[1];

def bear_cond_2 = if bear_cond_1 then if bearish_bull_fvg_low + bearish_bull_fvg_high + bear_fvg_low + bear_fvg_high > Max(bearish_bull_fvg_low, bear_fvg_low) - Min(bearish_bull_fvg_high, bear_fvg_high) then yes else no else no;

def bear_combined_low = if bear_cond_1 and bear_cond_2 then Max(bearish_bull_fvg_low, bear_fvg_low) else bear_combined_low[1];
def bear_combined_high = if bear_cond_1 and bear_cond_2 then Min(bearish_bull_fvg_high, bear_fvg_high) else bear_combined_high[1];

def bear_cond_3 = if only_clean_bpr then fold k = 2 to bars_since with p = yes do if k < new_bull and low[k] < bear_combined_high then p and no else p and yes else yes;

def bear_result = if bear_cond_1 and bear_cond_2 and bear_cond_3 and (bear_combined_high - bear_combined_low > bpr_threshold) then yes else if high > bear_combined_high then double.nan else bear_result[1];

def bear_result_high = if bear_result and bear_combined_low < bear_combined_high then bear_combined_high else bear_result_high[1];
def bear_result_low = if bear_result and bear_combined_low < bear_combined_high then bear_combined_low else bear_result_low[1];


# Plotting
def bull_signal = if bull_result then yes else no;
def bh = if bull_result then bull_result_high else bh[1];
def bh1 = bh;
def bl = if bull_result then bull_result_low else bl[1];
def bl1 = bl;

def bear_signal = if bear_result then yes else no;
def ph = if bear_result then bear_result_high else ph[1];
def ph1 = ph;
def pl = if bear_result then bear_result_low else pl[1];
def pl1 = pl;

DefineGlobalColor("Bullish", Color.GREEN);
DefineGlobalColor("Bearish", Color.RED);
AddCloud(if bull_signal then bh1 else Double.NaN, if bull_signal then bl1 else Double.NaN, GlobalColor("Bullish"));
AddCloud(if bear_signal then ph1 else Double.NaN, if bear_signal then pl1 else Double.NaN, GlobalColor("Bearish"));
 
RpNhmUF.png


This indicator has multilabel FVG options. [ATR/Auto/Manual].
feel free to test.

CODE:

CSS:
# All-in-One FVG indicator inspired from "ICT Fair Value Gap [LM]" and "Fair Value Gap [LUX]"
# Created by Sam4Cok@Samer800 - 09/2022
#// atr settings
input ShowTodayOnly = yes;
input ShowMidLine   = yes;
input ColorFVGBar   = no;
input thresholdType = {default ATR, Auto, Manual};
input atrLength = 28;         # 'ATR MA length'
input atrMultiplier = 1.5;    # 'ATR multiplier'
input ManualThreshold = 0.05;   # 'Percentage for Manual entry'
input UseChartTime = yes;
input aggregation = AggregationPeriod.FIFTEEN_MIN;
#//Data
def na = Double.NaN;
def today = if ShowTodayOnly == 0 or GetDay() == GetLastDay() then 1 else 0;
#--------------
DefineGlobalColor("up"  , CreateColor(49, 121, 245));
DefineGlobalColor("dn"  , CreateColor(194, 24, 91));
DefineGlobalColor("mid" , CreateColor(244, 143, 177));
DefineGlobalColor("bgUP" , CreateColor(3.9, 32.5, 81.2));
DefineGlobalColor("bgDN" , CreateColor(49.4, 6.3, 23.1));

def L;
def L1;
def L2;
def H;
def H1;
def H2;
def C;
def C1;
def C2;
def C3;
def O;
def O1;
if UseChartTime
then {
    L  = low;
    L1 = low[1];
    L2 = low[2];
    H  = high;
    H1 = high[1];
    H2 = high[2];
    C  = close;
    C1 = close[1];
    C2 = close[2];
    C3 = close[3];
    O  = open;
    O1 = open[1];
} else {
    L  = low(period = aggregation);
    L1 = low(period = aggregation)[1];
    L2 = low(period = aggregation)[2];
    H  = high(period = aggregation);
    H1 = high(period = aggregation)[1];
    H2 = high(period = aggregation)[2];
    C  = close(period = aggregation);
    C1 = close(period = aggregation)[1];
    C2 = close(period = aggregation)[2];
    C3 = close(period = aggregation)[3];
    O  = open(period = aggregation);
    O1 = open(period = aggregation)[1];
}
#f_isUpCandle(_index) =>
script f_isUpCandle {
    input _index = 0;
    def f_isUpCandle = open[_index] <= close[_index];
    plot return = f_isUpCandle;
}

def isUpCandle = C1 > O1;
def isDnCandle = L2 > H;
def n = BarNumber();
def atrValue = ATR(Length=atrLength);
def priceDiff = H1 - L1;

def delta_per = (C1 - O1) / O1 * 100;
def threshold = TotalSum(AbsValue(delta_per)) / n * 2;

def bullCondition = if thresholdType == thresholdType.ATR then
                    (C3 >= L2 and C1>= C2 and L>H2) else
                    if thresholdType == thresholdType.Auto then
                    (L > H2 and C1 > H2) else L > H2;

def bearCondition = if thresholdType == thresholdType.ATR then
                    (C3 <= H2 and C1 <= C2 and H < L2) else
                    if thresholdType == thresholdType.Auto then
                    (H < L2  and C1<L2) else H < L2;

def midCandlVolatCond = if thresholdType == thresholdType.ATR then
                           priceDiff > atrValue*atrMultiplier else
                        if thresholdType == thresholdType.Auto then
                       (if bullCondition then delta_per > threshold else - delta_per > threshold) else
                       (if bullCondition then AbsValue((H2 - L) / H2) > ManualThreshold / 100 else
                           AbsValue((H - L2) / L2) > ManualThreshold / 100) ;

def isGapClosed = if f_isUpCandle(1) then H2 >= L else L2 <= H;

def isFVGup = if isNaN(isFVGup[1]) then 0 else
              bullCondition and !isGapClosed and midCandlVolatCond and
             (if thresholdType == thresholdType.ATR then !isFVGup[1] else 1);

def isFVGdn = if isNaN(isFVGdn[1]) then 0 else
             bearCondition and !isGapClosed and midCandlVolatCond and
             (if thresholdType == thresholdType.ATR then !isFVGdn[1] else 1);
def topup;
def bottomup;
def isUpCandleup;
def midup;
def topdn;
def bottomdn;
def isUpCandledn;
def middn;

if isFVGup {
    isUpCandleup = f_isUpCandle(1);
    topup        = if isUpCandle then L else L2;
    bottomup     = if isUpCandle then H2 else H;
    midup        = (topup + bottomup) / 2;
} else {
    isUpCandleup = isUpCandleup[1];
    topup        = topup[1];
    bottomup     = bottomup[1];
    midup        = midup[1];
}
if isFVGdn
{
    isUpCandledn = f_isUpCandle(1);
    topdn        = if isUpCandle then L else L2;
    bottomdn     = if isUpCandle then H2 else H;
    middn        = (topdn + bottomdn) / 2;
} else {
    isUpCandledn = isUpCandledn[1];
    topdn        = topdn[1];
    bottomdn     = bottomdn[1];
    middn        = middn[1];
}
def hasClosed = (O <= bottomdn and H > bottomdn and H >= topdn or O >= topup and L < topup and L <= bottomup);

def BgColorup = isUpCandleup;
def BgColordn = topdn;
#--------------------------------------------------------------
def TopLineup  = if today then topup else na;
plot MidLineup = if IsNaN(close) then na else if today then midup else na;
def BotLineup  = if today then bottomup else na;


MidLineup.AssignValueColor( if BgColorup then Color.LIME else GlobalColor("bgdn"));
MidLineup.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLineup.SetHiding(!ShowMidLine);

#----------------------------------------------------------------
def TopLinedn  = if today then topdn else na;
plot MidLinedn = if IsNaN(close) then na else if today then middn else na;
def BotLinedn  = if today then bottomdn else na;

MidLinedn.AssignValueColor( if !BgColorup then GlobalColor("bgUP") else Color.PINK);
MidLinedn.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLinedn.SetHiding(!ShowMidLine);

#------ Cloud
AddCloud(TopLineup[-1], BotLineup[-1], CreateColor(4, 181, 4), CreateColor(184, 6, 6));
AddCloud(BotLinedn[-1], TopLinedn[-1], CreateColor(4, 181, 4), CreateColor(184, 6, 6));
#------ Bar Color
AssignPriceColor( if ColorFVGBar and topup<>topup[-1] then Color.BLUE else Color.CURRENT);
AssignPriceColor( if ColorFVGBar and topdn<>topdn[-1] then color.PINK else Color.CURRENT);


##### END #######

Hi @samer800,

Fantastic work on this. I do have one question on the cloud extensions... I can see how the clouds disappear as the price comes back to fill the gap (at least I think that's how it works), any reason the 2 red areas with my arrows don't extend even though the price has not filled the gap? But the top green one is extended?

AK9PcUQ.png


Regards.
 
Not sure if this is what you may want but this is one I have:

# FVG Fair Value Gaps Bearish & Bullish Levels
# @TradeForOpp 5/6/2022
# Mod by Sam4COK @ Samer800 08/2022
# V 1.1 - Color FVG bar, option to enable/disable line extention + Show Today only option

input ShowHLLines = yes;
input ExtendLines = yes;
input ColorFVGBar = yes;
input ShowCloud = yes;
input CurrentChartTime = no;
input ShowTodayOnly = yes;
input thresholdPctg = 0.01;
input aggregation = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
script nz {
input data = 1;
input repl = 0;
def ret_val = if IsNaN(data) then repl else data;
plot return = ret_val;
}

def today = if ShowTodayOnly==0 or getday()==getlastday() then 1 else 0;
#//IMBALANCE
#//Data
def L1; def H3; Def H1; Def L3;
if CurrentChartTime then {
L1 = low;
H3 = high[2];
H1 = high;
L3 = low[2];} else {
L1 = low(period = aggregation);
H3 = high(period = aggregation)[2];
H1 = high(period = aggregation);
L3 = low(period = aggregation)[2];
}
def fvgUpPctg = if AbsValue((H3 - L1) / H3) > thresholdPctg / 100 then 1 else 0;
def FVGUp = if H3 < L1 then 1 else 0;
def plotFVGUH = if FVGUp and fvgUpPctg then H3 else na;
def plotFVGUL = if FVGUp and fvgUpPctg then L1 else na;
def plotFVGUH2 = plotFVGUH[-1];
def plotFVGUL2 = plotFVGUL[-1];
def midFVGUp = (plotFVGUH2 + plotFVGUL2) / 2;

def fvgDnPctg = if AbsValue((H1 - L3) / L3) > thresholdPctg / 100 then 1 else 0;
def FVGDown = if L3 > H1 then 1 else 0;
def plotFVGDL = if FVGDown and fvgDnPctg then L3 else na;
def plotFVGDH = if FVGDown and fvgDnPctg then H1 else na;
def plotFVGDL2 = plotFVGDL[-1];
def plotFVGDH2 = plotFVGDH[-1];
def midFVGDown = (plotFVGDL2 + plotFVGDH2) / 2;

#--------------------------------------------------------------
def LastFVGUH = CompoundValue(1, if isNaN(plotFVGUH2) then LastFVGUH[1] else plotFVGUH2, H3);
def LastFVGUL = CompoundValue(1, if isNan(plotFVGUL2) then LastFVGUL[1] else plotFVGUL2, L1);
def LastMidUP = CompoundValue(1, if isNan(midFVGUp) then LastMidUP[1] else midFVGUp, midFVGUp);
#--------------------------------------------------------------
plot HLine = if today then if ExtendLines then LastFVGUH else plotFVGUH2 else na;
plot LLine = if today then if ExtendLines then LastFVGUL else plotFVGUL2 else na;
plot MLine = if today then if ExtendLines then LastMidUP else midFVGUp else na;
HLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HLine.SetDefaultColor(CreateColor(49,121,245));
HLine.SetHiding(!ShowHLLines);
LLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LLine.SetDefaultColor(CreateColor(49,121,245));
LLine.SetHiding(!ShowHLLines);
MLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
MLine.SetDefaultColor(CreateColor(144,191,249));

AddCloud(if ShowCloud and LLine==LLine[1] or LLine==LLine[-1] then LLine else na ,HLine, CreateColor(144,191,249));
AssignPriceColor( if ColorFVGBar and LLine<>LLine[1] then Color.BLUE else Color.CURRENT);

#--------------------------------------------------------------
def LastFVGDL = CompoundValue(1, if isNaN(plotFVGDL2) then LastFVGDL[1] else plotFVGDL2, L3);
def LastFVGDH = CompoundValue(1, if isNan(plotFVGDH2) then LastFVGDH[1] else plotFVGDH2, H1);
def LastMidDN = CompoundValue(1, if isNan(midFVGDown) then LastMidDN[1] else midFVGDown, midFVGDown);
#--------------------------------------------------------------
plot HLineL = if today then if ExtendLines then LastFVGDL else plotFVGDL2 else na;
plot LLineL = if today then if ExtendLines then LastFVGDH else plotFVGDH2 else na;
plot MLineL = if today then if ExtendLines then LastMidDN else midFVGDown else na;
HLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HLineL.SetDefaultColor(CreateColor(194,24,91));
HLineL.SetHiding(!ShowHLLines);
LLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LLineL.SetDefaultColor(CreateColor(194,24,91));
LLineL.SetHiding(!ShowHLLines);
MLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
MLineL.SetDefaultColor(CreateColor(244,143,177));

AddCloud(if ShowCloud and (HLineL == HLineL[1] or HLineL == HLineL[-1]) then HLineL else na ,LLineL, CreateColor(244,143,177));
AssignPriceColor( if ColorFVGBar and HLineL<>HLineL[1] then color.YELLOW else Color.CURRENT);

##### END #######
 
Thank you. Not sure if this is the same. I don't think this one is highlighting all the BOSs (especially to the upside) but rather FVG
 
Thank you. Not sure if this is the same. I don't think this one is highlighting all the BOSs (especially to the upside) but rather FVG
It may not be but thought I'd put it out there...Maybe we'll be able to get the actual..Not sure how many ICT traders we have on this platform.
 
Hi @samer800,

Fantastic work on this. I do have one question on the cloud extensions... I can see how the clouds disappear as the price comes back to fill the gap (at least I think that's how it works), any reason the 2 red areas with my arrows don't extend even though the price has not filled the gap? But the top green one is extended?

AK9PcUQ.png


Regards.
Bro, if you refer to the chart, for example the green cloud, it will "stop" and bring forward to the next "signal" and the green cloud will appear again. If you use the FVG signal only on RTH, then it should look more "clean" and less noise, I suppose! Just my 2 cents. All the best!
 
RpNhmUF.png


This indicator has multilabel FVG options. [ATR/Auto/Manual].
feel free to test.

CODE:

CSS:
# All-in-One FVG indicator inspired from "ICT Fair Value Gap [LM]" and "Fair Value Gap [LUX]"
# Created by Sam4Cok@Samer800 - 09/2022
#// atr settings
input ShowTodayOnly = yes;
input ShowMidLine   = yes;
input ColorFVGBar   = no;
input thresholdType = {default ATR, Auto, Manual};
input atrLength = 28;         # 'ATR MA length'
input atrMultiplier = 1.5;    # 'ATR multiplier'
input ManualThreshold = 0.05;   # 'Percentage for Manual entry'
input UseChartTime = yes;
input aggregation = AggregationPeriod.FIFTEEN_MIN;
#//Data
def na = Double.NaN;
def today = if ShowTodayOnly == 0 or GetDay() == GetLastDay() then 1 else 0;
#--------------
DefineGlobalColor("up"  , CreateColor(49, 121, 245));
DefineGlobalColor("dn"  , CreateColor(194, 24, 91));
DefineGlobalColor("mid" , CreateColor(244, 143, 177));
DefineGlobalColor("bgUP" , CreateColor(3.9, 32.5, 81.2));
DefineGlobalColor("bgDN" , CreateColor(49.4, 6.3, 23.1));

def L;
def L1;
def L2;
def H;
def H1;
def H2;
def C;
def C1;
def C2;
def C3;
def O;
def O1;
if UseChartTime
then {
    L  = low;
    L1 = low[1];
    L2 = low[2];
    H  = high;
    H1 = high[1];
    H2 = high[2];
    C  = close;
    C1 = close[1];
    C2 = close[2];
    C3 = close[3];
    O  = open;
    O1 = open[1];
} else {
    L  = low(period = aggregation);
    L1 = low(period = aggregation)[1];
    L2 = low(period = aggregation)[2];
    H  = high(period = aggregation);
    H1 = high(period = aggregation)[1];
    H2 = high(period = aggregation)[2];
    C  = close(period = aggregation);
    C1 = close(period = aggregation)[1];
    C2 = close(period = aggregation)[2];
    C3 = close(period = aggregation)[3];
    O  = open(period = aggregation);
    O1 = open(period = aggregation)[1];
}
#f_isUpCandle(_index) =>
script f_isUpCandle {
    input _index = 0;
    def f_isUpCandle = open[_index] <= close[_index];
    plot return = f_isUpCandle;
}

def isUpCandle = C1 > O1;
def isDnCandle = L2 > H;
def n = BarNumber();
def atrValue = ATR(Length=atrLength);
def priceDiff = H1 - L1;

def delta_per = (C1 - O1) / O1 * 100;
def threshold = TotalSum(AbsValue(delta_per)) / n * 2;

def bullCondition = if thresholdType == thresholdType.ATR then
                    (C3 >= L2 and C1>= C2 and L>H2) else
                    if thresholdType == thresholdType.Auto then
                    (L > H2 and C1 > H2) else L > H2;

def bearCondition = if thresholdType == thresholdType.ATR then
                    (C3 <= H2 and C1 <= C2 and H < L2) else
                    if thresholdType == thresholdType.Auto then
                    (H < L2  and C1<L2) else H < L2;

def midCandlVolatCond = if thresholdType == thresholdType.ATR then
                           priceDiff > atrValue*atrMultiplier else
                        if thresholdType == thresholdType.Auto then
                       (if bullCondition then delta_per > threshold else - delta_per > threshold) else
                       (if bullCondition then AbsValue((H2 - L) / H2) > ManualThreshold / 100 else
                           AbsValue((H - L2) / L2) > ManualThreshold / 100) ;

def isGapClosed = if f_isUpCandle(1) then H2 >= L else L2 <= H;

def isFVGup = if isNaN(isFVGup[1]) then 0 else
              bullCondition and !isGapClosed and midCandlVolatCond and
             (if thresholdType == thresholdType.ATR then !isFVGup[1] else 1);

def isFVGdn = if isNaN(isFVGdn[1]) then 0 else
             bearCondition and !isGapClosed and midCandlVolatCond and
             (if thresholdType == thresholdType.ATR then !isFVGdn[1] else 1);
def topup;
def bottomup;
def isUpCandleup;
def midup;
def topdn;
def bottomdn;
def isUpCandledn;
def middn;

if isFVGup {
    isUpCandleup = f_isUpCandle(1);
    topup        = if isUpCandle then L else L2;
    bottomup     = if isUpCandle then H2 else H;
    midup        = (topup + bottomup) / 2;
} else {
    isUpCandleup = isUpCandleup[1];
    topup        = topup[1];
    bottomup     = bottomup[1];
    midup        = midup[1];
}
if isFVGdn
{
    isUpCandledn = f_isUpCandle(1);
    topdn        = if isUpCandle then L else L2;
    bottomdn     = if isUpCandle then H2 else H;
    middn        = (topdn + bottomdn) / 2;
} else {
    isUpCandledn = isUpCandledn[1];
    topdn        = topdn[1];
    bottomdn     = bottomdn[1];
    middn        = middn[1];
}
def hasClosed = (O <= bottomdn and H > bottomdn and H >= topdn or O >= topup and L < topup and L <= bottomup);

def BgColorup = isUpCandleup;
def BgColordn = topdn;
#--------------------------------------------------------------
def TopLineup  = if today then topup else na;
plot MidLineup = if IsNaN(close) then na else if today then midup else na;
def BotLineup  = if today then bottomup else na;


MidLineup.AssignValueColor( if BgColorup then Color.LIME else GlobalColor("bgdn"));
MidLineup.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLineup.SetHiding(!ShowMidLine);

#----------------------------------------------------------------
def TopLinedn  = if today then topdn else na;
plot MidLinedn = if IsNaN(close) then na else if today then middn else na;
def BotLinedn  = if today then bottomdn else na;

MidLinedn.AssignValueColor( if !BgColorup then GlobalColor("bgUP") else Color.PINK);
MidLinedn.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLinedn.SetHiding(!ShowMidLine);

#------ Cloud
AddCloud(TopLineup[-1], BotLineup[-1], CreateColor(4, 181, 4), CreateColor(184, 6, 6));
AddCloud(BotLinedn[-1], TopLinedn[-1], CreateColor(4, 181, 4), CreateColor(184, 6, 6));
#------ Bar Color
AssignPriceColor( if ColorFVGBar and topup<>topup[-1] then Color.BLUE else Color.CURRENT);
AssignPriceColor( if ColorFVGBar and topdn<>topdn[-1] then color.PINK else Color.CURRENT);


##### END #######
Thank you.This is incredible. Is it possible to add ATR lines above and below the zones?Something like the ATR trailing stop in TOS that would use the top of the zone as the reference point for the ATR line above and the bottom of the zone as the reference point for the ATR line below.

While the lines are moving it wouldn't be any help.
After the candle close and the zone is set is where the ATR lines would be of great help on lower time frames.
 
Last edited by a moderator:
Once the FVG is filled, you should (I highly suggest) extend a line from the mid of the FVG forward. They often repeat and use the mid of the high/low or the ohcl4. We use it in futures and it tracks very well (we also compare to Gamma gaps where Gamma flips from positive to negative on the upper price chart). Gaps often repeat due to the algo trading systems out there. Try it and see.
 
Thank you.This is incredible. Is it possible to add ATR lines above and below the zones?Something like the ATR trailing stop in TOS that would use the top of the zone as the reference point for the ATR line above and the bottom of the zone as the reference point for the ATR line below.

While the lines are moving it wouldn't be any help.
After the candle close and the zone is set is where the ATR lines would be of great help on lower time frames.
I was wandering how difficult would it be to stop the cloud once price comes back and fill the gap. it could print the midline along with the cloud and then stop once price gets to the midline or fill entirely.
 
* Edited because this was moved and there's no longer a title. Just wondering if this script can be edited for the following interests...First, i'd like both bullish and bearish fvg's to plot light gray, as well as extend all the way across the screen (to the right) so that I don't have to look left for the imbalances. And second, if at all possible, i'd like them to disappear once they're trading back into. (If not 100% traded back through, leave what remains of them...) *Pic at the bottom for reference. The script is visible to the right of price action about an inch or two but im stretching my own fvg areas to the following day so that I can always see where my imbalances are. I'm curious because i'd rather not have to look back at what i've missed overnight and have to spend an hour manually adding them..

https://usethinkscript.com/threads/...ish-levels-for-thinkorswim.14500/#post-108071
Code:
# All-in-One FVG indicator inspired from "ICT Fair Value Gap [LM]" and "Fair Value Gap [LUX]"
# Created by Sam4Cok@Samer800 - 09/2022
#// atr settings
input ShowTodayOnly = yes;
input ShowMidLine = yes;
input ColorFVGBar = no;
input thresholdType = {default ATR, Auto, Manual};
input atrLength = 28; # 'ATR MA length'
input atrMultiplier = 1.5; # 'ATR multiplier'
input ManualThreshold = 0.05; # 'Percentage for Manual entry'
input UseChartTime = yes;
input aggregation = AggregationPeriod.FIFTEEN_MIN;
#//Data
def na = Double.NaN;
def today = if ShowTodayOnly == 0 or GetDay() == GetLastDay() then 1 else 0;
#--------------
DefineGlobalColor("up" , CreateColor(49, 121, 245));
DefineGlobalColor("dn" , CreateColor(194, 24, 91));
DefineGlobalColor("mid" , CreateColor(244, 143, 177));
DefineGlobalColor("bgUP" , CreateColor(3.9, 32.5, 81.2));
DefineGlobalColor("bgDN" , CreateColor(49.4, 6.3, 23.1));

def L;
def L1;
def L2;
def H;
def H1;
def H2;
def C;
def C1;
def C2;
def C3;
def O;
def O1;
if UseChartTime
then {
L = low;
L1 = low[1];
L2 = low[2];
H = high;
H1 = high[1];
H2 = high[2];
C = close;
C1 = close[1];
C2 = close[2];
C3 = close[3];
O = open;
O1 = open[1];
} else {
L = low(period = aggregation);
L1 = low(period = aggregation)[1];
L2 = low(period = aggregation)[2];
H = high(period = aggregation);
H1 = high(period = aggregation)[1];
H2 = high(period = aggregation)[2];
C = close(period = aggregation);
C1 = close(period = aggregation)[1];
C2 = close(period = aggregation)[2];
C3 = close(period = aggregation)[3];
O = open(period = aggregation);
O1 = open(period = aggregation)[1];
}
#f_isUpCandle(_index) =>
script f_isUpCandle {
input _index = 0;
def f_isUpCandle = open[_index] <= close[_index];
plot return = f_isUpCandle;
}

def isUpCandle = C1 > O1;
def isDnCandle = L2 > H;
def n = BarNumber();
def atrValue = ATR(Length=atrLength);
def priceDiff = H1 - L1;

def delta_per = (C1 - O1) / O1 * 100;
def threshold = TotalSum(AbsValue(delta_per)) / n * 2;

def bullCondition = if thresholdType == thresholdType.ATR then
(C3 >= L2 and C1>= C2 and L>H2) else
if thresholdType == thresholdType.Auto then
(L > H2 and C1 > H2) else L > H2;

def bearCondition = if thresholdType == thresholdType.ATR then
(C3 <= H2 and C1 <= C2 and H < L2) else
if thresholdType == thresholdType.Auto then
(H < L2 and C1<L2) else H < L2;

def midCandlVolatCond = if thresholdType == thresholdType.ATR then
priceDiff > atrValue*atrMultiplier else
if thresholdType == thresholdType.Auto then
(if bullCondition then delta_per > threshold else - delta_per > threshold) else
(if bullCondition then AbsValue((H2 - L) / H2) > ManualThreshold / 100 else
AbsValue((H - L2) / L2) > ManualThreshold / 100) ;

def isGapClosed = if f_isUpCandle(1) then H2 >= L else L2 <= H;

def isFVGup = if isNaN(isFVGup[1]) then 0 else
bullCondition and !isGapClosed and midCandlVolatCond and
(if thresholdType == thresholdType.ATR then !isFVGup[1] else 1);

def isFVGdn = if isNaN(isFVGdn[1]) then 0 else
bearCondition and !isGapClosed and midCandlVolatCond and
(if thresholdType == thresholdType.ATR then !isFVGdn[1] else 1);
def topup;
def bottomup;
def isUpCandleup;
def midup;
def topdn;
def bottomdn;
def isUpCandledn;
def middn;

if isFVGup {
isUpCandleup = f_isUpCandle(1);
topup = if isUpCandle then L else L2;
bottomup = if isUpCandle then H2 else H;
midup = (topup + bottomup) / 2;
} else {
isUpCandleup = isUpCandleup[1];
topup = topup[1];
bottomup = bottomup[1];
midup = midup[1];
}
if isFVGdn
{
isUpCandledn = f_isUpCandle(1);
topdn = if isUpCandle then L else L2;
bottomdn = if isUpCandle then H2 else H;
middn = (topdn + bottomdn) / 2;
} else {
isUpCandledn = isUpCandledn[1];
topdn = topdn[1];
bottomdn = bottomdn[1];
middn = middn[1];
}
def hasClosed = (O <= bottomdn and H > bottomdn and H >= topdn or O >= topup and L < topup and L <= bottomup);

def BgColorup = isUpCandleup;
def BgColordn = topdn;
#--------------------------------------------------------------
def TopLineup = if today then topup else na;
plot MidLineup = if IsNaN(close) then na else if today then midup else na;
def BotLineup = if today then bottomup else na;


MidLineup.AssignValueColor( if BgColorup then Color.LIME else GlobalColor("bgdn"));
MidLineup.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLineup.SetHiding(!ShowMidLine);

#----------------------------------------------------------------
def TopLinedn = if today then topdn else na;
plot MidLinedn = if IsNaN(close) then na else if today then middn else na;
def BotLinedn = if today then bottomdn else na;

MidLinedn.AssignValueColor( if !BgColorup then GlobalColor("bgUP") else Color.PINK);
MidLinedn.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLinedn.SetHiding(!ShowMidLine);

#------ Cloud
AddCloud(TopLineup[-1], BotLineup[-1], CreateColor(4, 181, 4), CreateColor(184, 6, 6));
AddCloud(BotLinedn[-1], TopLinedn[-1], CreateColor(4, 181, 4), CreateColor(184, 6, 6));
#------ Bar Color
AssignPriceColor( if ColorFVGBar and topup<>topup[-1] then Color.BLUE else Color.CURRENT);
AssignPriceColor( if ColorFVGBar and topdn<>topdn[-1] then color.PINK else Color.CURRENT);
 

Attachments

  • Screenshot (3).png
    Screenshot (3).png
    319.2 KB · Views: 444
Last edited:
I amended the code to add option to disable the line extension and color FVG bar. I couldn't delete earlier lines, however, I added option to show today plots only.

CSS:
# FVG  Fair Value Gaps Bearish & Bullish Levels
# @TradeForOpp  5/6/2022
# Mod by Sam4COK @ Samer800 08/2022
# V 1.1 - Color FVG bar, option to enable/disable line extention + Show Today only option

input ShowHLLines = yes;
input ExtendLines = yes;
input ColorFVGBar = yes;
input ShowCloud   = yes;
input CurrentChartTime = no;
input ShowTodayOnly = yes;
input thresholdPctg = 0.01;
input aggregation = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
script nz {
    input data  = 1;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}

def today = if ShowTodayOnly==0 or getday()==getlastday() then 1 else 0;
#//IMBALANCE
#//Data
def L1; def H3; Def H1; Def L3;
if CurrentChartTime then {
 L1 = low;
 H3 = high[2];
 H1 = high;
 L3 = low[2];} else {
 L1 = low(period = aggregation);
 H3 = high(period = aggregation)[2];
 H1 = high(period = aggregation);
 L3 = low(period = aggregation)[2];
}
def fvgUpPctg = if AbsValue((H3 - L1) / H3) > thresholdPctg / 100 then 1 else 0;
def FVGUp      = if H3 < L1 then 1 else 0;
def plotFVGUH  = if FVGUp and fvgUpPctg then H3 else na;
def plotFVGUL  = if FVGUp and fvgUpPctg then L1 else na;
def plotFVGUH2 =  plotFVGUH[-1];
def plotFVGUL2 = plotFVGUL[-1];
def midFVGUp   = (plotFVGUH2 + plotFVGUL2) / 2;

def fvgDnPctg = if AbsValue((H1 - L3) / L3) > thresholdPctg / 100 then 1 else 0;
def FVGDown = if L3 > H1 then 1 else 0;
def plotFVGDL = if FVGDown and fvgDnPctg then L3 else na;
def plotFVGDH = if FVGDown and fvgDnPctg then H1 else na;
def plotFVGDL2 = plotFVGDL[-1];
def plotFVGDH2 = plotFVGDH[-1];
def midFVGDown = (plotFVGDL2 + plotFVGDH2) / 2;

#--------------------------------------------------------------
def LastFVGUH = CompoundValue(1, if isNaN(plotFVGUH2) then LastFVGUH[1] else plotFVGUH2, H3);
def LastFVGUL = CompoundValue(1, if isNan(plotFVGUL2) then LastFVGUL[1] else plotFVGUL2, L1);
def LastMidUP = CompoundValue(1, if isNan(midFVGUp) then LastMidUP[1] else midFVGUp, midFVGUp);
#--------------------------------------------------------------
plot HLine = if today then if ExtendLines then LastFVGUH else plotFVGUH2 else na;
plot LLine = if today then if ExtendLines then LastFVGUL else plotFVGUL2 else na;
plot MLine = if today then if ExtendLines then LastMidUP else midFVGUp else na;
HLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HLine.SetDefaultColor(CreateColor(49,121,245));
HLine.SetHiding(!ShowHLLines);
LLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LLine.SetDefaultColor(CreateColor(49,121,245));
LLine.SetHiding(!ShowHLLines);
MLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
MLine.SetDefaultColor(CreateColor(144,191,249));

AddCloud(if ShowCloud and LLine==LLine[1] or LLine==LLine[-1] then LLine else na ,HLine, CreateColor(144,191,249));
AssignPriceColor( if ColorFVGBar and LLine<>LLine[1] then Color.BLUE else Color.CURRENT);

#--------------------------------------------------------------
def LastFVGDL = CompoundValue(1, if isNaN(plotFVGDL2) then LastFVGDL[1] else plotFVGDL2, L3);
def LastFVGDH = CompoundValue(1, if isNan(plotFVGDH2) then LastFVGDH[1] else plotFVGDH2, H1);
def LastMidDN = CompoundValue(1, if isNan(midFVGDown) then LastMidDN[1] else midFVGDown, midFVGDown);
#--------------------------------------------------------------
plot HLineL = if today then if ExtendLines then LastFVGDL else plotFVGDL2 else na;
plot LLineL = if today then if ExtendLines then LastFVGDH else plotFVGDH2 else na;
plot MLineL = if today then if ExtendLines then LastMidDN else midFVGDown else na;
HLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HLineL.SetDefaultColor(CreateColor(194,24,91));
HLineL.SetHiding(!ShowHLLines);
LLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LLineL.SetDefaultColor(CreateColor(194,24,91));
LLineL.SetHiding(!ShowHLLines);
MLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
MLineL.SetDefaultColor(CreateColor(244,143,177));

AddCloud(if ShowCloud and (HLineL == HLineL[1] or HLineL == HLineL[-1]) then HLineL else na ,LLineL, CreateColor(244,143,177));
AssignPriceColor( if ColorFVGBar and HLineL<>HLineL[1] then color.PINK else Color.CURRENT);

##### END #######
@samer800 Thank you for this indicator! Can you please add an alert when a candle crosses and closes below or above a FVG? Like an alert when IFVG happens. I would appreciate it very much!
 
Here i made changed to the code and added 2 more time frames and the option to turn the background on or off.
1755201552755.png



Code:
declare upper;

input ShowTodayOnly = no;
input ShowMidLine = yes;
input ColorFVGBar = no;
input thresholdType = {default Manual, ATR, Auto};
input atrLength = 28;
input atrMultiplier = 1.5;
input ManualThreshold = 0.15;
input UseChartTime = no;

input aggregation1 = AggregationPeriod.HOUR;
input aggregation2 = AggregationPeriod.DAY;
input aggregation3 = AggregationPeriod.WEEK;

input ShowBackgrounds = yes;
## Removed: input ShowLines = yes;

def na = Double.NaN;

def today = if ShowTodayOnly == no or GetDay() == GetLastDay() then 1 else 0;

DefineGlobalColor("bgUP", CreateColor(3, 32, 81));
DefineGlobalColor("bgDN", CreateColor(49, 6, 23));

# === Helper bullish candle detection for each timeframe ===
def isUpCandle_1 = open(period = aggregation1)[1] <= close(period = aggregation1)[1];
def isUpCandle_2 = open(period = aggregation2)[1] <= close(period = aggregation2)[1];
def isUpCandle_3 = open(period = aggregation3)[1] <= close(period = aggregation3)[1];

# === FVG logic for aggregation 1 ===
def o1_1 = open(period = aggregation1)[1];
def c1_1 = close(period = aggregation1)[1];
def c2_1 = close(period = aggregation1)[2];
def c3_1 = close(period = aggregation1)[3];
def l1_1 = low(period = aggregation1)[1];
def l2_1 = low(period = aggregation1)[2];
def h1_1 = high(period = aggregation1)[1];
def h2_1 = high(period = aggregation1)[2];
def o_1 = open(period = aggregation1);
def c_1 = close(period = aggregation1);
def h_1 = high(period = aggregation1);
def l_1 = low(period = aggregation1);
def atr_1 = MovingAverage(AverageType.SIMPLE, TrueRange(h_1, c_1, l_1), atrLength);
def priceDiff_1 = h1_1 - l1_1;
def delta_1 = (c1_1 - o1_1) / o1_1 * 100;

def bullCond_1 = if thresholdType == thresholdType.ATR then (c3_1 >= l2_1 and c1_1 >= c2_1 and l_1 > h2_1) else
    if thresholdType == thresholdType.Auto then (l_1 > h2_1 and c1_1 > h2_1) else (l_1 > h2_1);

def bearCond_1 = if thresholdType == thresholdType.ATR then (c3_1 <= h2_1 and c1_1 <= c2_1 and h_1 < l2_1) else
    if thresholdType == thresholdType.Auto then (h_1 < l2_1 and c1_1 < l2_1) else (h_1 < l2_1);

def volatile_1 = if thresholdType == thresholdType.ATR then (priceDiff_1 > atr_1 * atrMultiplier) else
    if thresholdType == thresholdType.Auto then (if bullCond_1 then delta_1 > 2 else -delta_1 > 2) else
    (if bullCond_1 then AbsValue((h2_1 - l_1) / h2_1) > ManualThreshold / 100 else AbsValue((h_1 - l2_1) / l2_1) > ManualThreshold / 100);

def gapClosed_1 = if isUpCandle_1 then (h2_1 >= l_1) else (l2_1 <= h_1);

def isFVGup_1 = bullCond_1 and !gapClosed_1 and volatile_1;
def isFVGdn_1 = bearCond_1 and !gapClosed_1 and volatile_1;

def topup_1 = if isFVGup_1 then (if isUpCandle_1 then l_1 else l2_1) else topup_1[1];
def botup_1 = if isFVGup_1 then (if isUpCandle_1 then h2_1 else h_1) else botup_1[1];
def topdn_1 = if isFVGdn_1 then (if isUpCandle_1 then l_1 else l2_1) else topdn_1[1];
def botdn_1 = if isFVGdn_1 then (if isUpCandle_1 then h2_1 else h_1) else botdn_1[1];

plot TopLineUp_1 = if today then topup_1 else Double.NaN;
plot BotLineUp_1 = if today then botup_1 else Double.NaN;
plot TopLineDn_1 = if today then topdn_1 else Double.NaN;
plot BotLineDn_1 = if today then botdn_1 else Double.NaN;

plot MidLineup_1 = if ShowMidLine and today then (topup_1 + botup_1) / 2 else Double.NaN;
plot MidLinedn_1 = if ShowMidLine and today then (topdn_1 + botdn_1) / 2 else Double.NaN;

MidLineup_1.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLineup_1.AssignValueColor(Color.WHITE);
MidLineup_1.SetLineWeight(1);

MidLinedn_1.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLinedn_1.AssignValueColor(Color.WHITE);
MidLinedn_1.SetLineWeight(1);

TopLineUp_1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TopLineUp_1.SetLineWeight(2);
TopLineUp_1.SetDefaultColor(CreateColor(4, 181, 4));

BotLineUp_1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BotLineUp_1.SetLineWeight(2);
BotLineUp_1.SetDefaultColor(CreateColor(4, 181, 4));

TopLineDn_1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TopLineDn_1.SetLineWeight(2);
TopLineDn_1.SetDefaultColor(CreateColor(184, 6, 6));

BotLineDn_1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BotLineDn_1.SetLineWeight(2);
BotLineDn_1.SetDefaultColor(CreateColor(184, 6, 6));

AddCloud(
    if ShowBackgrounds then TopLineUp_1[1] else Double.NaN,
    if ShowBackgrounds then BotLineUp_1[1] else Double.NaN,
    CreateColor(4, 181, 4),
    CreateColor(184, 6, 6)
);

AddCloud(
    if ShowBackgrounds then BotLineDn_1[1] else Double.NaN,
    if ShowBackgrounds then TopLineDn_1[1] else Double.NaN,
    CreateColor(184, 6, 6),
    CreateColor(4, 181, 4)
);

# === FVG logic for aggregation 2 ===

def o1_2 = open(period = aggregation2)[1];
def c1_2 = close(period = aggregation2)[1];
def c2_2 = close(period = aggregation2)[2];
def c3_2 = close(period = aggregation2)[3];
def l1_2 = low(period = aggregation2)[1];
def l2_2 = low(period = aggregation2)[2];
def h1_2 = high(period = aggregation2)[1];
def h2_2 = high(period = aggregation2)[2];
def o_2 = open(period = aggregation2);
def c_2 = close(period = aggregation2);
def h_2 = high(period = aggregation2);
def l_2 = low(period = aggregation2);
def atr_2 = MovingAverage(AverageType.SIMPLE, TrueRange(h_2, c_2, l_2), atrLength);
def priceDiff_2 = h1_2 - l1_2;
def delta_2 = (c1_2 - o1_2) / o1_2 * 100;

def bullCond_2 = if thresholdType == thresholdType.ATR then (c3_2 >= l2_2 and c1_2 >= c2_2 and l_2 > h2_2) else
    if thresholdType == thresholdType.Auto then (l_2 > h2_2 and c1_2 > h2_2) else (l_2 > h2_2);

def bearCond_2 = if thresholdType == thresholdType.ATR then (c3_2 <= h2_2 and c1_2 <= c2_2 and h_2 < l2_2) else
    if thresholdType == thresholdType.Auto then (h_2 < l2_2 and c1_2 < l2_2) else (h_2 < l2_2);

def volatile_2 = if thresholdType == thresholdType.ATR then (priceDiff_2 > atr_2 * atrMultiplier) else
    if thresholdType == thresholdType.Auto then (if bullCond_2 then delta_2 > 2 else -delta_2 > 2) else
    (if bullCond_2 then AbsValue((h2_2 - l_2) / h2_2) > ManualThreshold / 100 else AbsValue((h_2 - l2_2) / l2_2) > ManualThreshold / 100);

def gapClosed_2 = if isUpCandle_2 then (h2_2 >= l_2) else (l2_2 <= h_2);

def isFVGup_2 = bullCond_2 and !gapClosed_2 and volatile_2;
def isFVGdn_2 = bearCond_2 and !gapClosed_2 and volatile_2;

def topup_2 = if isFVGup_2 then (if isUpCandle_2 then l_2 else l2_2) else topup_2[1];
def botup_2 = if isFVGup_2 then (if isUpCandle_2 then h2_2 else h_2) else botup_2[1];
def topdn_2 = if isFVGdn_2 then (if isUpCandle_2 then l_2 else l2_2) else topdn_2[1];
def botdn_2 = if isFVGdn_2 then (if isUpCandle_2 then h2_2 else h_2) else botdn_2[1];

plot TopLineUp_2 = if today then topup_2 else Double.NaN;
plot BotLineUp_2 = if today then botup_2 else Double.NaN;
plot TopLineDn_2 = if today then topdn_2 else Double.NaN;
plot BotLineDn_2 = if today then botdn_2 else Double.NaN;

plot MidLineup_2 = if ShowMidLine and today then (topup_2 + botup_2) / 2 else Double.NaN;
plot MidLinedn_2 = if ShowMidLine and today then (topdn_2 + botdn_2) / 2 else Double.NaN;

MidLineup_2.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLineup_2.AssignValueColor(Color.WHITE);
MidLineup_2.SetLineWeight(1);

MidLinedn_2.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLinedn_2.AssignValueColor(Color.WHITE);
MidLinedn_2.SetLineWeight(1);

TopLineUp_2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TopLineUp_2.SetLineWeight(2);
TopLineUp_2.SetDefaultColor(CreateColor(0, 255, 255));

BotLineUp_2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BotLineUp_2.SetLineWeight(2);
BotLineUp_2.SetDefaultColor(CreateColor(0, 255, 255));

TopLineDn_2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TopLineDn_2.SetLineWeight(2);
TopLineDn_2.SetDefaultColor(CreateColor(255, 0, 255));

BotLineDn_2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BotLineDn_2.SetLineWeight(2);
BotLineDn_2.SetDefaultColor(CreateColor(255, 0, 255));

AddCloud(
    if ShowBackgrounds then TopLineUp_2[1] else Double.NaN,
    if ShowBackgrounds then BotLineUp_2[1] else Double.NaN,
    CreateColor(0, 255, 255),
    CreateColor(255, 0, 255)
);

AddCloud(
    if ShowBackgrounds then BotLineDn_2[1] else Double.NaN,
    if ShowBackgrounds then TopLineDn_2[1] else Double.NaN,
    CreateColor(255, 0, 255),
    CreateColor(0, 255, 255)
);

# === FVG logic for aggregation 3 ===

def o1_3 = open(period = aggregation3)[1];
def c1_3 = close(period = aggregation3)[1];
def c2_3 = close(period = aggregation3)[2];
def c3_3 = close(period = aggregation3)[3];
def l1_3 = low(period = aggregation3)[1];
def l2_3 = low(period = aggregation3)[2];
def h1_3 = high(period = aggregation3)[1];
def h2_3 = high(period = aggregation3)[2];
def o_3 = open(period = aggregation3);
def c_3 = close(period = aggregation3);
def h_3 = high(period = aggregation3);
def l_3 = low(period = aggregation3);
def atr_3 = MovingAverage(AverageType.SIMPLE, TrueRange(h_3, c_3, l_3), atrLength);
def priceDiff_3 = h1_3 - l1_3;
def delta_3 = (c1_3 - o1_3) / o1_3 * 100;

def bullCond_3 = if thresholdType == thresholdType.ATR then (c3_3 >= l2_3 and c1_3 >= c2_3 and l_3 > h2_3) else
    if thresholdType == thresholdType.Auto then (l_3 > h2_3 and c1_3 > h2_3) else (l_3 > h2_3);

def bearCond_3 = if thresholdType == thresholdType.ATR then (c3_3 <= h2_3 and c1_3 <= c2_3 and h_3 < l2_3) else
    if thresholdType == thresholdType.Auto then (h_3 < l2_3 and c1_3 < l2_3) else (h_3 < l2_3);

def volatile_3 = if thresholdType == thresholdType.ATR then (priceDiff_3 > atr_3 * atrMultiplier) else
    if thresholdType == thresholdType.Auto then (if bullCond_3 then delta_3 > 2 else -delta_3 > 2) else
    (if bullCond_3 then AbsValue((h2_3 - l_3) / h2_3) > ManualThreshold / 100 else AbsValue((h_3 - l2_3) / l2_3) > ManualThreshold / 100);

def gapClosed_3 = if isUpCandle_3 then (h2_3 >= l_3) else (l2_3 <= h_3);

def isFVGup_3 = bullCond_3 and !gapClosed_3 and volatile_3;
def isFVGdn_3 = bearCond_3 and !gapClosed_3 and volatile_3;

def topup_3 = if isFVGup_3 then (if isUpCandle_3 then l_3 else l2_3) else topup_3[1];
def botup_3 = if isFVGup_3 then (if isUpCandle_3 then h2_3 else h_3) else botup_3[1];
def topdn_3 = if isFVGdn_3 then (if isUpCandle_3 then l_3 else l2_3) else topdn_3[1];
def botdn_3 = if isFVGdn_3 then (if isUpCandle_3 then h2_3 else h_3) else botdn_3[1];

plot TopLineUp_3 = if today then topup_3 else Double.NaN;
plot BotLineUp_3 = if today then botup_3 else Double.NaN;
plot TopLineDn_3 = if today then topdn_3 else Double.NaN;
plot BotLineDn_3 = if today then botdn_3 else Double.NaN;

plot MidLineup_3 = if ShowMidLine and today then (topup_3 + botup_3) / 2 else Double.NaN;
plot MidLinedn_3 = if ShowMidLine and today then (topdn_3 + botdn_3) / 2 else Double.NaN;

MidLineup_3.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLineup_3.AssignValueColor(Color.WHITE);
MidLineup_3.SetLineWeight(1);

MidLinedn_3.SetPaintingStrategy(PaintingStrategy.DASHES);
MidLinedn_3.AssignValueColor(Color.WHITE);
MidLinedn_3.SetLineWeight(1);

TopLineUp_3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TopLineUp_3.SetLineWeight(2);
TopLineUp_3.SetDefaultColor(CreateColor(255, 255, 0));

BotLineUp_3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BotLineUp_3.SetLineWeight(2);
BotLineUp_3.SetDefaultColor(CreateColor(255, 255, 0));

TopLineDn_3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TopLineDn_3.SetLineWeight(2);
TopLineDn_3.SetDefaultColor(CreateColor(255, 165, 0));

BotLineDn_3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
BotLineDn_3.SetLineWeight(2);
BotLineDn_3.SetDefaultColor(CreateColor(255, 165, 0));

AddCloud(
    if ShowBackgrounds then TopLineUp_3[1] else Double.NaN,
    if ShowBackgrounds then BotLineUp_3[1] else Double.NaN,
    CreateColor(255, 255, 0),
    CreateColor(255, 165, 0)
);

AddCloud(
    if ShowBackgrounds then BotLineDn_3[1] else Double.NaN,
    if ShowBackgrounds then TopLineDn_3[1] else Double.NaN,
    CreateColor(255, 165, 0),
    CreateColor(255, 255, 0)
);
 
I amended the code to add option to disable the line extension and color FVG bar. I couldn't delete earlier lines, however, I added option to show today plots only.

CSS:
# FVG  Fair Value Gaps Bearish & Bullish Levels
# @TradeForOpp  5/6/2022
# Mod by Sam4COK @ Samer800 08/2022
# V 1.1 - Color FVG bar, option to enable/disable line extention + Show Today only option

input ShowHLLines = yes;
input ExtendLines = yes;
input ColorFVGBar = yes;
input ShowCloud   = yes;
input CurrentChartTime = no;
input ShowTodayOnly = yes;
input thresholdPctg = 0.01;
input aggregation = AggregationPeriod.FIFTEEN_MIN;

def na = Double.NaN;
script nz {
    input data  = 1;
    input repl  = 0;
    def ret_val = if IsNaN(data) then repl else data;
    plot return = ret_val;
}

def today = if ShowTodayOnly==0 or getday()==getlastday() then 1 else 0;
#//IMBALANCE
#//Data
def L1; def H3; Def H1; Def L3;
if CurrentChartTime then {
 L1 = low;
 H3 = high[2];
 H1 = high;
 L3 = low[2];} else {
 L1 = low(period = aggregation);
 H3 = high(period = aggregation)[2];
 H1 = high(period = aggregation);
 L3 = low(period = aggregation)[2];
}
def fvgUpPctg = if AbsValue((H3 - L1) / H3) > thresholdPctg / 100 then 1 else 0;
def FVGUp      = if H3 < L1 then 1 else 0;
def plotFVGUH  = if FVGUp and fvgUpPctg then H3 else na;
def plotFVGUL  = if FVGUp and fvgUpPctg then L1 else na;
def plotFVGUH2 =  plotFVGUH[-1];
def plotFVGUL2 = plotFVGUL[-1];
def midFVGUp   = (plotFVGUH2 + plotFVGUL2) / 2;

def fvgDnPctg = if AbsValue((H1 - L3) / L3) > thresholdPctg / 100 then 1 else 0;
def FVGDown = if L3 > H1 then 1 else 0;
def plotFVGDL = if FVGDown and fvgDnPctg then L3 else na;
def plotFVGDH = if FVGDown and fvgDnPctg then H1 else na;
def plotFVGDL2 = plotFVGDL[-1];
def plotFVGDH2 = plotFVGDH[-1];
def midFVGDown = (plotFVGDL2 + plotFVGDH2) / 2;

#--------------------------------------------------------------
def LastFVGUH = CompoundValue(1, if isNaN(plotFVGUH2) then LastFVGUH[1] else plotFVGUH2, H3);
def LastFVGUL = CompoundValue(1, if isNan(plotFVGUL2) then LastFVGUL[1] else plotFVGUL2, L1);
def LastMidUP = CompoundValue(1, if isNan(midFVGUp) then LastMidUP[1] else midFVGUp, midFVGUp);
#--------------------------------------------------------------
plot HLine = if today then if ExtendLines then LastFVGUH else plotFVGUH2 else na;
plot LLine = if today then if ExtendLines then LastFVGUL else plotFVGUL2 else na;
plot MLine = if today then if ExtendLines then LastMidUP else midFVGUp else na;
HLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HLine.SetDefaultColor(CreateColor(49,121,245));
HLine.SetHiding(!ShowHLLines);
LLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LLine.SetDefaultColor(CreateColor(49,121,245));
LLine.SetHiding(!ShowHLLines);
MLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
MLine.SetDefaultColor(CreateColor(144,191,249));

AddCloud(if ShowCloud and LLine==LLine[1] or LLine==LLine[-1] then LLine else na ,HLine, CreateColor(144,191,249));
AssignPriceColor( if ColorFVGBar and LLine<>LLine[1] then Color.BLUE else Color.CURRENT);

#--------------------------------------------------------------
def LastFVGDL = CompoundValue(1, if isNaN(plotFVGDL2) then LastFVGDL[1] else plotFVGDL2, L3);
def LastFVGDH = CompoundValue(1, if isNan(plotFVGDH2) then LastFVGDH[1] else plotFVGDH2, H1);
def LastMidDN = CompoundValue(1, if isNan(midFVGDown) then LastMidDN[1] else midFVGDown, midFVGDown);
#--------------------------------------------------------------
plot HLineL = if today then if ExtendLines then LastFVGDL else plotFVGDL2 else na;
plot LLineL = if today then if ExtendLines then LastFVGDH else plotFVGDH2 else na;
plot MLineL = if today then if ExtendLines then LastMidDN else midFVGDown else na;
HLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HLineL.SetDefaultColor(CreateColor(194,24,91));
HLineL.SetHiding(!ShowHLLines);
LLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LLineL.SetDefaultColor(CreateColor(194,24,91));
LLineL.SetHiding(!ShowHLLines);
MLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
MLineL.SetDefaultColor(CreateColor(244,143,177));

AddCloud(if ShowCloud and (HLineL == HLineL[1] or HLineL == HLineL[-1]) then HLineL else na ,LLineL, CreateColor(244,143,177));
AssignPriceColor( if ColorFVGBar and HLineL<>HLineL[1] then color.PINK else Color.CURRENT);

##### END #######
This is a great script! is there a way to make the fvg stop plotting once price goes back into it at least 50% of the fvg since its not relevant anymore.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
1148 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top