Conversion example · Strategy

SuperTrend trend following: Pine Script to Python

A SuperTrend trend-following strategy (ATR 10, multiplier 3.0): two ratchet lines 3 × ATR above and below the bar midpoint (hl2); the trend flips when a close crosses the opposite line, and the position flips with it — long only, flat when the trend is down. It is a good test of the thing the RSI example only touches: var state that has to survive from bar to bar — the trend itself, and the previous values of the two lines — plus nz(), ternary ?:, and history on a var (trend[1]).

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

supertrend.pinePine Script v6
//@version=6
strategy("SuperTrend Trend Following", overlay=true, initial_capital=10000,
     default_qty_type=strategy.percent_of_equity, default_qty_value=100)

atrLen = input.int(10, "ATR length", minval=1)
mult   = input.float(3.0, "Multiplier", minval=0.1, step=0.1)

atrVal = ta.atr(atrLen)
src = hl2

up = src - mult * atrVal
dn = src + mult * atrVal

var float up1 = na
var float dn1 = na
var int trend = 1

up1 := nz(up[1], up)
dn1 := nz(dn[1], dn)

up := close[1] > up1 ? math.max(up, up1) : up
dn := close[1] < dn1 ? math.min(dn, dn1) : dn

trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend

buySignal  = trend == 1 and trend[1] == -1
sellSignal = trend == -1 and trend[1] == 1

if buySignal
    strategy.entry("Long", strategy.long)
if sellSignal
    strategy.close("Long", comment="Trend flipped")

plot(up, "Up", color=color.new(color.green, 0))
plot(dn, "Dn", color=color.new(color.red, 0))

The Python output

supertrend.pyPython · PyneCore
"""
@pyne
"""

from pynecore import Persistent, PersistentSeries, Series
from pynecore.lib import close, color, hl2, input, math, na, nz, plot
from pynecore.lib import script, strategy, ta

_pyne_in_atrLen: int = input.int(10, "ATR length", minval=1)
_pyne_in_mult: float = input.float(3, "Multiplier", minval=0.1, step=0.1)

@script.strategy(title="SuperTrend Trend Following", overlay=True, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
def main():
    atrVal: Series[float] = ta.atr(_pyne_in_atrLen)
    src: Series[float] = hl2
    up: Series[float] = (src - (_pyne_in_mult * atrVal))
    dn: Series[float] = (src + (_pyne_in_mult * atrVal))
    up1: Persistent[float] = na
    dn1: Persistent[float] = na
    trend: PersistentSeries[int] = 1
    up1 = nz(up[1], up)
    dn1 = nz(dn[1], dn)
    up = (math.max(up, up1) if (close[1] > up1) else up)
    dn = (math.min(dn, dn1) if (close[1] < dn1) else dn)
    trend = (1 if ((trend == (-1)) and (close > dn1)) else ((-1) if ((trend == 1) and (close < up1)) else trend))
    buySignal: Series[bool] = ((trend == 1) and (trend[1] == (-1)))
    sellSignal: Series[bool] = ((trend == (-1)) and (trend[1] == 1))
    if buySignal:
        strategy.entry("Long", strategy.long)
    if sellSignal:
        strategy.close("Long", comment="Trend flipped")
    plot(up, "Up", color=color.new(color.green, 0))
    plot(dn, "Dn", color=color.new(color.red, 0))

What translates 1:1

The var declarations are the interesting lines. var float up1 = na and var float dn1 = na become Persistent[float] — a value that keeps its previous bar's value unless reassigned — while var int trend = 1 becomes PersistentSeries[int], because the script reads its own history (trend[1]). That distinction is the whole lesson: a var you only read as a scalar is a Persistent; a var you index into the past is a PersistentSeries. Get it wrong and the flip detection reads the wrong bar. The ternary ?: becomes a Python conditional expression, nz(up[1], up) keeps its name and shape, and hl2 is a series in both languages, so src needs no conversion at all.

What else changes meaning

The ratchet only moves one way — that is the strategy. The upper line can only move down while the trend is down, and the lower line can only move up while the trend is up. That asymmetry is what makes the lines "follow" the price instead of tracking it: a line that never gives back ground is a trailing stop in disguise. In the Python, the two if/else expressions are evaluated fresh every bar, so the ratchet reads the current bar's close[1] against the previous bar's stored line — same as Pine, but it is worth checking, because a translator that hoisted the comparison would break the ratchet silently.

There is no stop except the flip. A trade exits only when the trend flips, however far price moves first. The largest losing trade in the run below is −$1,959.68, and the average trade lasts 34.9 bars — more than twice the Bollinger example's 15.3. The strategy is patient by construction: it waits out the chop and pays for it with the occasional long, slow loss. That is not a converter artifact. It is what the script says.

The run: profitable, lumpy, and one trade deep — in places

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:

The honest version of that headline: the strategy beats buy & hold by a wide margin, but 7 of the 17 years are losing years (worst: 2018, −$2,608), the largest single win (+$3,328.92) is 38.8% of the net, and the top 5 winners are 51.0% of all gross profit. Without the largest win the net is still +$5,249.69 — so this one is not a one-trade story the way the RSI series' first profitable variant was — but the 45% drawdown is the number to stare at: a strategy that makes 86% over sixteen years while once falling 45% is a strategy most people would have switched off halfway. 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.