# plot overlap ranges

#### DanielManahan

##### Member
I want to plot the amount of overlap from the current candle to the previous candle. there will be zero overlap if there is a gap up or gap down. a minimal overlap if a stock is trending with great momentum. a lot of overlap if a stock is consolidating.

I created a script but my only way to do this was to create six different plots for all six possible overlaps. Overlap_Trending up, Overlap Trending Down, Overlap Consolidation Expansion, Overlap Consolidation Contraction, no Overlap gap up, and no overlap gap down.

I want a single plot that handles all overlap conditions such that any gap up returns a zero and all others plots the amount of overlap

here is my code to show my progress so far. and again I just want the logic and math for the code to compare the current candle with the previous candle and return a value of the amount that both candles overlap one another and such that if they gap up or down that value must be zero as there is zero overlap.

thanks.

Input AverageRangeLength = 5;
Input AverageRangeMultiplier = 0.5;

def currentRange = high-low;
def previousRange = high-low;

def GapDown = if high<low then 1 else 0;
def GapUp = if low>high then 1 else 0;

def OverlapExpand = if high>high and low<low then 1 else 0;
def OverlapContract = if high<high and low>low then 1 else 0;

def OverlapUp = if high>high and low>low and low<high then 1 else 0;
def OverlapDown = if high<high and low<low and high>low then 1 else 0;

def smallerRange = if currentRange<previousRange
then currentRange else previousRange;

plot OLE = if OverlapExpand is true then smallerRange else 0;
plot OLC = if OverlapContract is true then smallerRange else 0;

plot OLD = if OverlapDown is true then high-low else 0;
plot OLU = if OverlapUp is true then high-low else 0;

plot GD = if GapDown is true then low-high else 0;
plot GU = if GapUp is true then low-high else 0;

plot AR = average(currentRange, AverageRangeLength)*AverageRangeMultiplier;

plot lineZERO = 0.0;

• halcyonguy
Solution
I took the time to teach myself a few things and came up with the following, which actually solves my original question perfectly, I need to test this out to see how useful it is, possibly combine it with the previous example, possibly create some alerts.

if you have other suggestions such as getting percentage of engulfing in the comparison that would be a great addition to check out

here is what I came up with:

and by all means if any experienced coders see inefficiencies in the method I used to code this, the syntax is clumsy, or your style is a different way to code this, would love suggestions so I can improve for the next time.

declare lower;

def currentRange = high - low;
def previousRange = high - low;
def...

#### halcyonguy

##### Well-known member
VIP
this sounds interesting. i will look at this today.
my thoughts, create + numbers when a stock price is changing alot,
and - #'s when price is flat.

% based numbers.
base overlap on % of bar ht
overlaps are -#'s
gaps are +#'s

possible options/ideas,
. current bar to previous bar
. current bar to x bars ago
. current bar to an 'averaged' bar

#### DanielManahan

##### Member
I took the time to teach myself a few things and came up with the following, which actually solves my original question perfectly, I need to test this out to see how useful it is, possibly combine it with the previous example, possibly create some alerts.

if you have other suggestions such as getting percentage of engulfing in the comparison that would be a great addition to check out

here is what I came up with:

and by all means if any experienced coders see inefficiencies in the method I used to code this, the syntax is clumsy, or your style is a different way to code this, would love suggestions so I can improve for the next time.

declare lower;

def currentRange = high - low;
def previousRange = high - low;
def smallerRange = if currentRange < previousRange then currentRange else previousRange;

def doesGap = if low > high or high < low then yes else 0;

def doesExpand = if doesGap == 0 and high>high and low<low then smallerRange else 0;
def doesContract = if doesGap == 0 and high < high and low > low then smallerRange else 0;

def overlapUpTrend = high-low;
def doesoverlapTrendUp = if doesGap == 0 and low<high and high>low and high>high and low>low then overlapUpTrend else 0;

def overlapDownTrend = high-low;
def doesoverlapTrendDown = if doesGap == 0 and low<high and high>low and high<high and low<low then overlapDownTrend else 0;

plot OverLapArea = doesExpand + doesContract + doesoverlapTrendUp + doesoverlapTrendDown;

• halcyonguy

#### halcyonguy

##### Well-known member
VIP
I took the time to teach myself a few things and came up with the following, which actually solves my original question perfectly, I need to test this out to see how useful it is, possibly combine it with the previous example, possibly create some alerts.

if you have other suggestions such as getting percentage of engulfing in the comparison that would be a great addition to check out

here is what I came up with:

and by all means if any experienced coders see inefficiencies in the method I used to code this, the syntax is clumsy, or your style is a different way to code this, would love suggestions so I can improve for the next time.

declare lower;

def currentRange = high - low;
def previousRange = high - low;
def smallerRange = if currentRange < previousRange then currentRange else previousRange;

def doesGap = if low > high or high < low then yes else 0;

def doesExpand = if doesGap == 0 and high>high and low<low then smallerRange else 0;
def doesContract = if doesGap == 0 and high < high and low > low then smallerRange else 0;

def overlapUpTrend = high-low;
def doesoverlapTrendUp = if doesGap == 0 and low<high and high>low and high>high and low>low then overlapUpTrend else 0;

def overlapDownTrend = high-low;
def doesoverlapTrendDown = if doesGap == 0 and low<high and high>low and high<high and low<low then overlapDownTrend else 0;

plot OverLapArea = doesExpand + doesContract + doesoverlapTrendUp + doesoverlapTrendDown;

glad to hear you got something working.

i will continue working on something as i mentioned in post#2.

i was/am a little confused on what you wanted/how you wanted the signals combined.
added a bubble to display several values, so i could see what is happening. uses an input to turn on/off.

Ruby:
``````declare lower;

Input AverageRangeLength = 5;
Input AverageRangeMultiplier = 0.5;

def currentRange = high-low;
def previousRange = high-low;

def GapDown = if high<low then 1 else 0;
def GapUp = if low>high then 1 else 0;

def OverlapExpand = if high>high and low<low then 1 else 0;
def OverlapContract = if high<high and low>low then 1 else 0;

def OverlapUp = if high>high and low>low and low<high then 1 else 0;
def OverlapDown = if high<high and low<low and high>low then 1 else 0;

def smallerRange = if currentRange < previousRange then currentRange else previousRange;

def OLE = if OverlapExpand is true then smallerRange else 0;
def OLC = if OverlapContract is true then smallerRange else 0;

def OLD = if OverlapDown is true then high-low else 0;
def OLU = if OverlapUp is true then high-low else 0;

def GD = if GapDown is true then low-high else 0;
def GU = if GapUp is true then low-high else 0;

plot AR = average(currentRange, AverageRangeLength)*AverageRangeMultiplier;
ar.SetDefaultColor(Color.yellow);

plot lineZERO = 0.0;
linezero.SetDefaultColor(Color.gray);

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

def sig = ( ole>0 or olc>0 or old>0 or olu>0 or gd>0 or gu>0);
def sigsum = ( ole + olc + old + olu + gd + gu);

input test1 = no;
ole + " ole\n" +
olc + " olc\n" +
old + " old\n" +
olu + " olu\n" +
gd + " gd\n" +
gu + " gu\n" +
sig + " sig\n" +
sigsum + " sum\n" +
ar
, ( if sig then color.yellow else color.gray), no);

plot z = sigsum;
# x.SetStyle(Curve.MEDIUM_DASH);
z.SetDefaultColor(Color.cyan);
# x.setlineweight(1);
z.hidebubble();
#``````

• MerryDay

#### DanielManahan

##### Member
okay great I will check it out. sorry for the confusion.

#### DanielManahan

##### Member
I want to add to my code something that calculates the percent retracement but only for the trend up and trend down overlap, not for the expand and contract cases.

this seems useful when working with large aggregation periods and similar to a fib except the high point is not the highest point but rather the engulfing point. basically how much is breaking the resistance and support levels. as if calculating the shake out values that stops out traders.

#### DanielManahan

##### Member
The purpose of this script is to compare the different aggregations on the same stock and see if there are any recognizable patterns that distinguish aggregations as not being random and not being fractals, but rather a tradable edge.

here is the code that gives a Fibonacci percentage of overlap. hopefully it is without error. let me know if there is something to fix.
declare lower;

input Fib000 = 00.0;
input Fib236 = 23.6;
input Fib382 = 38.2;
input Fib500 = 50.0;
input Fib618 = 61.8;
input Fib786 = 78.6;

def currentRange = high - low;
def previousRange = high - low;
def smallerRange = if currentRange < previousRange then currentRange else previousRange;

def doesGap = if low > high or high < low then yes else 0;

def doesExpand = if doesGap == 0 and high > high and low < low then smallerRange else 0;
def doesContract = if doesGap == 0 and high < high and low > low then smallerRange else 0;

def overlapUpTrend = high - low;
def doesoverlapTrendUp = if doesGap == 0 and low < high and high > low and high > high and low > low then overlapUpTrend else 0;

def overlapDownTrend = high - low;
def doesoverlapTrendDown = if doesGap == 0 and low < high and high > low and high < high and low < low then overlapDownTrend else 0;

def OverLapArea = doesExpand + doesContract + doesoverlapTrendUp + doesoverlapTrendDown;

plot percentPrevious = OverLapArea / previousRange;

plot Fib_000 = 0;
plot Fib_236 = Fib236 / 100;
plot Fib_382 = Fib382 / 100;
plot Fib_500 = Fib500 / 100;
plot Fib_618 = Fib618 / 100;
plot Fib_786 = Fib786 / 100;
plot Fib_1000 = 1;