Conversion example · Strategy

MACD crossover: Pine Script to Python

A MACD crossover strategy (12/26/9) that goes long when the MACD line crosses above its signal line and exits when it crosses back down. No bracket, no time stop — the position is held until the indicator itself says otherwise, which makes this a good test of the one thing converters most often fumble: a function that returns more than one series. In Pine, ta.macd returns a three-value tuple. In Python, a tuple is not a series — so the converter has to do something explicit with it, and this page shows exactly what.

The Python below 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.

The Pine original

macd_crossover.pinePine Script v6
//@version=6
strategy("MACD Crossover", overlay=true, initial_capital=10000,
     default_qty_type=strategy.percent_of_equity, default_qty_value=100)

fastLen   = input.int(12, "Fast length", minval=1)
slowLen   = input.int(26, "Slow length", minval=1)
signalLen = input.int(9, "Signal length", minval=1)

[macdLine, signalLine, histLine] = ta.macd(close, fastLen, slowLen, signalLen)

longSignal  = ta.crossover(macdLine, signalLine)
shortSignal = ta.crossunder(macdLine, signalLine)

if longSignal
    strategy.entry("Long", strategy.long)
if shortSignal
    strategy.close("Long", comment="MACD cross down")

plot(macdLine, "MACD", color=color.new(color.blue, 0))
plot(signalLine, "Signal", color=color.new(color.orange, 0))
plot(histLine, "Histogram", color=color.new(color.gray, 0))

The Python output

macd_crossover.pyPython · PyneCore
"""
@pyne
"""

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

_pyne_in_fastLen: int = input.int(12, "Fast length", minval=1)
_pyne_in_slowLen: int = input.int(26, "Slow length", minval=1)
_pyne_in_signalLen: int = input.int(9, "Signal length", minval=1)

@script.strategy(title="MACD Crossover", overlay=True, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
def main():
    _pyne_br0 = ta.macd(close, _pyne_in_fastLen, _pyne_in_slowLen, _pyne_in_signalLen)
    macdLine: Series[float] = _pyne_br0[0]
    signalLine: Series[float] = _pyne_br0[1]
    histLine: Series[float] = _pyne_br0[2]
    longSignal: Series[float] = ta.crossover(macdLine, signalLine)
    shortSignal: Series[float] = ta.crossunder(macdLine, signalLine)
    if longSignal:
        strategy.entry("Long", strategy.long)
    if shortSignal:
        strategy.close("Long", comment="MACD cross down")
    plot(macdLine, "MACD", color=color.new(color.blue, 0))
    plot(signalLine, "Signal", color=color.new(color.orange, 0))
    plot(histLine, "Histogram", color=color.new(color.gray, 0))

What translates 1:1

The tuple destructuring is the interesting line. Pine's [macdLine, signalLine, histLine] = ta.macd(...) becomes a single call bound to a temporary (_pyne_br0), then three indexed assignments, each typed Series[float]. The call happens once — the MACD is computed once per bar, not three times — and each name keeps its series type, so ta.crossover(macdLine, signalLine) below reads exactly as it does in Pine. The strategy.entry / strategy.close pair, the comment on the close, and the three plot calls all carry over with their names and argument shapes intact.

What else changes meaning

There is no exit rule except the indicator. Unlike the RSI strategy on this site, this script has no bracket and no time stop: a trade ends only when the MACD line crosses back under its signal line. That is a design fact worth stating before you read the numbers below — the average hold is 12.8 bars, and the strategy spends long stretches fully exposed to the trend it is riding, for better and worse.

The fill is the next bar's open. The entry signaled on bar N fills at bar N+1's open in the emulator, and the cross-down exit fills the same way. A crossover that looks clean on the close can fill a full bar later, after the move it was chasing.

The run: 154 trades, and a thin edge

We ran this exact script in the PyneCore patched runtime on 4,089 bars of CSI 300 daily data (2010-01-04 to 2026-11-06), $10,000, no commission, no slippage — in our testing:

It beats buy & hold on this one symbol and one period — and the edge is thin: a 34.4% win rate with a Profit Factor of 1.12, on 154 trades, is a suggestion, not a verdict. The wins are bigger than the losses (about 2.1×), which is the whole story: the strategy wins by letting the occasional trend run, not by being right often. 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.