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.
 

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

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