Pine Script v4 · Strategy
Simple Buy-Sell Strategy
A Pine Script v4 strategy built on SAR (0.02, 0.02), SMA (close, 200) and MACD (close, 12). It has 4 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. 8 settings; defaults include MA Length 200, Fast EMA Length 12 and Slow EMA Length 26.
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 Simple Buy-Sell Strategy repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Simple Buy-Sell Strategy do on BTCUSDT 1D with default settings?
- +43.68%
- From 26 Jan 2024 to 21 Oct 2026, over 6 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Simple Buy-Sell Strategy 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 Simple Buy-Sell Strategy use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Simple Buy-Sell Strategy run in Zeal Terminal?
- Yes
- It compiles in 9 ms and runs 1,000 bars in 30 ms, in the browser.
BTCUSDT · 1D · default settings
- Buy Signals
- Sell Signals
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +43.68%
- $43,677.10
- Buy and hold
- +102.5%
- same period
- Closed trades
- 6
- 3 won · 3 lost
- Profitable
- 50.0%
- of closed trades
- Profit factor
- 2.91
- gross profit ÷ gross loss
- Max drawdown
- 15.49%
- −$15,488.40
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.
-
Note
Uses an argument v6 removed
when = on line 28, 29, 30, 31. The upgraded version below moves each call inside an if block.
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 sar → ta.sar, sma → ta.sma, macd → ta.macd, rsi → ta.rsi.
- 4 strategy calls use the when = argument (line 28, 29, 30, 31). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 6: sar → ta.sar
- Line 10: sma → ta.sma
- Line 15: macd → ta.macd
- Line 20: rsi → ta.rsi
⋯// © BlockchainSpecialists-//@version=4+//@version=5//Original Indicator by @Shizaru - simply made into a strategy!strategy("Simple Buy/Sell Strategy", overlay=false)-psar = sar(0.02,0.02,0.2)+psar = ta.sar(0.02,0.02,0.2)c1a = close > psar⋯malen = input(200, title="MA Length")-mm200 = sma(close, malen)+mm200 = ta.sma(close, malen)c2a = close > mm200⋯slow = input(26, title="Slow EMA Length")-[macd,signal,hist] = macd(close, fast,slow, 9)+[macd,signal,hist] = ta.macd(close, fast,slow, 9)c3a = macd >= 0⋯th = input(50, title="RSI Threshold")-rsi14 = rsi(close, rsilen)+rsi14 = ta.rsi(close, rsilen)c4a = rsi14 >= th⋯Same 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 9: input() → input.int()
- Line 13: input() → input.int()
- Line 14: input() → input.int()
- Line 18: input() → input.int()
- Line 19: input() → input.int()
- Line 25: input() → input.bool()
6 more edits
- Line 26: input() → input.bool()
- Line 27: input() → input.bool()
- Line 28: when = buy==1 and longtrades==true → if block
- Line 29: when = quickexit==true ? buy==0 : sell==-1 → if block
- Line 30: when = sell==-1 and shorttrades==true → if block
- Line 31: when = quickexit==true ? sell==0 : buy==1 → if block
⋯c1v = close < psar-malen = input(200, title="MA Length")+malen = input.int(200, title="MA Length")mm200 = ta.sma(close, malen)⋯c2v = close < mm200-fast = input(12, title="Fast EMA Length")+fast = input.int(12, title="Fast EMA Length")-slow = input(26, title="Slow EMA Length")+slow = input.int(26, title="Slow EMA Length")[macd,signal,hist] = ta.macd(close, fast,slow, 9)⋯c3v = macd <= 0-rsilen = input(7, title="RSI Length")+rsilen = input.int(7, title="RSI Length")-th = input(50, title="RSI Threshold")+th = input.int(50, title="RSI Threshold")rsi14 = ta.rsi(close, rsilen)⋯sell = c1v and c2v and c3v and c4v ? -1 : 0-longtrades = input(true, title="Long Trades")+longtrades = input.bool(true, title="Long Trades")-shorttrades = input(true, title="Short Trades")+shorttrades = input.bool(true, title="Short Trades")-quickexit = input(false, title="Quick Exits")+quickexit = input.bool(false, title="Quick Exits")-strategy.entry("Buy", strategy.long, when=buy==1 and longtrades==true)+if buy==1 and longtrades==true+ strategy.entry("Buy", strategy.long)-strategy.close("Buy", when=quickexit==true ? buy==0 : sell==-1)+if quickexit==true ? buy==0 : sell==-1+ strategy.close("Buy")-strategy.entry("Sell", strategy.short, when=sell==-1 and shorttrades==true)+if sell==-1 and shorttrades==true+ strategy.entry("Sell", strategy.short)-strategy.close("Sell", when=quickexit==true ? sell==0 : buy==1)+if quickexit==true ? sell==0 : buy==1+ strategy.close("Sell")plot(buy, style=plot.style_histogram, color=color.green, linewidth=3, title="Buy Signals")⋯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: 6 before, 9 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.
⋯// © BlockchainSpecialists-//@version=5+//@version=6//Original Indicator by @Shizaru - simply made into a strategy!⋯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 |
|---|---|---|---|
| MA Length | 200 | int | mm200 |
| Fast EMA Length | 12 | int | macd signal hist |
| Slow EMA Length | 26 | int | macd signal hist |
| RSI Length | 7 | int | rsi14 |
| RSI Threshold | 50 | int | c4a c4v |
| Long Trades | true | bool | entry Buy |
| Short Trades | true | bool | entry Sell |
| Quick Exits | false | bool | close Buy close Sell |
What it calculates
- SAR (
sar,ta.sarin v5)0.02, 0.02, 0.2psar · line 6 - SMA (
sma,ta.smain v5)close, MA Length (200)mm200 · line 10 - MACD (
macd,ta.macdin v5)close, Fast EMA Length (12), Slow EMA Length (26), 9macd · line 15 - MACD (
macd,ta.macdin v5)close, Fast EMA Length (12), Slow EMA Length (26), 9signal · line 15 - MACD (
macd,ta.macdin v5)close, Fast EMA Length (12), Slow EMA Length (26), 9hist · line 15 - RSI (
rsi,ta.rsiin v5)close, RSI Length (7)rsi14 · line 20
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==1 and longtrades==true | On | 28 |
| Close Buy | quickexit==true ? buy==0 : sell==-1 | On | 29 |
| Open Sell short | sell==-1 and shorttrades==true | On | 30 |
| Close Sell | quickexit==true ? sell==0 : buy==1 | 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.
- 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 | +43.7% | +102.5% | 6 | 50% | 2.91 | 15.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −4.0% | +5.5% | 15 | 13% | 0.81 | 11.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.4% | +9.8% | 7 | 43% | 1.26 | 1.5% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.7% | +8.4% | 4 | 50% | 7.29 | 0.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 9 | 33% | 0.97 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 10 | 20% | 1.76 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.2% | +148.3% | 16 | 13% | 0.71 | 0.5% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.1% | +17.1% | 12 | 17% | 1.45 | 0.1% | 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 | 0 | — | $0.00 |
| Sideways market | 5 | 60% | $56,385.60 |
| Falling market | 1 | 0% | −$12,708.50 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
20.9%
The 95th percentile of the worst drawdown when the same 6 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 12.0% to 20.9%; the tick is what actually happened, 12.0%. Order luck alone moves it this much.
All 6 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 25 Aug 2024 64,157.00 | 31 Aug 2024 59,124.00 | 6 | −$5,033.03 |
| short | 31 Aug 2024 59,124.00 | 25 Sep 2024 64,262.70 | 25 | −$5,138.71 |
| long | 25 Sep 2024 64,262.70 | 10 Mar 2025 80,734.50 | 166 | $16,471.80 |
| short | 10 Mar 2025 80,734.50 | 23 Apr 2025 93,443.00 | 44 | −$12,708.50 |
| long | 23 Apr 2025 93,443.00 | 18 Oct 2025 106,432.00 | 178 | $12,988.70 |
| short | 18 Oct 2025 106,432.00 | 20 Aug 2026 69,334.80 | 306 | $37,096.90 |
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 |
|---|---|---|---|---|---|
| Simple Buy-Sell Strategy this page | — | v4 strategy | +43.7% | 6 | 15.5% |
| [VJ]Phoenix Force of PSAR +MACD +RSI by vikris | sar sma macd rsi | v4 strategy | No trades on this market | ||
| PowerX by jwitt98 by jwitt98 | rsi sma macd | v5 strategy | +107.9% | 78 | 107.8% |
| PSAR + EMA-TEMA-RSI-OBV by UnknownUnicorn19826629 | sar rsi sma | v4 strategy | No trades on this market | ||
| SRJ RSI Outperformer Strategy by Sapt_Jash | rsi sma | v5 strategy | +0.6% | 8 | 2.4% |
| RSI SMA Crossover Strategy by reees | rsi sma | v5 strategy | +39.8% | 42 | 30.8% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 9 ms | Yes 1,000 bars in 30 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.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © BlockchainSpecialists//@version=4//Original Indicator by @Shizaru - simply made into a strategy!strategy("Simple Buy/Sell Strategy", overlay=false)psar = sar(0.02,0.02,0.2)c1a = close > psarc1v = close < psarmalen = input(200, title="MA Length")mm200 = sma(close, malen)c2a = close > mm200c2v = close < mm200fast = input(12, title="Fast EMA Length")slow = input(26, title="Slow EMA Length")[macd,signal,hist] = macd(close, fast,slow, 9)c3a = macd >= 0c3v = macd <= 0rsilen = input(7, title="RSI Length")th = input(50, title="RSI Threshold")rsi14 = rsi(close, rsilen)c4a = rsi14 >= thc4v = rsi14 <= thbuy = c1a and c2a and c3a and c4a ? 1 : 0sell = c1v and c2v and c3v and c4v ? -1 : 0longtrades = input(true, title="Long Trades")shorttrades = input(true, title="Short Trades")quickexit = input(false, title="Quick Exits")strategy.entry("Buy", strategy.long, when=buy==1 and longtrades==true)strategy.close("Buy", when=quickexit==true ? buy==0 : sell==-1)strategy.entry("Sell", strategy.short, when=sell==-1 and shorttrades==true)strategy.close("Sell", when=quickexit==true ? sell==0 : buy==1)plot(buy, style=plot.style_histogram, color=color.green, linewidth=3, title="Buy Signals")plot(sell, style=plot.style_histogram, color=color.red, linewidth=3, title="Sell Signals")