Pine Script v4 · Strategy
Crypto EMA Trend Reversal Strategy
A Pine Script v4 strategy built on EMA (open, 9), SMA (volume, 7) and ATR (5). It has 8 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 100% of equity and pays no commission. 10 settings; defaults include Fast EMA 9, Slow EMA 21 and Exit EMA 5.
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 Crypto EMA Trend Reversal Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 11 of 11 trades.
- How did Crypto EMA Trend Reversal Strategy do on BTCUSDT 1D with default settings?
- +37.46%
- From 26 Jan 2024 to 21 Oct 2026, over 37 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Crypto EMA Trend Reversal 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 Crypto EMA Trend Reversal Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 65, 66, 67, 68.
- Does Crypto EMA Trend Reversal Strategy run in Zeal Terminal?
- Yes
- It compiles in 21 ms and runs 1,000 bars in 60 ms, in the browser.
BTCUSDT · 1D · default settings
- fastEMA
- slowEMA
- exitEMA
- conf1EMA
- conf2EMA
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +37.46%
- $374.65
- Buy and hold
- +102.5%
- same period
- Closed trades
- 37
- 14 won · 23 lost
- Profitable
- 37.8%
- of closed trades
- Profit factor
- 1.42
- gross profit ÷ gross loss
- Max drawdown
- 18.25%
- −$223.27
One run with the author's default settings, $1,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 commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
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 62, 63, 70, 71. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 16
transp = on line 16, 17, 18, 19, 20. v5 folds the transparency into the colour with color.new(c, transp).
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, atr → ta.atr, crossover → ta.crossover, crossunder → ta.crossunder, valuewhen → ta.valuewhen.
- 4 strategy calls use the when = argument (line 62, 63, 70, 71). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 37 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 11: ema → ta.ema
- Line 12: ema → ta.ema
- Line 13: ema → ta.ema
- Line 14: ema → ta.ema
- Line 15: ema → ta.ema
17 more edits
- Line 16: transp = 0 → color.new(color.green, 0)
- Line 17: transp = 0 → color.new(color.red, 0)
- Line 18: transp = 0 → color.new(color.orange, 0)
- Line 19: transp = 0 → color.new(color.blue, 0)
- Line 20: transp = 0 → color.new(color.black, 0)
- Line 22: sma → ta.sma
- Line 24: atr → ta.atr
- Line 26: crossover → ta.crossover
- Line 27: crossunder → ta.crossunder
- Line 42: valuewhen → ta.valuewhen
- Line 43: valuewhen → ta.valuewhen
- Line 44: valuewhen → ta.valuewhen
- Line 45: valuewhen → ta.valuewhen
- Line 46: crossover → ta.crossover
- Line 47: crossover → ta.crossover
- Line 57: input(type = input.time) → input.time()
- Line 58: input(type = input.time) → input.time()
⋯// @author ADHDCRYPT0-//@version=4+//@version=5strategy(title = "EMA double crossover", shorttitle = "(TEST) double cross over", overlay = true, default_qty_value = 100, initial_capital = 1000,default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true)⋯ema_len5 = input(4, title="SlowConf EMA")-fastEMA = ema(open, ema_len1)+fastEMA = ta.ema(open, ema_len1)-slowEMA = ema(open, ema_len2)+slowEMA = ta.ema(open, ema_len2)-exitEMA = ema(open, ema_len3)+exitEMA = ta.ema(open, ema_len3)-conf1EMA = ema(open, ema_len4)+conf1EMA = ta.ema(open, ema_len4)-conf2EMA = ema(open, ema_len5)+conf2EMA = ta.ema(open, ema_len5)-plot(fastEMA, title='fastEMA', transp=0, color=color.green)+plot(fastEMA, title='fastEMA', color=color.new(color.green, 0))-plot(slowEMA, title='slowEMA', transp=0, color=color.red )+plot(slowEMA, title='slowEMA', color=color.new(color.red, 0) )-plot(exitEMA, title='exitEMA', transp=0, color=color.orange)+plot(exitEMA, title='exitEMA', color=color.new(color.orange, 0))-plot(conf1EMA, title='conf1EMA', transp=0, color=color.blue)+plot(conf1EMA, title='conf1EMA', color=color.new(color.blue, 0))-plot(conf2EMA, title='conf2EMA', transp=0, color=color.black)+plot(conf2EMA, title='conf2EMA', color=color.new(color.black, 0))vol = volume -volma = sma(volume,7)+volma = ta.sma(volume,7)vol_cond = vol>volma-atr = atr(5)+atr = ta.atr(5)// Entry Conditions and vol_cond-long = crossover(fastEMA, slowEMA) and (conf1EMA > conf2EMA) and (fastEMA < exitEMA)+long = ta.crossover(fastEMA, slowEMA) and (conf1EMA > conf2EMA) and (fastEMA < exitEMA)-short= crossunder(fastEMA, slowEMA) and (conf1EMA < conf2EMA) and (fastEMA > exitEMA)+short= ta.crossunder(fastEMA, slowEMA) and (conf1EMA < conf2EMA) and (fastEMA > exitEMA)tradeType = input("BOTH", title="What trades should be taken : ", options=["LONG", "SHORT", "BOTH", "NONE"])⋯// Simple Stop Loss + 2 Take Profits-sl_long = valuewhen(longCond , low - atr * sl, 0)+sl_long = ta.valuewhen(longCond , low - atr * sl, 0)-sl_short = valuewhen(shortCond, high+ atr * sl, 0)+sl_short = ta.valuewhen(shortCond, high+ atr * sl, 0)-tp_long = valuewhen(longCond , high + atr * tp, 0)+tp_long = ta.valuewhen(longCond , high + atr * tp, 0)-tp_short = valuewhen(shortCond, low - atr * tp, 0)+tp_short = ta.valuewhen(shortCond, low - atr * tp, 0)-long_exit = crossover(fastEMA, exitEMA) and pos[1]==1+long_exit = ta.crossover(fastEMA, exitEMA) and pos[1]==1-short_exit= crossover(exitEMA, fastEMA) and pos[1]==-1+short_exit= ta.crossover(exitEMA, fastEMA) and pos[1]==-1if long_exit or short_exit⋯// Strategy Backtest Limiting Algorithm-i_startTime = input(defval = timestamp("01 Sep 2002 13:30 +0000"), title = "Backtesting Start Time", type = input.time)+i_startTime = input.time(defval = timestamp("01 Sep 2002 13:30 +0000"), title = "Backtesting Start Time")-i_endTime = input(defval = timestamp("30 Sep 2099 19:30 +0000"), title = "Backtesting End Time", type = input.time)+i_endTime = input.time(defval = timestamp("30 Sep 2099 19:30 +0000"), title = "Backtesting End Time")timeCond = (time > i_startTime) and (time < i_endTime)⋯Same 37 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.int()
- Line 7: input() → input.int()
- Line 8: input() → input.int()
- Line 9: input() → input.int()
- Line 10: input() → input.int()
- Line 28: input() → input.string()
6 more edits
- Line 39: input() → input.int()
- Line 40: input() → input.int()
- Line 62: when = longCond and timeCond and tradeType!="SHORT" → if block
- Line 63: when = shortCond and timeCond and tradeType!="LONG" → if block
- Line 70: when = long_exit → if block
- Line 71: when = short_exit → if block
⋯// Variables-ema_len1 = input(9 , title="Fast EMA")+ema_len1 = input.int(9 , title="Fast EMA")-ema_len2 = input(21, title="Slow EMA")+ema_len2 = input.int(21, title="Slow EMA")-ema_len3 = input(5, title="Exit EMA")+ema_len3 = input.int(5, title="Exit EMA")-ema_len4 = input(1, title="FastConf EMA")+ema_len4 = input.int(1, title="FastConf EMA")-ema_len5 = input(4, title="SlowConf EMA")+ema_len5 = input.int(4, title="SlowConf EMA")fastEMA = ta.ema(open, ema_len1)⋯short= ta.crossunder(fastEMA, slowEMA) and (conf1EMA < conf2EMA) and (fastEMA > exitEMA)-tradeType = input("BOTH", title="What trades should be taken : ", options=["LONG", "SHORT", "BOTH", "NONE"])+tradeType = input.string("BOTH", title="What trades should be taken : ", options=["LONG", "SHORT", "BOTH", "NONE"])pos = 0.0⋯// EXIT FUNCTIONS //-sl = input(1, title="Stop Loss (ATR)", minval=0)+sl = input.int(1, title="Stop Loss (ATR)", minval=0)-tp = input(6, title="Take Profit 1 (ATR)", minval=0)+tp = input.int(6, title="Take Profit 1 (ATR)", minval=0)// Simple Stop Loss + 2 Take Profits⋯if strategy.equity >0- strategy.entry("long" , strategy.long , when=longCond and timeCond and tradeType!="SHORT" , alert_message="INSERT MESSAGE HERE")+ if longCond and timeCond and tradeType!="SHORT"+ strategy.entry("long" , strategy.long , alert_message="INSERT MESSAGE HERE")- strategy.entry("short", strategy.short, when=shortCond and timeCond and tradeType!="LONG" , alert_message="INSERT MESSAGE HERE")+ if shortCond and timeCond and tradeType!="LONG"+ strategy.entry("short", strategy.short , alert_message="INSERT MESSAGE HERE") ⋯ - strategy.close("long", when=long_exit , comment="TP2", alert_message="INSERT MESSAGE HERE")+ if long_exit+ strategy.close("long" , comment="TP2", alert_message="INSERT MESSAGE HERE")- strategy.close("short", when=short_exit, comment="TP2", alert_message="INSERT MESSAGE HERE")+ if short_exit+ strategy.close("short", comment="TP2", alert_message="INSERT MESSAGE HERE")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: 37 before, 38 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.
⋯// @author ADHDCRYPT0-//@version=5+//@version=6strategy(title = "EMA double crossover", shorttitle = "(TEST) double cross over", overlay = true, default_qty_value = 100, initial_capital = 1000,default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true)⋯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 |
|---|---|---|---|
| Fast EMA | 9 | int | fastEMA |
| Slow EMA | 21 | int | slowEMA |
| Exit EMA | 5 | int | exitEMA |
| FastConf EMA | 1 | int | conf1EMA |
| SlowConf EMA | 4 | int | conf2EMA |
| What trades should be taken : | BOTH | string · options ["LONG", "SHORT", "BOTH", "NONE"] | line 30 line 32 line 34 entry long entry short |
| Stop Loss (ATR) | 1 | int · min 0 | sl_long sl_short |
| Take Profit 1 (ATR) | 6 | int · min 0 | tp_long tp_short |
| Backtesting Start Time | timestamp("01 Sep 2002 13:30 +0000") | time | timeCond |
| Backtesting End Time | timestamp("30 Sep 2099 19:30 +0000") | time | timeCond |
What it calculates
- EMA (
ema,ta.emain v5)open, Fast EMA (9)fastEMA · line 11 - EMA (
ema,ta.emain v5)open, Slow EMA (21)slowEMA · line 12 - EMA (
ema,ta.emain v5)open, Exit EMA (5)exitEMA · line 13 - EMA (
ema,ta.emain v5)open, FastConf EMA (1)conf1EMA · line 14 - EMA (
ema,ta.emain v5)open, SlowConf EMA (4)conf2EMA · line 15 - SMA (
sma,ta.smain v5)volume, 7volma · line 22 - ATR (
atr,ta.atrin v5)5atr · line 24 - Valuewhen (
valuewhen,ta.valuewhenin v5)longCond, low - atr * sl, 0sl_long · line 42 - Valuewhen (
valuewhen,ta.valuewhenin v5)shortCond, high+ atr * sl, 0sl_short · line 43 - Valuewhen (
valuewhen,ta.valuewhenin v5)longCond, high + atr * tp, 0tp_long · line 44 - Valuewhen (
valuewhen,ta.valuewhenin v5)shortCond, low - atr * tp, 0tp_short · line 45
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 | longCond and timeCond and tradeType!="SHORT" | On | 62 |
| Open short short | shortCond and timeCond and tradeType!="LONG" | On | 63 |
| Exit SL/TP1 stop and limit | strategy.equity >0 | On | 65 |
| Exit SL/TP1 stop and limit | strategy.equity >0 | On | 66 |
| Exit SL stop | strategy.equity >0 | On | 67 |
| Exit SL stop | strategy.equity >0 | On | 68 |
| Close long | long_exitcrossover(fastEMA, exitEMA) and pos[1]==1 | On | 70 |
| Close short | short_exitcrossover(exitEMA, fastEMA) and pos[1]==-1 | On | 71 |
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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.
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 | +37.5% | +102.5% | 37 | 38% | 1.42 | 18.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −12.7% | +5.5% | 48 | 25% | 0.70 | 16.4% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +7.0% | +9.8% | 36 | 25% | 1.06 | 42.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −11.1% | +8.4% | 55 | 29% | 0.77 | 19.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +11.5% | +16.7% | 34 | 35% | 1.08 | 49.4% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +7.9% | +21.5% | 34 | 35% | 1.21 | 12.8% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −40.5% | +148.3% | 38 | 26% | 0.53 | 52.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +31.2% | +17.1% | 46 | 43% | 1.94 | 7.7% | 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 | 12 | 50% | $94.05 |
| Sideways market | 16 | 31% | $4.83 |
| Falling market | 9 | 33% | $275.76 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
33.9%
The 95th percentile of the worst drawdown when the same 37 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 15.6% to 33.9%; the tick is what actually happened, 16.5%. Order luck alone moves it this much.
All 37 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 14 Apr 2024 65,661.80 | 23 Apr 2024 66,414.00 | 9 | −$11.46 |
| long | 17 May 2024 67,024.00 | 1 Jun 2024 67,766.90 | 15 | $10.96 |
| short | 15 Jun 2024 66,228.30 | 5 Jul 2024 54,634.90 | 20 | $174.96 |
| long | 17 Jul 2024 64,088.00 | 1 Aug 2024 65,354.00 | 15 | $23.20 |
| short | 4 Aug 2024 58,161.00 | 22 Aug 2024 60,375.80 | 18 | −$45.61 |
| long | 24 Aug 2024 64,157.00 | 27 Aug 2024 61,024.80 | 3 | −$56.25 |
| short | 30 Aug 2024 59,124.00 | 13 Sep 2024 60,498.00 | 14 | −$25.47 |
| long | 18 Sep 2024 61,760.00 | 2 Oct 2024 60,649.30 | 14 | −$19.25 |
| short | 10 Oct 2024 60,326.40 | 11 Oct 2024 63,267.40 | 1 | −$51.24 |
| long | 13 Oct 2024 62,870.00 | 5 Nov 2024 69,372.00 | 23 | $103.40 |
| short | 24 Dec 2024 98,663.60 | 4 Jan 2025 98,220.50 | 11 | $4.95 |
| long | 7 Jan 2025 96,954.60 | 9 Jan 2025 92,482.60 | 2 | −$51.12 |
| short | 10 Jan 2025 94,726.10 | 15 Jan 2025 99,734.90 | 5 | −$55.90 |
| long | 16 Jan 2025 99,987.30 | 29 Jan 2025 103,733.00 | 13 | $37.51 |
| short | 5 Feb 2025 96,612.40 | 21 Feb 2025 96,182.00 | 16 | $4.63 |
| long | 28 Mar 2025 84,424.40 | 30 Mar 2025 82,390.00 | 2 | −$25.14 |
| long | 17 Apr 2025 84,947.90 | 9 May 2025 104,196.00 | 22 | $230.71 |
| short | 19 Jun 2025 104,659.00 | 25 Jun 2025 107,837.00 | 6 | −$37.93 |
| long | 27 Jun 2025 107,048.00 | 1 Aug 2025 113,298.00 | 35 | $70.71 |
| short | 3 Aug 2025 114,209.00 | 7 Aug 2025 117,541.00 | 4 | −$37.39 |
| long | 10 Aug 2025 119,294.00 | 18 Aug 2025 116,227.00 | 8 | −$31.99 |
| short | 20 Aug 2025 114,271.00 | 9 Sep 2025 111,546.00 | 20 | $28.91 |
| long | 12 Sep 2025 116,029.00 | 22 Sep 2025 112,382.00 | 10 | −$39.01 |
| short | 26 Sep 2025 109,643.00 | 29 Sep 2025 112,795.00 | 3 | −$34.55 |
| long | 2 Oct 2025 120,529.00 | 10 Oct 2025 115,460.00 | 8 | −$49.11 |
| short | 14 Oct 2025 113,028.00 | 26 Oct 2025 114,559.00 | 12 | −$15.15 |
| long | 4 Jan 2026 91,529.70 | 20 Jan 2026 88,790.10 | 16 | −$33.02 |
| short | 23 Jan 2026 89,600.30 | 3 Feb 2026 73,155.80 | 11 | $196.44 |
| long | 13 Mar 2026 70,930.00 | 22 Mar 2026 67,410.60 | 9 | −$62.86 |
| short | 28 Mar 2026 66,377.00 | 6 Apr 2026 69,891.40 | 9 | −$63.74 |
| long | 8 Apr 2026 71,069.90 | 1 May 2026 78,231.10 | 23 | $114.89 |
| short | 20 May 2026 77,552.20 | 3 Jun 2026 65,745.20 | 14 | $191.08 |
| long | 11 Jul 2026 63,819.00 | 13 Jul 2026 62,070.20 | 2 | −$39.63 |
| short | 1 Aug 2026 62,823.60 | 5 Aug 2026 64,779.40 | 4 | −$43.79 |
| long | 8 Aug 2026 64,962.60 | 11 Aug 2026 63,672.60 | 3 | −$27.06 |
| short | 13 Aug 2026 63,490.90 | 19 Aug 2026 65,192.90 | 6 | −$35.81 |
| long | 20 Aug 2026 73,025.10 | 11 Sep 2026 77,225.70 | 22 | $74.77 |
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 |
|---|---|---|---|---|---|
| Crypto EMA Trend Reversal Strategy this page | — | v4 strategy | +37.5% | 37 | 18.3% |
| [D] Dudu 95 Strategy Template ver.1.1. by DuDu95 | atr ema sma valuewhen | v5 strategy | +10.5% | 41 | 14.8% |
| ATR with EOM and VORTEX by SoftKill21 | atr ema sma | v4 strategy | +66.2% | 22 | 29.9% |
| cATRpillar Strategy by Obi-wanClintobe | atr sma ema | v4 strategy | +1.02827e+24% | 27 | 88,097.2% |
| Optimized Zhaocaijinbao strategy by fzj20020403 | sma ema atr | v5 strategy | +15.6% | 12 | 9.8% |
| EMA & Supertrend by bhavikmota | ema sma atr | v4 strategy | +2,123.6% | 13 | 980.9% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 21 ms | Yes 1,000 bars in 60 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/// @author ADHDCRYPT0//@version=4strategy(title = "EMA double crossover", shorttitle = "(TEST) double cross over", overlay = true, default_qty_value = 100, initial_capital = 1000,default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true)// Variablesema_len1 = input(9 , title="Fast EMA")ema_len2 = input(21, title="Slow EMA")ema_len3 = input(5, title="Exit EMA")ema_len4 = input(1, title="FastConf EMA")ema_len5 = input(4, title="SlowConf EMA")fastEMA = ema(open, ema_len1)slowEMA = ema(open, ema_len2)exitEMA = ema(open, ema_len3)conf1EMA = ema(open, ema_len4)conf2EMA = ema(open, ema_len5)plot(fastEMA, title='fastEMA', transp=0, color=color.green)plot(slowEMA, title='slowEMA', transp=0, color=color.red )plot(exitEMA, title='exitEMA', transp=0, color=color.orange)plot(conf1EMA, title='conf1EMA', transp=0, color=color.blue)plot(conf2EMA, title='conf2EMA', transp=0, color=color.black)vol = volume volma = sma(volume,7)vol_cond = vol>volmaatr = atr(5)// Entry Conditions and vol_condlong = crossover(fastEMA, slowEMA) and (conf1EMA > conf2EMA) and (fastEMA < exitEMA)short= crossunder(fastEMA, slowEMA) and (conf1EMA < conf2EMA) and (fastEMA > exitEMA)tradeType = input("BOTH", title="What trades should be taken : ", options=["LONG", "SHORT", "BOTH", "NONE"])pos = 0.0if tradeType=="BOTH" pos:= long? 1 : short? -1 : pos[1]if tradeType=="LONG" pos:= long? 1 : pos[1]if tradeType=="SHORT" pos:=short? -1 : pos[1]longCond = long and (pos[1]!= 1 or na(pos[1]))shortCond = short and (pos[1]!=-1 or na(pos[1]))// EXIT FUNCTIONS //sl = input(1, title="Stop Loss (ATR)", minval=0)tp = input(6, title="Take Profit 1 (ATR)", minval=0)// Simple Stop Loss + 2 Take Profitssl_long = valuewhen(longCond , low - atr * sl, 0)sl_short = valuewhen(shortCond, high+ atr * sl, 0)tp_long = valuewhen(longCond , high + atr * tp, 0)tp_short = valuewhen(shortCond, low - atr * tp, 0)long_exit = crossover(fastEMA, exitEMA) and pos[1]==1short_exit= crossover(exitEMA, fastEMA) and pos[1]==-1if long_exit or short_exit pos:=0// Position Adjustmentlong_sl = low <sl_long [1] and pos[1]==1short_sl = high>sl_short[1] and pos[1]==-1if long_sl or short_sl pos:=0 // Strategy Backtest Limiting Algorithmi_startTime = input(defval = timestamp("01 Sep 2002 13:30 +0000"), title = "Backtesting Start Time", type = input.time)i_endTime = input(defval = timestamp("30 Sep 2099 19:30 +0000"), title = "Backtesting End Time", type = input.time)timeCond = (time > i_startTime) and (time < i_endTime)// Make sure we are within the bar range, Set up entries and exit conditionsif strategy.equity >0 strategy.entry("long" , strategy.long , when=longCond and timeCond and tradeType!="SHORT" , alert_message="INSERT MESSAGE HERE") strategy.entry("short", strategy.short, when=shortCond and timeCond and tradeType!="LONG" , alert_message="INSERT MESSAGE HERE") strategy.exit("SL/TP1", from_entry = "long" , stop=sl_long , limit=tp_long , alert_message="INSERT MESSAGE HERE") strategy.exit("SL/TP1", from_entry = "short", stop=sl_short, limit=tp_short, alert_message="INSERT MESSAGE HERE") strategy.exit("SL", from_entry = "long" , stop=sl_long, alert_message="INSERT MESSAGE HERE") strategy.exit("SL", from_entry = "short", stop=sl_short, alert_message="INSERT MESSAGE HERE") strategy.close("long", when=long_exit , comment="TP2", alert_message="INSERT MESSAGE HERE") strategy.close("short", when=short_exit, comment="TP2", alert_message="INSERT MESSAGE HERE")