Pivot Points in MetaTrader 5 — MQL5 Source Code

Daily pivot levels drawn from the prior day, for MT5

Pivot Points in MetaTrader 5
Pivot Points in MetaTrader 5

Pivot Points in MQL5 mean pulling the prior day’s high, low, and close and drawing horizontal levels for the current day. See the explainer for the formulas.

What you’ll need

  • MetaTrader 5 with MetaEditor
  • Save as AlgoGen.Pivots.mq5 under MQL5/Indicators/

The source

//+------------------------------------------------------------------+
//|                                               AlgoGen.Pivots.mq5 |
//|                                          Copyright 2026, AlgoGen |
//|                                           https://www.algogen.io |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, AlgoGen"
#property link      "https://www.algogen.io"
#property version   "1.00"
#property indicator_chart_window

void DrawLevel(string name, double price, color clr)
  {
   ObjectDelete(0, name);
   ObjectCreate(0, name, OBJ_HLINE, 0, 0, price);
   ObjectSetInteger(0, name, OBJPROP_COLOR, clr);
   ObjectSetString(0, name, OBJPROP_TEXT, name);
  }

int OnCalculate(const int rates_total, const int prev_calculated,
                const datetime &time[], const double &open[],
                const double &high[],   const double &low[],
                const double &close[],  const long &tick_volume[],
                const long &volume[],   const int &spread[])
  {
   // Prior completed daily bar (shift 1 on the D1 timeframe).
   double pH = iHigh(_Symbol, PERIOD_D1, 1);
   double pL = iLow(_Symbol, PERIOD_D1, 1);
   double pC = iClose(_Symbol, PERIOD_D1, 1);
   if(pH == 0 || pL == 0) return(rates_total);

   double P  = (pH + pL + pC) / 3.0;
   double rng = pH - pL;

   DrawLevel("Pivot_P",  P,                    clrOrange);
   DrawLevel("Pivot_R1", 2 * P - pL,           clrIndianRed);
   DrawLevel("Pivot_S1", 2 * P - pH,           clrMediumSeaGreen);
   DrawLevel("Pivot_R2", P + rng,              clrIndianRed);
   DrawLevel("Pivot_S2", P - rng,              clrMediumSeaGreen);
   DrawLevel("Pivot_R3", pH + 2 * (P - pL),    clrIndianRed);
   DrawLevel("Pivot_S3", pL - 2 * (pH - P),    clrMediumSeaGreen);

   return(rates_total);
  }
//+------------------------------------------------------------------+

Install & compile

  1. Drop it in MQL5/Indicators/, press F7.
  2. Apply to an intraday chart — the seven pivot levels overlay price as horizontal lines, matching the output chart.

Gotchas

  • iHigh(..., PERIOD_D1, 1). Shift 1 is the prior completed daily bar — the correct source. Shift 0 is today’s still-forming bar (look-ahead).
  • Redraw, don’t duplicate. ObjectDelete before ObjectCreate each pass so the lines update in place rather than stacking.
  • MQL4. Same idea with iHigh/iLow/iClose on PERIOD_D1; the object API differs slightly.

Same indicator elsewhere: Python, Pine Script, EasyLanguage, NinjaScript. Then test a pivot rule 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