Count of black candlesticks during a particular period - AFL help

#1
Dear friends,
I have tried writing AFL for gathering count of negative/red/black candlesticks for a particular period (consider 10 days).

Also tried with the 'cum' and 'sum' functions.

I wanted to 'Explore' by counting black candlesticks (>6 out of 10 candlesticks) for investment.

Can anyone kindly help please. I recently joined 'traderji' and also now started learning AFL for Amibroker.

Thanks in advance.
 

amsin21

Well-Known Member
#3
#4
Really thanks for the reply :)

I am very near to my result. But I lag in getting EOD +ve and -ve counts.

The AFL referred has reference to intraday ticks.

Any help on getting EOD counts for a period please.
 
#6
Second link shared just counts total number of bars.

But I need +/-ve bars count from a particular period (for eg: from-to dates will be chosen during 'Explore' or 'Scan'). Then a column should display 'Number of +ve bars count' and 'Number of -ve bars count'.
 
Last edited:

Raghuveer

Well-Known Member
#7
Dear friends,
I have tried writing AFL for gathering count of negative/red/black candlesticks for a particular period (consider 10 days).

Also tried with the 'cum' and 'sum' functions.

I wanted to 'Explore' by counting black candlesticks (>6 out of 10 candlesticks) for investment.

Can anyone kindly help please. I recently joined 'traderji' and also now started learning AFL for Amibroker.

Thanks in advance.
AFL below. Check if it has any off-by-one error. May not be the best implementation, but I don't know much afl.
In the code "(IsTrue(isGreenBar));" is a hack. Maybe ask some AFL expert to write it better.

Code:
// AFL for gathering count of negative/red/black candlesticks for a particular period. 
// Will count candles in the chart timeframe, whether EOD or IEOD doesn't matter.

/* If no bar is selected, LookBackPeriod will start from right most bar.
If any bar is selected then LookBackPeriod will start from the left of that selected bar.
*/

_SECTION_BEGIN("CountCandle's in LookBackPeriod");
ShowCandleCount=ParamToggle("ShowCandleCount", "No|Yes", 1);
if(ShowCandleCount)
{
LookBackPeriod=Param("LookBackPeriod",10,Minimum=0,Maximum=1000);

totalRedCandles=0;
totalGreenCandles=0;
totalDojiCandles=0;

for(i=1; i<=LookBackPeriod AND i<BarCount;i++)
{
iO=Ref(O,-i);
iC=Ref(C,-i);

isGreenBar =IIf((iO<iC),1,0);
isRedBar   =IIf((iO>iC),1,0);
isDojiBar  =IIf((iO==iC),1,0);

totalGreenCandles=totalGreenCandles+(IsTrue(isGreenBar));
totalRedCandles=totalRedCandles+(IsTrue(isRedBar));
totalDojiCandles=totalDojiCandles+(IsTrue(isDojiBar));

}

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("totalGreenCandles="+(totalGreenCandles),xR,yR+25);
GfxTextOut("totalRedCandles="+(totalRedCandles),xR,yR+50);
GfxTextOut("totalDojiCandles="+(totalDojiCandles),xR,yR+75);

}
_SECTION_END();
 
Last edited:
#9
Simply to say, without clicking on charts but by 'Explore' option (through 'Analysis' option), I want data like below instead of duplicate rows in it:

What I expect (through 'Analysis' -> 'Explore' option; I want the duplicates removed):
==================================
Ticker Date/Time Count
———————————————
3MINDIA 9/8/2014 2


What I see:
========
Ticker Date/Time
———————————
3MINDIA 9/8/2014
3MINDIA 9/9/2014

Note:
'Count' column has to show number of duplicates (3MINIDIA scrip duplicated 2 times).
 

Raghuveer

Well-Known Member
#10
Simply to say, without clicking on charts but by 'Explore' option (through 'Analysis' option), I want data like below instead of duplicate rows in it:

What I expect (through 'Analysis' -> 'Explore' option; I want the duplicates removed):
==================================
Ticker Date/Time Count
———————————————
3MINDIA 9/8/2014 2


What I see:
========
Ticker Date/Time
———————————
3MINDIA 9/8/2014
3MINDIA 9/9/2014

Note:
'Count' column has to show number of duplicates (3MINIDIA scrip duplicated 2 times).
Still not getting the result that you wanted. Maybe someone else who knows can post the correct code and procedure.
Slightly updated afl below.

If you get it to work please pm me.

Code:
// AFL for gathering count of negative/red/black candlesticks for a particular period. Will count candles in the chart timeframe, whether EOD or IEOD doesn't matter.

/* In chart: If no bar is selected, LookBackPeriod will start from right most bar.
If any bar is selected then LookBackPeriod will start from the left of that selected bar.
*/

_SECTION_BEGIN("CountCandle's in LookBackPeriod");
ShowCandleCount=ParamToggle("ShowCandleCount", "No|Yes", 1);
if(ShowCandleCount)
{
LookBackPeriod=Param("LookBackPeriod",10,Minimum=1,Maximum=1000);

totalRedCandles=0;
totalGreenCandles=0;
totalDojiCandles=0;

for(i=1; i<=LookBackPeriod AND i<BarCount;i++)
{
iO=Ref(O,-i);
iC=Ref(C,-i);

isGreenBar =IIf((iO<iC),1,0);
isRedBar   =IIf((iO>iC),1,0);
isDojiBar  =IIf((iO==iC),1,0);

totalGreenCandles=totalGreenCandles+(IsTrue(isGreenBar));
totalRedCandles=totalRedCandles+(IsTrue(isRedBar));
totalDojiCandles=totalDojiCandles+(IsTrue(isDojiBar));

}

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("totalGreenCandles="+(totalGreenCandles),xR,yR+25);
GfxTextOut("totalRedCandles="+(totalRedCandles),xR,yR+40);
GfxTextOut("totalDojiCandles="+(totalDojiCandles),xR,yR+55);

Filter=(totalGreenCandles>0) OR (totalRedCandles>0);
greenTotalColor=IIf(totalGreenCandles>totalRedCandles AND totalGreenCandles>=totalDojiCandles, colorPaleGreen, colorLavender);
redTotalColor=IIf(totalRedCandles>totalGreenCandles AND totalRedCandles>=totalDojiCandles, colorRed, colorLavender);
flatTotalColor=IIf(totalDojiCandles>=totalGreenCandles AND totalDojiCandles>=totalRedCandles, colorLightYellow, colorLavender);

AddColumn(totalGreenCandles,"totalGreenCandles", format=1, textColor=colorBlack, bkgndColor=greenTotalColor);
AddColumn(totalRedCandles,"totalRedCandles", format=1, textColor=colorBlack, bkgndColor=redTotalColor);
AddColumn(totalDojiCandles,"totalDojiCandles", format=1, textColor=colorBlack,bkgndColor=flatTotalColor);

}
_SECTION_END();