# Counting the Number of candles in a zone

#### amitrandive

##### Well-Known Member
Is it possible to make the following AFL.

I want to know number of candles in a particular range with respect to the current close.
Variables which can be change would be in Parameter menu
they are
1. Range eg in percentage of price, like 0.5%, 1%, 1.5% etc. above and below the closing price.
the range is formed by say x percent above and below the closing price of the current close.
Visual cloud of this zone may also be useful.
2. Look back period say 75 candles, 100 candles, 375 candles etc.

Result should show us the number of candles in that percentage zone in the given look back period .

As an example see the attached Bank Nifty Chart

Last edited:

Seniors

Thanks
Amit

#### Raghuveer

##### Well-Known Member
Is it possible to make the following AFL.

I want to know number of candles in a particular range with respect to the current close.
Variables which can be change would be in Parameter menu
they are
1. Range eg in percentage of price, like 0.5%, 1%, 1.5% etc. above and below the closing price.
the range is formed by say x percent above and below the closing price of the current close.
Visual cloud of this zone may also be useful.
2. Look back period say 75 candles, 100 candles, 375 candles etc.

Result should show us the number of candles in that percentage zone in the given look back period .

As an example see the attached Bank Nifty Chart
Try the below code. I am sure there is a better way to do it but this is all I know.
Code:
``````_SECTION_BEGIN("Count Candle's within H/L range");
rangeHighPercentage=Param("RangeHigh% above close",0,Minimum=0,Maximum=100,step=0.01);
rangeLowPercentage=Param("RangeLow% below close",0,Minimum=0,Maximum=100,step=0.01);
LookBackPeriod=Param("LookBackPeriod",0,Minimum=0,Maximum=1000);

selectedClose=SelectedValue(C);
RangeHigh=selectedClose*(1+rangeHighPercentage/100);
RangeLow=selectedClose*(1-rangeLowPercentage/100);
Plot(RangeHigh,"",colorLime,styleDashed|styleNoLabel);
Plot(RangeLow,"",colorRed,styleDashed|styleNoLabel);

totalCandles=0;
for(i=1; i<=LookBackPeriod AND i<BarCount;i++)
{
insideRange=IIf(((Ref(H,-i)<=RangeHigh) AND (Ref(L,-i)>=RangeLow)),True,False);
totalCandles=totalCandles+(IsTrue(insideRange)); // need a better way to do it
}

GfxSetTextColor(textColor=ParamColor("TextColor",colorLavender));
GfxSetBkColor(bkColor = ParamColor("bkColor",colorBlack));
xR=Param("xR",0,0,1000,1);
yR=Param("yR",80,0,1000,1);
GfxTextOut("totalCandles="+(totalCandles),xR,yR+25);
_SECTION_END();``````

#### amitrandive

##### Well-Known Member
Raghuveer

Thanks for this AFL.:clap:

Just one more request.

Can we form a cloud if a particular zone is selected ,say 1% above and 1% below the current price close,the zone gets a colour ?

Thanks
Amit

#### amitrandive

##### Well-Known Member
Raghuveer

I have modified the AFL for the Cloud.

_SECTION_BEGIN("Price");
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 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("Count Candle's within H/L range");
rangeHighPercentage=Param("RangeHigh% above close",0,Minimum=0,Maximum=100,step=0.01);
rangeLowPercentage=Param("RangeLow% below close",0,Minimum=0,Maximum=100,step=0.01);
LookBackPeriod=Param("LookBackPeriod",0,Minimum=0,Maximum=1000);

selectedClose=SelectedValue(C);
RangeHigh=selectedClose*(1+rangeHighPercentage/100);
RangeLow=selectedClose*(1-rangeLowPercentage/100);
Plot(RangeHigh,"",colorYellow,styleLine|styleNoLabel);
Plot(RangeLow,"",colorYellow,styleLine|styleNoLabel);
a= RangeHigh;
b=RangeLow;
PlotOHLC(a,a,b,b,"",colorLime,styleCloud);

totalCandles=0;
for(i=1; i<=LookBackPeriod AND i<BarCount;i++)
{
insideRange=IIf(((Ref(H,-i)<=RangeHigh) AND (Ref(L,-i)>=RangeLow)),True,False);
totalCandles=totalCandles+(IsTrue(insideRange)); // need a better way to do it
}

GfxSetTextColor(textColor=ParamColor("TextColor",colorLavender));
GfxSetBkColor(bkColor = ParamColor("bkColor",colorBlack));
xR=Param("xR",0,0,1000,1);
yR=Param("yR",80,0,1000,1);
GfxTextOut("totalCandles="+(totalCandles),xR,yR+25);
_SECTION_END();

This AFL is Great.
Just one more request .

The AFL should also count the candle , if the High and Low of any candle is inside the zone defined.Currently the counted candles are only if they are totally included inside the zone.

I want all candles irrespective of their closing or opening to be included in the candle count(even if 20% of the candle body is within the zone).

Please refer the attached image.I have taken a 0.5% above and below the Candle at the red cursor with a 250 period lookback.I need all the candles in this zone to be counted.

Thanks Amit

Last edited:

#### nms

##### Member
Dear Raghuveer,
Thanks for this AFL. The price of range low is showing in the price line and the price of range high is not showing. Can you pl. add a line in this AFL, so that the range high price can be shown in the chart for quick study? I am zero in AFL.
Regards,
nms

#### Raghuveer

##### Well-Known Member
The AFL should also count the candle , if the High and Low of any candle is inside the zone defined.Currently the counted candles are only if they are totally included inside the zone.

I want all candles irrespective of their closing or opening to be included in the candle count(even if 20% of the candle body is within the zone).
Below code uses candle body High/Low instead of candle High/Low.
Check if this is how it should be counted.
Code:
``````_SECTION_BEGIN("CountCandle's in HL range");
ShowCandleCount=ParamToggle("ShowCandleCount", "No|Yes", 0);
if(ShowCandleCount)
{
rangeHighPercentage=Param("RangeHigh% above close",0.03,Minimum=0,Maximum=100,step=0.01);
rangeLowPercentage=Param("RangeLow% below close",0.03,Minimum=0,Maximum=100,step=0.01);
LookBackPeriod=Param("LookBackPeriod",0,Minimum=0,Maximum=1000);

selectedClose=SelectedValue(C);
RangeHigh=selectedClose*(1+rangeHighPercentage/100);
RangeLow=selectedClose*(1-rangeLowPercentage/100);
PlotOHLC(RangeHigh,RangeHigh,RangeLow,RangeLow,"",ParamColor("Range color",colorTeal),styleCloud|styleNoLabel);
//Plot(RangeHigh,"",ParamColor("RangeHigh color",colorLime),styleDashed|styleNoLabel);
//Plot(RangeLow,"",ParamColor("RangeLow color",colorRed),styleDashed|styleNoLabel);
candleBodyPercentage=Param("Body % inside Range",10,Minimum=0,Maximum=100,step=0.5);
zoneRange=RangeHigh-RangeLow;

totalCandles=0;
for(i=1; i<=LookBackPeriod AND i<BarCount;i++)
{
iO=Ref(O,-i);
iH=Ref(H,-i);
iL=Ref(L,-i);
iC=Ref(C,-i);
bodyHigh=IIf(iO>=iC,iO,iC);
bodyLow=IIf(iO>=iC,iC,iO);

insideRange=
IIf(((iH<RangeLow) OR (iL>RangeHigh)), False /* completelyOutsideRange */ ,
IIf(((iH<=RangeHigh) AND (iL>=RangeLow)), True /* completelyInsideRange */ ,
IIf(((bodyHigh>=RangeHigh) AND (bodyLow<=RangeLow)), True /* bodyLargerThanRange */ ,
IIf((RangeLow<=bodyHigh<=RangeHigh) AND ((100*(bodyHigh-RangeLow))/(bodyHigh-bodyLow))>=candleBodyPercentage, True /* candleBodyHighInRange */ ,
IIf((RangeHigh>=bodyLow>=RangeLow) AND ((100*(RangeHigh-bodyLow))/(bodyHigh-bodyLow))>=candleBodyPercentage, True /* candleBodyLowInRange */ , False
)))));

totalCandles=totalCandles+(IsTrue(insideRange)); // need a better way to do it
}

GfxSetTextColor(textColor=ParamColor("TextColor",colorLavender));
GfxSetBkColor(bkColor = ParamColor("bkColor",colorBlack));
xR=Param("xR",0,0,1000,1);
yR=Param("yR",80,0,1000,1);

GfxTextOut("totalCandles="+(totalCandles),xR,yR+25);
}
_SECTION_END();``````

Last edited:

#### Raghuveer

##### Well-Known Member
Dear Raghuveer,
Thanks for this AFL. The price of range low is showing in the price line and the price of range high is not showing. Can you pl. add a line in this AFL, so that the range high price can be shown in the chart for quick study? I am zero in AFL.
Regards,
nms
The default setting for range high=range low=0. So only one line will be visible by default.
If range high and low are separated by some value (greater than 0), then two lines will be visible.
In the new code changed the default to 0.03 so two lines/cloud will be visible easily.

#### amitrandive

##### Well-Known Member
Raghuveer

Thank You very much.

Amit :thumb::clapping: