The Translator Got Better: One Fix, One Script That Used to Fail, Before and After
Your Pine strategy uses
strategy.entry(..., when=...). The converter passeswhen=through verbatim. The output compiles clean — and dies on the first bar withTypeError: entry() got an unexpected keyword argument 'when'. The fix is four lines ofif. This post measures the before and the after, trade for trade.
This is the eighth post in a series where I convert Pine strategies to Python line by line. The last post was about 366 scripts that draw and never plot. This one is about a fix: one family of calls, one script that used to fail, and the check that proves the fixed version means the same thing as the Pine.
The short version.
-
Before: the converter passed
when=through to PyneCore, which has nowhenparameter on anystrategy.*function. The output compiled clean and died on the first bar:TypeError: entry() got an unexpected keyword argument 'when'. -
After: the same script converts with zero diagnostics. The two
when=calls becomeifwrappers, and the run produces 66 trades on 4,089 bars. - The check: an independent pandas re-implementation of the same rules produces the identical 66 entry bars and 66 exit bars — trade for trade, no exceptions.
- The gate actually mattered once: of 67 EMA crossovers, one was skipped because RSI was 77.28 — 2024-09-26, the sharp rally week. The filter is not decoration.
- Honest scope: this fix shipped in the October build, and it is in the deployed converter today. The numbers below were re-measured tonight on the current compiler. The strategy itself is a demo — 66 trades, profit factor 1.50 in our testing — not a system.
How we tested
- Feed: 4,089 bars of CSI 300 daily data, 2010-01-04 to 2026-11-06, same feed as posts #4–#7.
- Compiler: the local compiler (
node bin/pine2pyne.mjs), zero diagnostics on the test script. - Runner: the open-source PyneCore runtime (6.10.12). Market orders fill at the next bar's open, the TradingView default.
- Control: a from-scratch pandas implementation of the same rules — Pine's
ta.emarecurrence (seeded with the SMA), Wilder RSI(14), crossover/crossunder, same fill rule. It shares no code with the converter or the runtime. - No tuning, no picking the good cases afterwards: the script below is the smallest strategy that uses
when=twice — once on entry, once on close.
The before: compiles clean, dies on bar one
Pine v5 lets you gate a strategy call with a when= argument: fire this call only on the bars where the condition is true. PyneCore has no when parameter on any strategy.* function, in any version — and Pine v6 removed the argument entirely. So the old converter did the literal thing: it passed the argument through.
That output is reproduced below, rebuilt line for line from the current output by putting the when= arguments back where the old emitter left them:
strategy.entry("Long", strategy.long, when=(longCond and (rsi < 70)))
strategy.close("Long", when=shortCond)
It compiles clean. Then you run it:
Traceback (most recent call last):
...
File "when_gate_before.py", line 15, in main
strategy.entry("Long", strategy.long, when=(longCond and (rsi < 70)))
TypeError: entry() got an unexpected keyword argument 'when'
That is the whole failure, and it is the worst kind: nothing is wrong with the script, nothing is wrong with the strategy, and the error message names a keyword the user wrote correctly in Pine. A converter that passes through what it cannot translate is not being faithful — it is handing you a program that was never going to run.
The after: four lines of if
The script:
//@version=5
strategy("When gate: EMA cross with RSI filter", 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)
rsi = ta.rsi(close, 14)
longCond = ta.crossover(fast, slow)
shortCond = ta.crossunder(fast, slow)
strategy.entry("Long", strategy.long, when=longCond and rsi < 70)
strategy.close("Long", when=shortCond)
plot(fast, "Fast")
plot(slow, "Slow")
The Python is the actual output of the converter, verified tonight with the local compiler (node bin/pine2pyne.mjs, zero diagnostics). Verbatim:
"""
@pyne
"""
from pynecore import Series
from pynecore.lib import close, plot, script, strategy, ta
@script.strategy(title="When gate: EMA cross with RSI filter", 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)
rsi: Series[float] = ta.rsi(close, 14)
longCond: Series[float] = ta.crossover(fast, slow)
shortCond: Series[float] = ta.crossunder(fast, slow)
if (longCond and (rsi < 70)):
strategy.entry("Long", strategy.long)
if shortCond:
strategy.close("Long")
plot(fast, "Fast")
plot(slow, "Slow")
The desugar is exactly Pine's semantics: a strategy.* call with when= fires on the bars where the condition is true, and an if around the call is the same thing. The fix covers seven functions — entry, exit, order, close, close_all, cancel, cancel_all — and it applies wherever the call sits, including inside if bodies, because the wrapper lands at the call's own indentation level.
The check: three versions, one trade list
Same script, same feed, three ways of running it:
| Before (verbatim passthrough) | After (desugared) | Independent pandas check | |
|---|---|---|---|
| Entries | — (dies on bar 1) | 66 | 66, same bars |
| Exits | — | 66 | 66, same bars |
| First entry | TypeError |
bar 57, 2010-03-31 @ 3,369.19 | bar 57 |
| Last exit | — | bar 4007, 2026-07-08 @ 4,804.48 | bar 4007 |
| Gated-out crossovers | — | 1 (bar 3579, RSI 77.28) | 1, same bar |
The pattern is simple: the fix changes nothing about the strategy and everything about whether it runs. Every entry bar and every exit bar matches the independent implementation exactly — 66 out of 66 on both sides. That is the claim the fix makes, and the table is the check.
The trade economics, for the record and not as a recommendation: 66 trades, 26 wins / 40 losses, net +$5,564.51 (+5.56%), profit factor 1.502 in our testing. The strategy is a two-line EMA crossover with a filter; the numbers say it is a demo, not a system.
And the claim-check table, because a fix deserves the same treatment as a strategy:
| What the fix claims | What I checked |
|---|---|
when= means "fire only on bars where the condition is true" |
Entry bars equal the pandas signal bars + 1, 66 out of 66 |
strategy.close with when= exits only on crossunder bars |
Exit bars equal the pandas crossunder bars + 1, 66 out of 66 |
| The gate actually filters something | 1 of 67 crossovers skipped — bar 3579, RSI 77.28 |
| Nothing else changes | Same fill rule, same position sizing, same 4,089-row plot output |
The one trade the gate changed
67 crossovers fired on this feed. 66 became entries. The one that didn't:
- Bar 3579, 2024-09-26. EMA(10) crossed above EMA(30) — fast 3,314.04 vs slow 3,296.37 — but RSI(14) read 77.28, above the 70 gate. No entry.
- That week was the sharp September 2024 rally. Whether skipping it helped or hurt is a separate question, and the run does not answer it — the gate is one skipped trade out of 67, not a verdict on RSI filters.
- The point is narrower and more useful: the gate is load-bearing. If the desugar had dropped the condition instead of wrapping it, that entry would have fired, and the trade list would differ by exactly one trade. The check above would have caught it — 66 vs 67 entries — which is why the check exists.
One more count for completeness: 67 crossunders fired, and 66 became exits. The one that didn't fired while flat — a close with no open position is a no-op in both worlds.
What the fix doesn't do
Two boundaries, both measured tonight, both worth knowing before you trust the fix:
1. The condition has to be named when=. Pass it positionally and the compiler has nothing to recognize:
strategy.entry("Long", strategy.long, c)
This compiles with zero diagnostics — the boolean lands in the qty slot — and it runs. What it does is worse than crashing: it enters 1 contract at the first crossover (bar 57) and holds it to the last bar (bar 4088), one trade, +$685.09. No error, no diagnostic, just a strategy that quietly means something else. The old project note said the positional form "errors at runtime"; on the current runtime it doesn't error — it silently mis-trades, which is worse, and the note is corrected here. If your script passes a condition positionally, the converter will not save you. Name the argument.
2. when= in value position is rejected before the emitter ever sees it. Try to assign the call:
x = strategy.entry("Long", strategy.long, when=c)
and the analyzer stops the compile:
when_value.pine: analyze failed
'strategy.entry' call is only supported in statement position (no return value — cannot be called in assignment/expression/argument position) (L6:19)
The emitter also carries a strip-and-diagnose fallback for any form that slips through analysis, but the measured behavior is the analyzer's: the call cannot be a value, so it cannot silently lose its gate inside an expression. Two layers, and the first one fires.
And the scope note: Pine v6 removed when entirely, so v6 scripts never hit this — the fix is a v5 bridge. If you are writing new Pine, write the if yourself; the converter exists for the library you already have.
Rules I now follow
- A clean compile is not a working program. The old output compiled. It died on bar one. Compile success is a claim about syntax; run it before you believe anything.
- A fix earns a control, not a testimonial. "It works now" is one run. The trade list matching an independent implementation bar for bar is the evidence.
- Count the gate. A filter that never fires is decoration. 67 crossovers, 1 gated out — the gate is real, and its one firing is named with a date and a number.
- Name the argument. The positional form compiles, runs, and silently means something else. Named arguments are not style; they are the difference between a gate and a quantity.
- Correct the old note in public. The project note said the positional form errors at runtime. It doesn't anymore — it silently mis-trades. A stale warning is worse than no warning, because it tells you to watch for the wrong failure.
Next in the series: the sweep numbers — 84 errors became 5, across three fixes, with zero regressions. What the version-to-version table actually says about a translator getting better.
What did your translator silently drop first — a gate, a condition, or a whole branch? And did it crash, or did it just run wrong? I read every comment — the silent-failure 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)