Pine Script's tuple + history-referencing operator error

Autoview · Guide · Updated August 11, 2026

If you've hit a Pine Script compile error mentioning the history-referencing operator and a tuple right after calling something like ta.macd(), this is why: [] needs a single already-named value to look back on, and a tuple-returning call hasn't produced one of those yet. The fix is two lines, unpack first, then index the piece you need.

What makes a call return a tuple

Most Pine Script functions hand back one value. A handful hand back several at once, a tuple. ta.macd(source, fastLength, slowLength, signalLength) is the standard example: one call, three values, the MACD line, the signal line, and the histogram. ta.stoch(), request.security() with more than one expression, and any custom function whose last line is a bracketed list all work the same way.

[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)

The square brackets on the left are a tuple declaration: one name per value the call returns, in order.

Why [] can't go straight on the call

[] is the history-referencing operator, close[1] means "close, one bar back." It works on one already-named series value. Trying to apply it directly to a call before that call has been unpacked, ta.macd(close, 12, 26, 9)[1], or inside the tuple declaration itself, doesn't give Pine a single value to offset, it gives it three at once with no name yet attached to any of them. That's what triggers the error. It's the same family of restriction as [] only ever applying once to the same already-named value, close[1[2]] is rejected for the same underlying reason: history-referencing needs a settled, single, named series to work against, not an expression still in the middle of producing one.

The fix: unpack first, then index the piece you need

Split the assignment into two steps. Unpack the tuple with a plain, unindexed declaration, then apply [] to whichever of the resulting variables you actually need history on:

[macdLine, signalLine, histLine] = ta.macd(close, 12, 26, 9)
prevMacd = macdLine[1]

macdLine is now an ordinary series variable, no different from close. macdLine[1] works exactly like close[1] does. If you don't need every value the call returns, drop in _ for the ones you're discarding:

[macdLine, _, histLine] = ta.macd(close, 12, 26, 9)
prevHist = histLine[1]

Two things about tuple-unpacked variables to know going in

  • Tuple declarations don't take declaration-mode or qualifier keywords, no var, no varip. Every variable a tuple declaration produces always uses Pine's default declaration mode and reinitializes every bar.
  • Every variable from the same tuple declaration inherits the strongest type qualifier among everything the call returned, including any value you named _ and never touch again. If one piece of a multi-return call is series-qualified, all of the unpacked names are too, even the ones that look like they should be simpler.

Getting the fixed value into an Autoview command

Once prevMacd (or whichever unpacked, indexed variable you need) is an ordinary number, it goes into a live command the same way any other computed Pine Script value does, str.tostring() plus alert(). See Pine Script functions for the full pattern; nothing about the tuple unpacking above changes it once you're past the [] step.

Test before you trust it

Add d=1 to the alert message while you're getting the unpacking right. Autoview logs what it would have sent instead of placing the order, so you can confirm prevMacd (or your equivalent) landed in the command as the number you expected before removing d=1. See the log guide for what to look for.

A note on what Autoview is. Autoview is an execution tool, not a trading or investment advisor. It places the orders your script and alerts tell it to; it doesn't generate signals or make any claim about results. Trading carries risk of loss, and you're responsible for the strategy you automate. See our disclosures.

Back to Pine Script basics