Conversion example · Strategy

request.security weekly filter: Pine Script to Python

A daily EMA 10/30 crossover on the CSI 300, long only, with one extra condition: the weekly close must be above the weekly SMA 20. Two versions of that condition, one line apart — and the backtest moves by $1,480.28 on the same 23 trades. The default version reads the developing weekly value; the fixed version reads the confirmed one. This page is about what request.security() actually returns, because a translator that compiles your multi-timeframe filter and quietly changes what the filter reads has not finished the job.

The Python below is the actual output of the Pine2Py converter, run on these exact Pine sources with the local compiler (node bin/pine2pyne.mjs), with zero diagnostics on both variants.

The Pine original — developing value (the default)

sec_strat_off.pinePine Script v5
//@version=5
strategy("SEC filter off: daily EMA cross + developing weekly close", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
fast = ta.ema(close, 10)
slow = ta.ema(close, 30)
wClose = request.security(syminfo.tickerid, "W", close)
wSma = request.security(syminfo.tickerid, "W", ta.sma(close, 20))
longCond = ta.crossover(fast, slow) and wClose > wSma
shortCond = ta.crossunder(fast, slow)
strategy.entry("Long", strategy.long, when=longCond)
strategy.close("Long", when=shortCond)
plot(fast, "Fast")
plot(slow, "Slow")

The Pine original — confirmed value (one line apart)

The only differences are the two request.security() lines: close[1] instead of close, and lookahead=barmerge.lookahead_on added.

sec_strat_on.pinePine Script v5
//@version=5
strategy("SEC filter on: daily EMA cross + confirmed weekly close", overlay=true, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
fast = ta.ema(close, 10)
slow = ta.ema(close, 30)
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)
longCond = ta.crossover(fast, slow) and wClose > wSma
shortCond = ta.crossunder(fast, slow)
strategy.entry("Long", strategy.long, when=longCond)
strategy.close("Long", when=shortCond)
plot(fast, "Fast")
plot(slow, "Slow")

The Python output — developing value

sec_strat_off.pyPython · PyneCore
"""
@pyne
"""

from pynecore import Series
from pynecore.lib import close, plot, request, script, strategy, syminfo, ta

@script.strategy(title="SEC filter off: daily EMA cross + developing weekly close", overlay=True, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
def main():
    fast: Series[float] = ta.ema(close, 10)
    slow: Series[float] = ta.ema(close, 30)
    wClose: Series[float] = request.security(syminfo.tickerid, "W", close)
    wSma: Series[float] = request.security(syminfo.tickerid, "W", ta.sma(close, 20))
    longCond: Series[bool] = (ta.crossover(fast, slow) and (wClose > wSma))
    shortCond: Series[float] = ta.crossunder(fast, slow)
    if longCond:
        strategy.entry("Long", strategy.long)
    if shortCond:
        strategy.close("Long")
    plot(fast, "Fast")
    plot(slow, "Slow")

The Python output — confirmed value

sec_strat_on.pyPython · PyneCore
"""
@pyne
"""

from pynecore import Series
from pynecore.lib import barmerge, close, plot, request, script, strategy, syminfo, ta

@script.strategy(title="SEC filter on: daily EMA cross + confirmed weekly close", overlay=True, initial_capital=100000, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
def main():
    fast: Series[float] = ta.ema(close, 10)
    slow: Series[float] = ta.ema(close, 30)
    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)
    longCond: Series[bool] = (ta.crossover(fast, slow) and (wClose > wSma))
    shortCond: Series[float] = ta.crossunder(fast, slow)
    if longCond:
        strategy.entry("Long", strategy.long)
    if shortCond:
        strategy.close("Long")
    plot(fast, "Fast")
    plot(slow, "Slow")

What translates 1:1

request.security() keeps its name, its argument order, and its timeframe string in both variants — syminfo.tickerid, "W", ... reads the same in Pine and in Python. lookahead=barmerge.lookahead_on passes through untouched, and ta.ema, ta.sma, ta.crossover and ta.crossunder all keep their names and shapes. The when= arguments on strategy.entry and strategy.close desugar into plain if statements around the calls — the converter does not drop the condition, it moves it where Python can see it. That is the whole point of the exercise: the translation is faithful, and the difference in results comes from what the two Pine lines mean, not from what the converter did to them.

What else changes meaning

The developing value is not last week's close. The default request.security(syminfo.tickerid, "W", close) returns the weekly bar as it stands so far — and we verified, bar by bar on the same 4,089-bar feed, that the confirmed version (close[1] with lookahead on) matches the daily close of the first trading day of the current week on 3,802 of 4,088 bars (93.0% at 0.05 tolerance; 2,859 at 1e-6, the rest is float32 storage noise in the data file). It is not reading "last week, already closed" the way the Pine reads. It is reading this week's Monday. 286 bars remain unidentified, spread across all years and weekdays — disclosed here, not hidden.

The SMA line is the one we could not verify. Note the asymmetry in the confirmed Python: close[1] keeps its [1] in the emitted code, but the SMA's [1] does not survive hoisting — the compiler lifts ta.sma(close, 20) into _pyne_h0 and passes the series bare. We checked the resulting series against the weekly SMA 20, the daily SMA 20 at every shift, the SMA 20 of the confirmed close, and the developing-bar variant; none matched, so we report the value as unidentified rather than guessing. The weekly-close line, by contrast, is fully verified above. A converter that compiled cleanly and returned a different number than the one you asked for is the dangerous case — and this page shows both halves of it: one line verified, one line honestly unidentified.

One line, $1,480.28. The two variants run on the same 23 trades, and the difference reconciles trade by trade: four trades account for all of it (+$1,164.52, +$189.22, −$6.10, +$132.64). The filter did not change the strategy. It changed what the filter was looking at.

The run: one line, $1,480.28 apart — and both trail buy & hold

We ran both variants in the PyneCore patched runtime on 4,089 bars of CSI 300 daily data (2010-01-04 to 2026-11-06) plus 862 weekly bars resampled from that same daily file, $100,000, 10% of equity per trade, no commission, no slippage — in our testing:

Developing (default)Confirmed ([1] + lookahead)
Closed trades2323
Wins10 (43.5%)11 (47.8%)
Net profit+$5,209.86+$6,690.14
Profit Factor2.513.02
Gross profit / gross loss+$8,649.62 / −$3,439.76+$9,997.26 / −$3,307.12
Max equity drawdown$2,239.53$2,050.31
Buy & hold, same span+$14,682.21+$14,682.21

The honest version of that headline: the confirmed filter beats the developing filter by $1,480.28 and lifts the Profit Factor from 2.51 to 3.02 — and both variants trail buy & hold (+$14,682.21 on the same span) by roughly $8,000–$9,500. Fixing what the filter reads made the strategy better than itself. It did not make it better than doing nothing. One symbol, one period, no costs — run it on your own symbol before believing any of it.

Try it on your own script

Paste this Pine script — or your own — into the converter on the home page and compare the output. Every account gets 3 free conversions, no card required; Pro is $12/month via Paddle for unlimited conversions.

The output runs on the open-source PyneCore runtime. We ship a patched runtime build for the two constructs stock PyneCore doesn't handle yet — details are on the home page. Pine2Py translates code; it does not provide investment advice, and no backtest result on this page is a promise about any strategy's future performance.