plot overlap ranges

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[0]-low[0];
def previousRange = high[1]-low[1];

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

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

def OverlapUp = if high[0]>high[1] and low[0]>low[1] and low[0]<high[1] then 1 else 0;
def OverlapDown = if high[0]<high[1] and low[0]<low[1] and high[0]>low[1] 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[0]-low[1] else 0;
plot OLU = if OverlapUp is true then high[1]-low[0] else 0;

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

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

plot lineZERO = 0.0;
 
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[0] - low[0];
def previousRange = high[1] - low[1];
def...
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
 
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[0] - low[0];
def previousRange = high[1] - low[1];
def smallerRange = if currentRange < previousRange then currentRange else previousRange;

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

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

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

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

plot OverLapArea = doesExpand + doesContract + doesoverlapTrendUp + doesoverlapTrendDown;
 
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[0] - low[0];
def previousRange = high[1] - low[1];
def smallerRange = if currentRange < previousRange then currentRange else previousRange;

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

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

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

def overlapDownTrend = high[0]-low[1];
def doesoverlapTrendDown = if doesGap == 0 and low[0]<high[1] and high[0]>low[1] and high[0]<high[1] and low[0]<low[1] 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.
i added some code to your initial code, that adds up the 6 signals and plots the value, along with your average.
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[0]-low[0];
def previousRange = high[1]-low[1];

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

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

def OverlapUp = if high[0]>high[1] and low[0]>low[1] and low[0]<high[1] then 1 else 0;
def OverlapDown = if high[0]<high[1] and low[0]<low[1] and high[0]>low[1] 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[0]-low[1] else 0;
def OLU = if OverlapUp is true then high[1]-low[0] else 0;

def GD = if GapDown is true then low[1]-high[0] else 0;
def GU = if GapUp is true then low[0]-high[1] 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;
addchartbubble(test1, 0, 
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();
#
 
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.
 
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[0] - low[0];
def previousRange = high[1] - low[1];
def smallerRange = if currentRange < previousRange then currentRange else previousRange;

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

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

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

def overlapDownTrend = high[0] - low[1];
def doesoverlapTrendDown = if doesGap == 0 and low[0] < high[1] and high[0] > low[1] and high[0] < high[1] and low[0] < low[1] 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;
 

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