# Applying more criteria while plotting shape: Need help

Hi,
Written a code for plotting a shape on upper edge of the candle based on certain criteria.
This is working fine.
Now I want this criteria to be modified as following

1. Shapes are drawn above the candles when some criteria meets => Working
2. If the current price is greater than high of that candle, no need to draw the shape => Working
3. Now I want to check, the highest price from that candle where shape was drawn to current candle, the highest price from this region should be less than the candle’s minm of(open, close) then only shape to be drwan => Need help
The logic I have in mind is as follows:
a. Shape drawn on -n1 th, -n2 th , -n3 th candles
b. minm(close,open) for those candles are m1,m2, m3 respectively
c. now finding highest price from -n1+1 candle to current candle is h1. Similar calculation for h2 & h3.
d. Check m1> h1, h2, h3=> if greater draw the shape or don’t draw.
``````function ctype(o1,h1,l1,c1) // function for determining Candle type, 1: Body less than 50% of the range, 2: Down candle, 3: Up Candle
{ body=abs(o1-c1);
hgt=h1-l1;
rati=body/hgt;
typ = IIf(rati<=.5,1,IIf(o1>c1,2,3));
return(typ);
}

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
col=ctype(O,H,L,C);
color = IIf(col==1, colorBlue, IIf(col==3,colorGreen,colorRed));
Plot(C,"Price",color,styleCandle );

b1=ctype(O,H,L,C);
b2=ctype(Ref(O,-1),Ref(h,-1),Ref(l,-1),Ref(c,-1));
b3=ctype(Ref(O,-2),Ref(h,-2),Ref(l,-2),Ref(c,-2));
lu=Ref(H,-3);
lb=Min(Ref(C,-3),Ref(O,-3));
//need code to find max from next candle where shape was drwan to current candle
//if that  max<lb, set color to RED, else set the color to BLACK to make it invisible.

color = IIf(b1==2 AND b2==2 AND b3==1 AND lb>C, colorwhite, colorblack);
PlotShapes(shapeDownTriangle, color, layer = 0, yposition = lu, offset = -12, XShift =-2 );

``````

I’m not exactly sure what you are trying to accomplish, but you might find using the HighestSince function may help.

Thanks for your response. The code currently identifies the candles where the downArrow will be drawn. Now I want to put those arrows if one more additional criteria is met.
We leave BLUE candle and next RED candle .. but will check the highest of the candles from cmp to that RED candle. If it touches the level of the bottom of the BLUE candle, system should not draw the downarrow.

Please let me know how we can use the highestSince function.
I am attaching a file to explain the requirement. Do you have the industry standard name for this candlestick pattern? If you do then perhaps someone has already coded this.

…but will check the highest of the candles from cmp (what is CMP?) to that RED candle.
…Now I want to put those arrows (WHERE?) if one more additional criteria is met.

Please try to simplify the logic description, step by step, as though you are baking a cake from a complex recipe. AFL uses math and values to plot bars and arrows, not patterns so you have to think in terms of one bar’s close value being greater or less than a previous bar, etc. Good luck!

1 Like

Please see my input as below :

Currently AFL plotting on few candles, those are as intended. Now I want to eliminate few plottings, details of the logic are given in the attached screenshot. Hi All,

Requesting you to have a look.

Thanks.

Hi @snbarma, My quick look at your code leads me to believe you just need to put an "IF" statement in to control when you Plotshape.

This sample code may help:

``````condition1 = m1> h1;
condition2 = m1>h2;
condition3 = m1>h3;

if (condition1 and condition2 and condition3)

``````

Hope this is what you need.

As cond1, cond2 & cond3 are arrays, if can not be used. We need to find other alternate.

``````function ctype(o1,h1,l1,c1) // function for determining Candle type, 1: Body less than 50% of the range, 2: Down candle, 3: Up Candle
{ body=abs(o1-c1);
hgt=h1-l1;
rati=body/hgt;
typ = IIf(rati<=.5,1,IIf(o1>c1,2,3));
return(typ);
}

SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
col=ctype(O,H,L,C);
color = IIf(col==1, colorBlue, IIf(col==3,colorGreen,colorRed));
Plot(C,"Price",color,styleCandle );

b1=ctype(O,H,L,C);
b2=ctype(Ref(O,-1),Ref(h,-1),Ref(l,-1),Ref(c,-1));
b3=ctype(Ref(O,-2),Ref(h,-2),Ref(l,-2),Ref(c,-2));
su=Ref(h,-2);
lb=min(Ref(C,-2),Ref(O,-2));
if (b1==1 AND b2==3 AND b3==3)
{PlotShapes(shapedownarrow,colorwhite, layer=0, yposition=su, offset=-14,XShift=-2);}
``````

@snbarma: Sorry, you are correct. I wasn't thinking correctly.

Another "off the top of my head" untested idea:

``````show = condition1 and condition2 and condition3
Plotshapes( IIF(show, shapedownarrow, shapeNone), ...with rest of your controls );

``````

Hope this idea is better.

Thank you very much.
It helped.

Now conditional plotting has been achieved.
How can I find out the highest value of high from the current market price to this bar where conditions met.

I have written a code, it is not working. Please suggest.

``````cond=(b1==2 AND b2==2 AND b3==1);
Plotshapes(IIf(cond,shapedownarrow, Null),colorwhite, layer=0, yposition=su, offset=-14,XShift=-2);
x=Cum(1);
px1=LastValue(ValueWhen(cond,x,1)); // want to find the bar nos for the 1st occurrence
_TRACE("This is a px1 :"+px1);
``````

The value px1 returning is zero.

Before your answer came, I posted the new issue in another topic.
As it is an new issue, though related I thought it will be better to keep it separate.
Please see my new post “How to find bar nos for the 1st or 2nd occurrence and use it for subsequent calculation”.

We can close this thread. Thank you so much.

@snbarma, good forum etiquette is to mark the issue solved, and how it was solved, so newcomers can quickly see the problem and then the solution.