Same Pine Script, Two Backtests, $1,480 Apart: What request.security() Actually Returns
TL;DR: One line —
lookahead=barmerge.lookahead_onplus[1]— moved a backtest from +$5,209.86 to +$6,690.14 (Profit Factor 2.51 → 3.02, in our testing) on the same strategy, same data, same 23 trades. The "confirmed" version looks better. It is also not reading what its Pine says it reads: the value it returns is, on 93% of bars, the daily close of the first trading day of the current week — not the previous weekly close. The translator compiled all of it with zero diagnostics. That is the point: the dangerous case is not the script that fails, it is the script that compiles cleanly and returns a different number than the one you asked for.
This is the tenth post in a series where I convert Pine strategies to Python line by line. The last post was the version-to-version table — 84 errors became 5 across three recorded stages. This one opens the biggest family in that fix ledger: request.security(), 25 files, one construct. Seventeen of those files were tuple literals, eight were scalar expressions, and the batch moved compile from 1,827 to 1,850 of 1,911 scripts. The construct compiles now. The question this post answers is what it returns — because a translator that compiles your multi-timeframe filter and quietly changes what the filter reads has not finished the job.
The setup: one strategy, two filters, one line apart
The strategy is deliberately boring: a daily EMA 10/30 crossover on the CSI 300, long only, 10% of equity per trade, strategy.close on the crossunder. The only difference between the two variants is the weekly filter — the daily cross only fires when the weekly close is above the weekly SMA 20.
Variant A — the developing value, the default:
wClose = request.security(syminfo.tickerid, "W", close)
wSma = request.security(syminfo.tickerid, "W", ta.sma(close, 20))
Variant B — the "confirmed" value, the pattern every guide recommends:
wClose = request.security(syminfo.tickerid, "W", close[1], lookahead=barmerge.lookahead_on)
wSma = request.security(syminfo.tickerid, "W", ta.sma(close, 20)[1], lookahead=barmerge.lookahead_on)
The data is the same feed every post in this series uses: CSI 300 daily, 4,089 bars, 2010-01-04 to 2026-11-06, with a weekly feed resampled from that same daily file (862 weekly bars — same price path, so every security value in this post is checkable against the daily file by hand). The runtime is the open-source PyneCore project, with the patched build we ship for the two constructs stock PyneCore doesn't handle. Everything below was run tonight, 2026-11-16, on the local machine — nothing in this post is quoted from documentation.
Probe 1: the scalar request works, and I can prove it bar by bar
The compiler output for the scalar probe, verbatim — this is what the translator emits, and what the runtime then runs:
wClose: Series[float] = request.security(syminfo.tickerid, "W", close)
wSma: Series[float] = request.security(syminfo.tickerid, "W", ta.sma(close, 20))
Zero diagnostics. And the values check out against the hand-computed weekly resample, bar for bar:
| Check | Result |
|---|---|
| First non-NaN weekly close | Row 5, 2010-01-11 — the first bar of week 2. The first week has no confirmed weekly bar yet, so there is nothing to return. |
| Weekly close, 2010-01-11 | 3480.13 — exactly the close of the week ending 2010-01-08 in the resampled file |
| Weekly close / SMA 20, 2020-06-01 | 3867.02 / 3914.76 — both match the hand-computed weekly series exactly |
| Last bar, 2026-11-06 | Weekly close reads 3985.50 — the previous week's close, not 4054.28, the current week's final close |
That last row is the documented lookahead_off behaviour, and it is worth stating plainly because it is the behaviour the whole "confirmed" pattern exists to work around: with the default, a daily bar never sees the weekly bar it belongs to. It sees the last one that finished. On the final bar of the sample the current week never "finishes" inside the run, so the value lags one week. That is not a bug. It is the price of not looking ahead — and the next section is about what happens when you try to stop paying it.
Probe 2: the tuple request returns the same numbers, in one call
The fix ledger said 17 files were tuple literals. Here is what the translator does with one:
[wSma, wRsi] = request.security(syminfo.tickerid, "W", [ta.sma(close, 20), ta.rsi(close, 14)])
_pyne_br0 = request.security(syminfo.tickerid, "W", [ta.sma(close, 20), ta.rsi(close, 14)])
wSma: Series[float] = _pyne_br0[0]
wRsi: Series[float] = _pyne_br0[1]
The single request returns both values, and the tuple's SMA is byte-for-byte the scalar probe's SMA: 0 differing rows out of 4,089. The RSI runs 22.66 to 92.1 across 4,015 non-NaN bars — a sane range for a weekly RSI 14 on this index. The tuple path, the part that used to fail analysis on 17 scripts, now returns exactly the numbers the two separate requests return. That is the check the fix ledger never had room for: not "it compiles", but "it returns the same values as the scalar form".
Probe 3: gaps_on is mostly NaN, and that is the documented behaviour
gOff = request.security(syminfo.tickerid, "W", close, gaps=barmerge.gaps_off)
gOn = request.security(syminfo.tickerid, "W", close, gaps=barmerge.gaps_on)
The two columns differ on 3,227 of 4,089 rows. gaps_on is NaN on 3,232 bars; gaps_off is NaN on 5. The 857 non-NaN values in the gaps_on column are exactly the 857 distinct weekly closes — one value per week, NaN on every other daily bar. That is what gaps_on means: no new confirmed weekly value, no value at all. If you plot a gaps_on series expecting a line, you get 857 dots. If you feed it into a condition expecting a filter, you get a filter that is false four days out of five. The translator emits both forms with zero diagnostics, and the difference between them is entirely in the runtime — which is why this probe exists: the compile step cannot tell you which one you wanted.
The two backtests: same 23 trades, $1,480 apart
Same strategy, same data, same 4,089 bars. The only difference is the filter — developing (A) versus "confirmed" (B).
A: developing (lookahead_off) |
B: "confirmed" ([1] + lookahead_on) |
|
|---|---|---|
| Trades | 23 | 23 |
| Wins | 10 (43.5%) | 11 (47.8%) |
| Net profit | +$5,209.86 | +$6,690.14 |
| Profit Factor | 2.515 | 3.023 |
| Max equity drawdown | 2.09% | 1.89% |
| Sharpe | 0.308 | 0.380 |
| Buy & hold, same window | +$14,682.21 | +$14,682.21 |
Three things to notice before the better number does any persuading.
First, both variants lose to doing nothing. Buy and hold on the same index over the same window returns +$14,682.21 — roughly double the better variant. The filter is not a money machine; it is a filter. The honest number in this table is the buy-and-hold row, and it is there so nobody reads the PF 3.02 as a recommendation. A translator moves your logic. It does not make it profitable.
Second, the better number comes from two extra trades, not from better timing. The first three trades are identical. The sequences diverge at trade 4: variant B takes a trade on 2012-12-13 (+$1,164.52) that variant A never takes, adds another on 2019-08-26 (+$189.22), and drops two of A's trades (2021-12-08, +$6.10; 2026-02-10, −$132.64). The arithmetic reconciles exactly: 1,164.52 + 189.22 − 6.10 + 132.64 = 1,480.28, the full net difference. The two variants re-sync at the last two trades (2026-04-15 and 2026-06-17, identical in both). So the $1,480 is not a slow accumulation of small edges — it is two entries that exist in one backtest and not in the other, both gated by the weekly filter.
Third, the filter that "wins" is the one whose values I cannot fully explain. Which is the next section.
What the "confirmed" value actually is
Here is the emitted Python for variant B, verbatim:
wClose: Series[float] = request.security(syminfo.tickerid, "W", close[1], lookahead=barmerge.lookahead_on)
_pyne_h0: Series[float] = ta.sma(close, 20)
wSma: Series[float] = request.security(syminfo.tickerid, "W", _pyne_h0, lookahead=barmerge.lookahead_on)
Read those three lines carefully. close[1] survives inline — the identifier history offset makes it through. But ta.sma(close, 20)[1] does not: the translator hoists the call into _pyne_h0 = ta.sma(close, 20) and passes the plain series in, and the [1] is gone. The Pine asked for the previous weekly bar's SMA. The Python asks for the SMA, no offset. That is a silent change — zero diagnostics, clean compile, different value — and it is visible only if you read the emitted code instead of the Pine.
Then I measured what the runtime actually returns for the [1] that did survive. Take one week, June 2020, and read the values off the run:
| Daily bar |
cOff (developing) |
cOn ("confirmed") |
What cOn actually is |
|---|---|---|---|
| 2020-05-29 (Fri) | 3824.06 | 3829.32 | Daily close of 2020-05-25 |
| 2020-06-01 (Mon) | 3867.02 | 3829.32 | Daily close of 2020-05-25 |
| 2020-06-02 (Tue) | 3867.02 | 3971.34 | Daily close of 2020-06-01 |
| 2020-06-05 (Fri) | 3867.02 | 3971.34 | Daily close of 2020-06-01 |
| 2020-06-08 (Mon) | 4001.25 | 3971.34 | Daily close of 2020-06-01 |
The previous weekly close on 2020-06-02 is 3867.02 — the close of the week ending 2020-05-29. The "confirmed" value that day is 3971.34, which appears in no weekly bar at all: it is the daily close of Monday 2020-06-01. Across the full sample, cOn equals the daily close of the first trading day of the current week (the previous week's first day, if today is that first day) on 3,802 of 4,088 bars (93.0%). The remaining 286 bars follow a pattern I have not identified — they are spread across all years and weekdays, and they are not float32 noise. I am not going to invent an explanation for them.
And the SMA leg is worse: sOn matches none of the candidate series I checked — not the weekly SMA 20, not the daily SMA 20 at any shift, not any daily SMA or EMA window from 5 to 250, not the SMA of the cOn series, not the weekly SMA with a developing bar. It changes on 4,003 of 4,088 day boundaries, so it is a daily-frequency series, but it is not any standard average of the closes I can construct. I report it as unidentified, not as a known quantity.
So the honest summary of variant B is this: its close leg returns Monday's daily close on 93% of bars and something unidentified on 7%, and its SMA leg returns a daily-frequency series I cannot identify at all — while its Pine says "previous weekly close" and "previous weekly SMA". The variant that looks better by $1,480 is built on a filter that is not reading what its author wrote. That does not make the $1,480 fake — the trades are real rows in a real trade list — but it does make the reason for the $1,480 unknowable from the Pine alone. A backtest that beats the alternative for a reason you cannot name is the exact thing the "confirmed" pattern was supposed to protect you from.
Rules I now follow
-
Read the emitted code for every
request.security()call. The Pine is the intent; the Python is the behaviour. If a[1]or alookaheaddoes not survive the trip, the backtest is measuring something else — and the compiler will not warn you, because the call itself is valid. -
Never put a history offset on a function call inside
request.security().close[1]survives;ta.sma(close, 20)[1]loses the offset when the call is hoisted. Compute the indicator, take the offset, then request — or request first and offset the returned series on the chart side, where the[1]is visible in the emitted code. - Verify a security value against a hand-computed series before trusting a strategy that uses it. One column, one resample, one equality check: the scalar probe in this post took four lines of Python and caught the one case (the last bar lagging a week) that the documentation describes but that is easy to forget.
-
gaps_onis a filter that is false four days out of five. Count the NaNs before you use it in a condition. If you wanted a step series, you wantedgaps_off. - A better backtest number is not evidence until the values behind it are identified. Variant B wins by $1,480 on values I cannot fully name. Until the SMA leg is identified, the honest statement is "B returned more on this sample", not "the confirmed filter is better".
Next in the series: the tutorial the series promised back in post #1 — an RSI mean-reversion strategy with strategy.exit and a var counter, run trade for trade on the same feed. The question it answers is the one this post leaves open: when the exit logic itself is the strategy, what does the translator do with the exit?
What does your request.security() call actually return — and when did a "confirmed" value turn out to be something else? I read every comment — the measurement stories are what the next posts are built from.
This conversion work was done by Pine2Py, the tool I build for exactly this job: paste a Pine script, get PyneCore-flavored Python back, diagnostics included when something can't be translated. The runtime is the open-source PyneCore project, with a patched build we ship for the two constructs stock PyneCore doesn't handle — details are on the site. It's free to try — every account gets 3 conversions, no card — and Pro is $12/month if you end up converting a whole library. No return promises, ever: a translator moves your logic, it doesn't make it profitable.
Top comments (0)