Repaints FVG Fair Value Gaps Bearish & Bullish Levels For ThinkOrSwim

Repaints

samer800

Conversion Expert
VIP
Lifetime
Can you extend all the unfilled FVG to the right? When the FVG was filled, make the lines disapper?
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.
@MerryDay this may can be in new post as custom indicator.

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 #######
 

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

check the below. Just enable chart time if you don't want aggregation period.

gDsmTgu.png


CSS:
# FVG  Fair Value Gaps Bearish & Bullish Levels
# @TradeForOpp  5/6/2022
#Mod by Sam4COK @ Samer800 08/2022

input ShowLines = yes;
input ShowCloud = yes;
input ChartTime = no;
input threshold = 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;
}
#//IMBALANCE
#//Data
def L1; def H3; Def H1; Def L3;
if ChartTime 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 fvgBullPctg = if AbsValue((H3 - L1) / H3) > threshold / 100 then 1 else 0;
def FVGUp     = if H3 < L1 then 1 else 0;
def plotFVGU  = if FVGUp and fvgBullPctg then H3 else na;
def plotFVGUL = if FVGUp and fvgBullPctg then L1 else na;
def plotFVGU2 =  plotFVGU[-1];
def plotFVGUL2 = plotFVGUL[-1];
def midFVGUp = (plotFVGU2 + plotFVGUL2) / 2;

def fvgBearPctg = if AbsValue((H1 - L3) / L3) > threshold / 100 then 1 else 0;
def FVGDown = if L3 > H1 then 1 else 0;
def plotFVGD = if FVGDown and fvgBearPctg then L3 else na;
def plotFVGH = if FVGDown and fvgBearPctg then H1 else na;
def plotFVGD2 = plotFVGD[-1];
def plotFVGH2 = plotFVGH[-1];
def midFVGDown = (plotFVGD2 + plotFVGH2) / 2;

#--------------------------------------------------------------
def LastTF    = CompoundValue(1, if isNaN(plotFVGU2) then LastTF[1] else plotFVGU2, plotFVGU2);
def LastBF    = CompoundValue(1, if isNan(plotFVGUL2) then LastBF[1] else plotFVGUL2, plotFVGUL2);
def LastMidUP = CompoundValue(1, if isNan(midFVGUp) then LastMidUP[1] else midFVGUp, midFVGUp);
#--------------------------------------------------------------

plot HLine =  LastTF ;
plot LLine =  LastBF ;
plot MLine =  LastMidUP ;
HLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HLine.SetDefaultColor(CreateColor(49,121,245));
HLine.SetHiding(!ShowLines);
LLine.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LLine.SetDefaultColor(CreateColor(49,121,245));
LLine.SetHiding(!ShowLines);
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));

#--------------------------------------------------------------
def LastTFL   = CompoundValue(1, if isNaN(plotFVGD2) then LastTFL[1] else plotFVGD2, plotFVGD2);
def LastBFL   = CompoundValue(1, if isNan(plotFVGH2) then LastBFL[1] else plotFVGH2, plotFVGH2);
def LastMidDN = CompoundValue(1, if isNan(midFVGDown) then LastMidDN[1] else midFVGDown, midFVGDown);
#--------------------------------------------------------------

plot HLineL =  LastTFL ;
plot LLineL =  LastBFL ;
plot MLineL =  LastMidDN;
HLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HLineL.SetDefaultColor(CreateColor(194,24,91));
HLineL.SetHiding(!ShowLines);
LLineL.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LLineL.SetDefaultColor(CreateColor(194,24,91));
LLineL.SetHiding(!ShowLines);
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));
##########################
@samer800 I noticed on your chart, that you are using 15mins Agg but used on 5mins timeframe. Do w need to change the agg from 15mins to 5mins if I am trading on a 5mins timeframe? What does "Chart Time" mean?
 
@samer800 I noticed on your chart, that you are using 15mins Agg but used on 5mins timeframe. Do w need to change the agg from 15mins to 5mins if I am trading on a 5mins timeframe? What does "Chart Time" mean?
chart time option to disable the MTF and use the chart aggression. you have the option to use the indicator in different time frame or same chart time frame
 
Great code! Is it possible to add a label that shows the next FVG above and below? So for example if the nearest FVGs levels are at 300, 310 and 295 then a label would state that. I've included an image. I hope that helps!

Image
 
chart time option to disable the MTF and use the chart aggression. you have the option to use the indicator in different time frame or same chart time frame
Is it possible to edit the code so that only unfilled gaps are shown and the lines are extended until the gap is filled?
 
Last edited:
Trade for Opportunity on YT
and Twitter has a great Fair Value script for TOS, it displays up to three different time frame gaps. In his video he shows how to use it.

shared study link: tos.mx/itIWwiq
Code:
input threshold = 0.1;
input aggregation1 = AggregationPeriod.FIFTEEN_MIN;
input aggregation2 = AggregationPeriod.TEN_MIN;
input aggregation3 = AggregationPeriod.FIVE_MIN;
input showAggregation2 = yes;
input showAggregation3 = yes;
input rthOnly = no;
input fillGaps = yes;

# Note that aggregation periods below then current chart period will NOT show

def rth = if SecondsTillTime(0930) <= 0 and SecondsTillTime(1600) > 0 then 1 else 0;
def rthCheck = if rthOnly then if rth then yes else no else yes;

# timeframe1

def low1 = low(period = aggregation1);
def high1 = high(period = aggregation1);

def fvgBear1 = if low1[2] - high1 > 0 then 1 else 0;
def fvgBearPctg1 = if AbsValue((high1 -low1[2])/low1[2]) > threshold/100 then 1 else 0;

def fvgBull1 = if low1 - high1[2] > 0 then 1 else 0;
def fvgBullPctg1 = if AbsValue((low1 - high1[2])/high1[2]) > threshold/100 then 1 else 0;

def fvgBearH1 = if fvgBear1 and fvgBearPctg1 then low1[2] else 0;
def fvgBearL1 = if fvgBearH1 then high1 else Double.NaN;

def fvgBullH1 = if fvgBull1 and fvgBullPctg1 then low1 else 0;
def fvgBullL1 = if fvgBullH1 then high1[2] else Double.NaN;

def fvgBear1MemHigh = if fvgBearH1 then fvgBearH1 else if high1 > fvgBear1MemHigh[1] then Double.NaN else fvgBear1MemHigh[1];
def fvgBear1MemLow = if fvgBearH1 then fvgBearL1 else if high1 > fvgBear1MemHigh[1] then Double.NaN else if fillGaps and high1 > fvgBear1MemLow[1] then high1 else fvgBear1MemLow[1];

def fvgBull1MemLow = if fvgBullH1 then fvgBullL1 else if low1 < fvgBull1MemLow[1] then Double.NaN else fvgBull1MemLow[1];
def fvgBull1MemHigh = if fvgBullH1 then fvgBullH1 else if low1 < fvgBull1MemLow[1] then Double.NaN else if fillGaps and low1 < fvgBull1MemHigh[1] then low1 else fvgBull1MemHigh[1];

def fvgBear1MH = if fvgBear1MemHigh then fvgBear1MemHigh else double.nan;
def fvgBear1ML = if fvgBear1MemLow then fvgBear1MemLow else double.nan;

def fvgBull1MH = if fvgBull1MemHigh then fvgBull1MemHigh else double.nan;
def fvgBull1ML = if fvgBull1MemLow then fvgBull1MemLow else double.nan;

# timeframe 2
 
def low2 = low(period = aggregation2);
def high2 = high(period = aggregation2);

def fvgBear2 = if low2[2] - high2 > 0 then 1 else 0;
def fvgBearPctg2 = if AbsValue((high2 -low2[2])/low2[2]) > threshold/100 then 1 else 0;

def fvgBull2 = if low2 - high2[2] > 0 then 1 else 0;
def fvgBullPctg2 = if AbsValue((low2 - high2[2])/high2[2]) > threshold/100 then 1 else 0;

def fvgBearH2 = if fvgBear2 and fvgBearPctg2 then low2[2] else 0;
def fvgBearL2 = if fvgBearH2 then high2 else Double.NaN;

def fvgBullH2 = if fvgBull2 and fvgBullPctg2 then low2 else 0;
def fvgBullL2 = if fvgBullH2 then high2[2] else Double.NaN;

def fvgBear2MemHigh = if fvgBearH2 then fvgBearH2 else if high2 > fvgBear2MemHigh[1] then Double.NaN else fvgBear2MemHigh[1];
def fvgBear2MemLow = if fvgBearH2 then fvgBearL2 else if high2 > fvgBear2MemHigh[1] then Double.NaN else if fillGaps and high2 > fvgBear2MemLow[1] then high2 else fvgBear2MemLow[1];

def fvgBull2MemLow = if fvgBullH2 then fvgBullL2 else if low2 < fvgBull2MemLow[1] then Double.NaN else fvgBull2MemLow[1];
def fvgBull2MemHigh = if fvgBullH2 then fvgBullH2 else if low2 < fvgBull2MemLow[1] then Double.NaN else if fillGaps and low2 < fvgBull2MemHigh[1] then low2 else fvgBull2MemHigh[1];

def fvgBear2MH = if fvgBear2MemHigh then fvgBear2MemHigh else double.nan;
def fvgBear2ML = if fvgBear2MemLow then fvgBear2MemLow else double.nan;

def fvgBull2MH = if fvgBull2MemHigh then fvgBull2MemHigh else double.nan;
def fvgBull2ML = if fvgBull2MemLow then fvgBull2MemLow else double.nan;

# timeframe 3

def low3 = low(period = aggregation3);
def high3 = high(period = aggregation3);

def fvgBear3 = if low3[2] - high3 > 0 then 1 else 0;
def fvgBearPctg3 = if AbsValue((high3 -low3[2])/low3[2]) > threshold/100 then 1 else 0;

def fvgBull3 = if low3 - high3[2] > 0 then 1 else 0;
def fvgBullPctg3 = if AbsValue((low3 - high3[2])/high3[2]) > threshold/100 then 1 else 0;

def fvgBearH3 = if fvgBear3 and fvgBearPctg3 then low3[2] else 0;
def fvgBearL3 = if fvgBearH3 then high3 else Double.NaN;

def fvgBullH3 = if fvgBull3 and fvgBullPctg3 then low3 else 0;
def fvgBullL3 = if fvgBullH3 then high3[2] else Double.NaN;

def fvgBear3MemHigh = if fvgBearH3 then fvgBearH3 else if high3 > fvgBear3MemHigh[1] then Double.NaN else fvgBear3MemHigh[1];
def fvgBear3MemLow = if fvgBearH3 then fvgBearL3 else if high3 > fvgBear3MemHigh[1] then Double.NaN else if fillGaps and high3 > fvgBear3MemLow[1] then high3 else fvgBear3MemLow[1];

def fvgBull3MemLow = if fvgBullH3 then fvgBullL3 else if low3 < fvgBull3MemLow[1] then Double.NaN else fvgBull3MemLow[1];
def fvgBull3MemHigh = if fvgBullH3 then fvgBullH3 else if low3 < fvgBull3MemLow[1] then Double.NaN else if fillGaps and low3 < fvgBull3MemHigh[1] then low3 else fvgBull3MemHigh[1];

def fvgBear3MH = if fvgBear3MemHigh then fvgBear3MemHigh else double.nan;
def fvgBear3ML = if fvgBear3MemLow then fvgBear3MemLow else double.nan;

def fvgBull3MH = if fvgBull3MemHigh then fvgBull3MemHigh else double.nan;
def fvgBull3ML = if fvgBull3MemLow then fvgBull3MemLow else double.nan;

# plotting

DefineGlobalColor("Aggregation 1", Color.GREEN);
DefineGlobalColor("Aggregation 2", Color.BLUE);
DefineGlobalColor("Aggregation 3", Color.MAGENTA);

AddCloud(if rthCheck then fvgBear1MH else double.nan, if rthCheck then fvgBear1ML else double.nan, GlobalColor("Aggregation 1"));
AddCloud(if rthCheck then fvgBull1MH else double.nan, if rthCheck then fvgBull1ML else double.nan,  GlobalColor("Aggregation 1"));

AddCloud(if rthCheck and showAggregation2 then fvgBear2MH else double.nan, if rthCheck and showAggregation2 then fvgBear2ML else double.nan, GlobalColor("Aggregation 2"));
AddCloud(if rthCheck and showAggregation2 then fvgBull2MH else double.nan, if rthCheck and showAggregation2 then fvgBull2ML else double.nan, GlobalColor("Aggregation 2"));

AddCloud(if rthCheck and showAggregation3 then fvgBear3MH else double.nan, if rthCheck and showAggregation3 then fvgBear3ML else double.nan, GlobalColor("Aggregation 3"));
AddCloud(if rthCheck and showAggregation3 then fvgBull3MH else double.nan, if rthCheck and showAggregation3 then fvgBull3ML else double.nan, GlobalColor("Aggregation 3"));
 
Last edited by a moderator:
This indicator worked very well this week for me, thank you! Only traded SPY, running a 5 min char with 1hr aggregation and a 1min chart with 30min aggregation and waited for the trades to come to me. Put it on a chart with nothing else and you'll see clean signals. Now how can I scan for touches of the zones?
 
This indicator worked very well this week for me, thank you! Only traded SPY, running a 5 min char with 1hr aggregation and a 1min chart with 30min aggregation and waited for the trades to come to me. Put it on a chart with nothing else and you'll see clean signals. Now how can I scan for touches of the zones
Do you mind sharing your chart? thanks
 
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 #######
 
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 #######

Screenshot_2022-09-23_133139.png


Just wanted to say great work @samer800 . I really love the mechanics of trading with FVG. I was wondering why some gaps don't show up and I am not familiar enough to dig in and tweak the code myself. Attached is an example with areas in yellow indicating where I might consider a FVG or volume imbalance.
I see that the indicator shifts away from a FVG as soon as another one of the same kind up or down is created. Is there a way to discern whether a gap has been mitigated or is that part of the order flow we really cant know for sure?

How do you like to use this indicator or FVG for that matter?

Also Have you considered a code for Order blocks? Last up/down candle before momentum away. (ICT)

Thanks
 
Last edited:
Screenshot_2022-09-23_133139.png


Just wanted to say great work @samer800 . I really love the mechanics of trading with FVG. I was wondering why some gaps don't show up and I am not familiar enough to dig in and tweak the code myself. Attached is an example with areas in yellow indicating where I might consider a FVG or volume imbalance.
I see that the indicator shifts away from a FVG as soon as another one of the same kind up or down is created. Is there a way to discern whether a gap has been mitigated or is that part of the order flow we really cant know for sure?

How do you like to use this indicator or FVG for that matter?

Also Have you considered a code for Order blocks? Last up/down candle before momentum away. (ICT)

Thanks
you can switch to manual option and adjust the threshold the percentage you want.
for Auto option I uses the code from Lux as below:

def delta_per = (close[1] - open[1]) / open[1]* 100;
def threshold = TotalSum(AbsValue(delta_per)) / BarNumber() * 2;
personally, I like to trade the ATR.

for order block. I already released a code below.

https://usethinkscript.com/threads/order-block-finder-for-thinkorswim.12162/#post-105272
 
Last edited:
you can switch to manual option and adjust the threshold the percentage you want.
for Auto option I uses the code from Lux as below:

def delta_per = (close[1] - open[1]) / open[1]* 100;
def threshold = TotalSum(AbsValue(delta_per)) / BarNumber() * 2;
personally, I like to trade the ATR.

for order block. I already released a code below.

https://usethinkscript.com/threads/order-block-finder-for-thinkorswim.12162/#post-105272
Awesome, I wasnt playing around with that, Its a little tricky adjusting both threshold and period but theres a sweet spot somewhere.
Any chance you or someone would be kind enough to adjust this so as to have a:
  • Showtoday only (or the last x rolling days)
  • A metric to consider blocks mitigated or not (tested x amount of times) ( I see this adjusts to end a block when a new like-one is created)
  • A MTF function
 
Awesome, I wasnt playing around with that, Its a little tricky adjusting both threshold and period but theres a sweet spot somewhere.
Any chance you or someone would be kind enough to adjust this so as to have a:
  • Showtoday only (or the last x rolling days)
  • A metric to consider blocks mitigated or not (tested x amount of times) ( I see this adjusts to end a block when a new like-one is created)
  • A MTF function
Show today only and MTF already included. Just change the setting “UseChartTime” to no and select the aggregation time
 
Last edited by a moderator:
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 for this script. I use ICT concepts and this works great.
 
Thread starter Similar threads Forum Replies Date
G Repaints Fair Value Gap (FVG) For ThinkOrSwim Indicators 26
BenTen Market Value Areas Indicator for ThinkorSwim Indicators 15

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
330 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