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