Rewrite each ta. indicator as an array calculation on the bars, turn strategy.entry and strategy.exit into one order with its stop and target, and move the settings in strategy() into the backtest call. Pine Script already decides once per bar, so the logic carries over line by line. What changes is where the bars come from and how orders are written.
What maps to what
strategy(...)settings (initial_capital,commission_value,slippage) becomeinitial_capital=,commission_per_unit=andspread=orslippage=onrr.backtest.close,close[1]aredata.close[-1]anddata.close[-2].lookbacksets how many past barsdataholds.ta.sma(close, 10)isdata.close[-10:].mean(). Other indicators are NumPy or pandas on the same arrays, or a library such as TA-Lib.ta.crossover(fast, slow)compares this bar's values with the previous bar's, as in the example below.strategy.position_sizeisdata.position(), the signed quantity.strategy.entrywithstrategy.exit(stop=, limit=)isrr.buy_market(qty, stop_loss=..., take_profit=...). Alimit=orstop=on the entry itself isrr.buy_limitorrr.buy_stop.strategy.closeisrr.close_position().request.securityon another symbol is a second ticker in the dict passed torr.backtest, read asdata['QQQ'].close.plot()andlabel.new()becomeprint()calls inon_bar; the visual trade diagnostics tool shows each printed value beside the trade it belongs to.- The Strategy Tester's figures are fields on the result: net P&L, closed trades, win rate, profit factor, Sharpe, Sortino and maximum drawdown print with
print(result), andresult.closed_tradeslists every trade.
The Pine Script version
A 10 and 30 bar SMA crossover that enters with a 1% stop and a 2% target, and exits early on the cross back down:
//@version=6
strategy("SMA cross with bracket", overlay = true, initial_capital = 100000,
commission_type = strategy.commission.cash_per_contract, commission_value = 0.005)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)
crossedUp = ta.crossover(fast, slow)
crossedDown = ta.crossunder(fast, slow)
if crossedUp and strategy.position_size == 0
strategy.entry("Long", strategy.long, qty = 100)
strategy.exit("Bracket", "Long", stop = close * 0.99, limit = close * 1.02)
if crossedDown and strategy.position_size > 0
strategy.close("Long")
The Reamer Research version
import reamer_research as rr
class SmaCross:
lookback = 31
def on_bar(self, data):
close = data.close
fast_now, slow_now = close[-10:].mean(), close[-30:].mean()
fast_prev, slow_prev = close[-11:-1].mean(), close[-31:-1].mean()
crossed_up = fast_prev <= slow_prev and fast_now > slow_now
crossed_down = fast_prev >= slow_prev and fast_now < slow_now
if data.position() == 0 and crossed_up:
price = close[-1]
return rr.buy_market(100, stop_loss=price * 0.99,
take_profit=price * 1.02)
if data.position() > 0 and crossed_down:
return rr.close_position()
result = rr.backtest(SmaCross, 'bars.csv', initial_capital=100_000,
commission_per_unit=0.005, spread=0.01, rng_seed=1)
print(result)
bars.csv is your own bars, from your data vendor or broker, with date, open, high, low, close and volume columns. The class could equally be a plain on_bar(data) function, or an object configured with its two lengths. Loaders for CSV, .bin, Parquet, pandas, Alpaca and Databento bars are in the Reamer Research examples repository under the MIT licence.
Where the two results can differ
- Fill timing. By default TradingView fills a market order at the next bar's open, and Reamer Research fills it on the next bar too, so entries line up. With
process_orders_on_close = true, Pine fills on the signal bar's close; to match, compare against that setting's off state. - A bar that touches the stop and the target. TradingView's broker emulator assumes price went open, high, low, close when the open is nearer the high, and open, low, high, close otherwise, unless the bar magnifier is on. Reamer Research walks a seeded tick path inside the bar, the same on every run with the same seed.
- The bars themselves. Your vendor's bars and TradingView's feed can differ in session times, adjustments and the odd print, which moves crossovers by a bar.
- Costs. The Pine version above charges no spread; the Python version charges 0.01 on every fill. Set
spread=0to compare the fill rules alone.
So expect the same trades with small differences in fill prices, and the odd trade that differs on a bar where both exits were in range.
What you get after the port
- Your own data and machine. Any bars you can load, on any number of tickers, without a chart in the loop.
- Visual trade diagnostics. Every closed trade on its own bars beside every value your strategy printed.
- Repeatable results. The same data, settings and seed give the same result to the byte.
- Monte Carlo and parameter sweeps in Python.
result.monte_carlo(simulations=10000, seed=0), or a loop over settings, in the same script. - Live and paper trading. The relay sends the orders
on_barreturns to your broker through the open-source receiver.
To port with an AI assistant, the Reamer Research MCP server lets Claude Code, Claude Desktop or Cursor write the on_bar version and backtest it on your bars.
Reamer Research runs on Linux, macOS, or Windows through WSL2, from Python or C++. The free package, pip install reamer-research, runs the example above in Evaluation Mode, on one ticker over up to 30 days of bars. A licence costs $20 per seat per month, cancel any time.