Conversion example · Strategy

EMA crossover: Pine Script to Python

A dual-EMA crossover — probably the first strategy most Pine developers ever write. Fast EMA (9) crossing above slow EMA (21) enters long; a cross under closes the position. It is the simplest possible test of a Pine-to-Python converter: if the tool cannot carry this across cleanly, nothing else it produces is worth trusting.

The Python below is not hand-written. It is the actual output of the Pine2Py converter, run on this exact Pine source with the local compiler (node bin/pine2pyne.mjs), with zero diagnostics — nothing unsupported, nothing dropped.

The Pine original

ema_cross.pinePine Script v6
//@version=6
strategy("EMA Cross Strategy", overlay=true, initial_capital=10000,
     default_qty_type=strategy.percent_of_equity, default_qty_value=100)

fastLen = input.int(9, "Fast EMA length", minval=1)
slowLen = input.int(21, "Slow EMA length", minval=2)

fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)

goldenCross = ta.crossover(fast, slow)
deathCross  = ta.crossunder(fast, slow)

if goldenCross
    strategy.entry("Long", strategy.long)

if deathCross
    strategy.close("Long", comment="Death cross exit")

plot(fast, "Fast EMA", color=color.new(color.teal, 0))
plot(slow, "Slow EMA", color=color.new(color.orange, 0))

The Python output

ema_cross.pyPython · PyneCore
"""
@pyne
"""

from pynecore import Series
from pynecore.lib import close, color, input, plot, script, strategy, ta

_pyne_in_fastLen: int = input.int(9, "Fast EMA length", minval=1)
_pyne_in_slowLen: int = input.int(21, "Slow EMA length", minval=2)

@script.strategy(title="EMA Cross Strategy", overlay=True, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
def main():
    fast: Series[float] = ta.ema(close, round(_pyne_in_fastLen))
    slow: Series[float] = ta.ema(close, round(_pyne_in_slowLen))
    goldenCross: Series[float] = ta.crossover(fast, slow)
    deathCross: Series[float] = ta.crossunder(fast, slow)
    if goldenCross:
        strategy.entry("Long", strategy.long)
    if deathCross:
        strategy.close("Long", comment="Death cross exit")
    plot(fast, "Fast EMA", color=color.new(color.teal, 0))
    plot(slow, "Slow EMA", color=color.new(color.orange, 0))

What translates 1:1

Most of the body is a straight transliteration, and that is by design. The output targets the open-source PyneCore runtime, which mirrors Pine's standard library under pynecore.lib. So ta.ema, ta.crossover, ta.crossunder, strategy.entry, strategy.close, plot, and even color.new(color.teal, 0) keep their names and argument shapes. If you can read the Pine, you can read the Python.

Three structural changes are worth pointing out. First, the strategy() call becomes a decorator: Pine's top-level declaration turns into @script.strategy(...) on a main() function, with every keyword argument — initial_capital, default_qty_type, all of it — carried over verbatim. Second, inputs are hoisted out of main(): the two input.int calls become module-level constants with a _pyne_in_ prefix, so you can override them for a parameter sweep. Third, integer arguments get an explicit round() — ta.ema(close, round(_pyne_in_fastLen)) — because Pine is loose about numeric types and PyneCore's ta.ema wants a real int.

What changes meaning

if goldenCross: is not Python's usual if. In plain Python, if on a container-like object would be a bug. Here it works because goldenCross is a PyneCore Series, and the runtime evaluates main() per bar, so the condition reads the current bar's value. The semantics are Pine's — one decision per bar, using values known at that bar — implemented through Python syntax. Code copied out of this context into pandas will not behave the same way.

The fill assumption lives in the emulator, not in the line of code. strategy.entry("Long", strategy.long) is character-for-character identical in both versions. When the order fills is not in that line at all. On TradingView's historical bars, a market entry signaled on bar N fills at bar N+1's open by default — not at bar N's close. A Python backtest only matches if its broker emulator makes the same choice. Verify this one setting before comparing a single number against TradingView.

barstate.* names survive; the realtime world doesn't. A variant of this script with if goldenCross and barstate.isconfirmed also converts cleanly, name intact. But there is no realtime bar in a historical Python backtest: no intrabar ticks, no unconfirmed final bar forming live. Logic that depends on the distinction between historical and realtime execution has no counterpart offline — that difference is yours to re-engineer.

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.