Conversion example · Strategy

Commission and fees: Pine Script to Python

The same RSI mean-reversion strategy as the trailing-stop example — same trailing exit, same 2×ATR stop, same 4,089 bars of CSI 300 daily data — with one change: a 0.1% round-trip fee, set through Pine's own commission_type=strategy.commission.percent, commission_value=0.05 (0.05% of notional on every order, entry and exit). The fee is charged by the runtime on every fill, not subtracted by hand afterwards. This page is the cost control for the whole series: every number before it ran with no commission and no slippage, and “costs would bite less on a slow strategy” was an assertion, not a measurement.

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

rsi_fee.pinePine Script v6
//@version=6
strategy("RSI Mean Reversion (trailing exit, stop 2xATR, fee 0.1% round-trip)", overlay=true, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.05,
     default_qty_type=strategy.percent_of_equity, default_qty_value=100)

rsiLen     = input.int(14, "RSI length", minval=1)
overbought = input.int(70, "Overbought level", minval=51, maxval=100)
oversold   = input.int(30, "Oversold level", minval=0, maxval=49)

rsi = ta.rsi(close, rsiLen)
atrVal = ta.atr(14)

// Trailing exit, fixed before running (pre-announced in #17):
// trail = highest close since entry minus 2xATR, recomputed each bar,
// tracked with a var so it only ever rises. The trade closes when the
// close falls below it. No time exit. Hard stop (2xATR below entry)
// stays as the backstop.
var float trailLevel = na
inTrade = strategy.position_size != 0
if inTrade
    trailLevel := na(trailLevel) ? close - atrVal * 2 : math.max(trailLevel, close - atrVal * 2)
else
    trailLevel := na

longSignal = ta.crossover(rsi, oversold)

if longSignal and not inTrade
    strategy.entry("Long", strategy.long)

if inTrade
    strategy.exit("Exit Long", "Long", stop=strategy.position_avg_price - atrVal * 2)
    if close < trailLevel
        strategy.close("Long", comment="Trail exit")

plot(rsi, "RSI", color=color.new(color.purple, 0))
hline(overbought, "Overbought", color=color.new(color.red, 0))
hline(oversold, "Oversold", color=color.new(color.green, 0))

The Python output

rsi_fee.pyPython · PyneCore
"""
@pyne
"""

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

_pyne_in_rsiLen: int = input.int(14, "RSI length", minval=1)
_pyne_in_overbought: int = input.int(70, "Overbought level", minval=51, maxval=100)
_pyne_in_oversold: int = input.int(30, "Oversold level", minval=0, maxval=49)

@script.strategy(title="RSI Mean Reversion (trailing exit, stop 2xATR, fee 0.1% round-trip)", overlay=True, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.05, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
def main():
    rsi: Series[float] = ta.rsi(close, _pyne_in_rsiLen)
    atrVal: Series[float] = ta.atr(14)
    trailLevel: Persistent[float] = na
    inTrade: Series[bool] = (strategy.position_size != 0)
    if inTrade:
        trailLevel = ((close - (atrVal * 2)) if na(trailLevel) else math.max(trailLevel, (close - (atrVal * 2))))
    else:
        trailLevel = na
    longSignal: Series[float] = ta.crossover(rsi, _pyne_in_oversold)
    if (longSignal and (not inTrade)):
        strategy.entry("Long", strategy.long)
    if inTrade:
        strategy.exit("Exit Long", "Long", stop=(strategy.position_avg_price - (atrVal * 2)))
        if (close < trailLevel):
            strategy.close("Long", comment="Trail exit")
    plot(rsi, "RSI", color=color.new(color.purple, 0))
    hline(_pyne_in_overbought, "Overbought", color=color.new(color.red, 0))
    hline(_pyne_in_oversold, "Oversold", color=color.new(color.green, 0))

What translates 1:1

commission_type and commission_value pass through the converter untouched, straight into the @script.strategy decorator — the compiler emits them verbatim, with zero diagnostics, and the PyneCore runtime charges 0.05% of the fill notional on every order. We verified the charge trade by trade: on every trade that ran the same number of contracts in both runs, the fee equals 0.05% × (entry notional + exit notional) to the cent, 40 out of 40 trades, zero mismatches. A converter that silently dropped the commission arguments would have produced a prettier backtest and a wrong one.

What else changes meaning

The fee itself is the smallest loss. Total fees paid on the 47 closed trades: $359.12 — mean $7.64 a trade, 4.0% of gross profit, biggest single fee $9.59. No winner flipped to a loser on the fee alone. But the strategy lost $1,592.79 against the no-cost run (net +$2,530.24 → +$937.45, Profit Factor 1.302 → 1.117, in our testing). The other $1,233.67 never appears on any fee line, and it splits into two measured pieces.

One entry never happened — over $51.41. The fee run has 48 entries against the no-cost run's 49. The missing one is 2015-08-28, the trail's third-biggest winner (+$925.95 over 59 bars). The signal fired and the position was flat; the arithmetic failed. Sizing is 100% of equity, computed off the signal bar's close (3,205.64): equity before that signal was $9,687.31 in the fee run — $156.72 of it already paid out as fees on trades 1–19 — so quantity was 3 contracts, and 3 contracts at the next bar's open (3,246.24) cost $9,738.72, $51.41 more than the account held. The runtime dropped the order silently, no diagnostic, no trade row. The no-cost run had $105.31 of headroom over the same fill, so the fee didn't create the fragility — it spent the last $51.41 of a buffer that was already thin.

The lower equity path shrank later positions. Seven closed trades ran short: six dropped from 3 contracts to 2 and one from 2 to 1, because percent-of-equity sizing buys fewer contracts off a smaller account. Smaller size cuts both wins and losses; on these seven trades the net effect was −$307.72. The biggest winner took the biggest cut: trade #45 (+$2,361.54 at 3 contracts) ran at 2 contracts with fees, so its gross fell by $787.18 before the fee even touched it.

The run: the fee took $359, the strategy lost $1,593

We ran the fee variant in the PyneCore patched runtime on 4,089 bars of CSI 300 daily data (2010-01-04 to 2026-11-06), $10,000, 100% of equity per trade, 0.1% round-trip fee, no slippage — in our testing. Same entries, exits and stops as the no-cost run, so every dollar below is a cost effect:

No costs0.1% round-trip fee
Closed trades4847
Stop exits / trail exits19 / 2919 / 28
Net profit+$2,530.24+$937.45
Profit Factor1.3021.117
Win rate19 / 48 (39.6%)18 / 47 (38.3%)
Gross profit / gross loss+$10,908.26 / −$8,378.02+$8,951.70 / −$8,014.25
Avg win / avg loss$574.12 / −$288.90$497.32 / −$276.35
Avg hold17.04 bars16.15 bars
Max equity drawdown$1,881.50 (18.82%)$2,231.62 (21.01%)
Median trade−$161.62−$172.61
Top-5 winners' share of gross profit63.7%67.6%
Net minus the biggest trade+$168.70−$936.86

The decomposition closes to the cent: direct fees −$359.12 (23%), sizing drift −$307.72 (19%), the dropped entry −$925.95 (58%). On the 48 entries both runs share, every exit bar is identical — the fee changed the money, not the logic. The strategy still ends positive, but net minus its biggest winner is now −$936.86: without one trade, costs turn it into a loser. The lesson isn't that fees are expensive — $7.64 a trade is cheap. It's that on a fully-invested strategy, the fee column is the smallest part of what costs cost you. Check the orders that never fired. One symbol, one period, one fee rate, no slippage — 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.