Conversion example · Strategy
Trailing stop distance: 1x vs 2x vs 3x ATR
Example conversion · Published November 27, 2026
The same RSI mean-reversion strategy as the trailing-stop example and the commission-fees example — same entries, same 2×ATR hard stop, same 4,089 bars of CSI 300 daily data, same 0.1% round-trip fee — with one change: the trailing distance. The trail level is the highest close since entry minus N×ATR, and N is 1, 2 or 3. The hard stop never moves. This page is the sensitivity test post #18 promised and post #20 asked for with the fee on: three runs, one number changed, everything else identical.
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 1× and 3× outputs differ from the 2× output in exactly two lines each: the strategy title and the trail multiplier.
The Pine original (2×ATR trail)
//@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, 1×ATR trail
"""
@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, trail 1xATR, 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 * 1)) if na(trailLevel) else math.max(trailLevel, (close - (atrVal * 1))))
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))The Python output, 3×ATR trail
"""
@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, trail 3xATR, 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 * 3)) if na(trailLevel) else math.max(trailLevel, (close - (atrVal * 3))))
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
The trail is a var float that only ever rises (math.max(trailLevel, close - atrVal * N)), reset to na out of trade, and the exit is a plain strategy.close when the close falls below it. The converter emits that state machine verbatim — the var, the na reset, the math.max ratchet — with zero diagnostics in all three variants. Changing the multiplier is a one-character edit in Pine and a one-line diff in the Python. A converter that silently dropped the var or the na reset would have produced a trail that resets every bar and a backtest that means something else entirely.
What else changes meaning
One number moves the headline $6,820. Same entries, same data, same fee — net profit −$1,560.26 at 1×ATR, +$937.45 at 2×, +$5,259.95 at 3×, in our testing (Profit Factor 0.753 / 1.117 / 1.508). The win rate goes the wrong way: 3× wins only 27.7% of its trades and makes the most money, because average win grows five-fold ($238 → $1,201) while average loss grows by half ($204 → $305). The trail doesn't make the strategy right more often. It makes the right times pay more.
Give-back scales with the multiplier. Highest close since entry minus exit price, per trail exit: median 99.83 points at 1×, 156.60 at 2×, 230.94 at 3×. At 3×, half the trail exits hand back more than 200 points. The wider trail pays more give-back per exit and collects far more per winner — the exchange rate on this data is $963 of extra average win for $130 of extra median give-back per exit. That rate is one sample's price, not a rule.
Which exit fires first flips. The hard stop is fixed at 2×ATR below entry, so a 1× trail fires before the stop can (12 of the 2× run's stop exits become trail exits, all still losers but 11 of 12 lose less), while a 3× trail is looser than the stop, so the stop fires first (5 of the 2× run's trail exits become stop exits, 4 of 5 lose more). Stop exits never win in any run: 0 winners out of 49 stop exits across the three runs.
The entry stream itself changes — including a second silent drop. 1× exits sooner, so it takes three entries the other runs never see (bars 850, 981, 1338), net −$432.86. And at the 2011-12-30 signal, the 3× run silently dropped the order: its equity was $9,402.97 — higher than the other two — so it sized 4 contracts, and 4 × $2,361.50 (the next bar's open) = $9,446.00, $43.03 more than the account held. The same gate as the $51.41 drop in the commission-fees example, and the irony is exact: the run was dropped because it was ahead. The dropped trade would have lost −$294.07. The affordability gate is a property of the equity path, and the trail distance moves the equity path.
The runs: 1× vs 2× vs 3× ATR trail
We ran all three variants 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:
| 1×ATR trail | 2×ATR trail | 3×ATR trail | |
|---|---|---|---|
| Closed trades | 51 | 47 | 47 |
| Trail exits / stop exits | 44 / 7 | 28 / 19 | 24 / 23 |
| Net profit | −$1,560.26 | +$937.45 | +$5,259.95 |
| Profit Factor | 0.753 | 1.117 | 1.508 |
| Win rate | 20 / 51 (39.2%) | 18 / 47 (38.3%) | 13 / 47 (27.7%) |
| Gross profit / gross loss | +$4,763.40 / −$6,323.66 | +$8,951.70 / −$8,014.25 | +$15,614.55 / −$10,354.60 |
| Avg win / avg loss | $238.17 / −$203.99 | $497.32 / −$276.35 | $1,201.12 / −$304.55 |
| Avg hold | 7.96 bars | 16.15 bars | 26.45 bars |
| Median trade | −$73.31 | −$172.61 | −$221.97 |
| Fees paid | $322.50 | $359.12 | $427.20 |
| Max equity drawdown | $3,534.99 (34.89%) | $2,231.62 (21.01%) | $2,839.78 (23.34%) |
| Top-5 winners' share of gross profit | 73.2% | 67.6% | 66.3% |
| Net minus the biggest trade | −$3,127.45 | −$936.86 | +$3,055.85 |
The median trade loses money in all three runs, and only the 3× run still stands without its biggest winner. Three points are a sensitivity test, not an optimum: 3× is the best of three on one script, one market, 4,089 bars — picking the best of three and reporting it as “the answer” would be tuning, not testing. The lesson is the discipline: a parameter you never tested is a parameter you chose by feel. Test the distance — including the entries it quietly adds and drops — or admit you didn't. 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.