[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [amibroker] Complete AFL code for "Flags and Pennants" Trading System



PureBytes Links

Trading Reference Links

Thanks for the quick reply Prashanth.
 
Anil Desai
----- Original Message -----
From: Prashanth
Sent: Tuesday, June 14, 2005 10:28 PM
Subject: Re: [amibroker] Complete AFL code for "Flags and Pennants" Trading System

Hello Anil,
 
Its only a word wrap which has caused the problem.
 
Attached is the code file.
 
Cheers
 
Prashanth
----- Original Message -----
From: gadee333
Sent: Tuesday, June 14, 2005 10:10 PM
Subject: Re: [amibroker] Complete AFL code for "Flags and Pennants" Trading System

I am getting a syntex error on R (underlined for identification) in the following line.
What should I do.
 
LOWER FLAG TRENDLINE
 
Anil
----- Original Message -----
Sent: Tuesday, June 14, 2005 6:42 PM
Subject: [amibroker] Complete AFL code for "Flags and Pennants" Trading System

Below is the complete AFL code for Markos Katsano's 'Flags and
Pennants' Trading System.  Markos articles appeared in the April and
May 2005 TASC issues.  The May 2005 issue had the Metastock code for
the trading system.  I've simply translated the Metastock code to
AFL.  To check for accuracy, I attempted to run the AFL code against
the examples illustrated in the articles.  My AFL translation DID NOT
pick up the 'flags and pennants' shown in the examples.  That makes
me suspect that my translation is not absolutely correct.  However, I
backtested the AFL code against a small database of stocks (over a 5
year period) and the backtest/scan was able to pick out 'flags and
pennants' breakouts with no problems.  For e.g., LJPC went on to make
a 77% profit in a mth after being identified as making a breakout by
the code.  Another e.g is IDEV which went on to make 82% in less than
5 wks.......there are many more.  Of course, some breakouts failed
but the AFL sell rules quickly eliminated them.  This system works
best on small caps and you would need more than 300 bars of data for
the trading rules to work (one of the indicators, VFI, needs more
than a year of data).  Also, the 'flags and pennants' don't occur too
frequently so you would need to scan a large database of stocks (at
least a 1000) to get a decent number of buy signals or to get a large
sample size for valid backtesting.  More signals are picked up during
overall market bull periods.  Some small caps have 3-4 'flags and
pennants' occurring over a period of 1-2 years.
BOTTOM LINE - THE CODE WORKS AND IS A GREAT SCAN FOR BREAKOUT STOCKS
BUT WOULD, PROBABLY, NEED TO BE TWEAKED BY AN 'METASTOCK TO AFL'
GURU.  I've included the Metastock code below the AFL for
afficianados to find my errors, if any.  All comments are welcome.

Also - Markos Katsanos uses the FVE, VFI, Stoch and ADX indicator
to 'strengthen' his system.  I don't believe its really necessary to
include those to find 'flags and pennants'.  You can remove the
sections - "FVE indicator code", "VFI indicator code" and Buy
Conditions 6 and 7 and still have a great system (call it 'F&P
Lite').  You would then need only 2 mths of data to run the system. 
Enough talk - here's the AFL code........ 

*****************************************************************

// Translation of Markos Katsanos Metastock Code for 'Flags and
Pennants' Trading System

ZZ = Zig(C,17);
X = BarsSince(ZZ<Ref(ZZ,-1) AND Ref(ZZ,-1)> Ref(ZZ,-2));
X1 = LastValue(X) + 1;
X2 = X1 + 1;
SD = StDev(C,X2);

// FVE indicator code

PERIOD = 22;
COEFF = 0.1;
intRA = log(H) - log(L);
VINTRA = StDev(intRA, PERIOD);
intER = log(Avg)-log(Ref(Avg,-1));
VINTER = StDev(intER,PERIOD);
CUTOFF = COEFF*(VINTER + VINTRA)*C;
MF = C - (H+L)/2 + Avg - Ref(Avg,-1);
MFV = IIf(MF>CUTOFF,V,IIf(MF<-CUTOFF,-V,0));
FVE = Sum(MFV,PERIOD)/MA(V,PERIOD)/PERIOD*100;

// VFI indicator code

PERIOD1 = 130;
COEF = 0.2;
VCoef = 2.5;
VINTER1 = StDev(intER, 30);
Cutoff1 = COEF * VINTER1 * C;
VAVE = Ref(MA(V, PERIOD1), -1);
VMAX = VAVE * VCoef;
Vc = Min (V, VMAX);
MF1 = Avg - Ref(Avg, -1);
VCP = IIf( MF1 > Cutoff1, Vc, IIf (MF1 < -Cutoff1, -Vc, 0));
VFI = Sum(VCP, PERIOD1)/VAVE;

// Buy Conditions

Buy = 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 StochK(20,3)>55 AND ADX(10)>30 //CONDITION 6
AND FVE>10 AND VFI>-3 //CONDITION 7
AND C>Ref(C,-1) AND C>O; //CONDITION 8

//SELL ORDER

D1 = ApplyStop(stopTypeNBar,stopModeBars, 24, True); D2 = LastValue
(D1);
zz = Zig(Ref(C,-D2),17);
X = BarsSince(ZZ<Ref(ZZ,-1) AND Ref(ZZ,-1)> 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 = ApplyStop(stopTypeNBar,stopModePercent, 45, True);
POLE = (C2-C1)/C1*100; //POLE HEIGHT %

// EXIT CONDITIONS

Sell = BREAK1 > 1.94*(POLE^0.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

****************************************************************
****************************************************************

//MARKOS KATSANOS METASTOCK CODE FOR DETECTING BREAKOUTS IN FLAGS AND
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;
COEFF: = 0.1;
INTRA: = log(H) - log(L);
VINTRA: = StDev(INTRA, PERIOD);
INTER: = log(Typical())-log(Ref(Typical(),-1));
VINTER: = StDev(INTER,PERIOD);
CUTOFF: = COEFF*(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 //AFL GURUS - CODE FOR VFI
IS BELOW

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,-1) AND Ref(ZZ,-1)> 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 - (in
article he has profit target as 1.94*pole^0.724)//

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

//METASTOCK CODE FOR VFI FORMULA//
PERIOD: = Input("PERIOD FOR VFI",5,1300,130);
COEF: = .2;
VCOEF: = Input("MAX VOLUME CUTOFF",0,50,2.5);
INTER: = Log(Typical())-Log(Ref(Typical(),-1));
VINTER: = Stdev(INTER,30);
CUTOFF: = COEF*VINTER*C;
VAVE: = Ref(Mov(V,PERIOD,S),-1);
VMAX: = VAVE*VCOEF;
VC: = If(V<VMAX,V,VMAX);
MF: = Typical()-Ref(Typical(),-1);
VFI: = Sum(If(MF>CUTOFF,+VC,If(MF<-CUTOFF,-VC,0)),PERIOD)/VAVE;
Mov(VFI,3,E);







Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html





Yahoo! Groups Links