Cow AFL
Posted: Wed Dec 19, 2007 1:30 pm
Below is an AFL for finding the best cows to milk. It's for use on Yahoo end-of-day data. The statistical computations in the AFL are translated from TRO's stockfetcher code. You can also use this AFL for backetesting. Hope someone finds it useful.
/**********************************/
// Daily Gaps version
SetOption("InitialEquity",100000 );
SetTradeDelays(0,0,0,0);
Multi = 6.25;//Optimize("Multi", 5, 5, 6, 0.2 );
bars = 29;//Optimize("ATR Periods", 25, 25, 30, 1);
pcnt=65;//Optimize("pcnt",65,5,20,5);
positions=25;//Optimize("positions",7,1,21,5);
risk = multi*ATR(bars);
Optimal_shares = (pcnt/100)*Equity()/risk;
SetOption("MaxOpenPositions", positions);
PositionSize = -100/positions;
// Gap statistics
gapped = O - Ref(C,-1);
GapUpp = Sum(gapped>0,1);
GapDwn = Sum(gapped<0,1);
absgap = abs(gapped);
gappedup100 = Sum(GapUpp>0,100);
gappeddn100 = Sum(GapDwn>0,100);
CloseLoR = Ref(C,-1)-L;
CloseHiR = H - Ref(C,-1);
fillup = Sum(CloseLoR>0,1);
filldn = Sum(CloseHiR>0,1);
GapUpfilled = GapUpp*fillup;
GapDnFilled = GapDwn*filldn;
GapUp100f = Sum(gapupfilled>0,100);
GapDn100f = Sum(gapdnfilled>0,100);
gapfilled = GapUp100f + gapdn100f;
//tbars=Optimize("tbars",10,10,40,10);
//bbars=Optimize("bbars",60,10,40,10);
ratio = 2;//Optimize("ratio",1.5,1.9,2.1,0.1);
Cond6=MA(V*C,10)/MA(V*C,20)>ratio;
BAdBuyDay = 4;//Optimize("BadBuyDay",1,1,5,1);
PositionScore = absgap;
// the following line is the trigger if all conditions satisfied
Buy =gapfilled>81 AND O<0.999*Ref(C,-1) AND MA(V,30)>1000000/100 AND C>5;
Short=gapfilled>76 AND O>1.001*Ref(C,-1) AND MA(V,30)>1000000/100 AND C>5;
BuyPrice = O;
ShortPrice = O;
// here we define variables used once in the trade
multi = 6.25;//Optimize("multi",5,5,7,0.25);
ApplyStop( stopTypeLoss, stopModePoint, amount=multi*ATR(29));
YourHold = BarIndex() - ValueWhen(Buy, BarIndex(), 1);//Replace <...> with your 'buy' formula
Multiple = 5;//Optimize("Multiple", 5, 3.5, 4.5, 0.5 ); //How many ATR?fs to be allowed below the highest high since latest "buy" bar
ATRPeriods = 29;//Optimize("ATR Periods", 29, 12, 18, 2); //How many periods to use for the ATR
Plot1 = HHV(Close,YourHold) - (Multiple * ATR(ATRPeriods)); //Chandelier exit line from highest close
Plot2 = HHV(High, YourHold) - (Multiple * ATR(ATRPeriods)); //Chandelier exit line from highest high
Plot(Plot1, "ADRCash", ParamColor( "ADRCash Chandelier", colorBlue ), ParamStyle("Chandelier Style") );
Sell= C>0 OR H>O+EMA(absgap,30);
Cover=C>0 OR L<O-EMA(absgap,30);
SellPrice = C;//IIf(H>O+EMA(absgap,30),O+EMA(absgap,30),C);
CoverPrice = C;//IIf(L<O-EMA(absgap,30),O-EMA(absgap,30),C);
// here we define what gets displayed on the chart
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy , colorYellow, colorRed ), 0, IIf( Buy , Low, High));
Filter = C>5 AND MA(V,30)>1000000/100 AND gapfilled>76; // lists exploration results conforming to our buy criteria
SetOption("NoDefaultColumns", True );
AddTextColumn( Name(), "Symbol", 77 , colorDefault);
AddColumn(Close,"Close",1.2);
AddColumn(Volume*100,"Volume",1.0);
AddColumn(gappedup100,"GappedUp100",1);
AddColumn(gappeddn100,"GappedDn100",1);
AddColumn(gapfilled,"GapFilled",1);
AddColumn(GapUp100f,"GapUp100f",1);
AddColumn(GapDn100f,"GapDn100f",1);
AddColumn(MA(absgap,100),"Avg. Gap",1.2);
/***********************************/