Pine Script v4 · Strategy
P-Signal Strategy (BTCUSD Multi Frame)
A Pine Script v4 strategy built on Stdev (ser, int), SMA (ser, int) and SMA or Change (nIntr_D). It has 6 order rules, 3 entries among them, and trades long only 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 Number of D Bars 9, Number of W Bars 4 and Number of M Bars 6.
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 P-Signal Strategy (BTCUSD Multi Frame) repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did P-Signal Strategy (BTCUSD Multi Frame) do on BTCUSDT 1D with default settings?
- +30.79%
- From 26 Jan 2024 to 21 Oct 2026, over 46 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can P-Signal Strategy (BTCUSD Multi Frame) 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 P-Signal Strategy (BTCUSD Multi Frame) use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does P-Signal Strategy (BTCUSD Multi Frame) run in Zeal Terminal?
- Yes
- It compiles in 28 ms and runs 1,000 bars in 54 ms, in the browser.
BTCUSDT · 1D · default settings
- HLine
- HLine
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +30.79%
- $30,789.30
- Buy and hold
- +102.5%
- same period
- Closed trades
- 46
- 22 won · 24 lost
- Profitable
- 47.8%
- of closed trades
- Profit factor
- 1.20
- gross profit ÷ gross loss
- Max drawdown
- 48.57%
- −$84,702.00
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A long 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.
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 abs → math.abs, exp → math.exp, stdev → ta.stdev, sma → ta.sma, sqrt → math.sqrt, change → ta.change, sign → math.sign, security → request.security.
Same 46 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 4: //@version=4 → //@version=5
- Line 8: input(type = input.integer) → input.int()
- Line 9: input(type = input.integer) → input.int()
- Line 10: input(type = input.integer) → input.int()
- Line 19: abs → math.abs
- Line 20: exp → math.exp
14 more edits
- Line 26: stdev → ta.stdev
- Line 27: sma → ta.sma
- Line 28: sqrt → math.sqrt
- Line 30: sma → ta.sma
- Line 30: change → ta.change
- Line 31: sign → math.sign
- Line 32: sma → ta.sma
- Line 32: security → request.security
- Line 32: change → ta.change
- Line 33: sign → math.sign
- Line 34: sma → ta.sma
- Line 34: security → request.security
- Line 34: change → ta.change
- Line 35: sign → math.sign
⋯// P-Signal Strategy © Kharevsky-// @version=4+// @version=5// **********************************************************************************************************⋯// Parameters and const of P-Signal.-nPoints_D = input(title = "Number of D Bars", type = input.integer, defval = 9, minval = 4, maxval = 100, group = "Parameters of observation.")+nPoints_D = input.int(title = "Number of D Bars", defval = 9, minval = 4, maxval = 100, group = "Parameters of observation.")-nPoints_W = input(title = "Number of W Bars", type = input.integer, defval = 4, minval = 4, maxval = 100, group = "Parameters of observation.")+nPoints_W = input.int(title = "Number of W Bars", defval = 4, minval = 4, maxval = 100, group = "Parameters of observation.")-nPoints_M = input(title = "Number of M Bars", type = input.integer, defval = 6, minval = 4, maxval = 100, group = "Parameters of observation.")+nPoints_M = input.int(title = "Number of M Bars", defval = 6, minval = 4, maxval = 100, group = "Parameters of observation.")int nIntr_D = nPoints_D - 1⋯fErf(x) =>- nT = 1.0/(1.0 + 0.5*abs(x))+ nT = 1.0/(1.0 + 0.5*math.abs(x))- nAns = 1.0 - nT*exp(-x*x - 1.26551223 + + nAns = 1.0 - nT*math.exp(-x*x - 1.26551223 + nT*( 1.00002368 + nT*( 0.37409196 + nT*( 0.09678418 + ⋯fPSignal(ser, int) => - nStDev = stdev(ser, int)+ nStDev = ta.stdev(ser, int)- nSma = sma(ser, int)+ nSma = ta.sma(ser, int)- fErf(nStDev > 0 ? nSma/nStDev/sqrt(2) : 1.0)+ fErf(nStDev > 0 ? nSma/nStDev/math.sqrt(2) : 1.0)// Strategy.-float nPSignal_D = sma(fPSignal(change(ohlc4), nIntr_D), nIntr_D)+float nPSignal_D = ta.sma(fPSignal(ta.change(ohlc4), nIntr_D), nIntr_D)-float ndPSignal_D = sign(nPSignal_D[0] - nPSignal_D[1])+float ndPSignal_D = math.sign(nPSignal_D[0] - nPSignal_D[1])-float nPSignal_W = sma(security(syminfo.tickerid, "W",fPSignal(change(ohlc4), nIntr_W)), nIntr_W)+float nPSignal_W = ta.sma(request.security(syminfo.tickerid, "W",fPSignal(ta.change(ohlc4), nIntr_W)), nIntr_W)-float ndPSignal_W = sign(nPSignal_W[0] - nPSignal_W[1])+float ndPSignal_W = math.sign(nPSignal_W[0] - nPSignal_W[1])-float nPSignal_M = sma(security(syminfo.tickerid, "M",fPSignal(change(ohlc4), nIntr_M)), nIntr_M)+float nPSignal_M = ta.sma(request.security(syminfo.tickerid, "M",fPSignal(ta.change(ohlc4), nIntr_M)), nIntr_M)-float ndPSignal_M = sign(nPSignal_M[0] - nPSignal_M[1])+float ndPSignal_M = math.sign(nPSignal_M[0] - nPSignal_M[1])// ... Day⋯Clean v5
VerifiedNothing to change.
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: 46 before, 29 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.
⋯// P-Signal Strategy © Kharevsky-// @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 |
|---|---|---|---|
| Parameters of observation. | |||
| Number of D Bars | 9 | int (written input.integer) · min 4 · max 100 | nIntr_D |
| Number of W Bars | 4 | int (written input.integer) · min 4 · max 100 | nIntr_W |
| Number of M Bars | 6 | int (written input.integer) · min 4 · max 100 | nIntr_M |
What it calculates
- Stdev (
stdev,ta.stdevin v5)ser, intnStDev · line 26 - SMA (
sma,ta.smain v5)ser, intnSma · line 27 - SMA / Change (
sma,ta.smain v5)fPSignal(change(ohlc4), nIntr_D), nIntr_DnPSignal_D · line 30 - SMA / Change (
sma,ta.smain v5)security(syminfo.tickerid, "W",fPSignal(change(ohlc4), nIntr_W)), nIntr_WnPSignal_W · line 32 - SMA / Change (
sma,ta.smain v5)security(syminfo.tickerid, "M",fPSignal(change(ohlc4), nIntr_M)), nIntr_MnPSignal_M · line 34
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_D long | (nPSignal_D < 0 and ndPSignal_D > 0 and bDValveOpen) | On | 38 |
| Close long_D | (nPSignal_D > 0 and ndPSignal_D < 0) | On | 41 |
| Open long_W long | (nPSignal_W < 0 and ndPSignal_W > 0 and bWValveOpen) | On | 45 |
| Close long_W | (nPSignal_W > 0 and ndPSignal_W < 0) | On | 48 |
| Open long_M long | (nPSignal_M < 0 and ndPSignal_M > 0 and bMValveOpen) | On | 52 |
| Close long_M | (nPSignal_M > 0 and ndPSignal_M < 0) | On | 55 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
Yes
At least one pattern in the source can repaint; it is marked Check below.
- ClearHigher-timeframe data2 security call(s), none with lookahead on.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- CheckIntrabar recalculationuses varip. Live values can differ from history.
- 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 | +30.8% | +102.5% | 46 | 48% | 1.20 | 48.6% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +15.3% | +5.5% | 33 | 52% | 1.50 | 24.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.4% | +9.8% | 43 | 49% | 0.87 | 4.9% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.7% | +8.4% | 39 | 51% | 1.39 | 1.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.1% | +16.7% | 46 | 48% | 1.22 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 35 | 40% | 1.47 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.6% | +148.3% | 52 | 46% | 1.49 | 0.7% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.3% | +17.1% | 31 | 68% | 2.23 | 0.2% | 8 May 2026 – 21 Oct 2026 |
Higher-timeframe data for request.security(): BINANCE:BTCUSDT W (Binance BTCUSDT 1w), BINANCE:BTCUSDT M (Binance BTCUSDT 1M), fetched from Binance's public market data for each market.
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 | 13 | 54% | $7,667.48 |
| Sideways market | 25 | 44% | $9,839.46 |
| Falling market | 8 | 50% | $13,282.40 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
46.8%
The 95th percentile of the worst drawdown when the same 46 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 22.8% to 46.8%; the tick is what actually happened, 40.6%. Order luck alone moves it this much.
All 46 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 5 Feb 2024 42,582.90 | 4 Mar 2024 63,114.00 | 28 | $20,531.10 |
| long | 26 Mar 2024 69,880.00 | 3 Apr 2024 65,464.00 | 8 | −$4,416.01 |
| long | 11 Apr 2024 70,631.10 | 15 Apr 2024 65,661.90 | 4 | −$4,969.23 |
| long | 22 Apr 2024 64,941.20 | 30 Apr 2024 63,866.00 | 8 | −$1,075.15 |
| long | 29 Apr 2024 63,118.60 | 13 May 2024 61,484.00 | 14 | −$1,634.62 |
| long | 7 May 2024 63,165.20 | 25 May 2024 68,550.00 | 18 | $5,384.81 |
| long | 16 May 2024 66,206.50 | 3 Jun 2024 67,765.60 | 18 | $1,559.11 |
| long | 6 Jun 2024 71,108.00 | 12 Jun 2024 67,314.20 | 6 | −$3,793.77 |
| long | 23 Jun 2024 64,262.00 | 24 Jul 2024 65,936.00 | 31 | $1,673.99 |
| long | 15 Jul 2024 60,797.90 | 5 Aug 2024 58,161.00 | 21 | −$2,636.91 |
| long | 11 Aug 2024 60,923.50 | 18 Aug 2024 59,492.00 | 7 | −$1,431.52 |
| long | 26 Aug 2024 64,220.00 | 9 Sep 2024 54,869.90 | 14 | −$9,350.04 |
| long | 10 Sep 2024 57,042.00 | 23 Sep 2024 63,578.80 | 13 | $6,536.75 |
| long | 23 Sep 2024 63,578.80 | 7 Oct 2024 62,819.90 | 14 | −$758.85 |
| long | 1 Oct 2024 63,327.60 | 24 Oct 2024 66,668.60 | 23 | $3,341.05 |
| long | 10 Oct 2024 60,636.00 | 12 Jan 2025 94,600.00 | 94 | $33,964.00 |
| long | 29 Dec 2024 95,300.00 | 26 Jan 2025 104,747.00 | 28 | $9,446.86 |
| long | 13 Jan 2025 94,545.10 | 27 Jan 2025 102,620.00 | 14 | $8,074.94 |
| long | 17 Jan 2025 99,987.30 | 1 Mar 2025 84,349.90 | 43 | −$15,637.40 |
| long | 11 Feb 2025 97,430.80 | 28 Mar 2025 87,232.00 | 45 | −$10,198.80 |
| long | 24 Feb 2025 96,258.00 | 24 Apr 2025 93,691.10 | 59 | −$2,566.93 |
| long | 11 Apr 2025 79,607.30 | 26 May 2025 109,004.00 | 45 | $29,396.90 |
| long | 8 Jun 2025 105,552.00 | 17 Jun 2025 106,795.00 | 9 | $1,242.38 |
| long | 26 Jun 2025 107,341.00 | 5 Jul 2025 107,984.00 | 9 | $643.66 |
| long | 30 Jun 2025 108,357.00 | 28 Jul 2025 119,416.00 | 28 | $11,058.60 |
| long | 28 Jul 2025 119,416.00 | 18 Aug 2025 117,405.00 | 21 | −$2,010.55 |
| long | 18 Aug 2025 117,405.00 | 25 Aug 2025 113,494.00 | 7 | −$3,911.42 |
| long | 28 Aug 2025 111,262.00 | 20 Sep 2025 115,632.00 | 23 | $4,370.38 |
| long | 15 Sep 2025 115,268.00 | 29 Sep 2025 112,164.00 | 14 | −$3,104.05 |
| long | 1 Oct 2025 114,049.00 | 11 Oct 2025 112,774.00 | 10 | −$1,274.45 |
| long | 20 Oct 2025 108,643.00 | 2 Nov 2025 110,098.00 | 13 | $1,455.33 |
| long | 3 Nov 2025 110,541.00 | 7 Dec 2025 89,236.80 | 34 | −$21,303.90 |
| long | 12 Nov 2025 103,059.00 | 22 Dec 2025 88,658.90 | 40 | −$14,400.10 |
| long | 23 Dec 2025 88,620.80 | 31 Dec 2025 88,485.50 | 8 | −$135.29 |
| long | 5 Jan 2026 91,529.70 | 26 Jan 2026 86,670.40 | 21 | −$4,859.38 |
| long | 30 Jan 2026 84,650.20 | 12 Mar 2026 70,191.90 | 41 | −$14,458.30 |
| long | 16 Feb 2026 68,832.60 | 30 Mar 2026 66,010.90 | 42 | −$2,821.66 |
| long | 31 Mar 2026 66,797.40 | 16 Apr 2026 74,810.00 | 16 | $8,012.61 |
| long | 13 Apr 2026 70,741.60 | 4 May 2026 78,568.60 | 21 | $7,827.02 |
| long | 1 May 2026 76,346.60 | 22 Jun 2026 63,312.00 | 52 | −$13,034.60 |
| long | 26 May 2026 77,322.00 | 13 Jul 2026 63,780.00 | 48 | −$13,542.00 |
| long | 8 Jun 2026 63,332.00 | 3 Aug 2026 63,570.00 | 56 | $238.00 |
| long | 3 Jul 2026 61,560.00 | 14 Aug 2026 63,490.90 | 42 | $1,930.86 |
| long | 6 Aug 2026 64,665.20 | 29 Aug 2026 77,845.90 | 23 | $13,180.60 |
| long | 20 Aug 2026 69,334.80 | 14 Sep 2026 76,842.00 | 25 | $7,507.23 |
| long | 24 Aug 2026 77,734.00 | 28 Sep 2026 84,472.00 | 35 | $6,738.01 |
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 |
|---|---|---|---|---|---|
| P-Signal Strategy (BTCUSD Multi Frame) this page | — | v4 strategy | +30.8% | 46 | 48.6% |
| Double Bollinger Binary Options by Trade_by_DB | sma stdev | v5 strategy | No trades on this market | ||
| Improved Bollinger Swing Strategy Stock Nasdaq by exlux99 | sma stdev | v4 strategy | −20.4% | 7 | 22.3% |
| I11L Long Put-Call Ratio Inversion by I11L | sma stdev | v5 strategy | No trades on this market | ||
| P-Signal Strategy by Kharevsky | stdev sma | v4 strategy | −9.7% | 29 | 34.6% |
| P-Signal Strategy & Alert by Kharevsky | stdev sma | v4 strategy | −9.7% | 29 | 34.6% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 28 ms | With a workaround 1,000 bars in 54 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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.
// **********************************************************************************************************// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// P-Signal Strategy © Kharevsky// @version=4// **********************************************************************************************************strategy("P-Signal Strategy:", precision = 3, pyramiding = 3)// Parameters and const of P-Signal.nPoints_D = input(title = "Number of D Bars", type = input.integer, defval = 9, minval = 4, maxval = 100, group = "Parameters of observation.")nPoints_W = input(title = "Number of W Bars", type = input.integer, defval = 4, minval = 4, maxval = 100, group = "Parameters of observation.")nPoints_M = input(title = "Number of M Bars", type = input.integer, defval = 6, minval = 4, maxval = 100, group = "Parameters of observation.")int nIntr_D = nPoints_D - 1int nIntr_W = nPoints_W - 1int nIntr_M = nPoints_M - 1varip bool bDValveOpen = truevarip bool bWValveOpen = truevarip bool bMValveOpen = true// Horner's method for Gauss & P-Signal functions.fErf(x) => nT = 1.0/(1.0 + 0.5*abs(x)) nAns = 1.0 - nT*exp(-x*x - 1.26551223 + nT*( 1.00002368 + nT*( 0.37409196 + nT*( 0.09678418 + nT*(-0.18628806 + nT*( 0.27886807 + nT*(-1.13520398 + nT*( 1.48851587 + nT*(-0.82215223 + nT*( 0.17087277 )))))))))) x >= 0 ? nAns : -nAnsfPSignal(ser, int) => nStDev = stdev(ser, int) nSma = sma(ser, int) fErf(nStDev > 0 ? nSma/nStDev/sqrt(2) : 1.0)// Strategy.float nPSignal_D = sma(fPSignal(change(ohlc4), nIntr_D), nIntr_D)float ndPSignal_D = sign(nPSignal_D[0] - nPSignal_D[1])float nPSignal_W = sma(security(syminfo.tickerid, "W",fPSignal(change(ohlc4), nIntr_W)), nIntr_W)float ndPSignal_W = sign(nPSignal_W[0] - nPSignal_W[1])float nPSignal_M = sma(security(syminfo.tickerid, "M",fPSignal(change(ohlc4), nIntr_M)), nIntr_M)float ndPSignal_M = sign(nPSignal_M[0] - nPSignal_M[1])// ... Dayif(nPSignal_D < 0 and ndPSignal_D > 0 and bDValveOpen) strategy.entry("long_D", strategy.long, 1) bDValveOpen := falseif(nPSignal_D > 0 and ndPSignal_D < 0) strategy.close("long_D") bDValveOpen := true// ... Weekif(nPSignal_W < 0 and ndPSignal_W > 0 and bWValveOpen) strategy.entry("long_W", strategy.long, 1) bWValveOpen := falseif(nPSignal_W > 0 and ndPSignal_W < 0) strategy.close("long_W") bWValveOpen := true// ... Monthif(nPSignal_M < 0 and ndPSignal_M > 0 and bMValveOpen) strategy.entry("long_M", strategy.long, 1) bMValveOpen := falseif(nPSignal_M > 0 and ndPSignal_M < 0) strategy.close("long_M") bMValveOpen := true// Plotting. hline(+1.0, color = color.new(color.orange,70), linestyle = hline.style_dotted)hline(-1.0, color = color.new(color.orange,70), linestyle = hline.style_dotted)plot(nPSignal_D, color = color.blue, style = plot.style_line)plot(strategy.position_size, color = color.white, style = plot.style_cross)// The end.