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);
#