Pine Script v5 · Strategy
Strategy BackTest Display Statistics - TraderHalai
A Pine Script v5 strategy built on SMA (close, 2) and Crossunder (smallMA, buyMA). It has 3 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 5% of equity and pays 0.1% commission. 5 settings; defaults include Source close, Small Moving Average 2 and Big Moving Average 8.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does Strategy BackTest Display Statistics - TraderHalai repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 14 of 14 trades.
- How did Strategy BackTest Display Statistics - TraderHalai do on BTCUSDT 1D with default settings?
- +0.98%
- From 26 Jan 2024 to 21 Oct 2026, over 51 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Strategy BackTest Display Statistics - TraderHalai run in Pine Script v6?
- No
- The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 51 before, 51 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does Strategy BackTest Display Statistics - TraderHalai use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Strategy BackTest Display Statistics - TraderHalai run in Zeal Terminal?
- Yes
- It compiles in 27 ms and runs 1,000 bars in 24 ms, in the browser.
BTCUSDT · 1D · default settings
- SMA
- SMA
- SMA
- Crossunder
- Winning trade
- Losing trade
- Equity
- Buy and hold
| Total Trades: 51 | Starting: $10000 | Avg Win: $24.93 | Profit Factor: 1.14 | Return: +0.98% |
| Win Rate: 60.78% | Ending: $10098.2 | Avg Loss: $33.74 | Max Runup: 372.21 | Max DD: 3.33% |
- Net profit
- +0.98%
- $98.20
- Buy and hold
- +102.5%
- same period
- Closed trades
- 51
- 31 won · 20 lost
- Profitable
- 60.8%
- of closed trades
- Profit factor
- 1.15
- gross profit ÷ gross loss
- Max drawdown
- 3.25%
- −$336.65
One run with the author's default settings, $10,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Source | close | source | smallMA bigMA buyMA |
| Small Moving Average | 2 | int | smallMA |
| Big Moving Average | 8 | int | bigMA buyMA |
| Percent below to buy % | 1 | int | buyMA |
| Draw Tester | true | bool | line 57 |
What it calculates
- SMASource (close), Small Moving Average (2)
smallMA · line 22 - SMASource (close), Big Moving Average (8)
bigMA · line 23 - SMASource (close), Big Moving Average (8)
buyMA · line 24 - CrossundersmallMA, buyMA
buy · line 25
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open BUY long | (buy) | On | 27 |
| Close BUY | (strategy.openprofit >= strategy.position_avg_price * 0.01) | On | 29 |
| Close BUY | (ta.barssince(buy) >= 7) | On | 31 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +1.0% | +102.5% | 51 | 61% | 1.15 | 3.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.1% | +5.5% | 31 | 52% | 0.89 | 0.8% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −2.9% | +9.8% | 60 | 50% | 0.76 | 4.7% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.7% | +8.4% | 40 | 68% | 1.38 | 1.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −2.5% | +16.7% | 72 | 76% | 0.72 | 5.3% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.1% | +21.5% | 50 | 74% | 1.09 | 0.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +1.4% | +148.3% | 60 | 80% | 1.32 | 2.3% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.2% | +17.1% | 32 | 63% | 0.86 | 0.9% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 19 | 68% | $321.82 |
| Sideways market | 17 | 53% | −$77.18 |
| Falling market | 15 | 60% | −$146.45 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.7%
The 95th percentile of the worst drawdown when the same 51 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.5% to 3.7%; the tick is what actually happened, 3.1%. Order luck alone moves it this much.
All 51 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 24 Feb 2024 50,744.20 | 2 Mar 2024 62,387.90 | 7 | $113.50 |
| long | 17 Mar 2024 65,300.60 | 30 Mar 2024 69,850.50 | 13 | $34.15 |
| long | 3 Apr 2024 65,464.00 | 10 Apr 2024 69,146.00 | 7 | $27.47 |
| long | 13 Apr 2024 67,116.50 | 20 Apr 2024 63,818.00 | 7 | −$25.97 |
| long | 27 Apr 2024 63,770.00 | 4 May 2024 62,882.00 | 7 | −$8.07 |
| long | 11 May 2024 60,800.00 | 18 May 2024 67,024.00 | 7 | $50.79 |
| long | 2 Jun 2024 67,766.80 | 9 Jun 2024 69,310.50 | 7 | $10.57 |
| long | 12 Jun 2024 67,314.20 | 26 Jun 2024 61,806.00 | 14 | −$42.68 |
| long | 5 Jul 2024 57,050.00 | 12 Jul 2024 57,339.90 | 7 | $1.56 |
| long | 26 Jul 2024 65,799.90 | 8 Aug 2024 55,133.80 | 13 | −$83.21 |
| long | 16 Aug 2024 57,541.10 | 23 Aug 2024 60,375.80 | 7 | $23.77 |
| long | 28 Aug 2024 59,415.00 | 4 Sep 2024 57,487.70 | 7 | −$17.36 |
| long | 5 Sep 2024 57,970.90 | 12 Sep 2024 57,338.00 | 7 | −$6.50 |
| long | 2 Oct 2024 60,804.90 | 9 Oct 2024 62,160.50 | 7 | $10.20 |
| long | 11 Oct 2024 60,326.40 | 18 Oct 2024 67,421.80 | 7 | $58.20 |
| long | 24 Oct 2024 66,668.60 | 3 Nov 2024 69,374.70 | 10 | $19.54 |
| long | 4 Nov 2024 68,776.00 | 11 Nov 2024 80,370.00 | 7 | $84.50 |
| long | 27 Nov 2024 91,965.20 | 4 Dec 2024 95,849.70 | 7 | $20.58 |
| long | 11 Dec 2024 96,593.00 | 18 Dec 2024 106,134.00 | 7 | $49.60 |
| long | 20 Dec 2024 97,461.90 | 27 Dec 2024 95,791.60 | 7 | −$9.86 |
| long | 28 Dec 2024 94,299.00 | 4 Jan 2025 98,174.20 | 7 | $20.11 |
| long | 9 Jan 2025 95,060.60 | 16 Jan 2025 100,497.00 | 7 | $28.45 |
| long | 28 Jan 2025 102,083.00 | 9 Feb 2025 96,444.80 | 12 | −$29.59 |
| long | 25 Feb 2025 91,552.90 | 4 Mar 2025 86,221.20 | 7 | −$31.05 |
| long | 9 Mar 2025 86,222.50 | 16 Mar 2025 84,338.40 | 7 | −$12.26 |
| long | 30 Mar 2025 82,648.50 | 14 Apr 2025 83,760.00 | 15 | $5.88 |
| long | 30 May 2025 105,590.00 | 13 Jun 2025 105,672.00 | 14 | −$0.63 |
| long | 14 Jun 2025 106,067.00 | 28 Jun 2025 107,048.00 | 14 | $3.72 |
| long | 2 Aug 2025 113,298.00 | 9 Aug 2025 116,675.00 | 7 | $14.28 |
| long | 17 Aug 2025 117,381.00 | 24 Aug 2025 115,438.00 | 7 | −$9.54 |
| long | 26 Aug 2025 110,112.00 | 6 Sep 2025 110,660.00 | 11 | $1.53 |
| long | 23 Sep 2025 112,651.00 | 30 Sep 2025 114,312.00 | 7 | $6.55 |
| long | 11 Oct 2025 112,774.00 | 18 Oct 2025 106,432.00 | 7 | −$29.96 |
| long | 31 Oct 2025 108,323.00 | 11 Nov 2025 106,011.00 | 11 | −$11.97 |
| long | 14 Nov 2025 99,692.00 | 21 Nov 2025 86,637.20 | 7 | −$68.11 |
| long | 2 Dec 2025 86,286.00 | 14 Dec 2025 90,240.00 | 12 | $22.30 |
| long | 15 Dec 2025 88,172.20 | 22 Dec 2025 88,658.90 | 7 | $1.79 |
| long | 11 Jan 2026 90,504.70 | 18 Jan 2026 95,147.80 | 7 | $25.15 |
| long | 20 Jan 2026 92,631.00 | 27 Jan 2026 88,347.10 | 7 | −$24.67 |
| long | 30 Jan 2026 84,650.20 | 6 Feb 2026 62,909.90 | 7 | −$132.03 |
| long | 12 Feb 2026 67,082.50 | 3 Mar 2026 68,830.10 | 19 | $12.11 |
| long | 8 Mar 2026 67,262.90 | 15 Mar 2026 71,211.90 | 7 | $28.59 |
| long | 20 Mar 2026 69,930.00 | 27 Mar 2026 68,820.30 | 7 | −$9.03 |
| long | 28 Mar 2026 66,407.30 | 4 Apr 2026 66,964.30 | 7 | $3.23 |
| long | 30 Apr 2026 75,780.00 | 7 May 2026 81,447.00 | 7 | $36.77 |
| long | 14 May 2026 79,313.60 | 4 Jun 2026 64,142.80 | 21 | −$98.01 |
| long | 19 Jun 2026 62,958.00 | 2 Jul 2026 60,024.00 | 13 | −$24.41 |
| long | 26 Jul 2026 64,375.00 | 9 Aug 2026 64,962.60 | 14 | $3.57 |
| long | 12 Aug 2026 63,600.00 | 19 Aug 2026 64,725.40 | 7 | $7.86 |
| long | 3 Sep 2026 77,340.00 | 24 Sep 2026 84,397.60 | 21 | $44.77 |
| long | 9 Oct 2026 81,754.50 | 16 Oct 2026 82,245.90 | 7 | $2.02 |
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Strategy BackTest Display Statistics - TraderHalai this page | — | v5 strategy | +1.0% | 51 | 3.2% |
| Follow the Crypto Shorts by xtradernet | sma | v4 strategy | No trades on this market | ||
| buy down and sell high trend 200 sma and ma cross by willi130503 | sma | v4 strategy | No trades on this market | ||
| Mayer Multiple Strategy by xtradernet | sma | v4 strategy | No trades on this market | ||
| Moving Average Cross Strategy early Closing by Geduldtrader | sma | v4 strategy | +73.8% | 20 | 151.5% |
| MFI Simple Strategy by OztheWoz | sma | v5 strategy | −0.2% | 28 | 3.2% |
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 4 settings use the untyped input() (line 18, 19, 20, 21). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 51 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 18: input() → input.source()
- Line 19: input() → input.int()
- Line 20: input() → input.int()
- Line 21: input() → input.int()
⋯//DEMO basic strategy - Use your own strategy here - Jaws Mean Reversion from my profile used here-source = input(title = "Source", defval = close)+source = input.source(title = "Source", defval = close)-smallMAPeriod = input(title = "Small Moving Average", defval = 2)+smallMAPeriod = input.int(title = "Small Moving Average", defval = 2)-bigMAPeriod = input(title = "Big Moving Average", defval = 8)+bigMAPeriod = input.int(title = "Big Moving Average", defval = 8)-percentBelowToBuy = input(title = "Percent below to buy %", defval = 1)+percentBelowToBuy = input.int(title = "Percent below to buy %", defval = 1)smallMA = ta.sma(source, smallMAPeriod)⋯Does it run in Pine Script v6?
No
The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 51 before, 51 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯//Thanks and enjoy! :)-//@version=5+//@version=6strategy("Strategy BackTest Display Statistics - TraderHalai", overlay=true, default_qty_value= 5, default_qty_type = strategy.percent_of_equity, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 27 ms | Yes 1,000 bars in 24 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © TraderHalai// This script was born out of my quest to be able to display strategy back test statistics on charts to allow for easier backtesting on devices that do not natively support backtest engine (such as mobile phones, when I am backtesting from away from my computer). There are already a few good ones on TradingView, but most / many are too complicated for my needs.////Found an excellent display backtest engine by 'The Art of Trading'. This script is a snippet of his hard work, with some very minor tweaks and changes. Much respect to the original author.////Full credit to the original author of this script. It can be found here: https://www.tradingview.com/script/t776tkZv-Hammers-Stars-Strategy/?offer_id=10&aff_id=15271//// This script can be copied and airlifted onto existing strategy scripts of your own, and integrates out of the box without implementation of additional functions. I've also added Max Runup, Average Win and Average Loss per trade to the orignal script.////Will look to add in more performance metrics in future, as I further develop this script.////Feel free to use this display panel in your scripts and strategies.//Thanks and enjoy! :)//@version=5strategy("Strategy BackTest Display Statistics - TraderHalai", overlay=true, default_qty_value= 5, default_qty_type = strategy.percent_of_equity, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.1)//DEMO basic strategy - Use your own strategy here - Jaws Mean Reversion from my profile used heresource = input(title = "Source", defval = close)smallMAPeriod = input(title = "Small Moving Average", defval = 2)bigMAPeriod = input(title = "Big Moving Average", defval = 8)percentBelowToBuy = input(title = "Percent below to buy %", defval = 1)smallMA = ta.sma(source, smallMAPeriod)bigMA = ta.sma(source, bigMAPeriod) buyMA = ((100 - percentBelowToBuy) / 100) * ta.sma(source, bigMAPeriod)[0]buy = ta.crossunder(smallMA, buyMA)if(buy) strategy.entry("BUY", strategy.long)if(strategy.openprofit >= strategy.position_avg_price * 0.01) // 1% profit target strategy.close("BUY")if(ta.barssince(buy) >= 7) //Timed Exit, if you fail to make 1 percent in 7 candles. strategy.close("BUY") ///////////////////////////// --- BEGIN TESTER CODE --- ////////////////////////// COPY below into your strategy to enable display////////////////////////////////////////////////////////////////////////////////// Declare performance tracking variablesdrawTester = input.bool(true, "Draw Tester")var balance = strategy.initial_capitalvar drawdown = 0.0var maxDrawdown = 0.0var maxBalance = 0.0var totalWins = 0var totalLoss = 0// Prepare stats tablevar table testTable = table.new(position.top_right, 5, 2, border_width=1)f_fillCell(_table, _column, _row, _title, _value, _bgcolor, _txtcolor) => _cellText = _title + "\n" + _value table.cell(_table, _column, _row, _cellText, bgcolor=_bgcolor, text_color=_txtcolor) // Custom function to truncate (cut) excess decimal placestruncate(_number, _decimalPlaces) => _factor = math.pow(10, _decimalPlaces) int(_number * _factor) / _factor // Draw stats tablevar bgcolor = color.new(color.black,0)if drawTester if barstate.islastconfirmedhistory // Update table dollarReturn = strategy.netprofit f_fillCell(testTable, 0, 0, "Total Trades:", str.tostring(strategy.closedtrades), bgcolor, color.white) f_fillCell(testTable, 0, 1, "Win Rate:", str.tostring(truncate((strategy.wintrades/strategy.closedtrades)*100,2)) + "%", bgcolor, color.white) f_fillCell(testTable, 1, 0, "Starting:", "$" + str.tostring(strategy.initial_capital), bgcolor, color.white) f_fillCell(testTable, 1, 1, "Ending:", "$" + str.tostring(truncate(strategy.initial_capital + strategy.netprofit,2)), bgcolor, color.white) f_fillCell(testTable, 2, 0, "Avg Win:", "$"+ str.tostring(truncate(strategy.grossprofit / strategy.wintrades, 2)), bgcolor, color.white) f_fillCell(testTable, 2, 1, "Avg Loss:", "$"+ str.tostring(truncate(strategy.grossloss / strategy.losstrades, 2)), bgcolor, color.white) f_fillCell(testTable, 3, 0, "Profit Factor:", str.tostring(truncate(strategy.grossprofit / strategy.grossloss,2)), strategy.grossprofit > strategy.grossloss ? color.green : color.red, color.white) f_fillCell(testTable, 3, 1, "Max Runup:", str.tostring(truncate(strategy.max_runup, 2 )), bgcolor, color.white) f_fillCell(testTable, 4, 0, "Return:", (dollarReturn > 0 ? "+" : "") + str.tostring(truncate((dollarReturn / strategy.initial_capital)*100,2)) + "%", dollarReturn > 0 ? color.green : color.red, color.white) f_fillCell(testTable, 4, 1, "Max DD:", str.tostring(truncate((strategy.max_drawdown / strategy.equity) * 100 ,2)) + "%", color.red, color.white)// --- END TESTER CODE --- ///////////////