Here is the MetaStock exploration for "Detecting Breakouts From Flags & Pennants" by Markos Katsanos. Using MetaStock's Explorer module, Katsanos tested his system by scanning a database of 1,250 stocks, which included all Standard & Poor's 500 stocks plus another 750 mainly small-cap stocks. The number of trades varied according to market conditions. The average number of trades was seven to eight under bullish market conditions, decreasing in sideways markets and drying out under bearish conditions.
MetaStock exploration for system to detect flag and pennant patterns

Go to the Explorer and choose the New button. Enter in the following column 
and filter formulas:

Column A
Name: Price
Formula: C

Column B
Name: Pole Top
Formula:
 zz:=Zig(C,17,%);
X:=BarsSince(ZZ < Ref(ZZ,-1)AND Ref(ZZ,-1) > Ref(ZZ,-2));
X1:=LastValue(X)+1;
c2:=Ref(HHV(C,(2+X1)),-1);C2

Column C
Name: Pole Bot
Formula:
 zz:=Zig(C,17,%);
X:=BarsSince(ZZ < Ref(ZZ,-1)AND Ref(ZZ,-1) > Ref(ZZ,-2));
X1:=LastValue(X)+1;
C3:=Ref(LLV(C,26),-X1);C3

Column D
Name: Pole %
Formula:
 zz:=Zig(C,17,%);
X:=BarsSince(ZZ < Ref(ZZ,-1)AND Ref(ZZ,-1) > Ref(ZZ,-2));
X1:=LastValue(X)+1;
c2:=Ref(HHV(C,(2+X1)),-1);
c3:=Ref(LLV(C,26),-X1);
POLE:=(C2-C3)/C3*100;POLE

Column E
Name: Target %
Formula:
 zz:=Zig(C,17,%);
X:=BarsSince(ZZ < Ref(ZZ,-1)AND Ref(ZZ,-1) > Ref(ZZ,-2));
X1:=LastValue(X)+1;
c2:=Ref(HHV(C,(2+X1)),-1);
c3:=Ref(LLV(C,26),-X1);
POLE:=(C2-C3)/C3*100;POLE
TARGET:=1.94* Power(pole ,.724);TARGET

Column F
Name: Target Price
Formula:
 zz:=Zig(C,17,%);
X:=BarsSince(ZZ < Ref(ZZ,-1)AND Ref(ZZ,-1) > Ref(ZZ,-2));
X1:=LastValue(X)+1;
c2:=Ref(HHV(C,(2+X1)),-1);
c3:=Ref(LLV(C,26),-X1);
POLE:=(C2-C3)/C3*100;POLE
TARGET:=1.94* Power(pole ,.724); (1+TARGET/100)*C

Filter
The filter formula is exactly the same used for the Enter Long Order of the 
System Test above.

Run the exploration on the desired securities and display the report. Column 
A is the prebreakout price, column B is the highest point on the flag pole, 
column C is the pole base, column D is the % pole height, column E is the 
target % profit, and column F shows the target price. All prices are on a 
closing basis. Columns B-F are not essential for running the exploration, but 
they will help to calculate your exit price target.

        -Markos Katsanos

Here is the MetaStock code for the system designed by Markos Katsanos in his article "Detecting Breakouts From Flags & Pennants." The system identifies some (but not all) flag and pennant patterns that might be detected visually or otherwise. This is done with only nine lines of code, as opposed to more than a hundred that would have been required had a lower-level programming language been used instead.
MetaStock code: System test, flags & pennants

Enter Long

ZZ:=Zig(C,17,%);
X:=BarsSince(ZZ < Ref(ZZ,-1) AND Ref(ZZ,-1) > Ref(ZZ,-2));
X1:=LASTVALUE(X)+1; {flag duration}
X2:=X1+1;
SD:=Stdev(C,X2);

PERIOD:=22;
COEF:=.1;
INTRA:=Log(H)-Log(L);
VINTRA:=Stdev(INTRA,PERIOD);
INTER:=Log(Typical())-Log(Ref(Typical(),-1));
VINTER:=Stdev(INTER,PERIOD);
CUTOFF:=COEF*(VINTER+VINTRA)*C;
MF:=C-(H+L)/2+Typical()-Ref(Typical(),-1);
FVE:=Sum(If(MF>CUTOFF,+V,If(MF<-CUTOFF,-V,0)),
        PERIOD)/Mov(V,PERIOD,S)/PERIOD*100;

X1<21 and X1>2 AND {Condition 1}
Ref(LinRegSlope(C,13)/Ref(C,-13)*100,-X1)>2.2{Condition 2}
AND Ref(LinRegSlope(C,X2)/Ref(C,-X2),-1)*100<.2
AND LinRegSlope(C,X1)/Ref(C,-X1)>-1.2 {Condition 3}
AND Ref(LinRegSlope(V,X2)/Ref(V,-X2),-1)*100<-2 {Condition 4}
AND Ref(LinRegSlope(SD,X1),-1)<0 {Condition 5}
AND Stoch(20,3)>55 AND ADX(10)>30{Condition 6}
AND FVE>10 AND Fml("VFI")>-3 {Condition 7}
 AND C>Ref(C,-1) AND C>O {Condition 8}

Sell Order

D1:=Simulation.CurrentPositionAge; D2:=LASTVALUE(D1);
 zz:=Zig(Ref(C,-D2),17,%);
X:=BarsSince(ZZRef(ZZ,-2));
X1:=LastValue(X)+1; {flag duration}
c1:=LLV(C,(D2+40)); {pole base}
c2:=ref(hhv(c,22),-D2); {pole top}

BREAK1:= Simulation.CurrentPositionPerformance*100;
POLE:=(C2-C1)/C1*100; {pole height %}

{EXIT CONDITIONS}
BREAK1 > 1.94* Power(pole ,.724) { Exit condition 1- profit target }
OR C < REF(LLV(C,X1),-D2) { Exit condition 2 stop loss on breaking }
                          { of lower flag trendline}
OR (D2 > 14 AND BREAK1 < .25*POLE) { Exit condition 3-inactivity }
OR (D2 > 3 AND C < .9*HHV(C,4)) { Exit condition 4-trailing stop loss}
OR D2 > 24 { Exit condition 5-time exit}




To recreate this test, first click on "Enhanced system tester" in 
MetaStock, then click on "New system," and type the buy and sell order 
code shown above. Then, to run the test, click on "New simulation," 
next add securities, select the stocks that you want to test, click on 
"Next," type in an initial equity of $10,000, select "Default size 
transaction cost 10000," select only long trades, and check "Close all 
positions on the last bar." Then click on "More...," fill in an interest 
rate of 3%, and fill in "10 points per transaction" for the commissions. 
Click on "trade execution," uncheck "realistic market prices," and select 
"Buy price at open" and "Sell price at close."  Fill in one day for the 
delay.
                
           -Markos Katsanos