Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (10)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,334$2,025Equity from $1,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots.
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

Verified

Same 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)⋯

Clean v5

VerifiedDownload the v5 script

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.

SettingDefaultType and rangeFeeds
Fast EMA9intfastEMA
Slow EMA21intslowEMA
Exit EMA5intexitEMA
FastConf EMA1intconf1EMA
SlowConf EMA4intconf2EMA
What trades should be taken : BOTHstring · options ["LONG", "SHORT", "BOTH", "NONE"]line 30 line 32 line 34 entry long entry short
Stop Loss (ATR)1int · min 0sl_long sl_short
Take Profit 1 (ATR)6int · min 0tp_long tp_short
Backtesting Start Timetimestamp("01 Sep 2002 13:30 +0000")timetimeCond
Backtesting End Timetimestamp("30 Sep 2099 19:30 +0000")timetimeCond

What it calculates

  • EMA (ema, ta.ema in v5)open, Fast EMA (9)fastEMA · line 11
  • EMA (ema, ta.ema in v5)open, Slow EMA (21)slowEMA · line 12
  • EMA (ema, ta.ema in v5)open, Exit EMA (5)exitEMA · line 13
  • EMA (ema, ta.ema in v5)open, FastConf EMA (1)conf1EMA · line 14
  • EMA (ema, ta.ema in v5)open, SlowConf EMA (4)conf2EMA · line 15
  • SMA (sma, ta.sma in v5)volume, 7volma · line 22
  • ATR (atr, ta.atr in v5)5atr · line 24
  • Valuewhen (valuewhen, ta.valuewhen in v5)longCond, low - atr * sl, 0sl_long · line 42
  • Valuewhen (valuewhen, ta.valuewhen in v5)shortCond, high+ atr * sl, 0sl_short · line 43
  • Valuewhen (valuewhen, ta.valuewhen in v5)longCond, high + atr * tp, 0tp_long · line 44
  • Valuewhen (valuewhen, ta.valuewhen in 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.

openopenprice dataema_len1Fast EMAsetting · 9ema_len2Slow EMAsetting · 21ema_len3Exit EMAsetting · 5ema_len4FastConf EMAsetting · 1ema_len5SlowConf EMAsetting · 4volumevolumeprice datahigh · low · closehigh · low · closeprice datalowlowprice dataslStop Loss (ATR)setting · 1highhighprice datatpTake Profit 1 (ATR)setting · 6timetimeprice datai_startTimeBacktesting Start Timesetting · timestamp("01 Sep 2…i_endTimeBacktesting End Timesetting · timestamp("30 Sep 2…tradeTypeWhat trades should be t…setting · BOTHfastEMAEMA→ fastEMAslowEMAEMA→ slowEMAexitEMAEMA→ exitEMAconf1EMAEMA→ conf1EMAconf2EMAEMA→ conf2EMAvolmaSMA→ volmaatrATR→ atrsl_longValuewhen→ sl_longsl_shortValuewhen→ sl_shorttp_longValuewhen→ tp_longtp_shortValuewhen→ tp_shortvolvolvalueposposvaluelong_exitlong_exitconditionshort_exitshort_exitconditionvol_condvol_condconditionlong, shortlong · short2 conditionslongCondlongCondconditionshortCondshortCondconditionlong_sllong_slconditionshort_slshort_slconditiontimeCondtimeCondconditionentry:longOpen longstrategy.entryentry:shortOpen shortstrategy.entryexit:SL/TP1exit SL/TP1strategy.exitexit:SLexit SLstrategy.exitclose:longClose longstrategy.closeclose:shortClose shortstrategy.closeplot:16fastEMAplotplot:17slowEMAplotplot:18exitEMAplotplot:19conf1EMAplotplot:20conf2EMAplot

Order rules

OrderFires whenBy defaultLine
Open long longlongCond and timeCond and tradeType!="SHORT"On62
Open short shortshortCond and timeCond and tradeType!="LONG"On63
Exit SL/TP1 stop and limitstrategy.equity >0On65
Exit SL/TP1 stop and limitstrategy.equity >0On66
Exit SL stopstrategy.equity >0On67
Exit SL stopstrategy.equity >0On68
Close longlong_exitcrossover(fastEMA, exitEMA) and pos[1]==1On70
Close shortshort_exitcrossover(exitEMA, fastEMA) and pos[1]==-1On71

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.

11/11trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+37.5%+102.5%3738%1.4218.3%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−12.7%+5.5%4825%0.7016.4%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+7.0%+9.8%3625%1.0642.4%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−11.1%+8.4%5529%0.7719.8%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+11.5%+16.7%3435%1.0849.4%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+7.9%+21.5%3435%1.2112.8%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−40.5%+148.3%3826%0.5352.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+31.2%+17.1%4643%1.947.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 barsTradesWonP&L
Rising market1250%$94.05
Sideways market1631%$4.83
Falling market933%$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.

0%39%
All 37 trades
SideEntryExitBarsP&L
short14 Apr 2024 65,661.8023 Apr 2024 66,414.009−$11.46
long17 May 2024 67,024.001 Jun 2024 67,766.9015$10.96
short15 Jun 2024 66,228.305 Jul 2024 54,634.9020$174.96
long17 Jul 2024 64,088.001 Aug 2024 65,354.0015$23.20
short4 Aug 2024 58,161.0022 Aug 2024 60,375.8018−$45.61
long24 Aug 2024 64,157.0027 Aug 2024 61,024.803−$56.25
short30 Aug 2024 59,124.0013 Sep 2024 60,498.0014−$25.47
long18 Sep 2024 61,760.002 Oct 2024 60,649.3014−$19.25
short10 Oct 2024 60,326.4011 Oct 2024 63,267.401−$51.24
long13 Oct 2024 62,870.005 Nov 2024 69,372.0023$103.40
short24 Dec 2024 98,663.604 Jan 2025 98,220.5011$4.95
long7 Jan 2025 96,954.609 Jan 2025 92,482.602−$51.12
short10 Jan 2025 94,726.1015 Jan 2025 99,734.905−$55.90
long16 Jan 2025 99,987.3029 Jan 2025 103,733.0013$37.51
short5 Feb 2025 96,612.4021 Feb 2025 96,182.0016$4.63
long28 Mar 2025 84,424.4030 Mar 2025 82,390.002−$25.14
long17 Apr 2025 84,947.909 May 2025 104,196.0022$230.71
short19 Jun 2025 104,659.0025 Jun 2025 107,837.006−$37.93
long27 Jun 2025 107,048.001 Aug 2025 113,298.0035$70.71
short3 Aug 2025 114,209.007 Aug 2025 117,541.004−$37.39
long10 Aug 2025 119,294.0018 Aug 2025 116,227.008−$31.99
short20 Aug 2025 114,271.009 Sep 2025 111,546.0020$28.91
long12 Sep 2025 116,029.0022 Sep 2025 112,382.0010−$39.01
short26 Sep 2025 109,643.0029 Sep 2025 112,795.003−$34.55
long2 Oct 2025 120,529.0010 Oct 2025 115,460.008−$49.11
short14 Oct 2025 113,028.0026 Oct 2025 114,559.0012−$15.15
long4 Jan 2026 91,529.7020 Jan 2026 88,790.1016−$33.02
short23 Jan 2026 89,600.303 Feb 2026 73,155.8011$196.44
long13 Mar 2026 70,930.0022 Mar 2026 67,410.609−$62.86
short28 Mar 2026 66,377.006 Apr 2026 69,891.409−$63.74
long8 Apr 2026 71,069.901 May 2026 78,231.1023$114.89
short20 May 2026 77,552.203 Jun 2026 65,745.2014$191.08
long11 Jul 2026 63,819.0013 Jul 2026 62,070.202−$39.63
short1 Aug 2026 62,823.605 Aug 2026 64,779.404−$43.79
long8 Aug 2026 64,962.6011 Aug 2026 63,672.603−$27.06
short13 Aug 2026 63,490.9019 Aug 2026 65,192.906−$35.81
long20 Aug 2026 73,025.1011 Sep 2026 77,225.7022$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.

ScriptSharesTypeNetTradesMax drawdown
Crypto EMA Trend Reversal Strategy this page—v4 strategy+37.5%3718.3%
[D] Dudu 95 Strategy Template ver.1.1. by DuDu95atr ema sma valuewhenv5 strategy+10.5%4114.8%
ATR with EOM and VORTEX by SoftKill21atr ema smav4 strategy+66.2%2229.9%
cATRpillar Strategy by Obi-wanClintobeatr sma emav4 strategy+1.02827e+24%2788,097.2%
Optimized Zhaocaijinbao strategy by fzj20020403sma ema atrv5 strategy+15.6%129.8%
EMA & Supertrend by bhavikmotaema sma atrv4 strategy+2,123.6%13980.9%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 21 msYes 1,000 bars in 60 msNot measured
PineTSNo 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")