Hello,
I am attempting to code the 20% flipper strategy from the unholy grails book, to learn the AFL programming.
20% Flipper = Buy if the stock has gone up to 20% from any low. Sell when the stock has gone below 20% from any high.
Can anyone suggest if this piece of code is efficient? I do notice that when I scroll through the chart, the buy arrows appear momentarily (they would not be visible on the chart in the first instance). So, I am guessing calculation is happening while I scroll and the Buy/Sell arrows start appearing.
Here is the code below. This only has the Buy built in. Sell is via trailing stop and not as per the 20% flipper strategy.
_SECTION_BEGIN("Custom Price");
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 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("20 Percent Flipper");
currentLow = C;
rateOfChange = C;
for(i=1 ; i<BarCount ; i++)
{
if(C[i] >= currentLow[i-1])
currentLow[i] = currentLow[i-1]; //Current Low value of close
else
currentLow[i] = c[i];
rateOfChange[i] = (((C[i] - currentLow[i-1]) * 100)/currentLow[i-1]);
if(rateofChange[i] >= 20)
currentLow[i] = C[i];
}
Buy = IIf(currentLow>Ref(currentLow,-1), True, False);
//Standard trailing stop code
StopLevel = 1 - Param("trailing stop %", 3, 0.1, 10, 0.1)/100;
Sell = 0;
trailARRAY = Null;
trailstop = 0;
for( i = 1; i < BarCount; i++ )
{
if( trailstop == 0 AND Buy[ i ] )
{
trailstop = High[ i ] * stoplevel;
}
else Buy[ i ] = 0; // remove excess buy signals
if( trailstop > 0 AND Low[ i ] < trailstop )
{
Sell[ i ] = 1;
SellPrice[ i ] = trailstop;
trailstop = 0;
}
if( trailstop > 0 )
{
trailstop = Max( High[ i ] * stoplevel, trailstop );
trailARRAY[ i ] = trailstop;
}
}
Plot( trailARRAY,"trailing stop level", colorRed );
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
_SECTION_END();
I have attempted to include the code below for the sell strategy for the 20% flipper(instead of trailing stop loss as in the above code), but seems incorrect. I would like to compare what I have coded to one that anyone has for this strategy to know how far off I am. Thanks.
_SECTION_BEGIN("Custom Price");
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 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("20 Percent Flipper");
currentLow[0] = C[0];
entryBar = 0;
currentLow = C;
currentHigh = C;
rateOfBuyChange = C;
rateOfSellChange = C;
for(i=1 ; i<BarCount ; i++)
{
//Identify Current Low and ROC.
if(C[i] >= currentLow[i-1])
currentLow[i] = currentLow[i-1]; //Current Low value of close
else
currentLow[i] = c[i];
rateOfBuyChange[i] = (((C[i] - currentLow[i-1]) * 100)/currentLow[i-1]);
if(rateofBuyChange[i] >= 20)
currentLow[i] = C[i];
// Identify Current high and ROC
if(C[i] <= currentHigh[i-1])
currentHigh[i] = currentHigh[i-1];
else
currentHigh[i] = C[i];
rateOfSellChange[i] = (((currentHigh[i-1] - C[i]) * 100)/C[i]);
if(rateofSellChange[i] >= 20)
currentHigh[i] = C[i];
}
//Plot(currentLow,"Current Low",colorRed);
//Plot(currentHigh,"Current Low",colorGreen);
Buy = IIf(currentLow>Ref(currentLow,-1), True, False);
Sell = IIf((Ref(currentHigh,-1) < currentHigh),True,False);
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Sell, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Sell, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);
_SECTION_END();