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(ZZ[Ref(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]
|