Repaints Gaps Not Completely Retraced For ThinkOrSwim

Repaints

Svanoy

Expert
VIP
Lifetime
Here is something you may have interest in. It will show the last 3 gaps above and below current close that have not been completely retraced. (2nd edit fixing bug with nested clouds, original code was written for price levels and had to be adapted to work with clouds, fixed)
NXKAnI7.png

Ruby:
def Bar = if !IsNaN(close) and BarNumber() > 0 then BarNumber() else Bar[1];
def VBar = HighestAll(Bar);
def FinalClose = fold c = 0 to VBar while !IsNaN(Bar) and !IsNaN(GetValue(close, -c)) do GetValue(close, -c);

#######################################################################################
#Define Level Plot Triggers#
def GreenBar = close >= open;
def RedBar = close <= open;
def GapAbove = open > (close[1] + TickSize()) and GreenBar and GreenBar[1];
def GapBelow = open < (close[1] - TickSize()) and RedBar and RedBar[1];

def GreenBarLevelCheck = if GapAbove then if (fold gblc = 0 to VBar - Bar with intgblc = 0 while (GetValue(low, -(gblc + 1)) > close[1]) do intgblc + 1) == (VBar - Bar) then 1 else 0 else 0;

def RedBarLevelCheck = if GapBelow then if (fold rblc = 0 to VBar - Bar with intrblc = 0 while (GetValue(high, -(rblc + 1)) < close[1]) do intrblc + 1) == (VBar - Bar)  then 1 else 0 else 0;

#######################################################################################
#Scripts#

script Targets {
    input BarCheck = 0;
    input price = open;
    input Final = open;
    input AB = {default "Above", "Below"};
    def Above_Below;
    switch (AB) {
    case Below:
        Above_Below = price[1] - Final < 0;
    default:
        Above_Below = price[1] - Final > 0;
}
    def TA = CompoundValue(1, if BarCheck and Above_Below then price[1] else TA[1], Double.NaN);
    plot Targets = TA;
}

script LookBackForLevel {
    input price = open;
    input Level = open;
    input BarCheck = 0;
    def Bar = if !IsNaN(close) and BarNumber() > 0 then BarNumber() else Bar[1];

    def CPACLookBackForLevel = fold C1L = 0 to Bar while GetValue(price, C1L + 1) == Level and BarCheck or            GetValue(CPACLookBackForLevel, C1L + 1) == 1 do if GetValue(price, C1L + 1) == Level and BarCheck then 1 else if GetValue(CPACLookBackForLevel, C1L + 1) == 1 then 1 else 0;
    plot LookBackForLevel = CPACLookBackForLevel;
}

#########################################################
#Active Targets Above Close#

def CPAC1 = LowestAll(Targets(RedBarLevelCheck,close,FinalClose,"Above"));
plot PAC1 = if LookBackForLevel(close,CPAC1,RedBarLevelCheck) or IsNaN(close[-1]) then CPAC1 else Double.NaN;
PAC1.Hide();
def CPAC2 = if IsNaN(PAC1[1]) and !IsNaN(PAC1) then open else CPAC2[1];
plot PAC2 = CPAC2;
PAC2.Hide();

def CPAC3 = LowestAll(Targets(RedBarLevelCheck,close,CPAC1,"Above"));
plot PAC3 = if LookBackForLevel(close,CPAC3,RedBarLevelCheck) or isnan(close[-1]) then CPAC3 else double.nan;
PAC3.Hide();
def CPAC4 = if IsNaN(PAC3[1]) and !IsNaN(PAC3) then open else CPAC4[1];
plot PAC4 = CPAC4;
PAC4.Hide();

def CPAC5 = LowestAll(Targets(RedBarLevelCheck,close,CPAC3,"Above"));
plot PAC5 = if LookBackForLevel(close,CPAC5,RedBarLevelCheck) or isnan(close[-1]) then CPAC5 else double.nan;
PAC5.Hide();
def CPAC6 = if IsNaN(PAC5[1]) and !IsNaN(PAC5) then open else CPAC6[1];
plot PAC6 = CPAC6;
PAC6.Hide();

AddCloud(PAC1,  PAC2,  Color.LIGHT_GREEN,  Color.LIGHT_GREEN);
AddCloud (PAC3, PAC4, color.light_green, color.light_green, no);
AddCloud (PAC5, PAC6, color.light_green, color.light_green, no);
#########################################################
#Active Targets Below Close#

def CPBC1 = HighestAll(Targets(GreenBarLevelCheck,close,FinalClose,"Below"));
plot PBC1 = if LookBackForLevel(close, CPBC1, GreenBarLevelCheck) or IsNaN(close[-1]) then CPBC1 else Double.NaN;
PBC1.Hide();
def CPBC2 = if IsNaN(PBC1[1]) and !IsNaN(PBC1) then open else CPBC2[1];
plot PBC2 = CPBC2;
PBC2.Hide();

def CPBC3 = HighestAll(Targets(GreenBarLevelCheck,close,CPBC1,"Below"));
plot PBC3 = if LookBackForLevel(close,CPBC3,GreenBarLevelCheck) or IsNaN(close[-1]) then CPBC3 else Double.NaN;
PBC3.Hide();
def CPBC4 = if IsNaN(PBC3[1]) and !IsNaN(PBC3) then open else CPBC4[1];
plot PBC4 = CPBC4;
PBC4.Hide();

def CPBC5 = HighestAll(Targets(GreenBarLevelCheck,close,CPBC3,"Below"));
plot PBC5 = if LookBackForLevel(close,CPBC5,GreenBarLevelCheck) or IsNaN(close[-1]) then CPBC5 else Double.NaN;
PBC5.Hide();
def CPBC6 = if IsNaN(PBC5[1]) and !IsNaN(PBC5) then open else CPBC6[1];
plot PBC6 = CPBC6;
PBC6.Hide();

AddCloud (PBC1, PBC2, Color.LIGHT_GREEN, Color.LIGHT_RED, no);
AddCloud (PBC3, PBC4, Color.LIGHT_GREEN, Color.LIGHT_RED, no);
AddCloud (PBC5, PBC6, Color.LIGHT_GREEN, Color.LIGHT_RED, no);
############################
 
Last edited:

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

Thread starter Similar threads Forum Replies Date
samer800 Opening Range Gaps [TFO] for ThinkOrSwim Custom 0

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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