Daily Pivot Points: TradingView (Pine Script) to MT5 (MQL5)
Short answer
The seven levels come from the previous day's high, low and close: request.security(..., "D", high[1]) in Pine, iHigh(_Symbol, PERIOD_D1, shift) with iBarShift in MQL5. Read the day relative to the bar being computed, not to now, and note that the broker's D1 bar may cover different hours.
Standard daily pivot points are seven levels computed once per day from the previous day's high, low and close. In Pine Script the previous day comes from request.security(..., "D", high[1]); in MQL5 from iHigh(_Symbol, PERIOD_D1, shift) with the shift found by iBarShift. The arithmetic is identical on both platforms. The two places a port goes wrong are reading the daily bar relative to now instead of relative to the bar being computed, which paints today's levels across the entire history, and the fact that TradingView's "day" and the broker's D1 bar do not always cover the same hours.
How daily pivot points are calculated
With H, L, C the previous day's high, low and close:
- P = (H + L + C) / 3
- R1 = 2P − L, S1 = 2P − H
- R2 = P + (H − L), S2 = P − (H − L)
- R3 = H + 2(P − L), S3 = L − 2(H − P)
The levels are constant for the whole of the current day and step to new values at the day boundary. There are no parameters in the classic form; variants (Fibonacci, Camarilla, Woodie) change the multipliers but not the data access, so everything below applies to them too.
Which bar is "the previous day" is a platform question. TradingView's daily bar follows the symbol's exchange session: for US stocks 09:30 to 16:00 New York time, for forex a 17:00 New York rollover. A MetaTrader broker's D1 bar runs from server midnight to server midnight, often in GMT+2 or GMT+3, and some brokers publish a short Sunday bar. Different hours give a different H, L and C and therefore different pivots; this is the main reason levels on the two platforms disagree by a few points.
Pine Script v6 version
An original script. The [1] inside the request.security expression together with lookahead_on is the standard idiom for "the completed previous daily bar on every intraday bar of today": the offset is applied in the daily context, and the lookahead returns the daily bar that contains the current bar instead of the one before it, so the result is yesterday's values and does not repaint.
//@version=6
indicator("Daily Pivot Points", shorttitle="Pivots", overlay=true)
// Yesterday's completed daily bar. [1] with lookahead_on gives the previous
// day's values on every intraday bar of today without repainting.
dHigh = request.security(syminfo.tickerid, "D", high[1], lookahead=barmerge.lookahead_on)
dLow = request.security(syminfo.tickerid, "D", low[1], lookahead=barmerge.lookahead_on)
dClose = request.security(syminfo.tickerid, "D", close[1], lookahead=barmerge.lookahead_on)
P = (dHigh + dLow + dClose) / 3
R1 = 2 * P - dLow
S1 = 2 * P - dHigh
R2 = P + (dHigh - dLow)
S2 = P - (dHigh - dLow)
R3 = dHigh + 2 * (P - dLow)
S3 = dLow - 2 * (dHigh - P)
plot(P, "P", color=color.gray, linewidth=2)
plot(R1, "R1", color=color.red)
plot(R2, "R2", color=color.red)
plot(R3, "R3", color=color.red)
plot(S1, "S1", color=color.green)
plot(S2, "S2", color=color.green)
plot(S3, "S3", color=color.green)On a daily or higher chart the script still works but is pointless: every bar is its own day and the levels step on every bar.
MQL5 version
Seven buffers, one per level. For each chart bar, iBarShift(_Symbol, PERIOD_D1, time[i]) finds the daily bar that contains it, and + 1 steps to the completed one before it. This is the whole translation of the request.security line: the daily index is measured from the bar being computed, not from the newest bar. A plain iHigh(_Symbol, PERIOD_D1, 1) would be correct only for today and would be written into every historical bar.
//+------------------------------------------------------------------+
//| Daily pivot points from the previous D1 bar's high, low, close. |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 7
#property indicator_plots 7
#property indicator_label1 "P"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGray
#property indicator_width1 2
#property indicator_label2 "R1"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_label3 "R2"
#property indicator_type3 DRAW_LINE
#property indicator_color3 clrRed
#property indicator_label4 "R3"
#property indicator_type4 DRAW_LINE
#property indicator_color4 clrRed
#property indicator_label5 "S1"
#property indicator_type5 DRAW_LINE
#property indicator_color5 clrGreen
#property indicator_label6 "S2"
#property indicator_type6 DRAW_LINE
#property indicator_color6 clrGreen
#property indicator_label7 "S3"
#property indicator_type7 DRAW_LINE
#property indicator_color7 clrGreen
double PBuf[], R1Buf[], R2Buf[], R3Buf[], S1Buf[], S2Buf[], S3Buf[];
int OnInit()
{
SetIndexBuffer(0, PBuf, INDICATOR_DATA);
SetIndexBuffer(1, R1Buf, INDICATOR_DATA);
SetIndexBuffer(2, R2Buf, INDICATOR_DATA);
SetIndexBuffer(3, R3Buf, INDICATOR_DATA);
SetIndexBuffer(4, S1Buf, INDICATOR_DATA);
SetIndexBuffer(5, S2Buf, INDICATOR_DATA);
SetIndexBuffer(6, S3Buf, INDICATOR_DATA);
for(int p = 0; p < 7; p++)
PlotIndexSetDouble(p, PLOT_EMPTY_VALUE, EMPTY_VALUE);
IndicatorSetString(INDICATOR_SHORTNAME, "Daily Pivots");
return INIT_SUCCEEDED;
}
void SetEmpty(const int i)
{
PBuf[i] = EMPTY_VALUE;
R1Buf[i] = EMPTY_VALUE; R2Buf[i] = EMPTY_VALUE; R3Buf[i] = EMPTY_VALUE;
S1Buf[i] = EMPTY_VALUE; S2Buf[i] = EMPTY_VALUE; S3Buf[i] = EMPTY_VALUE;
}
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[])
{
int start = (prev_calculated > 0) ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
// The D1 bar that contains this bar, then the completed one before it.
// This is request.security(syminfo.tickerid, "D", high[1], lookahead_on).
int d = iBarShift(_Symbol, PERIOD_D1, time[i], false);
double h = iHigh(_Symbol, PERIOD_D1, d + 1);
double l = iLow(_Symbol, PERIOD_D1, d + 1);
double c = iClose(_Symbol, PERIOD_D1, d + 1);
if(d < 0 || h <= 0.0 || l <= 0.0 || c <= 0.0)
{
SetEmpty(i);
if(i == rates_total - 1)
return 0; // D1 history still loading: redo everything on the next tick
continue;
}
double p = (h + l + c) / 3.0;
PBuf[i] = p;
R1Buf[i] = 2.0 * p - l;
S1Buf[i] = 2.0 * p - h;
R2Buf[i] = p + (h - l);
S2Buf[i] = p - (h - l);
R3Buf[i] = h + 2.0 * (p - l);
S3Buf[i] = l - 2.0 * (h - p);
}
return rates_total;
}The early return 0 handles a MetaTrader quirk: the first time an indicator asks for another timeframe the terminal may still be downloading it and iHigh returns 0. Returning 0 from OnCalculate makes the next tick recalculate the whole chart, by which time the daily bars are usually there. On a long chart the per-bar iBarShift call is the slow part; if it matters, cache the daily values and refresh them only when time[i] / 86400 changes.
If you only want today's levels, draw objects instead of buffers. The following fragment, run for the newest bar, keeps seven horizontal lines current and needs no history:
string names[7] = {"P", "R1", "R2", "R3", "S1", "S2", "S3"};
double level[7] = {p, r1, r2, r3, s1, s2, s3};
for(int k = 0; k < 7; k++)
{
string id = "Pivot_" + names[k];
if(ObjectFind(0, id) < 0)
ObjectCreate(0, id, OBJ_HLINE, 0, 0, level[k]);
ObjectSetDouble(0, id, OBJPROP_PRICE, level[k]);
}Buffers give you the history, work in the Strategy Tester and can be read with CopyBuffer; objects are lighter and never need the alignment logic. Most pivot indicators on MetaTrader use buffers for exactly the reasons the Pine version plots series.
Differences you will see on the chart
| TradingView | MetaTrader 5 | |
|---|---|---|
| Source of the daily bar | Exchange session of the symbol | Broker D1 bar, server midnight to midnight |
| Day boundary on the chart | Levels step at the session open | Levels step at server midnight |
| Sunday bars | None for most symbols | Some brokers produce a short Sunday D1 bar whose H, L, C become Monday's pivots |
| Join between days | plot() draws a vertical joint; plot.style_linebr would need na to break | DRAW_LINE draws the same joint; use DRAW_SECTION or write EMPTY_VALUE on the first bar of the day to break it |
| Oldest bars | na where there is no previous daily bar | EMPTY_VALUE where D1 history ends before the chart does |
| Realtime bar | Constant during the day | Constant during the day |
Where the sessions coincide (for example a forex broker on New York-aligned server time) the levels match to the tick. Where they do not, both programs are right about their own data.
Using it in MetaTrader 5
Save as DailyPivots.mq5 under MQL5\Indicators, compile with F7 and attach to an intraday chart. Open the D1 chart of the same symbol once so the daily history is present. From an EA, iCustom(_Symbol, _Period, "DailyPivots") creates the handle; buffer 0 is P, 1 to 3 are R1 to R3, 4 to 6 are S1 to S3, and index 0 in CopyBuffer holds today's values. For alerts, test a cross of close over a level on the last closed bar, since the level itself does not move during the day.
What the Stratlate converter emits
The Pine version converts with status APPROXIMATED, not CLEAN, and the report says why. Each request.security line becomes pine_htf_price(NULL, PERIOD_D1, kind, iBarShift(NULL, PERIOD_D1, time[bar]) + 1): the engine anchors the daily index on the bar being replayed exactly as the hand-written version does, applies the [1] as + 1, and because lookahead_on is set it does not add the extra step back that lookahead_off would need. Two notes accompany it. PM292 (warning) states that TradingView aligns the higher timeframe to the exchange session and MetaTrader to the server day, so values at a session boundary can differ and should be checked against the broker's daily bars. PM291 (informational) states that with lookahead_on the historical read is reproduced but the way the source repaints on the still-forming daily bar is not; with the [1] idiom that case never arises, since yesterday's bar is complete. The seven plot() calls become seven DRAW_LINE buffers. Compile the output and compare the levels against your broker's D1 chart before trading on it.
Pitfalls
iHigh(_Symbol, PERIOD_D1, 1)inside the bar loop. One value, today's, is written to every bar in history; the backtest sees levels that did not exist yet. Anchor withiBarShift(..., time[i]).- Dropping
lookahead_onbut keeping[1]in Pine. The script then reads the day before yesterday on most bars; levels are a day stale. Either[1]with lookahead on, or no offset with lookahead off, not both. - Not handling missing D1 data. Zero highs produce pivots near zero and the chart autoscale collapses. Check the values and return 0 to retry.
- Comparing against TradingView on a broker whose server day differs from the exchange session. The levels will not match and no amount of code changes that; compare H, L and C first.
- Letting the Sunday bar count as a day. On brokers with a Sunday candle, Monday's pivots come from a two-hour bar. Skip D1 bars with a Sunday date if you want Friday's values instead.
FAQ
Is there a built-in pivot point indicator in MetaTrader 5?
No. The terminal has none; the indicator above or any of the many from the Code Base is needed. The formula is simple enough that writing your own is usually quicker than vetting someone else's alignment logic.
Why are my MT5 pivots a few points off TradingView's?
Almost always because the previous day's high, low or close differs: the broker's D1 bar covers server midnight to midnight while TradingView's daily bar covers the exchange session. Put the two daily bars side by side; if H, L and C differ, the pivots must too.
Should I compute the daily values from the intraday bars instead of iHigh(PERIOD_D1)?
You can scan the chart's own bars for the previous day's extremes, which makes the indicator independent of D1 history and lets you define the day boundary yourself (a session start instead of midnight). The cost is that gaps in intraday history give wrong extremes, which the D1 bar never has.
Convert your own version
Have a variant of this indicator, or any other TradingView script? Paste it and get MQL5 that is checked against the original on the same price history, with every construct that could not be carried over reported.
Open the converter