Parabolic SAR in MetaTrader 5 — MQL5 Source Code

Reading the built-in iSAR for a trailing-stop EA

Parabolic SAR in MetaTrader 5
Parabolic SAR in MetaTrader 5

The Parabolic SAR’s recursive flip logic is fiddly to get exactly right (see the explainer), and MT5’s built-in iSAR implements it correctly — so for MQL5 the sensible path is to read the handle and use it, especially as a trailing stop in an Expert Advisor.

What you’ll need

  • MetaTrader 5 with MetaEditor

Reading iSAR in an EA

//+------------------------------------------------------------------+
//| AlgoGen Parabolic SAR reader — for use inside an EA              |
//+------------------------------------------------------------------+
input double InpStep = 0.02;   // acceleration step
input double InpMax  = 0.20;   // acceleration maximum

int sarHandle;

int OnInit()
  {
   sarHandle = iSAR(_Symbol, _Period, InpStep, InpMax);
   if(sarHandle == INVALID_HANDLE)
      return(INIT_FAILED);
   return(INIT_SUCCEEDED);
  }

// Read the last two SAR values and detect a flip vs price.
bool ReadSAR(double &sarNow, double &sarPrev)
  {
   double s[];
   if(CopyBuffer(sarHandle, 0, 0, 3, s) < 3) return(false);
   sarNow  = s[2];   // last closed bar
   sarPrev = s[1];
   return(true);
  }

// Example: is the SAR below price (uptrend) on the last closed bar?
bool SarBullish()
  {
   double sarNow, sarPrev;
   if(!ReadSAR(sarNow, sarPrev)) return(false);
   double closes[];
   if(CopyClose(_Symbol, _Period, 0, 3, closes) < 3) return(false);
   return(sarNow < closes[2]);
  }

iSAR(symbol, period, step, max) gives you Wilder’s Parabolic SAR with the standard 0.02 / 0.20 acceleration parameters. Reading buffer 0 with CopyBuffer and comparing to price tells you which side the dots are on — the trailing-stop value you’d manage a position against.

The algorithm behind the buffer

If you must hand-roll it (for a custom acceleration schedule, say), the logic is exactly the Python version: track trend direction, extreme point, and acceleration factor; step SAR += AF·(EP − SAR) each bar; clamp the SAR out of the prior two bars’ range; and on a price cross, flip direction, seed the new SAR at the old EP, and reset AF. It’s a genuine state machine, which is why the built-in is the pragmatic default.

Gotchas

  • Trailing stops. The most robust use is as a trailing stop: on each new bar, move your stop to the SAR value if it’s more protective. Don’t move it against the trade.
  • Use [2] / last closed bar. Reading the still-forming bar [0] gives a SAR that can still change; use the last closed value for decisions.
  • MQL4. iSAR exists there too, with the same step/max parameters.

Same indicator elsewhere: Python, Pine Script, EasyLanguage, NinjaScript. Then test a SAR trailing stop in AlgoGen.


This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.

Sources and further reading

  1. Technical indicator functions (MetaQuotes)

Historical research from the Algogen archive. Not investment advice.

Blog

Have a strategy idea?

Describe it in plain English, preview where your rules fire, then decide whether the evidence is worth a backtest.

Keep me postedHow it works