Quick Answer
The runtime error "The requested historical offset (X) is beyond the historical buffer's limit (Y)" (RE10143 in the TradingView documentation) means that your script used the [] operator to read further back than the history that Pine kept for that series. Pine sizes each history buffer automatically, from the references it sees in the first 244 bars. If a script needs a deeper reference only later, most often on realtime bars, the buffer does not grow and the error is raised.
The fix is to tell Pine how much history you need. The documentation prefers the max_bars_back(series, length) function, which sizes the buffer of one series (up to 5000 bars back). The other options are the max_bars_back parameter of indicator() or strategy(), which sizes every series and costs more memory, and clamping the offset with math.min(offset, 5000). This post shows the broken pattern, each fix as a short script, the drawing case that surprises many authors, and what the same idea looks like in MQL5.
From the author: I convert Pine Script to MQL5 and cTrader C#. This error is a good example of a Pine habit that has no direct copy: Pine gives you
naon early bars and manages the history for you, while MQL5 and C# make you ask for the bars you need. The error often shows up only when a chart goes live, so a script that "worked" on history can fail the next morning.
Key Facts
| Topic | What the documentation says |
|---|---|
| Message | The requested historical offset (X) is beyond the historical buffer's limit (Y) |
| Automatic sizing | Based on historical references seen in the first 244 bars |
| When it appears | Mostly on realtime bars, because buffers are not resized after the script loads |
| Preferred fix | max_bars_back(series, length), up to 5000 bars back |
| Other fix | max_bars_back parameter in indicator() or strategy() (sizes all series) |
| Offsets above the maximum | Cap them with math.min(offset, 5000) |
| Drawings | A drawing anchored to a past bar_index can need time history |
| Cost of a large buffer | It can hurt performance and reach the memory limit |
The Broken Script
This is the pattern from the documentation, written as a short indicator. The historical bars need 500 bars back, and the realtime bars need 1000:
//@version=6
indicator("Broken: realtime offset")
float myVar = close[barstate.ishistory ? 500 : 1000]
plot(myVar)
On history, the script only ever looks 500 bars back, so the automatic sizing makes a buffer for 500. When the first realtime bar arrives and the script asks for 1000, the buffer is too small and does not grow.
Fix 1: The max_bars_back() Function
The documentation prefers this fix, because it sizes only the series that needs it. The call can sit anywhere in the script, even after the line that causes the problem:
//@version=6
indicator("Fixed: max_bars_back()")
float myVar = close[barstate.ishistory ? 500 : 1000]
plot(myVar)
max_bars_back(close, 1000) // keep at least 1000 past values of close
Fix 2: Make the Deep Reference Run on an Early Bar
The documentation shows a second way: a larger reference on the first bar returns na, so the output does not change, but it makes the system create a big enough buffer from the start:
//@version=6
indicator("Fixed: early-bar reference")
float myVar = close[barstate.isfirst ? 1000 : barstate.ishistory ? 500 : 1000]
plot(myVar)
This works, but it is harder to read. Use max_bars_back() unless you have a reason.
Fix 3: The max_bars_back Parameter of indicator() or strategy()
//@version=6
indicator("Declaration parameter", max_bars_back = 1000)
float myVar = close[barstate.ishistory ? 500 : 1000]
plot(myVar)
This sets the initial size of every series in the script. The documentation advises to use it only when most series need the same size, because it costs memory and speed.
Fix 4: Clamp the Offset
If the offset comes from an input or a calculation, cap it at the largest allowed value:
//@version=6
indicator("Clamped offset")
lookback = input.int(2000, "Lookback", minval = 1)
offset = math.min(lookback, 5000)
plot(close[offset])
The documentation says that referencing further back than the maximum buffer size gives a different runtime error, and clamping avoids it. Note that 5000 is the limit that the page gives for max_bars_back(); check the current limits of your plan in the TradingView documentation.
The Drawing Case
The documentation lists a cause that is easy to miss: a drawing placed with a past bar_index needs the time of that bar, so it reads the time series internally. On realtime bars this can go beyond the time buffer:
//@version=6
indicator("Broken: drawing", overlay = true)
if barstate.isrealtime
line.new(x1 = bar_index - 500, y1 = close, x2 = bar_index, y2 = close)
Size the time buffer for the longest reach:
//@version=6
indicator("Fixed: drawing", overlay = true)
if barstate.isrealtime
line.new(x1 = bar_index - 500, y1 = close, x2 = bar_index, y2 = close)
max_bars_back(time, 500)
Keep the Buffers Small
The documentation warns that oversized buffers can slow a script and hit the memory limit, and it advises to rely on automatic sizing until this error appears. So the sequence is: leave it alone, and when the error shows, size the one series that needs more.
What the Same Idea Looks Like in MQL5 and cTrader
Pine hides the history from you, and a converted EA has to ask for it:
| Pine Script | MQL5 | cTrader C# |
|---|---|---|
| close[n] on a series | CopyClose(_Symbol, _Period, 0, count, array) with enough count | Bars.ClosePrices.Last(n) |
| History too short early on | Bars(_Symbol, _Period) is smaller than needed | Bars.Count is smaller than needed |
| na is returned for early bars | The copy returns fewer bars, or an array index is out of range | An index or argument out of range exception |
In MQL5, check how many bars you have before you read, and return when there are too few. See MQL5 array out of range and, for the Pine side of reading older bars, Pine Script close[1] in MQL5. In cTrader the matching problem is in the cBot index out of range exception. The reading of closed bars is explained in barstate.isnew to MQL5.
Similar Pine Script Messages
| Message | What it means | Where to look |
|---|---|---|
| Historical offset beyond the buffer's limit | A [] reference is deeper than the buffer | This post |
| Function should be called on each calculation | A history function runs conditionally | CW10003 warning |
| Cannot use plot in local scope | plot() is inside a block | Pine plot in local scope |
| series int vs simple int | A function needs a constant length | series int, simple int |
| Undeclared identifier | A name Pine does not know | Pine undeclared identifier |
Checklist: What to Check When You See This Error
- Find the
[]reference with the largest or most variable offset. - Check if it depends on
barstate.isrealtime, an input, or a state. - Size the series with
max_bars_back(series, length). - Use the
timeseries inmax_bars_backif a drawing reaches into the past. - Clamp the offset with
math.min(offset, 5000). - Use the declaration parameter only when many series need the same size.
- Keep the size as small as needed.
- Test on a live chart and wait for realtime bars.
Common Mistakes
- Testing on history only. The error often shows on realtime bars.
- Setting a huge
max_bars_backin the declaration for every series. - Forgetting drawings that reach into the past.
- Using an input offset with no upper limit.
- Assuming a converted EA has the same history without checking the bar count.
- Ignoring the memory limit when you raise the buffers.
FAQ
Related Guides
- Pine Script close[1] in MQL5: reading older bars in MQL5.
- Pine Script barstate.isnew to MQL5: closed bars and realtime bars.
- MQL5 array out of range: the MQL5 side of reading too far back.
- cTrader cBot index out of range exception: the cTrader side.
- Pine Script function should be called on each calculation: history functions and conditions.
- Pine Script var and varip to MQL5: keep state between bars.
- Pine Script series int, simple int: a length that must be a constant.
- Pine Script to MQL5 guide: converting a script step by step.
- Pine Script memory limits exceeded: the RE10139 runtime error and how to reduce memory use.
Convert Without Guessing
If you want a first draft of your Pine Script in MQL5 or cTrader C#, try the CodeFlowOS converter. It produces a draft that you still have to review, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong logic, so always compare the result with the chart. If the converted code does not compile, paste it with the message into the Error Fixer: it suggests a minimal fix and shows what changed (1 credit, with up to 3 attempts included).
This article is educational. It does not describe trading performance and is not financial advice. Test every script on a chart or a demo account before you rely on it.