Previous body max/min is in the top/bottom 5% of the current body

Bentley

Member
Hi guys,
Here is my code that attempt to find when a previous body max is in the top 5% of the current body or when previous body min is in the bottom 5% of the current body.

Code:
def BodyMaxDiff = Min(BodyMax, BodyMax[1]) / Max(BodyMax, BodyMax[1]);
def BodyMinDiff = Min(BodyMin, BodyMin[1]) / Max(BodyMin, BodyMin[1]);
def BodyMaxLimit = BodyMaxDiff >= 0.95 ;
def BodyMinLimit = BodyMinDiff >= 0.95 ;

Seem to work sometime ... :LOL::unsure:o_O

Appreciate any help or idea ...

Thanks 🙏 and have a nice day :cool:
 
Solution
Hi guys,
Here is my code that attempt to find when a previous body max is in the top 5% of the current body or when previous body min is in the bottom 5% of the current body.

Code:
def BodyMaxDiff = Min(BodyMax, BodyMax[1]) / Max(BodyMax, BodyMax[1]);
def BodyMinDiff = Min(BodyMin, BodyMin[1]) / Max(BodyMin, BodyMin[1]);
def BodyMaxLimit = BodyMaxDiff >= 0.95 ;
def BodyMinLimit = BodyMinDiff >= 0.95 ;


when posting code, please post complete code.
your code uses an undefined variable , BodyMax

you are dividing prices, so comparing absolute prices, not the body height.

to compare a price to a bodysize, you should calculate a percentage.
then compare that % to your % limit of 95.

% = ( a max or min number -...
Hi guys,
Here is my code that attempt to find when a previous body max is in the top 5% of the current body or when previous body min is in the bottom 5% of the current body.

Code:
def BodyMaxDiff = Min(BodyMax, BodyMax[1]) / Max(BodyMax, BodyMax[1]);
def BodyMinDiff = Min(BodyMin, BodyMin[1]) / Max(BodyMin, BodyMin[1]);
def BodyMaxLimit = BodyMaxDiff >= 0.95 ;
def BodyMinLimit = BodyMinDiff >= 0.95 ;


when posting code, please post complete code.
your code uses an undefined variable , BodyMax

you are dividing prices, so comparing absolute prices, not the body height.

to compare a price to a bodysize, you should calculate a percentage.
then compare that % to your % limit of 95.

% = ( a max or min number - the previous number ) / the candle body height.


this calcs a % of , the previous body max to the current body height.
i used similar formulas for top and bottom.
this way a 100% top is equal to the top, and a 100% bottom is equal to the bottom. (not 100% and 0%)

comparing previous body top
-50% means the prev body top is lower than the current body bottom.
0 to 100% means the prev body top is within the current candle body.
100% means the prev body top is equal to the body top.
200% means the prev body top is higher than the current body top.


comparing previous body bottom
-50% means the prev body bottom is higher than the current body top.
0 to 100% means the prev body bottom is within the current candle body.
100% means the prev body bottom is equal to the body bottom.
200% means the prev body bottom is lower than the current body bottom.


green dots above if bodymax % is reached.
red dots below the candles if bodymin % is reached.
if both conditions true, then both dots are yellow.

i like to move shapes away from candles, so i use a vertical multiplier of 0.001.

can turn on a bubble to see the values


Code:
# compare_prev_body_ht

#https://usethinkscript.com/threads/previous-body-max-min-is-in-the-top-bottom-5-of-the-current-body.15515/
#Previous body max/min is in the top/bottom 5% of the current body

def na = double.nan;
def bn = barnumber();

def bodymax = Max(open, close);
def bodymin = Min(open, close);
def rng = bodyheight();

input min_limit_per = 95.0;

# calc a % of prev price compared to current body
# compare % to a limit
def prevmaxper = round(100*(bodymax[1] - bodymin)/rng,1);
def prevminper = round(100*(bodymax - bodymin[1])/rng,1);

def BodyMaxLimit = prevmaxper >= min_limit_per;
def BodyMinLimit = prevminper >= min_limit_per;
def both = BodyMaxLimit and BodyMinLimit;

plot zmax = if BodyMaxLimit then high*1.001 else na;
zmax.SetPaintingStrategy(PaintingStrategy.POINTS);
#zmax.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zmax.AssignValueColor(if both then color.yellow else Color.green);
#zmax.SetDefaultColor(Color.green);
zmax.setlineweight(4);
zmax.hidebubble();

plot zmin = if BodyMinLimit then low*0.999 else na;
zmin.SetPaintingStrategy(PaintingStrategy.POINTS);
#zmin.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zmin.AssignValueColor(if both then color.yellow else Color.red);
#zmin.SetDefaultColor(Color.red);
zmin.setlineweight(4);
zmin.hidebubble();


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


input test1 = no;
addchartbubble(test1, low*0.998,
 bodymax + "\n" +
 bodymin + "\n" +
 prevmaxper + " % max\n" +
 prevminper + " % min"
, color.cyan, no);

#

6UOw9mm.jpg
 
Last edited:
Solution
when posting code, please post complete code.
your code uses an undefined variable , BodyMax

you are dividing prices, so comparing absolute prices, not the body height.

to compare a price to a bodysize, you should calculate a percentage.
then compare that % to your % limit of 95.

% = ( a max or min number - the previous number ) / the candle body height.


this calcs a % of , the previous body max to the current body height.
i used similar formulas for top and bottom.
this way a 100% top is equal to the top, and a 100% bottom is equal to the bottom. (not 100% and 0%)

comparing previous body top
-50% means the prev body top is lower than the current body bottom.
0 to 100% means the prev body top is within the current candle body.
100% means the prev body top is equal to the body top.
200% means the prev body top is higher than the current body top.


comparing previous body bottom
-50% means the prev body bottom is higher than the current body top.
0 to 100% means the prev body bottom is within the current candle body.
100% means the prev body bottom is equal to the body bottom.
200% means the prev body bottom is lower than the current body bottom.


green dots above if bodymax % is reached.
red dots below the candles if bodymin % is reached.
if both conditions true, then both dots are yellow.

i like to move shapes away from candles, so i use a vertical multiplier of 0.001.

can turn on a bubble to see the values


Code:
# compare_prev_body_ht

#https://usethinkscript.com/threads/previous-body-max-min-is-in-the-top-bottom-5-of-the-current-body.15515/
#Previous body max/min is in the top/bottom 5% of the current body

def na = double.nan;
def bn = barnumber();

def bodymax = Max(open, close);
def bodymin = Min(open, close);
def rng = bodyheight();

input min_limit_per = 95.0;

# calc a % of prev price compared to current body
# compare % to a limit
def prevmaxper = round(100*(bodymax[1] - bodymin)/rng,1);
def prevminper = round(100*(bodymax - bodymin[1])/rng,1);

def BodyMaxLimit = prevmaxper >= min_limit_per;
def BodyMinLimit = prevminper >= min_limit_per;
def both = BodyMaxLimit and BodyMinLimit;

plot zmax = if BodyMaxLimit then high*1.001 else na;
zmax.SetPaintingStrategy(PaintingStrategy.POINTS);
#zmax.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zmax.AssignValueColor(if both then color.yellow else Color.green);
#zmax.SetDefaultColor(Color.green);
zmax.setlineweight(4);
zmax.hidebubble();

plot zmin = if BodyMinLimit then low*0.999 else na;
zmin.SetPaintingStrategy(PaintingStrategy.POINTS);
#zmin.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zmin.AssignValueColor(if both then color.yellow else Color.red);
#zmin.SetDefaultColor(Color.red);
zmin.setlineweight(4);
zmin.hidebubble();


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


input test1 = no;
addchartbubble(test1, low*0.998,
 bodymax + "\n" +
 bodymin + "\n" +
 prevmaxper + " % max\n" +
 prevminper + " % min"
, color.cyan, no);

#

6UOw9mm.jpg
Again million thanks for the reply ...
No wonder why my code work only sometimes. 😅
Really can't thank u enough for taking the time to teach me where I'm getting it wrong ... even provide the chart bubble for me to check thru ... 2 thumb up ...(y)(y)

Looking at it, I think I couldn't come up with it because my basic concept is wrong ... especially for thinking previous body top/bottom in negative percentage ... I wouldn't have figure it out ...

Anyway, I also hope u get some value out of this also ... there seem to pitpoint turning point nicely ...
 
when posting code, please post complete code.
your code uses an undefined variable , BodyMax

you are dividing prices, so comparing absolute prices, not the body height.

to compare a price to a bodysize, you should calculate a percentage.
then compare that % to your % limit of 95.

% = ( a max or min number - the previous number ) / the candle body height.


this calcs a % of , the previous body max to the current body height.
i used similar formulas for top and bottom.
this way a 100% top is equal to the top, and a 100% bottom is equal to the bottom. (not 100% and 0%)

comparing previous body top
-50% means the prev body top is lower than the current body bottom.
0 to 100% means the prev body top is within the current candle body.
100% means the prev body top is equal to the body top.
200% means the prev body top is higher than the current body top.


comparing previous body bottom
-50% means the prev body bottom is higher than the current body top.
0 to 100% means the prev body bottom is within the current candle body.
100% means the prev body bottom is equal to the body bottom.
200% means the prev body bottom is lower than the current body bottom.


green dots above if bodymax % is reached.
red dots below the candles if bodymin % is reached.
if both conditions true, then both dots are yellow.

i like to move shapes away from candles, so i use a vertical multiplier of 0.001.

can turn on a bubble to see the values


Code:
# compare_prev_body_ht

#https://usethinkscript.com/threads/previous-body-max-min-is-in-the-top-bottom-5-of-the-current-body.15515/
#Previous body max/min is in the top/bottom 5% of the current body

def na = double.nan;
def bn = barnumber();

def bodymax = Max(open, close);
def bodymin = Min(open, close);
def rng = bodyheight();

input min_limit_per = 95.0;

# calc a % of prev price compared to current body
# compare % to a limit
def prevmaxper = round(100*(bodymax[1] - bodymin)/rng,1);
def prevminper = round(100*(bodymax - bodymin[1])/rng,1);

def BodyMaxLimit = prevmaxper >= min_limit_per;
def BodyMinLimit = prevminper >= min_limit_per;
def both = BodyMaxLimit and BodyMinLimit;

plot zmax = if BodyMaxLimit then high*1.001 else na;
zmax.SetPaintingStrategy(PaintingStrategy.POINTS);
#zmax.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zmax.AssignValueColor(if both then color.yellow else Color.green);
#zmax.SetDefaultColor(Color.green);
zmax.setlineweight(4);
zmax.hidebubble();

plot zmin = if BodyMinLimit then low*0.999 else na;
zmin.SetPaintingStrategy(PaintingStrategy.POINTS);
#zmin.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
zmin.AssignValueColor(if both then color.yellow else Color.red);
#zmin.SetDefaultColor(Color.red);
zmin.setlineweight(4);
zmin.hidebubble();


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


input test1 = no;
addchartbubble(test1, low*0.998,
 bodymax + "\n" +
 bodymin + "\n" +
 prevmaxper + " % max\n" +
 prevminper + " % min"
, color.cyan, no);

#

6UOw9mm.jpg
Please add a condition in the settings so that only a change in color is shown. This would be helpful, but my ultimate goal is to have the candle turn yellow when the current candle closes below the previous candle and below the 5 day EMA and turns orange when the current candle closes above the previous candle and above the 5 day EMA.
 
Last edited:

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
438 Online
Create Post

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