7

Parabolic SAR Trading System: Amibroker AFL

Parabolic SAR, also known as PSAR is one of the simplest yet powerful technical indicators. Most of the technical indicators that we have discussed so far assists in determining the start or direction of trend, but parabolic SAR helps to know when the ongoing trend ends and the next one starts. SAR stands for “Stop and Reverse”. PSAR is developed by famous technical analyst “Welles Wilder“, who also developed other popular indicators like RSI, ADX and ATR. This indicator is based on momentum with respect to time and very accurately predicts price reversals. In this article, we would go through a Parabolic SAR Trading system for NSE Nifty. This profitable strategy can very well be used for other scrips and instruments as well.

Please visit Trading Tuitions Academy to learn AFL coding and create your own Trading systems.

How to use Parabolic SAR for trading?

Parabolic SAR is incredibly simple to use. When imposed over a stock chart, it plots a series of dots either above or below the price line.

  • If PSAR dotted line is below the price, it indicates buy signal
  • If PSAR dotted line is above the price, it indicates sell signal
ParabolicSAR

Source: http://www.onlinetradingconcepts.com/

It works well in trending markets, and exhibits whipsaws during sideways market. Many traders also uses parabolic SAR as a stop loss indicator to exit their trade.

Parabolic SAR Trading System – AFL Overview

Parameter Value
Preferred Time-frame
 Daily
Indicators Used  SAR (Stop and Reverse)
Buy Condition  Price candle crosses SAR dotted line from bottom
Short Condition  Price candle crosses SAR dotted line from top
Sell Condition
  • Same as Short
  • Stop Loss Hit
  • Target met
Cover Condition
  • Same as Buy
  • Stop Loss Hit
  • Target met
Stop Loss  1%
Targets  5%
Position Size  100% of Equity
Initial Equity  200000
Brokerage  100 per order
Margin  10%

Parabolic SAR Trading System – AFL Code

//------------------------------------------------------
//
//  Formula Name:    Parabolic SAR
//  Author/Uploader: Trading Tuitions
//  E-mail:          support@tradingtuitions.com
//  Website:         www.tradingtuitions.com
//------------------------------------------------------

_SECTION_BEGIN("Parabolic SAR");

SetTradeDelays( 1, 1, 1, 1 );
SetOption( "InitialEquity", 200000);
SetOption("FuturesMode" ,True);
SetOption("MinShares",1);
SetOption("CommissionMode",2);
SetOption("CommissionAmount",100);
SetOption("AccountMargin",10);
SetOption("RefreshWhenCompleted",True);
SetPositionSize(100,spsPercentOfEquity);
SetOption( "AllowPositionShrinking", True );
BuyPrice=Open;
SellPrice=Open;
ShortPrice=Open;
CoverPrice=Open;

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( Close, "Price", colorWhite, styleCandle );

accel = Param("Acceleration", 0.1, 0, 1, 0.01); 
mx = Param("Max. acceleration", 0.2, 0, 1, 0.1); 

F_SAR = SAR(accel,mx); 

colordots = IIf(F_SAR < L,colorGreen,IIf(F_SAR> H,colorRed,colorWhite)); 

Buy = Cross(C,F_SAR); 
Short = Cross(F_SAR,C); 
Sell=Short;
Cover=Buy;
Plot(F_SAR,"\nF_SAR",colordots,styleDots|styleNoLine); 


Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);

Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);

StopLoss=Param("stop",1,0.5,3,0.5);
ApplyStop(Type=0,Mode=1,Amount=StopLoss);
	
Target=Param("Target",5,0.5,5,0.5);
ApplyStop(Type=1,Mode=1,Amount=Target);

printf("\nBuy : " + Buy );  
printf("\nSell : " + Sell );  
printf("\nShort : " + Short );  
printf("\nCover : " + Cover ); 


/* Plot Buy and Sell Signal Arrows */
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(Cover, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Cover, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50);
PlotShapes(IIf(Cover, 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);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);



_SECTION_END();

AFL Screenshot

As you can see in the chart, the frequency of signals is quite high. It can be further optimized by changing the parameters or using it in conjunction with other indicators.

Parabolic SAR Trading System

Also Read: USD-INR Currency Trading Strategy

Parabolic SAR Trading System- Backtest Report

Nifty SAR trading system has a consistent performance with very low drawdown. Monte Carlo simulation also shows impressive results, with less than 10% chances of failures.

Parameter Value
Nifty
Initial Capital 200000
Final Capital 1311059.26
Scrip Name NSE Nifty
Backtest Period 01-Jan-2004 to 25-Sep-2017
Timeframe Daily
Net Profit % 555.53%
Annual Return % 15.45%
Number of Trades 556
Winning Trade % 34.35%
Average holding Period 4.33 periods
Max consecutive losses 14
Max system % drawdown -22.18%
Max Trade % drawdown -5.98%

Download the detailed backtest report here.

Equity Curve

The equity curve is consistent and almost linear

Profit Table

Parabolic SAR Profit Table

Additional Amibroker settings for backtesting

Goto Symbol–>Information, and specify the lot size and margin requirement. The below screenshot shows lot size of 75 and margin requirement of 10% for NSE Nifty:

Symbol Info_Nifty

Disclaimer:

All the AFL’s posted in this section are for learning purpose. Trading Tuitions does not necessarily own these AFL’s and we don’t have any intellectual property rights on them. We might copy useful AFL’s from public forums and post it in this section in a presentable format. The intent is not to copy anybody’s work but to share knowledge. If you find any misleading or non-reproducible content then please inform us at support@tradingtuitions.com

Related Posts

7 Comments

  1. Hi, Can you help me on the code of the modified version of the same strategy
    Example:
    In candle 1: Suppose a stock is trading at 100 and SAR is 98.
    In candle 2: It cross over and move upwards of SAR. CMP post cross over is 105 and new SAR is 103.
    In candle 3: Then suppose stock closes at 102, remains a close above new SAR of candle 2 but below close of candle 2.
    In candle 4: stock closes at 106, above the close of cross over candle.
    Basically want a code to find list of all stocks which closes above the close of cross over candle amongst all the stocks in my universe.
    awaiting positive response from your end…

  2. Hi Admin,
    Why do we have to setTradeDelays in this AFL? What is the use of trade delays?

    • Hi Richard,

      Trade Delay is set so that position is taken at the open price of next candle. For ex: if signals comes at EOD today, trade should be taken at Open price tomorrow.

  3. Check open equals high candle for sell side, if any candle forms like that in a sell parabolic trend…. Sell below that candle low.. Similar for buy just reverse

  4. im having trouble running a back trest on trhe formula.
    Ive cut and pasted it and tried to run a back test. a message comes up format specifier error.

    what can i do??

  5. do any of these formulas work?????

    another error. that 3 out of 3

    can someone suggest a formular that actually works.

    john

    • Hi John,

      Sorry to hear that. Few of the functions do not work in some versions of Amibroker. Please trying commenting or deleting below line in the formula:

      _N(Title = StrFormat(“{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}”, O, H, L, C ));

Leave a Reply

Your email address will not be published.