Help / Trading and testing
Backtesting a strategy
The strategy tester runs a strategy script over history and shows what it would have done, with fees and slippage counted. You need a strategy script on the chart: start from the “Moving average cross strategy” template in the script editor (see strategies).
Running a backtest#
- Add a strategy script to the chart.
- Open the Strategy tester tab. Results update by themselves as you change the script or the chart.
- Read the Overview (equity curve and headline numbers), Performance (the full statistics), Monte Carlo (how much luck was involved) and Trades (every trade, with its entry, exit and result).
Properties#
Under Properties, set the starting capital, order size, commission, slippage, pyramiding, margin and leverage. Bar magnifier looks inside each bar at a finer timeframe so stops and targets fill in the right order. An order size in % of equity is of the buying power (equity × leverage); one in cash is the order’s value, whatever the leverage.
Reading the numbers#
Sharpe and Sortino are yearly figures, from the equity’s bar-to-bar returns scaled by how many bars the tested dates really have in a year: about 252 for daily stock bars, 365 for crypto, and for intraday stock bars only those in the sessions. So they compare fairly across markets and timeframes. They take no risk-free rate. Profit factor shows ∞ when there were winning trades and no losing ones.
Testing further back#
Turn on Deep backtest to test up to 50,000 bars between dates you choose, rather than just the bars on the chart.
Several symbols#
Symbols runs the same strategy on several symbols, and as an equal-weight portfolio of them, to see whether it works beyond the one market you built it on.
Optimising#
- Open Optimise.
- For each input to try, tick Vary and set From, To and Step.
- Press Optimise to try every combination, or Walk-forward to optimise on one stretch of history and test on the next, window by window.
Both split the backtest’s dates (the deep backtest range, or the latest bars without one), never the older bars that only warm up the indicators. Each stretch needs at least 30 bars, so a short range asks you to widen it or use fewer windows.
Keeping a result#
Send to Strategy Lab saves the trades as a strategy in the Strategy Lab, to track and compare with others (see The Strategy Lab).
TradingView and Pine Script are trademarks of TradingView, Inc. OpenTapefolio is not affiliated with or endorsed by TradingView.