Repaints Fair Value Gap (FVG) For ThinkOrSwim

Repaints

greco26

Active member
Hi All,

I created the an indicator to plot the Fair Value Gaps on intraday charts (mainly 1, 2, 3 min charts). I have everything working as you can see the magenta or green plot lines where the gaps are however I am not able to figure out the following;
1) I can't get the cloud to plot for each of the gaps
2) Once I get the cloud on the chart, I want the cloud to continue to be reduced when future price action trades into it.

I used part of the code from BenTen's post here.
https://usethinkscript.com/threads/...n-for-thinkorswim-highlight-potential-gap.45/




Code:
input marketOpenTime = 0930;
input marketCloseTime = 1615;
input ATRLength = 28;
input ATRMultiplier = 1.5;

def secondsFromOpen =  SecondsFromTime(marketOpenTime);
def secondsTillClose = SecondsTillTime(marketCloseTime);
def marketOpen = if secondsFromOpen >= 0 and secondsTillClose >= 0 then 1 else 0;
def newDay = if GetDay() != GetDay()[1] then 1 else 0;

#FVG Calculations
def FVGbearCondition = close[3] <= high[2] and close[1] <= close[2] and high < low[2];
def FVGbullCondition = close[3] >= low[2] and close[1] >= close[2] and low > high[2];

def priceDiff = high[1] - low[1];
def ATRValue = Average(TrueRange(high,  close,  low),  ATRLength);
def middleCandleVolatilityCondition = priceDiff > ATRValue * ATRMultiplier;

def isUpCandle = close > open;
def isGapClosed = if isUpCandle then high[2] >= low else low[2] <= high;
def isFairValueGap = (FVGbearCondition or FVGbullCondition) and !isGapClosed and middleCandleVolatilityCondition and !isFairValueGap[1];

def gapUp = FVGbullCondition and !isGapClosed and middleCandleVolatilityCondition and !isFairValueGap[1];
def GUhigh = if gapUp then low else GUhigh[1];
def GUlow = if gapUp then high[2] else GUlow[1];
def gapDown = FVGbearCondition and !isGapClosed and middleCandleVolatilityCondition and !isFairValueGap[1];
def GDhigh = if gapDown then high else GDhigh[1];
def GDlow = if gapDown then low[2] else GDlow[1];
def hg = (if gapUp then high[2] + (GUhigh - GUlow) else if gapDown then high[2] + (GDlow - GDhigh) else 0) / 2;
def gapRemaining = if gapUp then GUhigh - GUlow else if gapDown then GDlow - GDhigh else 0;
def percentRemaining = 100 * gapRemaining / if gapUp then AbsValue(GUhigh - GUlow) else if gapDown then AbsValue(GDlow - GDhigh) else 0;
def gapFilled = if percentRemaining == 0 then 1 else 0;
def halfGapFilled = if percentRemaining <= 50 then 1 else 0;


#FVG Plots
plot gH = if (gapUp and  marketOpen and !newDay[-1])
then GUhigh else if (gapDown and marketOpen and !newDay[-1])
then GDhigh else Double.NaN;
plot gL = if (gapUp and marketOpen and !newDay[-1])
then GUlow else if (gapDown and  marketOpen and !newDay[-1])
then GDlow else Double.NaN;
plot hGF = if !gapFilled and !halfGapFilled and marketOpen and !newDay[-1]
then hg else Double.NaN;

gH.SetPaintingStrategy(PaintingStrategy.DASHES);
gH.AssignValueColor(if gapDown then Color.MAGENTA else Color.DARK_GREEN);
gL.SetPaintingStrategy(PaintingStrategy.DASHES);
gL.AssignValueColor(if gapDown then Color.MAGENTA else Color.DARK_GREEN);

gH.HideBubble();
gL.HideBubble();

AddCloud(gH, gL, Color.VIOLET, Color.VIOLET);

#Troubleshooting Bubbles
AddChartBubble(gapRemaining, high, gapRemaining, Color.ORANGE);
#addchartbubble(gapUp, high, GUhigh +" | " + GUlow, color.green);
#addchartbubble(gapDown, low, GDhigh +" | " + GDlow, color.red);
 

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

Ruby:
# FVG  Fair Value Gaps Bearish & Bullish Levels
# @TradeForOpp  5/6/2022
input threshold = 0.001;
input aggregation = AggregationPeriod.FIFTEEN_MIN;

def fvgBear = if low(period = aggregation)[2] - high(period = aggregation) > 0 then 1 else 0;
def fvgBearPctg = if AbsValue((high(period = aggregation) - low(period = aggregation)[2]) / low(period = aggregation)[2]) > threshold then 1 else 0;

def fvgBull = if low(period = aggregation) - high(period = aggregation)[2] > 0 then 1 else 0;
def fvgBullPctg = if AbsValue((low(period = aggregation) - high(period = aggregation)[2]) / high(period = aggregation)[2]) > threshold then 1 else 0;

def fvgBearH = if fvgBear and fvgBearPctg then low(period = aggregation)[2] else 0;
def fvgBearL = if fvgBearH then high(period = aggregation) else Double.NaN;

def fvgBullH = if fvgBull and fvgBearPctg then low(period = aggregation) else 0;
def fvgBullL = if fvgBullH then high(period = aggregation)[2] else Double.NaN;

def fvgBearMem1 = if fvgBearH then fvgBearH else if high(period = aggregation) > fvgBearMem1[1] then Double.NaN else fvgBearMem1[1];
def fvgBearMem2 = if fvgBearH then fvgBearL else if high(period = aggregation) > fvgBearMem2[1] then Double.NaN else fvgBearMem2[1];

def fvgBullMem1 = if fvgBullH then fvgBullH else if low(period = aggregation) < fvgBullMem1[1] then Double.NaN else fvgBullMem1[1];
def fvgBullMem2 = if fvgBullH then fvgBullL else if low(period = aggregation) < fvgBullMem2[1] then Double.NaN else fvgBullMem2[1];

plot fvgBearM1 = fvgBearMem1;
plot fvgBearM2 = fvgBearMem2;

plot fvgBullM1 = fvgBullMem1;
plot fvgBullM2 = fvgBullMem2;

fvgBearM1.SetLineWeight(3);
fvgBearM2.SetLineWeight(3);
fvgBearM1.SetDefaultColor(Color.MAGENTA);
fvgBearM2.SetDefaultColor(Color.MAGENTA);
fvgBearM1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fvgBearM2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

fvgBullM1.SetLineWeight(3);
fvgBullM2.SetLineWeight(3);
fvgBullM1.SetDefaultColor(Color.YELLOW);
fvgBullM2.SetDefaultColor(Color.YELLOW);
fvgBullM1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fvgBullM2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
 
Last edited by a moderator:
FVG (Fair Value Gap) and 50% Premium and Discount as ICT describes it in this video

I don`t know if anyone in here has come across ICT and his teachings, but here goes his video ICT Mentorship Episode 6
(The Inner Circle Trader) YouTube Channel
We are all in here because we are looking for an indicator to aid us in perfecting out trades, our read on the market, etc... So based on this I was wondering if anyone could come up with an indicator that could mimic what he explains and teaches in this video above and many others.
The goal for all of us aspiring traders is to trade NAKED charts, but for now, many of us do need the "training wheels".
I am not a coder, but many of you are.
@BenTen @MerryDay @wtf_dude
 
Last edited by a moderator:
FVG (Fair Value Gap) and 50% Premium and Discount as ICT describes it in this video
@Beltrame1 I am watching this video series now. Any luck if anyone has been able to come up with a FVG script yet? If not, I think you can do it based off building a candle pattern in ToS. I am not skilled in ToS to do it. Maybe some here can?
There are a couple of scripts in this thread to get you started:
https://usethinkscript.com/threads/fair-value-gap-fvg-for-thinkorswim.11103/
 
Hi All,

I created the an indicator to plot the Fair Value Gaps on intraday charts (mainly 1, 2, 3 min charts). I have everything working as you can see the magenta or green plot lines where the gaps are however I am not able to figure out the following;
1) I can't get the cloud to plot for each of the gaps
2) Once I get the cloud on the chart, I want the cloud to continue to be reduced when future price action trades into it.

I used part of the code from BenTen's post here.
https://usethinkscript.com/threads/...n-for-thinkorswim-highlight-potential-gap.45/




Code:
input marketOpenTime = 0930;
input marketCloseTime = 1615;
input ATRLength = 28;
input ATRMultiplier = 1.5;

def secondsFromOpen =  SecondsFromTime(marketOpenTime);
def secondsTillClose = SecondsTillTime(marketCloseTime);
def marketOpen = if secondsFromOpen >= 0 and secondsTillClose >= 0 then 1 else 0;
def newDay = if GetDay() != GetDay()[1] then 1 else 0;

#FVG Calculations
def FVGbearCondition = close[3] <= high[2] and close[1] <= close[2] and high < low[2];
def FVGbullCondition = close[3] >= low[2] and close[1] >= close[2] and low > high[2];

def priceDiff = high[1] - low[1];
def ATRValue = Average(TrueRange(high,  close,  low),  ATRLength);
def middleCandleVolatilityCondition = priceDiff > ATRValue * ATRMultiplier;

def isUpCandle = close > open;
def isGapClosed = if isUpCandle then high[2] >= low else low[2] <= high;
def isFairValueGap = (FVGbearCondition or FVGbullCondition) and !isGapClosed and middleCandleVolatilityCondition and !isFairValueGap[1];

def gapUp = FVGbullCondition and !isGapClosed and middleCandleVolatilityCondition and !isFairValueGap[1];
def GUhigh = if gapUp then low else GUhigh[1];
def GUlow = if gapUp then high[2] else GUlow[1];
def gapDown = FVGbearCondition and !isGapClosed and middleCandleVolatilityCondition and !isFairValueGap[1];
def GDhigh = if gapDown then high else GDhigh[1];
def GDlow = if gapDown then low[2] else GDlow[1];
def hg = (if gapUp then high[2] + (GUhigh - GUlow) else if gapDown then high[2] + (GDlow - GDhigh) else 0) / 2;
def gapRemaining = if gapUp then GUhigh - GUlow else if gapDown then GDlow - GDhigh else 0;
def percentRemaining = 100 * gapRemaining / if gapUp then AbsValue(GUhigh - GUlow) else if gapDown then AbsValue(GDlow - GDhigh) else 0;
def gapFilled = if percentRemaining == 0 then 1 else 0;
def halfGapFilled = if percentRemaining <= 50 then 1 else 0;


#FVG Plots
plot gH = if (gapUp and  marketOpen and !newDay[-1])
then GUhigh else if (gapDown and marketOpen and !newDay[-1])
then GDhigh else Double.NaN;
plot gL = if (gapUp and marketOpen and !newDay[-1])
then GUlow else if (gapDown and  marketOpen and !newDay[-1])
then GDlow else Double.NaN;
plot hGF = if !gapFilled and !halfGapFilled and marketOpen and !newDay[-1]
then hg else Double.NaN;

gH.SetPaintingStrategy(PaintingStrategy.DASHES);
gH.AssignValueColor(if gapDown then Color.MAGENTA else Color.DARK_GREEN);
gL.SetPaintingStrategy(PaintingStrategy.DASHES);
gL.AssignValueColor(if gapDown then Color.MAGENTA else Color.DARK_GREEN);

gH.HideBubble();
gL.HideBubble();

AddCloud(gH, gL, Color.VIOLET, Color.VIOLET);

#Troubleshooting Bubbles
AddChartBubble(gapRemaining, high, gapRemaining, Color.ORANGE);
#addchartbubble(gapUp, high, GUhigh +" | " + GUlow, color.green);
#addchartbubble(gapDown, low, GDhigh +" | " + GDlow, color.red);
I have been trying to get the cloud to appear. I have gone through all the cloud script I could find from here and used ToS instructions to add a cloud and nothing has worked so far.
 
I have been trying to get the cloud to appear. I have gone through all the cloud script I could find from here and used ToS instructions to add a cloud and nothing has worked so far.
No clouds are not working on this script:
I created the an indicator to plot the Fair Value Gaps
I can't get the cloud to plot for each of the gaps
 
Ruby:
# FVG  Fair Value Gaps Bearish & Bullish Levels
# @TradeForOpp  5/6/2022
input threshold = 0.001;
input aggregation = AggregationPeriod.FIFTEEN_MIN;

def fvgBear = if low(period = aggregation)[2] - high(period = aggregation) > 0 then 1 else 0;
def fvgBearPctg = if AbsValue((high(period = aggregation) - low(period = aggregation)[2]) / low(period = aggregation)[2]) > threshold then 1 else 0;

def fvgBull = if low(period = aggregation) - high(period = aggregation)[2] > 0 then 1 else 0;
def fvgBullPctg = if AbsValue((low(period = aggregation) - high(period = aggregation)[2]) / high(period = aggregation)[2]) > threshold then 1 else 0;

def fvgBearH = if fvgBear and fvgBearPctg then low(period = aggregation)[2] else 0;
def fvgBearL = if fvgBearH then high(period = aggregation) else Double.NaN;

def fvgBullH = if fvgBull and fvgBearPctg then low(period = aggregation) else 0;
def fvgBullL = if fvgBullH then high(period = aggregation)[2] else Double.NaN;

def fvgBearMem1 = if fvgBearH then fvgBearH else if high(period = aggregation) > fvgBearMem1[1] then Double.NaN else fvgBearMem1[1];
def fvgBearMem2 = if fvgBearH then fvgBearL else if high(period = aggregation) > fvgBearMem2[1] then Double.NaN else fvgBearMem2[1];

def fvgBullMem1 = if fvgBullH then fvgBullH else if low(period = aggregation) < fvgBullMem1[1] then Double.NaN else fvgBullMem1[1];
def fvgBullMem2 = if fvgBullH then fvgBullL else if low(period = aggregation) < fvgBullMem2[1] then Double.NaN else fvgBullMem2[1];

plot fvgBearM1 = fvgBearMem1;
plot fvgBearM2 = fvgBearMem2;

plot fvgBullM1 = fvgBullMem1;
plot fvgBullM2 = fvgBullMem2;

fvgBearM1.SetLineWeight(3);
fvgBearM2.SetLineWeight(3);
fvgBearM1.SetDefaultColor(Color.MAGENTA);
fvgBearM2.SetDefaultColor(Color.MAGENTA);
fvgBearM1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fvgBearM2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

fvgBullM1.SetLineWeight(3);
fvgBullM2.SetLineWeight(3);
fvgBullM1.SetDefaultColor(Color.YELLOW);
fvgBullM2.SetDefaultColor(Color.YELLOW);
fvgBullM1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fvgBullM2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
How to extend all the unfilled lines to the right?
 
Ruby:
# FVG  Fair Value Gaps Bearish & Bullish Levels
# @TradeForOpp  5/6/2022
input threshold = 0.001;
input aggregation = AggregationPeriod.FIFTEEN_MIN;

def fvgBear = if low(period = aggregation)[2] - high(period = aggregation) > 0 then 1 else 0;
def fvgBearPctg = if AbsValue((high(period = aggregation) - low(period = aggregation)[2]) / low(period = aggregation)[2]) > threshold then 1 else 0;

def fvgBull = if low(period = aggregation) - high(period = aggregation)[2] > 0 then 1 else 0;
def fvgBullPctg = if AbsValue((low(period = aggregation) - high(period = aggregation)[2]) / high(period = aggregation)[2]) > threshold then 1 else 0;

def fvgBearH = if fvgBear and fvgBearPctg then low(period = aggregation)[2] else 0;
def fvgBearL = if fvgBearH then high(period = aggregation) else Double.NaN;

def fvgBullH = if fvgBull and fvgBearPctg then low(period = aggregation) else 0;
def fvgBullL = if fvgBullH then high(period = aggregation)[2] else Double.NaN;

def fvgBearMem1 = if fvgBearH then fvgBearH else if high(period = aggregation) > fvgBearMem1[1] then Double.NaN else fvgBearMem1[1];
def fvgBearMem2 = if fvgBearH then fvgBearL else if high(period = aggregation) > fvgBearMem2[1] then Double.NaN else fvgBearMem2[1];

def fvgBullMem1 = if fvgBullH then fvgBullH else if low(period = aggregation) < fvgBullMem1[1] then Double.NaN else fvgBullMem1[1];
def fvgBullMem2 = if fvgBullH then fvgBullL else if low(period = aggregation) < fvgBullMem2[1] then Double.NaN else fvgBullMem2[1];

plot fvgBearM1 = fvgBearMem1;
plot fvgBearM2 = fvgBearMem2;

plot fvgBullM1 = fvgBullMem1;
plot fvgBullM2 = fvgBullMem2;

fvgBearM1.SetLineWeight(3);
fvgBearM2.SetLineWeight(3);
fvgBearM1.SetDefaultColor(Color.MAGENTA);
fvgBearM2.SetDefaultColor(Color.MAGENTA);
fvgBearM1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fvgBearM2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

fvgBullM1.SetLineWeight(3);
fvgBullM2.SetLineWeight(3);
fvgBullM1.SetDefaultColor(Color.YELLOW);
fvgBullM2.SetDefaultColor(Color.YELLOW);
fvgBullM1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fvgBullM2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
How do I load this to thinkorswim?
Thanks
 
If anyone is up for it, I think you could utilize this type of logic to extend the FVG's forward.

Code:
# bargaps_06


def hi = high;
def lo = low;
def opn = open;
def cls = close;
def na = Double.NaN;
def bn = BarNumber();

# pick price level - prev bar
input prev_bar_price = { high_low , default close };

def prevhi;
def prevlo;
switch (prev_bar_price) {
case high_low:
 prevhi = hi[1];
 prevlo = lo[1];
case close:
 prevhi = cls[1];
 prevlo = cls[1];
}

# ----------------------
# current bar
def currhi = hi;
def currlo = lo;

#----------------------------------

input reduce_gaps = yes;

input min_gap_percent = 1.0;
#def per_digits = 1;
AddLabel(1, "find gaps that are > " + min_gap_percent + " % of price", Color.YELLOW);


input show_gap_clouds = yes;
input show_gap_stats_bubbles = no;

#----------------------------------

# if a gap, set gap high and low, and gap dir
def gaphi;
def gaplo;
def dir;
if bn == 1 then {
    gaphi = hl2;
    gaplo = hl2;
    dir = 0;
} else if prevhi < currlo then {
# gap up , high to low
    gaphi = currlo;
    gaplo = prevhi;
    dir = if gaphi > gaplo then 1 else 0;
} else if prevlo > currhi then {
# gap down , low to high
    gaphi = prevlo;
    gaplo = currhi;
    dir = if gaphi > gaplo then -1 else 0;
} else {
    gaphi = hl2;
    gaplo = hl2;
    dir = 0;
}

# ---------------------------------

# determine the gap , + or - number
def gap = if dir > 0 then round(gaphi - gaplo, 2)
 else if dir < 0 then round(gaplo - gaphi, 2)
 else 0;

# check if gap is > min %
def gap_per = if gap == 0 then 0 else Round((gap / gaplo) * 100, 1);
def gap_en = (absvalue(gap_per) > min_gap_percent);

# ----------------------------------------------

# counter of gaps
def gapqty = 4;

def cnt = if bn == 1 then 0
  else if gap_en then cnt[1] + 1
  else cnt[1];

# sequence counter, thru the qty
#  1,2,3,0,1,2,3,0...
def seq1 = (cnt % gapqty);
# chg to be,  1,2,3,4,1,2,3,4,...
def seq = if seq1 == 0 then gapqty else seq1;

#-----------------------------

# seq thru diff lines, and update 1 at a time
def gap1_en = (gap_en and seq == 1);
def gap2_en = (gap_en and seq == 2);
def gap3_en = (gap_en and seq == 3);
def gap4_en = (gap_en and seq == 4);

def p = (gap1_en or gap2_en or gap3_en or gap4_en);

#---------------------------

# bubble location
def bubble_vert_offset = 0.005;
def bubhiy = (gaphi[0] * ( 1 + bubble_vert_offset));
def bubloy = (gaplo[0] * ( 1 - bubble_vert_offset));

# ----------------------------
# gap1

def gap1dir = if gap1_en then dir else gap1dir[1];

AddChartBubble(show_gap_stats_bubbles and gap1_en, (if gap1dir > 0 then bubloy else bubhiy) ,  gap + "\n" + gap_per + "%", if gap1dir < 0 then Color.RED else if gap1dir > 0 then Color.GREEN else Color.GRAY, (if gap1dir < 0 then yes else no) );
 
def top1 = if (bn == 1 or gap1_en[-1] == 1) then na else if gap1_en then gaphi else top1[1];
def bot1 = if (bn == 1 or gap1_en[-1] == 1) then na else if gap1_en then gaplo else bot1[1];

def top1b = if (bn == 1 or gap1_en[-1] == 1) then na
 else if gap1_en then gaphi
 else if !reduce_gaps then top1b[1]
 else if (lo < top1[0] and lo < top1b[1] and gap1dir == 1) then lo
 else top1b[1];

def bot1b = if (bn == 1 or gap1_en[-1] == 1) then na
 else if gap1_en then gaplo
 else if !reduce_gaps then bot1b[1]
 else if (hi > bot1[0] and hi > bot1b[1] and gap1dir == -1) then hi
 else bot1b[1];

# is the gap < 0 ,  then cancel
def gap1cancel = if gap1_en then 0 else if (top1b - bot1b) < 0 then 1 else 0;

def top1c = if gap1_en then top1
 else if gap1cancel then na
 else top1b[0];

def bot1c = if gap1_en then bot1
 else if gap1cancel then na
 else bot1b[0];


plot z1 = top1c;
z1.AssignValueColor(if gap1dir > 0 then color.green else color.red);
z1.SetStyle(Curve.MEDIUM_DASH);

plot z2 = bot1c;
z2.AssignValueColor(if gap1dir > 0 then color.green else color.red);
z2.SetStyle(Curve.MEDIUM_DASH);

def gap1top = if !show_gap_clouds then na else if gap1dir > 0 then z1 else z2;
def gap1bot = if !show_gap_clouds then na else if gap1dir > 0 then z2 else z1;
AddCloud(gap1top, gap1bot, Color.LIGHT_GREEN, Color.LIGHT_RED);


# ----------------------------------
# gap2

def gap2dir = if gap2_en then dir else gap2dir[1];

AddChartBubble(show_gap_stats_bubbles and gap2_en, (if gap2dir > 0 then bubloy else bubhiy) ,  gap + "\n" + gap_per + "%", if gap2dir < 0 then Color.RED else if gap2dir > 0 then Color.GREEN else Color.GRAY, (if gap2dir < 0 then yes else no) );
 
def top2 = if (bn == 1 or gap2_en[-1] == 1) then na else if gap2_en then gaphi else top2[1];
def bot2 = if (bn == 1 or gap2_en[-1] == 1) then na else if gap2_en then gaplo else bot2[1];

def top2b = if (bn == 1 or gap2_en[-1] == 1) then na
 else if gap2_en then gaphi
 else if !reduce_gaps then top2b[1]
 else if (lo < top2[0] and lo < top2b[1] and gap2dir == 1) then lo
 else top2b[1];

def bot2b = if (bn == 1 or gap2_en[-1] == 1) then na
 else if gap2_en then gaplo
 else if !reduce_gaps then bot2b[1]
 else if (hi > bot2[0] and hi > bot2b[1] and gap2dir == -1) then hi
 else bot2b[1];

# is the gap < 0 ,  then cancel
def gap2cancel = if gap2_en then 0 else if (top2b - bot2b) < 0 then 1 else 0;

def top2c = if gap2_en then top2
 else if gap2cancel then na
 else top2b[0];

def bot2c = if gap2_en then bot2
 else if gap2cancel then na
 else bot2b[0];

plot z3 = top2c;
z3.AssignValueColor(if gap2dir > 0 then color.green else color.red);
z3.SetStyle(Curve.MEDIUM_DASH);

plot z4 = bot2c;
z4.AssignValueColor(if gap2dir > 0 then color.green else color.red);
z4.SetStyle(Curve.MEDIUM_DASH);

def gap2top = if !show_gap_clouds then na else if gap2dir > 0 then z3 else z4;
def gap2bot = if !show_gap_clouds then na else if gap2dir > 0 then z4 else z3;
AddCloud(gap2top, gap2bot, Color.LIGHT_GREEN, Color.LIGHT_RED);


# --------------------------------
# gap3

def gap3dir = if gap3_en then dir else gap3dir[1];

AddChartBubble(show_gap_stats_bubbles and gap3_en, (if gap3dir > 0 then bubloy else bubhiy) ,  gap + "\n" + gap_per + "%", if gap3dir < 0 then Color.RED else if gap3dir > 0 then Color.GREEN else Color.GRAY, (if gap3dir < 0 then yes else no) );
 
def top3 = if (bn == 1 or gap3_en[-1] == 1) then na else if gap3_en then gaphi else top3[1];
def bot3 = if (bn == 1 or gap3_en[-1] == 1) then na else if gap3_en then gaplo else bot3[1];

def top3b = if (bn == 1 or gap3_en[-1] == 1) then na
 else if gap3_en then gaphi
 else if !reduce_gaps then top3b[1]
 else if (lo < top3[0] and lo < top3b[1] and gap3dir == 1) then lo
 else top3b[1];

def bot3b = if (bn == 1 or gap3_en[-1] == 1) then na
 else if gap3_en then gaplo
 else if !reduce_gaps then bot3b[1]
 else if (hi > bot3[0] and hi > bot3b[1] and gap3dir == -1) then hi
 else bot3b[1];

# is the gap < 0 ,  then cancel
def gap3cancel = if gap3_en then 0 else if (top3b - bot3b) < 0 then 1 else 0;

def top3c = if gap3_en then top3
 else if gap3cancel then na
 else top3b[0];

def bot3c = if gap3_en then bot3
 else if gap3cancel then na
 else bot3b[0];

plot z5 = top3c;
z5.AssignValueColor(if gap3dir > 0 then color.green else color.red);
z5.SetStyle(Curve.MEDIUM_DASH);

plot z6 = bot3c;
z6.AssignValueColor(if gap3dir > 0 then color.green else color.red);
z6.SetStyle(Curve.MEDIUM_DASH);

def gap3top = if !show_gap_clouds then na else if gap3dir > 0 then z5 else z6;
def gap3bot = if !show_gap_clouds then na else if gap3dir > 0 then z6 else z5;
AddCloud(gap3top, gap3bot, Color.LIGHT_GREEN, Color.LIGHT_RED);


# --------------------------------
# gap4

def gap4dir = if gap4_en then dir else gap4dir[1];

AddChartBubble(show_gap_stats_bubbles and gap4_en, (if gap4dir > 0 then bubloy else bubhiy) ,  gap + "\n" + gap_per + "%", if gap4dir < 0 then Color.RED else if gap4dir > 0 then Color.GREEN else Color.GRAY, (if gap4dir < 0 then yes else no) );
 
def top4 = if (bn == 1 or gap4_en[-1] == 1) then na else if gap4_en then gaphi else top4[1];
def bot4 = if (bn == 1 or gap4_en[-1] == 1) then na else if gap4_en then gaplo else bot4[1];

def top4b = if (bn == 1 or gap4_en[-1] == 1) then na
 else if gap4_en then gaphi
 else if !reduce_gaps then top4b[1]
 else if (lo < top4[0] and lo < top4b[1] and gap4dir == 1) then lo
 else top4b[1];

def bot4b = if (bn == 1 or gap4_en[-1] == 1) then na
 else if gap4_en then gaplo
 else if !reduce_gaps then bot4b[1]
 else if (hi > bot4[0] and hi > bot4b[1] and gap4dir == -1) then hi
 else bot4b[1];

# is the gap < 0 ,  then cancel
def gap4cancel = if gap4_en then 0 else if (top4b - bot4b) < 0 then 1 else 0;

def top4c = if gap4_en then top4
 else if gap4cancel then na
 else top4b[0];

def bot4c = if gap4_en then bot4
 else if gap4cancel then na
 else bot4b[0];

plot z7 = top4c;
z7.AssignValueColor(if gap4dir > 0 then color.green else color.red);
z7.SetStyle(Curve.MEDIUM_DASH);

plot z8 = bot4c;
z8.AssignValueColor(if gap4dir > 0 then color.green else color.red);
z8.SetStyle(Curve.MEDIUM_DASH);

def gap4top = if !show_gap_clouds then na else if gap4dir > 0 then z7 else z8;
def gap4bot = if !show_gap_clouds then na else if gap4dir > 0 then z8 else z7;
AddCloud(gap4top, gap4bot, Color.LIGHT_GREEN, Color.LIGHT_RED);

# --------------------------------


# ----------------------------------------
# test stuff

input debug_data = 0;

input test_seq_counter = no;
addchartbubble(test_seq_counter and p, hi, seq, color.cyan, yes);

input test_show_all_gaps = no;
addchartbubble(test_show_all_gaps and p, high, gap, (if dir > 0 then color.green else color.red), yes);

input test_gap_hi_lo = no;
addchartbubble(test_gap_hi_lo and dir != 0,lo,
 gaphi + " hi\n" +
 gaplo + " lo\n" +
 dir + " dir"
, (if dir > 0 then color.green else if dir < 0 then color.red else color.gray), no);
#
#
 
input threshold = 0.1;

input aggregation1 = AggregationPeriod.FIFTEEN_MIN;
input aggregation2 = AggregationPeriod.TEN_MIN;
input aggregation3 = AggregationPeriod.FIVE_MIN;
input aggregation4 = AggregationPeriod.Hour;
input aggregation5 = AggregationPeriod.DAY;
input showAggregation2 = yes;
input showAggregation3 = yes;
input showAggregation4 = yes;
input showAggregation5 = 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;

# timeframe 4

def low4 = low(period = aggregation4);
def high4 = high(period = aggregation4);

def fvgBear4 = if low4[2] - high4 > 0 then 1 else 0;
def fvgBearPctg4 = if AbsValue((high4 -low4[2])/low4[2]) > threshold/100 then 1 else 0;

def fvgBull4 = if low4 - high4[2] > 0 then 1 else 0;
def fvgBullPctg4 = if AbsValue((low4 - high4[2])/high4[2]) > threshold/100 then 1 else 0;

def fvgBearH4 = if fvgBear4 and fvgBearPctg4 then low4[2] else 0;
def fvgBearL4 = if fvgBearH4 then high4 else Double.NaN;

def fvgBullH4 = if fvgBull4 and fvgBullPctg4 then low4 else 0;
def fvgBullL4 = if fvgBullH4 then high4[2] else Double.NaN;

def fvgBear4MemHigh = if fvgBearH4 then fvgBearH4 else if high4 > fvgBear4MemHigh[1] then Double.NaN else fvgBear4MemHigh[1];
def fvgBear4MemLow = if fvgBearH4 then fvgBearL4 else if high4 > fvgBear4MemHigh[1] then Double.NaN else if fillGaps and high4 > fvgBear4MemLow[1] then high4 else fvgBear4MemLow[1];

def fvgBull4MemLow = if fvgBullH4 then fvgBullL4 else if low4 < fvgBull4MemLow[1] then Double.NaN else fvgBull4MemLow[1];
def fvgBull4MemHigh = if fvgBullH4 then fvgBullH4 else if low4 < fvgBull4MemLow[1] then Double.NaN else if fillGaps and low4 < fvgBull4MemHigh[1] then low4 else fvgBull4MemHigh[1];

def fvgBear4MH = if fvgBear4MemHigh then fvgBear4MemHigh else double.nan;
def fvgBear4ML = if fvgBear4MemLow then fvgBear4MemLow else double.nan;

def fvgBull4MH = if fvgBull4MemHigh then fvgBull4MemHigh else double.nan;
def fvgBull4ML = if fvgBull4MemLow then fvgBull4MemLow else double.nan;

# timeframe 5

def low5 = low(period = aggregation5);
def high5 = high(period = aggregation5);

def fvgBear5 = if low5[2] - high5 > 0 then 1 else 0;
def fvgBearPctg5 = if AbsValue((high5 -low5[2])/low5[2]) > threshold/100 then 1 else 0;

def fvgBull5 = if low5 - high5[2] > 0 then 1 else 0;
def fvgBullPctg5 = if AbsValue((low5 - high5[2])/high5[2]) > threshold/100 then 1 else 0;

def fvgBearH5 = if fvgBear5 and fvgBearPctg5 then low5[2] else 0;
def fvgBearL5 = if fvgBearH5 then high5 else Double.NaN;

def fvgBullH5 = if fvgBull5 and fvgBullPctg5 then low5 else 0;
def fvgBullL5 = if fvgBullH5 then high5[2] else Double.NaN;

def fvgBear5MemHigh = if fvgBearH5 then fvgBearH5 else if high5 > fvgBear5MemHigh[1] then Double.NaN else fvgBear5MemHigh[1];
def fvgBear5MemLow = if fvgBearH5 then fvgBearL5 else if high5 > fvgBear5MemHigh[1] then Double.NaN else if fillGaps and high5 > fvgBear5MemLow[1] then high5 else fvgBear5MemLow[1];

def fvgBull5MemLow = if fvgBullH5 then fvgBullL5 else if low5 < fvgBull5MemLow[1] then Double.NaN else fvgBull5MemLow[1];
def fvgBull5MemHigh = if fvgBullH5 then fvgBullH5 else if low5 < fvgBull5MemLow[1] then Double.NaN else if fillGaps and low5 < fvgBull5MemHigh[1] then low5 else fvgBull5MemHigh[1];

def fvgBear5MH = if fvgBear5MemHigh then fvgBear5MemHigh else double.nan;
def fvgBear5ML = if fvgBear5MemLow then fvgBear5MemLow else double.nan;

def fvgBull5MH = if fvgBull5MemHigh then fvgBull5MemHigh else double.nan;
def fvgBull5ML = if fvgBull5MemLow then fvgBull5MemLow else double.nan;

# plotting

DefineGlobalColor("Aggregation 1", Color.GREEN);
DefineGlobalColor("Aggregation 2", Color.BLUE);
DefineGlobalColor("Aggregation 3", Color.MAGENTA);
DefineGlobalColor("Aggregation 4", Color.CYAN);
DefineGlobalColor("Aggregation 5", Color.dark_orange);

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"));

AddCloud(if rthCheck and showAggregation4 then fvgBear4MH else double.nan, if rthCheck and showAggregation4 then fvgBear4ML else double.nan, GlobalColor("Aggregation 4"));
AddCloud(if rthCheck and showAggregation4 then fvgBull4MH else double.nan, if rthCheck and showAggregation4 then fvgBull4ML else double.nan, GlobalColor("Aggregation 4"));

AddCloud(if rthCheck and showAggregation5 then fvgBear5MH else double.nan, if rthCheck and showAggregation5 then fvgBear5ML else double.nan, GlobalColor("Aggregation 5"));
AddCloud(if rthCheck and showAggregation5 then fvgBull5MH else double.nan, if rthCheck and showAggregation5 then fvgBull5ML else double.nan, GlobalColor("Aggregation 5"));
 
Love this script... how could I get it to populate based off of the current time chart and not a specific aggregation period.
 
Love this script... how could I get it to populate based off of the current time chart and not a specific aggregation period.
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));
##########################
 
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));
##########################
Can you extend all the unfilled FVG to the right? When the FVG was filled, make the lines disapper?
 
Ruby:
# FVG  Fair Value Gaps Bearish & Bullish Levels
# @TradeForOpp  5/6/2022
input threshold = 0.001;
input aggregation = AggregationPeriod.FIFTEEN_MIN;

def fvgBear = if low(period = aggregation)[2] - high(period = aggregation) > 0 then 1 else 0;
def fvgBearPctg = if AbsValue((high(period = aggregation) - low(period = aggregation)[2]) / low(period = aggregation)[2]) > threshold then 1 else 0;

def fvgBull = if low(period = aggregation) - high(period = aggregation)[2] > 0 then 1 else 0;
def fvgBullPctg = if AbsValue((low(period = aggregation) - high(period = aggregation)[2]) / high(period = aggregation)[2]) > threshold then 1 else 0;

def fvgBearH = if fvgBear and fvgBearPctg then low(period = aggregation)[2] else 0;
def fvgBearL = if fvgBearH then high(period = aggregation) else Double.NaN;

def fvgBullH = if fvgBull and fvgBearPctg then low(period = aggregation) else 0;
def fvgBullL = if fvgBullH then high(period = aggregation)[2] else Double.NaN;

def fvgBearMem1 = if fvgBearH then fvgBearH else if high(period = aggregation) > fvgBearMem1[1] then Double.NaN else fvgBearMem1[1];
def fvgBearMem2 = if fvgBearH then fvgBearL else if high(period = aggregation) > fvgBearMem2[1] then Double.NaN else fvgBearMem2[1];

def fvgBullMem1 = if fvgBullH then fvgBullH else if low(period = aggregation) < fvgBullMem1[1] then Double.NaN else fvgBullMem1[1];
def fvgBullMem2 = if fvgBullH then fvgBullL else if low(period = aggregation) < fvgBullMem2[1] then Double.NaN else fvgBullMem2[1];

plot fvgBearM1 = fvgBearMem1;
plot fvgBearM2 = fvgBearMem2;

plot fvgBullM1 = fvgBullMem1;
plot fvgBullM2 = fvgBullMem2;

fvgBearM1.SetLineWeight(3);
fvgBearM2.SetLineWeight(3);
fvgBearM1.SetDefaultColor(Color.MAGENTA);
fvgBearM2.SetDefaultColor(Color.MAGENTA);
fvgBearM1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fvgBearM2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

fvgBullM1.SetLineWeight(3);
fvgBullM2.SetLineWeight(3);
fvgBullM1.SetDefaultColor(Color.YELLOW);
fvgBullM2.SetDefaultColor(Color.YELLOW);
fvgBullM1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
fvgBullM2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
This is great, is there any way to include possibly a gray dashed 50% ?
 
Anyone know how to color the middle candle of a FVG? I think this would make them easier to spot without having so many boxes on the chart.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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