Gap Removal Indicator [wbburgin] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
ll64Hmg.png


Author Message:

This gap indicator shows the price of your chosen instrument as if no gaps had occurred overnight. It can be especially useful on highly-volatile exchange-listed instruments that track other 24/7 assets, because the normal candlestick chart of these instruments will create a large amount of noise that may decrease the accuracy of your indicators or make the trend harder to see.

CSS:
# https://www.tradingview.com/v/WWEwaJQu/
#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © wbburgin
#indicator("Gap Removal Indicator", overlay=false)
# Converted by Sam4Cok@Samer800    - 02/2024

Declare lower;

input colorBars = yes;
input candleType = {default "Candlestick", "Heikin-Ashi"};     # "Candle Type"
input movAvgType = AverageType.EXPONENTIAL;
input showFastMovAvg = yes;
input fastLength = 14;
input showSlowMovAvg = yes;
input slowLength = 50;
input showBollingerBands = yes;
input BollingerBandsAvgType = AverageType.SIMPLE;
input bbLength = 14;
input bbMult = 2.0;
input showSupertrend = yes;
input atrLength = 14;
input supertrendFactor = 2.0;

def na = Double.NaN;
def maxx = Max(open, close);
def minn = Min(open, close);
def dayofmonth = GetDay();
def daychange = dayofmonth - dayofmonth[1];
def gapup = daychange and open > maxx[1];
def gapdown = daychange and open < minn[1];
def downgap_change = minn[1] - open;
def upgap_change = open - maxx[1];
def gap_jumper;# = close
def cumul_up;# = 0.
def cumul_dn;# = 0.

if gapup {
    gap_jumper = gap_jumper + upgap_change;
    cumul_up = cumul_up[1] + upgap_change;
    cumul_dn = cumul_dn[1];
} else if gapdown {
    gap_jumper = gap_jumper - downgap_change;
    cumul_dn = cumul_dn[1] + downgap_change;
    cumul_up = cumul_up[1];
} else {
    gap_jumper = if gap_jumper[1] == 0 then close else gap_jumper[1];
    cumul_up = cumul_up[1];
    cumul_dn = cumul_dn[1];
}
def cumulativegap = cumul_up - cumul_dn;
def ga_p = close - cumulativegap;
def g_open;
switch (candleType) {
case "Heikin-Ashi" :
    g_open = (ga_p[2] + ga_p[1]) / 2;
default            :
    g_open = ga_p[1];
}
def g_high = (high / maxx) * ga_p;
def g_low = (low / minn) * ga_p;
def g_close;
switch (candleType) {
case "Heikin-Ashi" :
    g_close = (ga_p[1] + g_high + g_low + ga_p) / 4;
default            :
    g_close = ga_p;
}
#// MovAvg
def fast = MovingAverage(movAvgType, g_close,fastLength);
def slow = MovingAverage(movAvgType, g_close,slowLength);
#// Bollinger Bands
def sDev = StDev(data = g_close, length = bbLength);
def MidLine = MovingAverage(BollingerBandsAvgType, data = g_close, length = bbLength);
def Lower = MidLine - bbMult * sDev;
def Upper = MidLine + bbMult * sDev;
#/ ATR ANY SOURCE
Script atr_anysource {
input source = close;
input atr_length = 10;
    def hh = highest(source,atr_length);
    def ll = lowest(source,atr_length);
    def trueRange = TrueRange(hh, source, ll);
    def nATR = WildersAverage(trueRange,atr_length);
    plot out = nATR;
}
#// SUPERTREND ANY SOURCE
Script supertrend_anysource {
input source = hl2;
input factor = 3;
input atr_length = 10;
    def atr = atr_anysource(source,atr_length);
    def upper = source + factor * atr;
    def lower = source - factor * atr;
    def lowerBand;def upperBand;
    def prevLowerBand = if lowerBand[1]== 0 then lower else lowerBand[1];
    def prevUpperBand = if upperBand[1]== 0 then upper else upperBand[1];
    lowerBand = if lower > prevLowerBand or source[1] < prevLowerBand then lower else prevLowerBand;
    upperBand = if upper < prevUpperBand or source[1] > prevUpperBand then upper else prevUpperBand;
    def direction; # = na
    def superTrend;# = na
    def prevSuperTrend = if superTrend[1]==0 then upper else superTrend[1];
    if atr[1]==0 {
        direction = 1;
    } else if prevSuperTrend == prevUpperBand {
        direction = if source > upperBand then -1 else 1;
    } else {
        direction = if source < lowerBand then 1 else -1;
    }
    superTrend = if direction == -1 then lowerBand else upperBand;
    plot ST = superTrend;
    plot dir = direction;
}
#// Supertrend
def supertrend = supertrend_anysource(g_close, supertrendFactor, atrLength).ST;
def direction = supertrend_anysource(g_close, supertrendFactor, atrLength).DIR;
#/ Display
def r = AbsValue((g_close/g_open)-1);
def increasingRate = r > r[1];
def gapCol = if g_close > g_open then
             if increasingRate then 2 else 1 else
             if increasingRate then -2 else -1;
def hlc3p = (g_high + g_low + g_close) / 3;

# Plot the new Chart
AddChart(open = if gapCol==2 then g_close else na, high = g_high , low = g_low ,   close = g_open,
         type = ChartType.CANDLE, growcolor =  CreateColor(38,166,154));
AddChart(open = if gapCol==1 then g_close else na, high = g_high , low = g_low ,   close = g_open,
         type = ChartType.CANDLE, growcolor =  CreateColor(178,223,219));
AddChart(open = if gapCol==-2 then g_open else na, high = g_high , low = g_low ,   close = g_close,
         type = ChartType.CANDLE, growcolor =  CreateColor(255,82,82));
AddChart(open = if gapCol==-1 then g_open else na, high = g_high , low = g_low ,   close = g_close,
         type = ChartType.CANDLE, growcolor =  Color.PINK);

plot emaLine1 = if showSlowMovAvg then slow else na; # "EMA 1"
plot emaLine2 = if showFastMovAvg then fast else na; # "EMA 2"
plot bbBandTop = if showBollingerBands then upper else na; # "Bollinger Bands - Top"
plot bbBandBot = if showBollingerBands then lower else na; # "Bollinger Bands - Bottom"
plot s_top = if showSupertrend and direction == 1 then supertrend else na;  # "Supertrend - Top"
plot s_bot = if showSupertrend and direction == -1 then supertrend else na; # "Supertrend - Bottom"

emaLine1.SetDefaultColor(Color.ORANGE);
emaLine2.SetDefaultColor(Color.DOWNTICK);
bbBandTop.SetDefaultColor(Color.GRAY);
bbBandBot.SetDefaultColor(Color.GRAY);
s_top.SetDefaultColor(Color.MAGENTA);
s_bot.SetDefaultColor(Color.GREEN);
AddCloud(s_top, hlc3p, Color.PLUM);
AddCloud(hlc3p, s_bot, Color.DARK_GREEN);

#-- BarColor
def col = if direction == 1 then
          if g_close < fast and g_close < slow then -2 else
          if g_close < slow then -1 else 0 else
          if g_close > fast and g_close > slow then 2 else
          if g_close > slow then 1 else 0;

AssignPriceColor(if !colorBars then Color.CURRENT else
                 if col == 2 then Color.GREEN else
                 if col == 1 then Color.DARK_GREEN else
                 if col ==-2 then Color.RED else
                 if col ==-1 then Color.DARK_RED else Color.GRAY);

#-- ENd of CODE
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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