20% Flipper Strategy Help

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();
1 Like

@hari Nick Radge’s book is a very nice start and presents some easy to understand fully disclosed trading systems. The problem with the “20% Flipper” is that for this system his rules are unclear. He bases the idea on Martin Zweig’s 4% rule from Zweig’s book “Winning on Wall Street”. But Zweig specifically looks for a rise of 4% in a single week (and he was looking at a market index for building a market timing model).

So Radge is looking for stocks that have a 20% rise in one week? Not in the book’s example chart. So, what should be the look-back period? I think that is for the reader to explore. Here is an example of looking back 20 bars (and you can alter it to 30, 40, 100 etc),

UpThreshold = LLV( L, 20) * 1.20; // Close is 20% higher than 20 bar Low

Buy = Cross( Close, UpThreshold );

Zweig's book was briefly discussed on this forum here,

As for the Stop Loss, I can't recall Radge's specific rules but you can simply use a trailing stop with AmiBroker's built in capabilities.
https://www.amibroker.com/guide/afl/applystop.html

If you are looking to Plot the trailing stop, search this forum for many examples but they all start with this Knowledge Base article.
https://www.amibroker.com/kb/2007/03/24/how-to-plot-a-trailing-stop-in-the-price-chart/comment-page-1/

5 Likes

Thanks Larry for the links and the information. I did not think that there was a look back period. Issue i think with using look back periods is that this becomes very specific to the individual stocks as volatility of the stocks are very different.

Infact, the strategy says rise of 20% from "any low". So, what I have done in the code is -

  1. Start from the first bar. ex. 1997-Jan-01 C array.
  2. In a new variable(currentlow), keep track of the low as I traverse towards the latest bar of yesterday.
  3. While moving through the C array in step 2, look for a rise in price of 20% from currentlow to c[i].
  4. If there is a 20% raise then we have the Buy bar. This raise can occur over any number of days.

As I said, the problem I am having are -

  1. As I scroll through the bar, the Buy symbols start appearing. Does this mean the code is in efficient?
  2. I can do the trailing stop exit but I am trying to code the exit as in the strategy(in the second piece of code in my original post), but it exits prematurely.

Thanks,
Hari

@hari I guess we will agree to disagree on the point of "no look back" period. It is not what you were asking but... Radge wrote this as a trend following system i.e. go long stocks in an uptrend. Take the example that a stock takes 10 years to climb 20%. That is hardly a stock in an uptrend.

With regards to your questions. For your charting problem try adding,

SetBarsRequired( sbrAll );

And your problem with the exit is perhaps a small mistake in your calculation of the 20% loss. Try changing the denominator in this line

rateOfSellChange[i] = (((currentHigh[i-1] - C[i]) * 100)/C[i]);

to this (I think this fits the logic concerning the mathematical calculation you want)

rateOfSellChange[i] = (((currentHigh[i-1] - C[i]) * 100)/currentHigh[i-1]);

Good luck!

P.S. in the book he uses the "LOW" and the "HIGH" for defining 20% changes, so a Close 20% above the lowest low. In your code you are using the lowest CLOSE. It's a small point but if you want to stick exactly to the book it's worth pointing out.

1 Like

Agree! Thanks for the pointers, i'll update the code accordingly. Appreciate your time to review and suggest.

1 Like