Filter in Candlestick Exploration

Hello,

I'm trying to create an exploration for candlestick patterns.
I'm having trouble with the Filter function.
I would like the exploration to display only those quotes where a candlestick pattern is formed.
I can't figure out why the text column I have added is displaying "error", when quotes without a candlestick pattern should be getting filtered out.
The PlotShape function is working fine, displaying the defined shape only where a candlestick pattern is formed.

I'm a complete beginner and this is the very first bit of AFL I am attempting.
The code is taken from some afl website (unfortunately I can't remember which and the code itself did not have any identification of the original coder) and then the code has been modified by me as per my requirements -

_SECTION_BEGIN("Definitions");

Green_Candle = C > O ;

Red_Candle = O > C ;

No_Upper_Shadow = (H - Max (O , C)) / Max (O , C) <= 0.001 ;

No_Lower_Shadow = ((Min (O , C) - L) / L ) <= 0.001 ;

Long_Lower_Shadow = (Min(O , C) - L) >= 2*(abs(O - C)) ;

Long_Upper_Shadow = (H - Max(O , C)) >= 2*(abs(O-C)) ;

Uptrend = H > Ref(H,-1) AND L > Ref(L,-1);

Downtrend = H < Ref(H,-1) AND L < Ref(L,-1);

Engulfing = Max(O,C) > Ref(Max(O,C),-1) AND Min(O,C) < Ref(Min(O,C),-1);

Doji = ((abs(O-C))/O) <= 0.001 ; 

Spinning_Top = ((abs(O-C))/O) <= 0.01 ;

_SECTION_END();


_SECTION_BEGIN("Candlestick Patterns");

Bullish_Marubozu = Green_Candle AND No_Upper_Shadow AND No_Lower_Shadow ;

Bearish_Marubozu = Red_Candle AND No_Upper_Shadow AND No_Lower_Shadow ;

Hammer = Downtrend AND No_Upper_Shadow AND Long_Lower_Shadow ;

Hanging_Man = Uptrend AND No_Upper_Shadow AND Long_Lower_Shadow ;

Shooting_Star = Uptrend AND No_Lower_Shadow AND Long_Upper_Shadow ;

Bullish_Engulfing = Ref(Downtrend,-1) AND Ref(Red_Candle,-1) AND Green_Candle AND Engulfing ;

Bearish_Engulfing = Ref(Uptrend,-1) AND Ref(Green_Candle, -1) AND Red_Candle AND Engulfing ;

Piercing = Ref(Downtrend,-1) AND Ref(Red_Candle,-1) AND Green_Candle AND C >= Ref((O-C)/2 , -1) ;

Dark_Cloud = Ref(Uptrend,-1) AND Ref(Green_Candle, -1) AND Red_Candle AND C <= Ref((C-O)/2, -1) ;

Bullish_Harami = Ref(Downtrend,-1) AND Ref(Red_Candle, -1) AND Green_Candle AND O > Ref (C, -1) AND C < Ref(O, -1) ;

Bearish_Harami = Ref(Uptrend,-1) AND Ref(Green_Candle, -1) AND Red_Candle AND C > Ref (O, -1) AND O < Ref(C, -1) ;

Morning_Star = Ref(Downtrend,-2) AND Ref(Red_Candle,-2) AND Ref(GapDown(),-1) AND Ref(Spinning_Top,-1) AND GapUp() AND Green_Candle AND 
C > Ref(O,-2) ;

Evening_Star = Ref(Uptrend,-2) AND Ref(Green_Candle,-2) AND Ref(GapUp(),-1) AND Ref(Spinning_Top,-1) AND GapDown() AND Red_Candle AND 
C < Ref(O,-2) ;

Morning_Doji_Star = Ref(Downtrend,-2) AND Ref(Red_Candle,-2) AND Ref(GapDown(),-1) AND Ref(Doji,-1) AND GapUp() AND Green_Candle AND 
C > Ref(O,-2) ;

Evening_Doji_Star = Ref(Uptrend,-2) AND Ref(Green_Candle,-2) AND Ref(GapUp(),-1) AND Ref(Doji,-1) AND GapDown() AND Red_Candle AND 
C < Ref(O,-2) ;

Candlestick = Bullish_Marubozu OR Bearish_Marubozu OR Hammer OR Hanging_Man OR Shooting_Star OR Bullish_Engulfing OR Bearish_Engulfing OR
Piercing OR Dark_Cloud OR Bullish_Harami OR Bearish_Harami OR Morning_Star OR Evening_Star OR Morning_Doji_Star OR Evening_Doji_Star ;

_SECTION_END();


_SECTION_BEGIN("Filters and Columns");

Filter = Candlestick AND V > EMA (V,20) ;


Candlestick_Pattern = 
WriteIf(Bullish_Marubozu, "Bullish_Marubozu", WriteIf(Bearish_Marubozu, "Bearish_Marubozu", WriteIf(Hammer, "Hammer",
WriteIf(Hanging_Man, "Hanging_Man", WriteIf(Shooting_Star, "Shooting_Star", WriteIf(Bullish_Engulfing, "Bullish_Engulfing",
WriteIf(Bearish_Engulfing, "Bearish_Engulfing", WriteIf(Piercing, "Piercing", WriteIf(Dark_Cloud, "Dark_Cloud", WriteIf(Bullish_Harami,
"Bullish_Harami", WriteIf(Bearish_Harami, "Bearish_Harami", WriteIf(Morning_Star, "Morning_Star", WriteIf(Evening_Star, "Evening_Star",
WriteIf(Morning_Doji_Star, "Morning_Doji_Star", WriteIf(Evening_Doji_Star, "Evening_Doji_Star", "Error")))))))))))))));

AddTextColumn(Candlestick_Pattern,"Candlestick");

_SECTION_END();

_SECTION_BEGIN ("Plotting");

Plot(C,"Price",colorDefault,styleCandle);
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 ) ) ));

PlotShapes(IIf(Candlestick, shapeSquare, shapeNone), colorBlue, 0, L) ;

_SECTION_END();

Instead of WriteIf, use AFL Function Reference - ADDMULTITEXTCOLUMN

2 Likes

Thank you, I really appreciate the support out here :pray:
So, I tried Addmultitextcolumn -

_SECTION_BEGIN("Definitions");

Green_Candle = C > O ;

Red_Candle = O > C ;

No_Upper_Shadow = (H - Max (O , C)) / Max (O , C) <= 0.001 ;

No_Lower_Shadow = ((Min (O , C) - L) / L ) <= 0.001 ;

Long_Lower_Shadow = (Min(O , C) - L) >= 2*(abs(O - C)) ;

Long_Upper_Shadow = (H - Max(O , C)) >= 2*(abs(O-C)) ;

Uptrend = H > Ref(H,-1) AND L > Ref(L,-1);

Downtrend = H < Ref(H,-1) AND L < Ref(L,-1);

Engulfing = Max(O,C) > Ref(Max(O,C),-1) AND Min(O,C) < Ref(Min(O,C),-1);

Doji = ((abs(O-C))/O) <= 0.001 ; 

Spinning_Top = ((abs(O-C))/O) <= 0.01 ;

_SECTION_END();


_SECTION_BEGIN("Candlestick Patterns");

Bullish_Marubozu = Green_Candle AND No_Upper_Shadow AND No_Lower_Shadow ;

Bearish_Marubozu = Red_Candle AND No_Upper_Shadow AND No_Lower_Shadow ;

Hammer = Downtrend AND No_Upper_Shadow AND Long_Lower_Shadow ;

Hanging_Man = Uptrend AND No_Upper_Shadow AND Long_Lower_Shadow ;

Shooting_Star = Uptrend AND No_Lower_Shadow AND Long_Upper_Shadow ;

Bullish_Engulfing = Ref(Downtrend,-1) AND Ref(Red_Candle,-1) AND Green_Candle AND Engulfing ;

Bearish_Engulfing = Ref(Uptrend,-1) AND Ref(Green_Candle, -1) AND Red_Candle AND Engulfing ;

Piercing = Ref(Downtrend,-1) AND Ref(Red_Candle,-1) AND Green_Candle AND C >= Ref((O-C)/2 , -1) ;

Dark_Cloud = Ref(Uptrend,-1) AND Ref(Green_Candle, -1) AND Red_Candle AND C <= Ref((C-O)/2, -1) ;

Bullish_Harami = Ref(Downtrend,-1) AND Ref(Red_Candle, -1) AND Green_Candle AND O > Ref (C, -1) AND C < Ref(O, -1) ;

Bearish_Harami = Ref(Uptrend,-1) AND Ref(Green_Candle, -1) AND Red_Candle AND C > Ref (O, -1) AND O < Ref(C, -1) ;

Morning_Star = Ref(Downtrend,-2) AND Ref(Red_Candle,-2) AND Ref(GapDown(),-1) AND Ref(Spinning_Top,-1) AND GapUp() AND Green_Candle AND 
C > Ref(O,-2) ;

Evening_Star = Ref(Uptrend,-2) AND Ref(Green_Candle,-2) AND Ref(GapUp(),-1) AND Ref(Spinning_Top,-1) AND GapDown() AND Red_Candle AND 
C < Ref(O,-2) ;

Morning_Doji_Star = Ref(Downtrend,-2) AND Ref(Red_Candle,-2) AND Ref(GapDown(),-1) AND Ref(Doji,-1) AND GapUp() AND Green_Candle AND 
C > Ref(O,-2) ;

Evening_Doji_Star = Ref(Uptrend,-2) AND Ref(Green_Candle,-2) AND Ref(GapUp(),-1) AND Ref(Doji,-1) AND GapDown() AND Red_Candle AND 
C < Ref(O,-2) ;

Candlestick = Bullish_Marubozu OR Bearish_Marubozu OR Hammer OR Hanging_Man OR Shooting_Star OR Bullish_Engulfing OR Bearish_Engulfing OR
Piercing OR Dark_Cloud OR Bullish_Harami OR Bearish_Harami OR Morning_Star OR Evening_Star OR Morning_Doji_Star OR Evening_Doji_Star ;

_SECTION_END();


_SECTION_BEGIN("Filters and Columns");

Cond1 = Candlestick == 1 ;
Cond2 = V > EMA (V,20) ;

Filter = Cond1 AND Cond2 ;

TextList = "None\nBullish_Marubozu\nBearish_Marubozu\nHammer\nHanging_Man\nShooting_Star\nBullish_Engulfing\nBearish_Engulfing\nPiercing
             \nDark_Cloud\nBullish_Harami\nBearish_Harami\nMorning_Star\nEvening_Star\nMorning_Doji_Star\nEvening_Doji_Star " ;


TextSelector = 1*Bullish_Marubozu + 2*Bearish_Marubozu + 3*Hammer + 4*Hanging_Man +5*Shooting_Star + 6*Bullish_Engulfing + 7*Bearish_Engulfing + 
				8*Piercing + 9*Dark_Cloud + 10*Bullish_Harami + 11*Bearish_Harami + 12*Morning_Star + 13*Evening_Star + 14*Morning_Doji_Star + 
				15*Evening_Doji_Star ;

AddMultiTextColumn (TextSelector, TextList, "CandleStick") ;				


_SECTION_END();

_SECTION_BEGIN ("Plotting");

Plot(C,"Price",colorDefault,styleCandle);
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 ) ) ));

PlotShapes(IIf(Cond1, shapeSquare, shapeNone), colorBlue, 0, L) ;

_SECTION_END();

Still getting results in the exploration where no candlestick exists. Trying to figure if my filter is incorrect or if the addmultitextcolumn is incorrect.

Also, now the interpretation window displays the entire textlist instead of the candlestick occurring at the selected date.

I'm sure I'm missing something very basic and I truly appreciate your patience and help

Why don't you debug and see.

Thank you. So it seems like the code needs to be reworked to be able to handle instances with multiple candlestick patterns.
That was actually very helpful :pray:

1 Like