Conversion example · Strategy

Bollinger Bands mean reversion: Pine Script to Python

A Bollinger Bands mean-reversion strategy (20, 2.0): buy when the close crosses under the lower band, exit when it crosses back over the middle band (the basis). No bracket, no time stop — the exit is the indicator itself. It is a good test of the same thing the MACD example tests — a ta.* function that returns more than one series — with a different trap attached: the entry signal fires on a cross under a band that is itself moving, so the price you buy at is already the price that just broke the level.

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

bollinger-bands.pinePine Script v6
//@version=6
strategy("Bollinger Bands Mean Reversion", overlay=true, initial_capital=10000,
     default_qty_type=strategy.percent_of_equity, default_qty_value=100)

bbLen   = input.int(20, "BB length", minval=1)
bbMult  = input.float(2.0, "BB multiplier", minval=0.1, step=0.1)

[basis, upper, lower] = ta.bb(close, bbLen, bbMult)

longSignal = ta.crossunder(close, lower)
exitSignal = ta.crossover(close, basis)

if longSignal
    strategy.entry("Long", strategy.long)
if exitSignal
    strategy.close("Long", comment="Back to basis")

plot(basis, "Basis", color=color.new(color.orange, 0))
plot(upper, "Upper", color=color.new(color.teal, 0))
plot(lower, "Lower", color=color.new(color.teal, 0))

The Python output

bollinger-bands.pyPython · PyneCore
"""
@pyne
"""

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

_pyne_in_bbLen: int = input.int(20, "BB length", minval=1)
_pyne_in_bbMult: float = input.float(2, "BB multiplier", minval=0.1, step=0.1)

@script.strategy(title="Bollinger Bands Mean Reversion", overlay=True, initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
def main():
    _pyne_br0 = ta.bb(close, _pyne_in_bbLen, _pyne_in_bbMult)
    basis: Series[float] = _pyne_br0[0]
    upper: Series[float] = _pyne_br0[1]
    lower: Series[float] = _pyne_br0[2]
    longSignal: Series[float] = ta.crossunder(close, lower)
    exitSignal: Series[float] = ta.crossover(close, basis)
    if longSignal:
        strategy.entry("Long", strategy.long)
    if exitSignal:
        strategy.close("Long", comment="Back to basis")
    plot(basis, "Basis", color=color.new(color.orange, 0))
    plot(upper, "Upper", color=color.new(color.teal, 0))
    plot(lower, "Lower", color=color.new(color.teal, 0))

What translates 1:1

The tuple destructuring is the interesting line, same as the MACD example. Pine's [basis, upper, lower] = ta.bb(...) becomes a single call bound to a temporary (_pyne_br0), then three indexed assignments, each typed Series[float]. The call happens once — the bands are computed once per bar, not three times — and the order is the Pine order: basis first, then upper, then lower. Get that order wrong and the strategy buys the top band and sells the middle one. The ta.crossunder(close, lower) entry, the ta.crossover(close, basis) exit with its comment, and the three plot calls all carry over with their names and argument shapes intact.

What else changes meaning

The entry buys a falling knife by construction. The signal fires when the close crosses under the lower band — that is, after the price has already fallen through it. The fill is the next bar's open, so the position opens a full bar after the break, at whatever price the market offers then. Mean reversion here means "buy after the drop and wait for the drift back to the middle" — the average winning trade lasts 9.7 bars and the average losing trade lasts 25.0, which tells you the exit is patient and the losers are the ones that never come back.

There is no stop except the indicator's patience. A trade that never crosses back over the basis never exits on its own. The largest losing trade in the run below is −$1,261.41 — a position that kept falling while the basis kept falling with it, because the basis is a moving average of the same falling price. That is not a converter artifact. It is what the script says.

The run: a 63% win rate that still loses money

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:

A 63% win rate that loses money is the whole lesson of this page: the average loss is almost exactly twice the average win ($284.70 vs $143.32), so winning often is not enough — the wins have to be at least as big as the losses, and here they are half the size. The strategy wins by catching small bounces and loses by holding the ones that keep falling. 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.