Pine Script v4 · Strategy
Backtest Signal To Noise
A Pine Script v4 strategy built on SMA (StN, 7). It has 3 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 3 settings; defaults include Days 21 and Smooth 7.
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 Backtest Signal To Noise repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 19 of 19 trades.
- How did Backtest Signal To Noise do on BTCUSDT 1D with default settings?
- +1.70%
- From 26 Jan 2024 to 21 Oct 2026, over 57 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Backtest Signal To Noise be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- Does Backtest Signal To Noise use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Backtest Signal To Noise run in Zeal Terminal?
- Yes
- It compiles in 9 ms and runs 1,000 bars in 41 ms, in the browser.
BTCUSDT · 1D · default settings
- StN
- Smooth
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +1.70%
- $1,700.54
- Buy and hold
- +102.5%
- same period
- Closed trades
- 57
- 21 won · 36 lost
- Profitable
- 36.8%
- of closed trades
- Profit factor
- 1.01
- gross profit ÷ gross loss
- Max drawdown
- 35.18%
- −$41,232.50
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A short position was still open at the last bar. 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.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses iff(), which v5 removed line 24
iff() on line 24, 25, 26, 27. v5 writes it as the ternary c ? a : b.
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames log → math.log, sma → ta.sma.
v4 → v5
VerifiedSame 57 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 1: //@version=4 → //@version=5
- Line 17: log → math.log
- Line 19: input(type = input.integer) → input.int()
- Line 20: input(type = input.integer) → input.int()
- Line 23: sma → ta.sma
- Line 24: iff(c, a, b) → (c ? a : b)
3 more edits
- Line 25: iff(c, a, b) → (c ? a : b)
- Line 26: iff(c, a, b) → (c ? a : b)
- Line 27: iff(c, a, b) → (c ? a : b)
-//@version=4+//@version=5////////////////////////////////////////////////////////////⋯ StN := StN + (1/close[i])/length- StN := -10*log(StN)+ StN := -10*math.log(StN)strategy(title="Backtest Signal To Noise ", shorttitle="StoN", overlay=false)-length = input(title="Days", type=input.integer, defval=21, minval=2)+length = input.int(title="Days", defval=21, minval=2)-Smooth = input(title="Smooth", type=input.integer, defval=7, minval=2)+Smooth = input.int(title="Smooth", defval=7, minval=2)reverse = input(false, title="Trade reverse")StN = SignalToNoise(length)-SMAStN = sma(StN, Smooth)+SMAStN = ta.sma(StN, Smooth)-pos = iff(SMAStN[1] > StN[1] , -1,- iff(SMAStN[1] < StN[1], 1, 0)) +pos = (SMAStN[1] > StN[1] ? -1 : (SMAStN[1] < StN[1] ? 1 : 0)) -possig = iff(reverse and pos == 1, -1,- iff(reverse and pos == -1 , 1, pos)) +possig = (reverse and pos == 1 ? -1 : (reverse and pos == -1 ? 1 : pos)) if (possig == 1) ⋯Same 57 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 21: input() → input.bool()
⋯Smooth = input.int(title="Smooth", defval=7, minval=2)-reverse = input(false, title="Trade reverse")+reverse = input.bool(false, title="Trade reverse")StN = SignalToNoise(length)⋯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: 57 before, 186 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.
-//@version=5+//@version=6////////////////////////////////////////////////////////////⋯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 |
|---|---|---|---|
| Days | 21 | int (written input.integer) · min 2 | StN line 15 |
| Smooth | 7 | int (written input.integer) · min 2 | SMAStN |
| Trade reverse | false | bool | possig |
What it calculates
- SMA (
sma,ta.smain v5)StN, Smooth (7)SMAStN · line 23
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 Long long | (possig == 1) | On | 29 |
| Open Short short | (possig == -1) | On | 31 |
| Close | (possig == 0) | On | 33 |
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.7% | +102.5% | 57 | 37% | 1.01 | 35.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −12.5% | +5.5% | 50 | 28% | 0.76 | 21.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.1% | +9.8% | 47 | 43% | 1.02 | 3.6% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.3% | +8.4% | 57 | 37% | 0.82 | 0.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.4% | +16.7% | 49 | 45% | 2.64 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 46 | 35% | 1.50 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.5% | +148.3% | 43 | 33% | 1.44 | 0.7% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.1% | +17.1% | 46 | 37% | 0.77 | 0.2% | 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 | 47% | −$5,038.39 |
| Sideways market | 20 | 35% | $19,563.70 |
| Falling market | 18 | 28% | −$12,824.80 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
58.8%
The 95th percentile of the worst drawdown when the same 57 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 31.6% to 58.8%; the tick is what actually happened, 33.3%. Order luck alone moves it this much.
All 57 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 23 Feb 2024 51,288.40 | 5 Apr 2024 68,487.80 | 42 | $17,199.40 |
| short | 5 Apr 2024 68,487.80 | 10 Apr 2024 69,146.00 | 5 | −$658.20 |
| long | 10 Apr 2024 69,146.00 | 18 Apr 2024 61,277.40 | 8 | −$7,868.62 |
| short | 18 Apr 2024 61,277.40 | 20 May 2024 66,274.00 | 32 | −$4,996.62 |
| long | 20 May 2024 66,274.00 | 14 Jun 2024 66,773.00 | 25 | $499.01 |
| short | 14 Jun 2024 66,773.00 | 19 Jul 2024 63,987.90 | 35 | $2,785.09 |
| long | 19 Jul 2024 63,987.90 | 8 Aug 2024 55,133.80 | 20 | −$8,854.16 |
| short | 8 Aug 2024 55,133.80 | 27 Aug 2024 62,834.00 | 19 | −$7,700.24 |
| long | 27 Aug 2024 62,834.00 | 3 Sep 2024 59,132.10 | 7 | −$3,701.88 |
| short | 3 Sep 2024 59,132.10 | 22 Sep 2024 63,349.00 | 19 | −$4,216.85 |
| long | 22 Sep 2024 63,349.00 | 13 Oct 2024 63,206.20 | 21 | −$142.74 |
| short | 13 Oct 2024 63,206.20 | 18 Oct 2024 67,421.80 | 5 | −$4,215.55 |
| long | 18 Oct 2024 67,421.80 | 26 Dec 2024 99,429.60 | 69 | $32,007.80 |
| short | 26 Dec 2024 99,429.60 | 28 Dec 2024 94,299.00 | 2 | $5,130.58 |
| long | 28 Dec 2024 94,299.00 | 29 Dec 2024 95,300.00 | 1 | $1,000.97 |
| short | 29 Dec 2024 95,300.00 | 19 Jan 2025 104,556.00 | 21 | −$9,256.23 |
| long | 19 Jan 2025 104,556.00 | 9 Feb 2025 96,444.80 | 21 | −$8,111.48 |
| short | 9 Feb 2025 96,444.80 | 2 Apr 2025 85,158.40 | 52 | $11,286.40 |
| long | 2 Apr 2025 85,158.40 | 9 Apr 2025 76,322.40 | 7 | −$8,835.93 |
| short | 9 Apr 2025 76,322.40 | 22 Apr 2025 87,516.20 | 13 | −$11,193.80 |
| long | 22 Apr 2025 87,516.20 | 11 Jun 2025 110,274.00 | 50 | $22,758.20 |
| short | 11 Jun 2025 110,274.00 | 12 Jun 2025 108,645.00 | 1 | $1,629.26 |
| long | 12 Jun 2025 108,645.00 | 14 Jun 2025 106,067.00 | 2 | −$2,578.54 |
| short | 14 Jun 2025 106,067.00 | 28 Jun 2025 107,048.00 | 14 | −$980.99 |
| long | 28 Jun 2025 107,048.00 | 3 Jul 2025 108,850.00 | 5 | $1,802.01 |
| short | 3 Jul 2025 108,850.00 | 6 Jul 2025 108,198.00 | 3 | $651.47 |
| long | 6 Jul 2025 108,198.00 | 5 Aug 2025 115,055.00 | 30 | $6,856.91 |
| short | 5 Aug 2025 115,055.00 | 15 Aug 2025 118,295.00 | 10 | −$3,240.06 |
| long | 15 Aug 2025 118,295.00 | 20 Aug 2025 112,873.00 | 5 | −$5,422.14 |
| short | 20 Aug 2025 112,873.00 | 26 Aug 2025 110,112.00 | 6 | $2,760.97 |
| long | 26 Aug 2025 110,112.00 | 28 Aug 2025 111,262.00 | 2 | $1,150.03 |
| short | 28 Aug 2025 111,262.00 | 16 Sep 2025 115,350.00 | 19 | −$4,087.70 |
| long | 16 Sep 2025 115,350.00 | 30 Sep 2025 114,312.00 | 14 | −$1,037.74 |
| short | 30 Sep 2025 114,312.00 | 2 Oct 2025 118,595.00 | 2 | −$4,283.02 |
| long | 2 Oct 2025 118,595.00 | 20 Oct 2025 108,643.00 | 18 | −$9,952.22 |
| short | 20 Oct 2025 108,643.00 | 13 Dec 2025 90,268.40 | 54 | $18,374.30 |
| long | 13 Dec 2025 90,268.40 | 20 Dec 2025 88,136.90 | 7 | −$2,131.48 |
| short | 20 Dec 2025 88,136.90 | 7 Jan 2026 93,748.00 | 18 | −$5,611.02 |
| long | 7 Jan 2026 93,748.00 | 27 Jan 2026 88,347.10 | 20 | −$5,400.89 |
| short | 27 Jan 2026 88,347.10 | 7 Mar 2026 68,114.00 | 39 | $20,233.10 |
| long | 7 Mar 2026 68,114.00 | 11 Mar 2026 69,948.60 | 4 | $1,834.62 |
| short | 11 Mar 2026 69,948.60 | 13 Mar 2026 70,541.30 | 2 | −$592.70 |
| long | 13 Mar 2026 70,541.30 | 30 Mar 2026 66,010.90 | 17 | −$4,530.41 |
| short | 30 Mar 2026 66,010.90 | 31 Mar 2026 66,797.40 | 1 | −$786.45 |
| long | 31 Mar 2026 66,797.40 | 1 Apr 2026 68,284.50 | 1 | $1,487.11 |
| short | 1 Apr 2026 68,284.50 | 13 Apr 2026 70,741.60 | 12 | −$2,457.07 |
| long | 13 Apr 2026 70,741.60 | 25 May 2026 77,065.00 | 42 | $6,323.40 |
| short | 25 May 2026 77,065.00 | 17 Jul 2026 63,830.20 | 53 | $13,234.80 |
| long | 17 Jul 2026 63,830.20 | 4 Aug 2026 63,520.00 | 18 | −$310.20 |
| short | 4 Aug 2026 63,520.00 | 5 Aug 2026 64,106.60 | 1 | −$586.55 |
| long | 5 Aug 2026 64,106.60 | 6 Aug 2026 64,665.20 | 1 | $558.69 |
| short | 6 Aug 2026 64,665.20 | 9 Aug 2026 64,962.60 | 3 | −$297.36 |
| long | 9 Aug 2026 64,962.60 | 13 Aug 2026 63,480.00 | 4 | −$1,482.61 |
| short | 13 Aug 2026 63,480.00 | 22 Aug 2026 78,338.00 | 9 | −$14,858.00 |
| long | 22 Aug 2026 78,338.00 | 17 Sep 2026 76,206.00 | 26 | −$2,132.03 |
| short | 17 Sep 2026 76,206.00 | 22 Sep 2026 86,620.00 | 5 | −$10,414.00 |
| long | 22 Sep 2026 86,620.00 | 15 Oct 2026 81,682.00 | 23 | −$4,937.99 |
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 |
|---|---|---|---|---|---|
| Backtest Signal To Noise this page | — | v4 strategy | +1.7% | 57 | 35.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% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 9 ms | Yes 1,000 bars in 41 ms | Not measured |
| PineTS | No v5 and v6 only | — | 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. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
//@version=4////////////////////////////////////////////////////////////// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © HPotter 05/01/2021// The signal-to-noise (S/N) ratio. // And Simple Moving Average.// Thank you for idea BlockchainYahoo//// WARNING:// - For purpose educate only// - This script to change bars colors. ////////////////////////////////////////////////////////////SignalToNoise(length) => StN = 0.0 for i = 1 to length-1 StN := StN + (1/close[i])/length StN := -10*log(StN)strategy(title="Backtest Signal To Noise ", shorttitle="StoN", overlay=false)length = input(title="Days", type=input.integer, defval=21, minval=2)Smooth = input(title="Smooth", type=input.integer, defval=7, minval=2)reverse = input(false, title="Trade reverse")StN = SignalToNoise(length)SMAStN = sma(StN, Smooth)pos = iff(SMAStN[1] > StN[1] , -1, iff(SMAStN[1] < StN[1], 1, 0)) possig = iff(reverse and pos == 1, -1, iff(reverse and pos == -1 , 1, pos)) if (possig == 1) strategy.entry("Long", strategy.long)if (possig == -1) strategy.entry("Short", strategy.short) if (possig == 0) strategy.close_all()barcolor(possig == -1 ? #b50404: possig == 1 ? #079605 : #0536b3 )plot(StN, title='StN' )plot(SMAStN, title='Smooth', color=#00ff00)