Pine Script v4 · Strategy
charl macd ema rsi
A Pine Script v4 strategy built on EMA (close, fast) and SMA (macd, 9). It has 6 order rules, 4 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. 2 settings; defaults include EMA 200 and RSI 14.
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 charl macd ema rsi 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 charl macd ema rsi do on BTCUSDT 1D with default settings?
- −49.47%
- From 26 Jan 2024 to 21 Oct 2026, over 59 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can charl macd ema rsi 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 charl macd ema rsi use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does charl macd ema rsi run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 21 ms, in the browser.
BTCUSDT · 1D · default settings
- EMA
- EMA
- SMA
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −49.47%
- −$49,471.30
- Buy and hold
- +102.5%
- same period
- Closed trades
- 59
- 20 won · 39 lost
- Profitable
- 33.9%
- of closed trades
- Profit factor
- 0.66
- gross profit ÷ gross loss
- Max drawdown
- 76.88%
- −$80,307.20
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
2 settings have no label
In the settings dialog ema (line 11), rsi (line 12) show an empty name. The labels on this page come from the variable names.
-
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 21, 24, 29, 31, 41, 45. 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 ema → ta.ema, sma → ta.sma, rsi → ta.rsi.
- 6 strategy calls use the when = argument (line 21, 24, 29, 31, 41, 45). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 59 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 4: //@version=4 → //@version=5
- Line 7: ema → ta.ema
- Line 8: ema → ta.ema
- Line 10: sma → ta.sma
- Line 11: ema → ta.ema
- Line 12: rsi → ta.rsi
4 more edits
- Line 21: strategy.entry(…, true) → strategy.entry(…, strategy.long)
- Line 29: strategy.entry(…, false) → strategy.entry(…, strategy.short)
- Line 41: strategy.entry(…, false) → strategy.entry(…, strategy.short)
- Line 45: strategy.entry(…, true) → strategy.entry(…, strategy.long)
⋯//Good with trending markets-//@version=4+//@version=5strategy("CHARL MACD EMA RSI")fast = 12, slow = 26-fastMA = ema(close, fast)+fastMA = ta.ema(close, fast)-slowMA = ema(close, slow)+slowMA = ta.ema(close, slow)macd = fastMA - slowMA-signal = sma(macd, 9)+signal = ta.sma(macd, 9)-ema = ema(close, input(200))+ema = ta.ema(close, input(200))-rsi = rsi(close, input(14))+rsi = ta.rsi(close, input(14))//when delta > 0 and close above ema buy⋯if (buy_entry and bought == false and rsi <= 70) - strategy.entry("Buy",true , when=buy_entry)+ strategy.entry("Buy",strategy.long , when=buy_entry) bought := true⋯if (sell_entry and sold == false and rsi >= 30)- strategy.entry("Sell",false , when=sell_entry)+ strategy.entry("Sell",strategy.short , when=sell_entry) sold := true⋯ if (rsi < 70 and sold == false and delta < 0)- strategy.entry("Sell",false , when= delta < 0)+ strategy.entry("Sell",strategy.short , when= delta < 0) sold := true⋯ else if (rsi > 30 and bought == false and delta > 0)- strategy.entry("Buy",true , when= delta > 0)+ strategy.entry("Buy",strategy.long , when= delta > 0) bought := true⋯Same 59 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 11: input() → input.int()
- Line 12: input() → input.int()
- Line 21: when = buy_entry → if block
- Line 24: when = delta<0 or rsi > 70 → if block
- Line 29: when = sell_entry → if block
- Line 31: when = delta>0 or rsi < 30 → if block
- Line 41: when = delta < 0 → if block
- Line 45: when = delta > 0 → if block
⋯signal = ta.sma(macd, 9)-ema = ta.ema(close, input(200))+ema = ta.ema(close, input.int(200))-rsi = ta.rsi(close, input(14))+rsi = ta.rsi(close, input.int(14))//when delta > 0 and close above ema buy⋯if (buy_entry and bought == false and rsi <= 70) - strategy.entry("Buy",strategy.long , when=buy_entry)+ if buy_entry+ strategy.entry("Buy",strategy.long ) bought := true -strategy.close("Buy",when= delta<0 or rsi > 70)+if delta<0 or rsi > 70+ strategy.close("Buy")if (delta<0 and bought==true)⋯if (sell_entry and sold == false and rsi >= 30)- strategy.entry("Sell",strategy.short , when=sell_entry)+ if sell_entry+ strategy.entry("Sell",strategy.short ) sold := true-strategy.close("Sell",when= delta>0 or rsi < 30)+if delta>0 or rsi < 30+ strategy.close("Sell")if (delta>0 and sold==true)⋯ if (rsi < 70 and sold == false and delta < 0)- strategy.entry("Sell",strategy.short , when= delta < 0)+ if delta < 0+ strategy.entry("Sell",strategy.short ) sold := true⋯ else if (rsi > 30 and bought == false and delta > 0)- strategy.entry("Buy",strategy.long , when= delta > 0)+ if delta > 0+ strategy.entry("Buy",strategy.long ) bought := true⋯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: 59 before, 48 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.
⋯//Good with trending markets-//@version=5+//@version=6strategy("CHARL MACD EMA RSI")⋯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 |
|---|---|---|---|
| EMA (no label in the script) | 200 | int | buy_entry sell_entry |
| RSI (no label in the script) | 14 | int | placing entry Buy entry Sell entry Sell entry Buy line 20 close Buy line 28 close Sell line 35 line 40 line 44 |
What it calculates
- EMA (
ema,ta.emain v5)close, fastfastMA · line 7 - EMA (
ema,ta.emain v5)close, slowslowMA · line 8 - SMA (
sma,ta.smain v5)macd, 9signal · line 10
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_entryclose>ema and delta > 0 | On | 21 |
| Close Buy | delta<0 or rsi > 70 | On | 24 |
| Open Sell short | sell_entryclose<ema and delta<0 | On | 29 |
| Close Sell | delta>0 or rsi < 30 | On | 31 |
| Open Sell short | delta < 0 | On | 41 |
| Open Buy long | delta > 0 | On | 45 |
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 | −49.5% | +102.5% | 59 | 34% | 0.66 | 76.9% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −19.5% | +5.5% | 67 | 34% | 0.59 | 20.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.6% | +9.8% | 54 | 46% | 0.76 | 3.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.5% | +8.4% | 64 | 33% | 0.68 | 0.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 49 | 53% | 1.05 | 0.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.0% | +21.5% | 58 | 43% | 0.72 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.6% | +148.3% | 61 | 46% | 0.63 | 0.6% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.1% | +17.1% | 53 | 45% | 1.42 | 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 | 18 | 44% | −$6,930.93 |
| Sideways market | 30 | 33% | −$33,559.20 |
| Falling market | 11 | 18% | −$8,981.19 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
81.6%
The 95th percentile of the worst drawdown when the same 59 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 56.6% to 81.6%; the tick is what actually happened, 74.2%. Order luck alone moves it this much.
All 59 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 29 Feb 2024 62,432.10 | 1 Mar 2024 61,131.00 | 1 | −$1,301.12 |
| short | 16 Mar 2024 69,499.80 | 30 Mar 2024 69,850.50 | 14 | −$350.69 |
| short | 25 Jun 2024 60,293.30 | 2 Jul 2024 62,900.00 | 7 | −$2,606.70 |
| short | 5 Jul 2024 57,050.00 | 6 Jul 2024 56,628.80 | 1 | $421.23 |
| long | 13 Jul 2024 57,889.10 | 31 Jul 2024 66,188.00 | 18 | $8,298.91 |
| short | 6 Aug 2024 54,018.80 | 14 Aug 2024 60,587.20 | 8 | −$6,568.34 |
| long | 24 Aug 2024 64,037.20 | 30 Aug 2024 59,359.00 | 6 | −$4,678.24 |
| short | 30 Aug 2024 59,359.00 | 12 Sep 2024 57,338.00 | 13 | $2,021.00 |
| long | 19 Sep 2024 61,760.00 | 2 Oct 2024 60,804.90 | 13 | −$955.06 |
| short | 2 Oct 2024 60,804.90 | 15 Oct 2024 66,084.00 | 13 | −$5,279.08 |
| long | 15 Oct 2024 66,084.00 | 21 Oct 2024 69,032.00 | 6 | $2,948.00 |
| short | 26 Oct 2024 66,698.30 | 30 Oct 2024 72,736.40 | 4 | −$6,038.09 |
| long | 30 Oct 2024 72,736.40 | 31 Oct 2024 72,344.80 | 1 | −$391.66 |
| short | 4 Nov 2024 68,776.00 | 7 Nov 2024 75,572.00 | 3 | −$6,796.00 |
| long | 7 Nov 2024 75,572.00 | 8 Nov 2024 75,857.90 | 1 | $285.90 |
| short | 26 Nov 2024 93,010.00 | 17 Dec 2024 106,059.00 | 21 | −$13,048.60 |
| long | 17 Dec 2024 106,059.00 | 18 Dec 2024 106,134.00 | 1 | $75.09 |
| short | 19 Dec 2024 100,204.00 | 5 Jan 2025 98,220.50 | 17 | $1,983.50 |
| long | 5 Jan 2025 98,220.50 | 10 Jan 2025 92,552.50 | 5 | −$5,668.02 |
| long | 16 Jan 2025 100,497.00 | 28 Jan 2025 102,083.00 | 12 | $1,585.48 |
| long | 16 Feb 2025 97,569.70 | 17 Feb 2025 96,118.10 | 1 | −$1,451.55 |
| long | 21 Feb 2025 98,305.00 | 25 Feb 2025 91,552.90 | 4 | −$6,752.13 |
| short | 26 Feb 2025 88,680.40 | 27 Feb 2025 84,250.10 | 1 | $4,430.30 |
| long | 6 Mar 2025 90,606.00 | 10 Mar 2025 80,734.50 | 4 | −$9,871.52 |
| short | 10 Mar 2025 80,734.50 | 16 Mar 2025 84,338.40 | 6 | −$3,603.96 |
| long | 16 Mar 2025 84,338.40 | 1 Apr 2025 82,550.00 | 16 | −$1,788.44 |
| short | 1 Apr 2025 82,550.00 | 6 Apr 2025 83,538.00 | 5 | −$987.99 |
| short | 7 Apr 2025 78,430.00 | 13 Apr 2025 85,276.90 | 6 | −$6,846.91 |
| long | 22 Apr 2025 87,516.20 | 3 May 2025 96,887.10 | 11 | $9,370.91 |
| short | 5 May 2025 94,277.60 | 9 May 2025 103,262.00 | 4 | −$8,984.00 |
| long | 9 May 2025 103,262.00 | 10 May 2025 102,972.00 | 1 | −$289.62 |
| short | 17 May 2025 103,464.00 | 22 May 2025 109,644.00 | 5 | −$6,180.09 |
| long | 22 May 2025 109,644.00 | 23 May 2025 111,696.00 | 1 | $2,052.23 |
| short | 26 May 2025 109,004.00 | 11 Jun 2025 110,274.00 | 16 | −$1,270.19 |
| long | 11 Jun 2025 110,274.00 | 15 Jun 2025 105,415.00 | 4 | −$4,859.76 |
| long | 26 Jun 2025 107,341.00 | 11 Jul 2025 116,010.00 | 15 | $8,669.42 |
| short | 21 Jul 2025 117,265.00 | 11 Aug 2025 119,294.00 | 21 | −$2,029.16 |
| long | 11 Aug 2025 119,294.00 | 18 Aug 2025 117,405.00 | 7 | −$1,889.26 |
| long | 5 Sep 2025 110,731.00 | 23 Sep 2025 112,651.00 | 18 | $1,920.12 |
| long | 2 Oct 2025 118,595.00 | 6 Oct 2025 123,482.00 | 4 | $4,887.33 |
| short | 11 Oct 2025 112,774.00 | 26 Oct 2025 111,646.00 | 15 | $1,128.22 |
| long | 26 Oct 2025 111,646.00 | 4 Nov 2025 106,583.00 | 9 | −$5,063.22 |
| short | 4 Nov 2025 106,583.00 | 18 Nov 2025 92,215.10 | 14 | $14,367.90 |
| long | 27 Nov 2025 90,484.00 | 17 Dec 2025 87,863.40 | 20 | −$2,620.58 |
| short | 17 Dec 2025 87,863.40 | 22 Dec 2025 88,658.90 | 5 | −$795.44 |
| long | 15 Jan 2026 96,951.80 | 21 Jan 2026 88,427.70 | 6 | −$8,524.12 |
| short | 21 Jan 2026 88,427.70 | 1 Feb 2026 78,741.10 | 11 | $9,686.56 |
| long | 15 Feb 2026 69,822.90 | 22 Mar 2026 68,918.10 | 35 | −$904.82 |
| short | 22 Mar 2026 68,918.10 | 6 Apr 2026 69,034.20 | 15 | −$116.06 |
| short | 29 Apr 2026 76,342.80 | 6 May 2026 80,905.50 | 7 | −$4,562.75 |
| long | 11 May 2026 82,210.10 | 13 May 2026 80,504.50 | 2 | −$1,705.60 |
| short | 13 May 2026 80,504.50 | 3 Jun 2026 66,760.80 | 21 | $13,743.60 |
| long | 13 Jun 2026 63,580.00 | 26 Jun 2026 59,794.60 | 13 | −$3,785.36 |
| short | 26 Jun 2026 59,794.60 | 3 Jul 2026 61,560.00 | 7 | −$1,765.36 |
| short | 28 Jul 2026 63,755.90 | 8 Aug 2026 64,923.20 | 11 | −$1,167.34 |
| short | 13 Aug 2026 63,480.00 | 19 Aug 2026 64,725.40 | 6 | −$1,245.43 |
| long | 20 Aug 2026 69,334.80 | 21 Aug 2026 73,027.00 | 1 | $3,692.24 |
| short | 2 Sep 2026 77,439.00 | 21 Sep 2026 81,178.00 | 19 | −$3,739.00 |
| long | 21 Sep 2026 81,178.00 | 22 Sep 2026 86,620.00 | 1 | $5,442.00 |
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 |
|---|---|---|---|---|---|
| charl macd ema rsi this page | — | v4 strategy | −49.5% | 59 | 76.9% |
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
| 4-Hour Stochastic EMA Trend by slymnturkoglu | ema sma | v4 strategy | No trades on this market | ||
| 4X EMA and volume strategy by SoftKill21 | ema sma | v4 strategy | +1.8% | 3 | 25.8% |
| Simple Momentum Strategy Based on SMA, EMA and Volume by slip_stream | sma ema | v4 strategy | +7.4% | 18 | 27.4% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | With a workaround 1,000 bars in 21 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as true, false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 21, 29, 41, 45) so the engine trades the way the script says.
“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/// © mbuthiacharles4//Good with trending markets//@version=4strategy("CHARL MACD EMA RSI")fast = 12, slow = 26fastMA = ema(close, fast)slowMA = ema(close, slow)macd = fastMA - slowMAsignal = sma(macd, 9)ema = ema(close, input(200))rsi = rsi(close, input(14))//when delta > 0 and close above ema buydelta = macd - signalbuy_entry= close>ema and delta > 0sell_entry = close<ema and delta<0 var bought = falsevar sold = falsevar reversal = falseif (buy_entry and bought == false and rsi <= 70) strategy.entry("Buy",true , when=buy_entry) bought := true strategy.close("Buy",when= delta<0 or rsi > 70)if (delta<0 and bought==true) bought := false//handle sellsif (sell_entry and sold == false and rsi >= 30) strategy.entry("Sell",false , when=sell_entry) sold := truestrategy.close("Sell",when= delta>0 or rsi < 30)if (delta>0 and sold==true) sold := false if (rsi > 70 or rsi < 30) reversal := true placing = rsi > 70 ? high :low label.new(bar_index, placing, style=label.style_flag, color=color.blue, size=size.tiny)if (reversal == true) if (rsi < 70 and sold == false and delta < 0) strategy.entry("Sell",false , when= delta < 0) sold := true reversal := false else if (rsi > 30 and bought == false and delta > 0) strategy.entry("Buy",true , when= delta > 0) bought := true reversal := false