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)
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: