Conversion example · Strategy
Clockwork entries: Pine Script to Python
Example conversion · Published November 25, 2026
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: the entry rule is gone. Instead of buying when the RSI crosses up through 30, the script buys every 83rd bar (bar_index % 83 == 0, from bar 83), 49 ticks, the same count as the RSI entry stream. The RSI is still computed and plotted, so the script is byte-identical apart from the entry line — but nothing in the entry decision reads it. This page is the placebo control for the whole series: if your entry rule can't beat a clock, it isn't a rule, it's a ritual.
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
//@version=6
strategy("RSI Mean Reversion (clockwork entries, trailing exit, stop 2xATR)", overlay=true, initial_capital=10000,
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, identical to #18: 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
// THE CONTROL: entries carry no price information at all. Every 83rd
// bar, from bar 83 — 83, 166, ..., 4067 — 49 ticks on 4,089 bars, the
// same count as the RSI entry stream in #15–#18. RSI is still computed
// (and plotted) so the script is byte-identical apart from this line,
// but nothing in the entry decision reads it.
longSignal = bar_index >= 83 and bar_index % 83 == 0
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
"""
@pyne
"""
from pynecore import Persistent, Series
from pynecore.lib import bar_index, close, color, hline, input, math, na, plot
from pynecore.lib import script, 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 (clockwork entries, trailing exit, stop 2xATR)", overlay=True, initial_capital=10000, 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[bool] = ((bar_index >= 83) and ((bar_index % 83) == 0))
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
bar_index % 83 == 0 becomes a Series[bool] in Python — the bar counter is a first-class value the converter carries across untouched, and the modulo comparison keeps its shape exactly. The entry gate longSignal and not inTrade is unchanged from the RSI version: the only line that differs in the whole compiled file is the longSignal assignment itself (plus the title string and the bar_index import). A control experiment you can verify by diffing two files is the cheapest honest experiment there is.
What else changes meaning
One of the 49 ticks never fired — silently. Bar 1660 (2016-11-07) was a scheduled entry, the signal plotted 1, the position was flat, and no trade appears in the log. The cause is sizing, not signal: equity was $10,076.37, the signal bar's close was 3,356.59, so 100%-of-equity sizing computed 3 contracts ($10,069.77 — inside equity by $6.60). But entries fill at the next bar's open, 3,367.70, and 3 contracts there cost $10,103.10 — $26.73 more than the account held. The runtime dropped the order with no diagnostic and no trade row. The control therefore ran on 48 entries, not 49. The missing trade is an affordability artifact, not a price-information leak, so it doesn't bias the comparison — but the same mechanism applies to every RSI entry in the series, and any signal whose fill gapped up past affordability would have vanished the same way.
The clock caught the best trade the RSI never saw. Trade #14 entered 2014-10-24 @ 2,397.85 and exited 2015-01-20 @ 3,336.79, 60 bars, +$2,816.82 — the biggest winner in either run. The RSI stream had no trade overlapping that window at all: the market was rising, the RSI never crossed down through 30, so the crossover never fired. And when both runs caught the same move — the 2024-09-30 spike, exit 2024-10-10 @ 3,979.61 in both — the $776.42 gap between them is one contract (2 vs 3), not timing: the clock entered nine days earlier.
The run: the clock made money too
We ran the clockwork 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, no commission, no slippage — in our testing. Same exits and stops as the RSI run, so every dollar below is an entry effect:
| RSI entries | Clock entries | |
|---|---|---|
| Closed trades | 48 | 48 |
| Trail exits / stop exits | 29 / 19 | 25 / 23 |
| Net profit | +$2,530.24 | +$706.11 |
| Profit Factor | 1.302 | 1.092 |
| Win rate | 19 / 48 (39.6%) | 16 / 48 (33.3%) |
| Gross profit / gross loss | +$10,908.26 / −$8,378.02 | +$8,389.21 / −$7,683.10 |
| Avg win / avg loss | $574.12 / −$288.90 | $524.33 / −$240.10 |
| Avg hold | 17.04 bars | 15.17 bars |
| Max equity drawdown | $1,881.50 (18.82%) | $2,325.22 (23.25%) |
| Median trade | −$163.07 | −$136.87 |
| Top-5 winners' share of gross profit | 63.7% | 73.6% |
| Net minus the biggest trade | +$168.70 | −$2,110.71 |
The honest version of that headline: the RSI crossover is doing real work — $1,824.13 of net, three extra wins, nine trades over +$500 against the clock's four, and a gap that grows to $2,279.41 once you remove each run's biggest trade. But the control also made money, and both runs are still carried by five trades: without them, the RSI loses $4,418.88 and the clock loses $5,468.56. The entry rule improves the concentration. It does not fix it. 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.