Zeal Terminal Open in Zeal Terminal

Pine Script v5 · Strategy

EMA Mean Reversion Strategy

A Pine Script v5 strategy built on EMA or SMA or RMA or WMA (close, 200), Highest (high) and Lowest (low). It has 40 order rules, 20 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 0.05% commission. 14 settings; defaults include Length 200, Type EMA and Source close.

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 EMA Mean Reversion Strategy repaint?
Yes
At least one pattern in the source can repaint: higher-timeframe data.
How did EMA Mean Reversion Strategy do on BTCUSDT 1D with default settings?
+4.66%
From 26 Jan 2024 to 21 Oct 2026, over 230 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Does EMA Mean Reversion Strategy 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: 230 before, 230 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
Does EMA Mean Reversion Strategy use a stop loss?
Yes
strategy.exit sets a stop on line 124, 125, 126, 127, 135, 152, 153, 154, 155.
Does EMA Mean Reversion Strategy run in Zeal Terminal?
Yes
It compiles in 62 ms and runs 1,000 bars in 116 ms, in the browser.

BTCUSDT · 1D · default settings

  • EMA
  • EMA Cloud Upper Band
  • EMA Cloud Upper Band
  • Plot
  • Plot
  • Plot
  • Plot
  • Plot
  • Long Conditions Met
  • Short Conditions Met
  • Plot
  • 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 (14)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$104,658$202,527Equity from $100,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. Zeal Terminal's engine currently reads an order side written as direction = strategy.long, direction = strategy.short as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 112, 113, 115, 116, 117, 119 and more) so the engine trades the way the script says. 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.
Net profit
+4.66%
$4,658.43
Buy and hold
+102.5%
same period
Closed trades
230
91 won · 139 lost
Profitable
39.6%
of closed trades
Profit factor
1.11
gross profit ÷ gross loss
Max drawdown
8.94%
−$9,129.79

One run with the author's default settings, $100,000 starting capital, commission as the script sets it. 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

    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 112, 113, 115, 116, 117, 119, 120, 121, 122, 124, 125, 126, 127, 129, 130, 131, 132, 134, 135, 136, 140, 141, 143, 144, 145, 147, 148, 149, 150, 152, 153, 154, 155, 157, 158, 159, 160, 162, 163, 164. The upgraded version below moves each call inside an if block.

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
EMA Settings
TimeframetimeframeEMA_
Length200int · min 1EMA
TypeEMAstring · options ["EMA", "SMA", "RMA", "WMA"]EMA
SourceclosesourceEMA
Entry Settings
Long Position Entry TriggerWhen the percentage that the price is away from the selected EMA reaches this point, a long postion will open.9floatEMA_cloud_upper_band_val EMA_cloud_lower_band_val long_conditions_met
Short Position Entry TriggerWhen the percentage that the price is away from the selected EMA reaches this point, a short postion will open.9floatshort_conditions_met
Close Position TriggerWhen the percentage that the price is away from the selected EMA reaches this point, open postion will close.1.0floatclose_long_entries close_short_entries
Cancel Unfilled Entries TriggerWhen the percentage that the price is away from the selected EMA reaches this point, any unfilled entries will be canceled.4floatcancel_entries
Ladder Into PositionsEnable this to use the laddering settings below.trueboolexit Close Long Ladder 1 exit Close Long Ladder 2 exit Close Long Ladder 3 exit Close Long Ladder 4 cancel Long Ladder 1 cancel Long Ladder 2 cancel Long Ladder 3 cancel Long Ladder 4 entry Long exit Close Long cancel Long exit Close Short Ladder 1 exit Close Short Ladder 2 exit Close Short Ladder 3 exit Close Short Ladder 4 cancel Short Ladder 1 cancel Short Ladder 2 cancel Short Ladder 3 cancel Short Ladder 4 entry Short exit Close Short cancel Short line 78 line 110 line 138
indent_4+"Ladder Rungs"4int · min 2 · max 4 · step 1position_qty entry Long Ladder 1 entry Long Ladder 2 entry Long Ladder 1 entry Long Ladder 2 entry Long Ladder 3 entry Long Ladder 1 entry Long Ladder 2 entry Long Ladder 3 entry Long Ladder 4 entry Short Ladder 1 entry Short Ladder 2 entry Short Ladder 1 entry Short Ladder 2 entry Short Ladder 3 entry Short Ladder 1 entry Short Ladder 2 entry Short Ladder 3 entry Short Ladder 4 line 111 line 114 line 118 line 139 line 142 line 146
indent_4+"Ladder Step (%)".5float · step .1long_ladder_1_limit_price long_ladder_2_limit_price long_ladder_3_limit_price long_ladder_4_limit_price short_ladder_1_limit_price short_ladder_2_limit_price short_ladder_3_limit_price short_ladder_4_limit_price
Stop Loss (%)4.0float · step 0.1long_stop_loss short_stop_loss
Start Trailing After (%)1float · step 0.1long_start_trailing_val short_start_trailing_val
Trail Behind (%)1float · step 0.1long_trail_behind_val short_trail_behind_val long_trailing_stop short_trailing_stop

What it calculates

  • EMA / SMA / RMA / WMASource (close), Length (200) · chosen by TypeEMA · line 52
  • Highesthigh, bar_index == 0 ? 5000: bars_since_entrylong_run_up · line 91
  • Lowestlow, bar_index == 0 ? 5000: bars_since_entryshort_run_up · line 96

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.

The script has 81 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.

Order rules

OrderFires whenBy defaultLine
Open Long Ladder 1 longlong_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionOn112
Open Long Ladder 2 longlong_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionOn113
Open Long Ladder 1 longlong_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionOn115
Open Long Ladder 2 longlong_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionOn116
Open Long Ladder 3 longlong_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionOn117
Open Long Ladder 1 longlong_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionOn119
Open Long Ladder 2 longlong_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionOn120
Open Long Ladder 3 longlong_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionOn121
Open Long Ladder 4 longlong_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionOn122
Exit Close Long Ladder 1 stop and limitclose_long_entriesdistance_from_EMA <= closeEntryBelow or close <= long_trailing_stopOn “Ladder Into Positions”124
Exit Close Long Ladder 2 stop and limitclose_long_entriesdistance_from_EMA <= closeEntryBelow or close <= long_trailing_stopOn “Ladder Into Positions”125
Exit Close Long Ladder 3 stop and limitclose_long_entriesdistance_from_EMA <= closeEntryBelow or close <= long_trailing_stopOn “Ladder Into Positions”126
Exit Close Long Ladder 4 stop and limitclose_long_entriesdistance_from_EMA <= closeEntryBelow or close <= long_trailing_stopOn “Ladder Into Positions”127
cancel Long Ladder 1cancel_entriesdistance_from_EMA <= cancelEntryBelowOn “Ladder Into Positions”129
cancel Long Ladder 2cancel_entriesdistance_from_EMA <= cancelEntryBelowOn “Ladder Into Positions”130
cancel Long Ladder 3cancel_entriesdistance_from_EMA <= cancelEntryBelowOn “Ladder Into Positions”131
cancel Long Ladder 4cancel_entriesdistance_from_EMA <= cancelEntryBelowOn “Ladder Into Positions”132
Open Long longlong_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionOn “Ladder Into Positions”134
Exit Close Long stop and limitclose_long_entriesdistance_from_EMA <= closeEntryBelow or close <= long_trailing_stopOn “Ladder Into Positions”135
cancel Longcancel_entriesdistance_from_EMA <= cancelEntryBelowOn “Ladder Into Positions”136
Open Short Ladder 1 shortshort_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionOn140
Open Short Ladder 2 shortshort_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionOn141
Open Short Ladder 1 shortshort_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionOn143
Open Short Ladder 2 shortshort_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionOn144
Open Short Ladder 3 shortshort_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionOn145
Open Short Ladder 1 shortshort_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionOn147
Open Short Ladder 2 shortshort_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionOn148
Open Short Ladder 3 shortshort_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionOn149
Open Short Ladder 4 shortshort_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionOn150
Exit Close Short Ladder 1 stop and limitclose_short_entriesdistance_from_EMA <= closeEntryBelow or close >= short_trailing_stopOn “Ladder Into Positions”152
Exit Close Short Ladder 2 stop and limitclose_short_entriesdistance_from_EMA <= closeEntryBelow or close >= short_trailing_stopOn “Ladder Into Positions”153
Exit Close Short Ladder 3 stop and limitclose_short_entriesdistance_from_EMA <= closeEntryBelow or close >= short_trailing_stopOn “Ladder Into Positions”154
Exit Close Short Ladder 4 stop and limitclose_short_entriesdistance_from_EMA <= closeEntryBelow or close >= short_trailing_stopOn “Ladder Into Positions”155
cancel Short Ladder 1cancel_entriesdistance_from_EMA <= cancelEntryBelowOn “Ladder Into Positions”157
cancel Short Ladder 2cancel_entriesdistance_from_EMA <= cancelEntryBelowOn “Ladder Into Positions”158
cancel Short Ladder 3cancel_entriesdistance_from_EMA <= cancelEntryBelowOn “Ladder Into Positions”159
cancel Short Ladder 4cancel_entriesdistance_from_EMA <= cancelEntryBelowOn “Ladder Into Positions”160
Open Short shortshort_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionOn “Ladder Into Positions”162
Exit Close Short limitclose_short_entriesdistance_from_EMA <= closeEntryBelow or close >= short_trailing_stopOn “Ladder Into Positions”163
cancel Shortcancel_entriesdistance_from_EMA <= cancelEntryBelowOn “Ladder Into Positions”164

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.

101/101trades 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.
  • CheckHigher-timeframe data1 request.security call(s) use lookahead_on (line 58). Without a [1] offset this reads future bars in history.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+4.7%+102.5%23040%1.118.9%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.3%+5.5%4635%0.913.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+6.0%+9.8%33441%1.0720.4%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.5%+8.4%5645%1.065.6%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−18.7%+16.7%32634%0.8021.6%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−2.5%+21.5%6843%0.849.6%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+4.2%+148.3%19542%1.1011.4%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−0.7%+17.1%3040%0.843.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 barsTradesWonP&L
Rising market8636%−$3,881.08
Sideways market5942%$818.67
Falling market8541%$7,720.84

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

13.9%

The 95th percentile of the worst drawdown when the same 230 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 6.2% to 13.9%; the tick is what actually happened, 8.4%. Order luck alone moves it this much.

0%16%
The first 150 of 230 trades
SideEntryExitBarsP&L
long6 Sep 2024 55,899.107 Sep 2024 53,963.001−$886.02
long8 Sep 2024 53,890.1010 Sep 2024 57,042.002$1,416.64
short20 Oct 2024 68,719.9023 Oct 2024 67,426.003$450.55
short28 Oct 2024 69,061.8028 Oct 2024 69,061.800−$25.38
short29 Oct 2024 70,312.004 Nov 2024 68,776.006$529.04
short6 Nov 2024 69,718.907 Nov 2024 75,572.001−$2,167.21
short8 Nov 2024 76,237.2011 Nov 2024 80,370.003−$1,378.37
short12 Nov 2024 89,091.2013 Nov 2024 87,952.001$290.21
short13 Nov 2024 89,534.5013 Nov 2024 89,534.500−$24.73
short14 Nov 2024 90,827.1016 Nov 2024 91,032.102−$80.39
short16 Nov 2024 91,279.0016 Nov 2024 91,279.000−$24.80
short17 Nov 2024 91,039.9018 Nov 2024 89,856.001$296.03
short18 Nov 2024 91,492.8018 Nov 2024 91,492.800−$24.77
short19 Nov 2024 90,916.4022 Nov 2024 98,317.103−$2,037.61
short24 Nov 2024 98,160.8025 Nov 2024 97,900.101$40.11
short25 Nov 2024 98,649.1025 Nov 2024 98,649.100−$24.32
short26 Nov 2024 93,475.1027 Nov 2024 91,965.201$367.02
short27 Nov 2024 93,940.1027 Nov 2024 93,940.100−$24.33
short28 Nov 2024 96,342.4029 Nov 2024 95,644.001$151.91
short29 Nov 2024 96,821.8029 Nov 2024 96,821.800−$24.41
short1 Dec 2024 96,890.002 Dec 2024 97,185.201−$98.46
short2 Dec 2024 97,372.102 Dec 2024 97,372.100−$24.45
short4 Dec 2024 96,328.905 Dec 2024 98,587.301−$594.16
short6 Dec 2024 97,430.407 Dec 2024 99,740.801−$597.03
short8 Dec 2024 100,331.009 Dec 2024 101,110.001−$210.27
short10 Dec 2024 97,762.9011 Dec 2024 96,593.001$262.71
short11 Dec 2024 98,249.2011 Dec 2024 98,249.200−$24.06
short12 Dec 2024 101,631.0014 Dec 2024 101,424.002$24.77
short14 Dec 2024 102,136.0014 Dec 2024 102,136.000−$24.12
short15 Dec 2024 101,927.0017 Dec 2024 106,059.002−$997.43
short19 Dec 2024 100,705.0020 Dec 2024 97,461.901$741.57
short21 Dec 2024 98,294.5024 Dec 2024 94,881.503$807.68
short24 Dec 2024 98,783.5024 Dec 2024 98,783.500−$24.06
short25 Dec 2024 99,156.9026 Dec 2024 99,429.601−$90.55
short26 Dec 2024 99,650.2026 Dec 2024 99,650.200−$24.26
short27 Dec 2024 96,270.6029 Dec 2024 95,300.002$219.05
short30 Dec 2024 94,206.9031 Dec 2024 92,792.101$338.89
short31 Dec 2024 94,675.6031 Dec 2024 94,675.600−$24.28
short1 Jan 2025 94,043.902 Jan 2025 94,591.801−$165.54
short3 Jan 2025 97,469.704 Jan 2025 98,174.201−$199.18
short5 Jan 2025 98,711.606 Jan 2025 98,363.601$61.03
short6 Jan 2025 99,202.706 Jan 2025 99,202.700−$24.27
short10 Jan 2025 93,015.3014 Jan 2025 94,536.104−$419.36
short15 Jan 2025 97,043.7018 Jan 2025 104,077.003−$1,768.31
short19 Jan 2025 105,079.0020 Jan 2025 101,332.001$818.77
short20 Jan 2025 105,602.0020 Jan 2025 105,602.000−$23.73
short21 Jan 2025 102,771.0022 Jan 2025 106,144.001−$805.49
short23 Jan 2025 104,225.0024 Jan 2025 103,910.001$47.74
short24 Jan 2025 104,744.0024 Jan 2025 104,744.000−$23.72
short26 Jan 2025 105,271.0028 Jan 2025 102,083.002$691.76
short29 Jan 2025 101,842.003 Feb 2025 97,700.605$944.00
short3 Feb 2025 102,349.003 Feb 2025 102,349.000−$23.90
short5 Feb 2025 98,251.9010 Feb 2025 96,462.805$413.56
short11 Feb 2025 97,918.0013 Feb 2025 97,870.002−$12.30
short14 Feb 2025 97,091.2019 Feb 2025 95,671.705$328.66
short20 Feb 2025 97,127.6022 Feb 2025 96,182.002$211.53
long11 Mar 2025 78,202.9013 Mar 2025 83,680.102$1,656.94
long7 Apr 2025 78,037.908 Apr 2025 79,163.201$327.63
long8 Apr 2025 77,645.708 Apr 2025 77,645.700−$24.30
long9 Apr 2025 75,940.8010 Apr 2025 82,615.201$2,127.72
short25 Apr 2025 94,450.4026 Apr 2025 94,638.701−$75.71
short26 Apr 2025 94,920.3026 Apr 2025 94,920.300−$25.41
short27 Apr 2025 95,101.1029 Apr 2025 95,011.202−$1.35
short1 May 2025 95,574.301 May 2025 95,574.300−$25.38
short2 May 2025 96,972.406 May 2025 94,733.704$557.93
short7 May 2025 97,318.208 May 2025 97,030.501$49.71
short8 May 2025 97,802.408 May 2025 97,802.400−$25.52
short9 May 2025 103,778.0011 May 2025 104,810.002−$277.96
short12 May 2025 104,639.0013 May 2025 102,791.001$421.98
short15 May 2025 104,025.0016 May 2025 103,764.001$38.57
short16 May 2025 104,543.0016 May 2025 104,543.000−$25.56
short18 May 2025 103,642.0020 May 2025 105,574.002−$499.66
short21 May 2025 107,384.0022 May 2025 109,644.001−$558.16
short24 May 2025 107,855.0026 May 2025 109,004.002−$293.50
short27 May 2025 109,982.0028 May 2025 108,938.001$213.19
short29 May 2025 108,321.001 Jun 2025 104,592.003$840.99
short3 Jun 2025 106,387.006 Jun 2025 101,509.003$1,138.14
short7 Jun 2025 104,810.0010 Jun 2025 110,263.003−$1,360.62
short15 Jun 2025 105,942.0016 Jun 2025 105,594.001$57.78
short16 Jun 2025 106,469.0016 Jun 2025 106,469.000−$25.43
short17 Jun 2025 107,329.0018 Jun 2025 104,551.001$630.01
short20 Jun 2025 105,182.0022 Jun 2025 102,120.002$716.35
short23 Jun 2025 105,705.0023 Jun 2025 105,705.000−$25.60
short24 Jun 2025 105,861.0025 Jun 2025 106,083.001−$79.55
short25 Jun 2025 106,387.0025 Jun 2025 106,387.000−$25.77
short26 Jun 2025 107,877.0030 Jun 2025 108,357.004−$139.57
short30 Jun 2025 108,414.0030 Jun 2025 108,414.000−$25.74
short2 Jul 2025 106,210.003 Jul 2025 108,850.001−$661.63
short6 Jul 2025 108,739.009 Jul 2025 108,923.003−$68.40
short9 Jul 2025 109,280.009 Jul 2025 109,280.000−$25.54
short10 Jul 2025 111,790.0011 Jul 2025 116,010.001−$984.13
short12 Jul 2025 118,115.0014 Jul 2025 119,087.002−$231.98
short16 Jul 2025 118,347.0017 Jul 2025 118,630.001−$85.20
short17 Jul 2025 118,936.0017 Jul 2025 118,936.000−$25.21
short18 Jul 2025 119,773.0023 Jul 2025 119,954.005−$62.93
short24 Jul 2025 119,350.0025 Jul 2025 118,341.001$186.69
short26 Jul 2025 118,202.0031 Jul 2025 117,840.005$51.80
short31 Jul 2025 118,790.0031 Jul 2025 118,790.000−$25.21
short2 Aug 2025 113,864.004 Aug 2025 114,209.002−$101.02
short4 Aug 2025 114,431.004 Aug 2025 114,431.000−$25.22
short6 Aug 2025 114,700.007 Aug 2025 114,992.001−$88.86
short7 Aug 2025 115,271.007 Aug 2025 115,271.000−$25.18
short9 Aug 2025 117,258.0014 Aug 2025 123,306.005−$1,316.76
short15 Aug 2025 118,887.0019 Aug 2025 116,227.004$528.10
short22 Aug 2025 114,843.0023 Aug 2025 116,936.001−$477.70
short12 Sep 2025 116,060.0013 Sep 2025 116,029.001−$18.17
short15 Sep 2025 116,498.0017 Sep 2025 116,789.002−$86.47
short17 Sep 2025 117,077.0017 Sep 2025 117,077.000−$24.83
short18 Sep 2025 117,030.0025 Sep 2025 113,307.007$760.78
short2 Oct 2025 119,188.007 Oct 2025 124,659.005−$1,166.96
short8 Oct 2025 121,940.0010 Oct 2025 121,662.002$31.32
short10 Oct 2025 122,546.0010 Oct 2025 122,546.000−$24.70
long16 Nov 2025 95,118.3017 Nov 2025 94,261.401−$243.42
long18 Nov 2025 91,754.1020 Nov 2025 91,555.002−$76.92
long20 Nov 2025 91,293.0020 Nov 2025 91,293.000−$24.15
long21 Nov 2025 86,204.0022 Nov 2025 85,129.401−$326.38
long24 Nov 2025 86,395.9025 Nov 2025 88,300.001$508.22
long26 Nov 2025 86,933.1029 Nov 2025 90,890.703$1,081.13
long30 Nov 2025 90,348.401 Dec 2025 90,360.001−$21.41
long1 Dec 2025 89,894.401 Dec 2025 89,894.400−$24.44
long4 Dec 2025 92,962.805 Dec 2025 92,078.101−$258.09
long7 Dec 2025 88,790.608 Dec 2025 90,395.301$417.85
long9 Dec 2025 90,181.2010 Dec 2025 92,678.801$656.14
long11 Dec 2025 91,555.3012 Dec 2025 92,513.401$234.16
long12 Dec 2025 91,095.2012 Dec 2025 91,095.200−$24.63
long13 Dec 2025 89,817.1019 Dec 2025 85,516.406−$1,212.01
long21 Dec 2025 87,919.1023 Dec 2025 88,620.802$170.98
long23 Dec 2025 87,477.3023 Dec 2025 87,477.300−$24.38
long24 Dec 2025 87,048.6026 Dec 2025 87,225.302$25.25
long28 Dec 2025 87,437.6030 Dec 2025 87,237.102−$80.81
long30 Dec 2025 86,998.2030 Dec 2025 86,998.200−$24.42
long31 Dec 2025 88,043.101 Jan 2026 87,648.201−$134.44
long1 Jan 2026 87,600.601 Jan 2026 87,600.600−$24.40
long2 Jan 2026 88,394.803 Jan 2026 89,995.101$418.52
long7 Jan 2026 91,072.108 Jan 2026 91,364.201$54.23
long8 Jan 2026 90,614.408 Jan 2026 90,614.400−$24.46
long9 Jan 2026 90,644.5010 Jan 2026 90,641.301−$25.47
long12 Jan 2026 90,558.6013 Jan 2026 91,296.201$175.58
long21 Jan 2026 87,985.5022 Jan 2026 89,454.701$386.46
long23 Jan 2026 89,111.9024 Jan 2026 89,600.301$110.72
long25 Jan 2026 88,779.2029 Jan 2026 89,300.004$120.39
long29 Jan 2026 88,333.1029 Jan 2026 88,333.100−$24.63
long30 Jan 2026 84,226.901 Feb 2026 78,741.102−$1,637.82
long2 Feb 2026 76,583.404 Feb 2026 75,770.202−$283.01
long5 Feb 2026 72,800.006 Feb 2026 62,909.901−$3,323.98
long7 Feb 2026 70,227.408 Feb 2026 69,289.401−$336.84
long9 Feb 2026 69,978.7010 Feb 2026 70,138.001$29.82
long10 Feb 2026 69,627.1010 Feb 2026 69,627.100−$23.27
long11 Feb 2026 68,497.1012 Feb 2026 67,082.501−$506.22
long13 Feb 2026 65,940.8015 Feb 2026 69,822.902$1,345.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
EMA Mean Reversion Strategy this page—v5 strategy+4.7%2308.9%
MACD Willy Strategy by PtGamblerrma sma ema highest lowest wmav5 strategyNo trades on this market
Bagheri IG Ether by Bagheri_IGema highest lowest rma smav4 strategy+0.0%30.0%
Bagheri IG Ether v2 by Bagheri_IGema highest lowest rma smav4 strategy−0.0%20.0%
Swing Multi Moving Averages Crypto and Stocks Strategy by exlux99ema wma highest lowest smav5 strategy+60.2%2213.1%
VCP pivot buy by perrycc007sma ema rma wma lowest highestv4 strategydoes not run in Zeal Terminal

Upgrade and export

Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.

  • 40 strategy calls use the when = argument (line 112, 113, 115, 116, 117, 119, 120, 121, 122, 124, 125, 126, 127, 129, 130, 131, 132, 134, 135, 136, 140, 141, 143, 144, 145, 147, 148, 149, 150, 152, 153, 154, 155, 157, 158, 159, 160, 162, 163, 164). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
  • No v6-only features. v6 is not needed to run this script.

Clean v5

VerifiedDownload the v5 script

Same 230 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 112: when = long_conditions_met → if block
  • Line 113: when = long_conditions_met → if block
  • Line 115: when = long_conditions_met → if block
  • Line 116: when = long_conditions_met → if block
  • Line 117: when = long_conditions_met → if block
  • Line 119: when = long_conditions_met → if block
34 more edits
  • Line 120: when = long_conditions_met → if block
  • Line 121: when = long_conditions_met → if block
  • Line 122: when = long_conditions_met → if block
  • Line 124: when = close_long_entries → if block
  • Line 125: when = close_long_entries → if block
  • Line 126: when = close_long_entries → if block
  • Line 127: when = close_long_entries → if block
  • Line 129: when = cancel_entries → if block
  • Line 130: when = cancel_entries → if block
  • Line 131: when = cancel_entries → if block
  • Line 132: when = cancel_entries → if block
  • Line 134: when = long_conditions_met → if block
  • Line 135: when = close_long_entries → if block
  • Line 136: when = cancel_entries → if block
  • Line 140: when = short_conditions_met → if block
  • Line 141: when = short_conditions_met → if block
  • Line 143: when = short_conditions_met → if block
  • Line 144: when = short_conditions_met → if block
  • Line 145: when = short_conditions_met → if block
  • Line 147: when = short_conditions_met → if block
  • Line 148: when = short_conditions_met → if block
  • Line 149: when = short_conditions_met → if block
  • Line 150: when = short_conditions_met → if block
  • Line 152: when = close_short_entries → if block
  • Line 153: when = close_short_entries → if block
  • Line 154: when = close_short_entries → if block
  • Line 155: when = close_short_entries → if block
  • Line 157: when = cancel_entries → if block
  • Line 158: when = cancel_entries → if block
  • Line 159: when = cancel_entries → if block
  • Line 160: when = cancel_entries → if block
  • Line 162: when = short_conditions_met → if block
  • Line 163: when = close_short_entries → if block
  • Line 164: when = cancel_entries → if block
⋯    if ladderRungs == 2-        strategy.entry(id="Long Ladder 1", direction=strategy.long, qty=position_qty, limit=long_ladder_1_limit_price, when=long_conditions_met)+        if long_conditions_met+            strategy.entry(id="Long Ladder 1", direction=strategy.long, qty=position_qty, limit=long_ladder_1_limit_price)-        strategy.entry(id="Long Ladder 2", direction=strategy.long, qty=position_qty, limit=long_ladder_2_limit_price, when=long_conditions_met)+        if long_conditions_met+            strategy.entry(id="Long Ladder 2", direction=strategy.long, qty=position_qty, limit=long_ladder_2_limit_price)    else if ladderRungs == 3-        strategy.entry(id="Long Ladder 1", direction=strategy.long, qty=position_qty, limit=long_ladder_1_limit_price, when=long_conditions_met)+        if long_conditions_met+            strategy.entry(id="Long Ladder 1", direction=strategy.long, qty=position_qty, limit=long_ladder_1_limit_price)-        strategy.entry(id="Long Ladder 2", direction=strategy.long, qty=position_qty, limit=long_ladder_2_limit_price, when=long_conditions_met)+        if long_conditions_met+            strategy.entry(id="Long Ladder 2", direction=strategy.long, qty=position_qty, limit=long_ladder_2_limit_price)-        strategy.entry(id="Long Ladder 3", direction=strategy.long, qty=position_qty, limit=long_ladder_3_limit_price, when=long_conditions_met)+        if long_conditions_met+            strategy.entry(id="Long Ladder 3", direction=strategy.long, qty=position_qty, limit=long_ladder_3_limit_price)    else if ladderRungs == 4-        strategy.entry(id="Long Ladder 1", direction=strategy.long, qty=position_qty, limit=long_ladder_1_limit_price, when=long_conditions_met)+        if long_conditions_met+            strategy.entry(id="Long Ladder 1", direction=strategy.long, qty=position_qty, limit=long_ladder_1_limit_price)-        strategy.entry(id="Long Ladder 2", direction=strategy.long, qty=position_qty, limit=long_ladder_2_limit_price, when=long_conditions_met)+        if long_conditions_met+            strategy.entry(id="Long Ladder 2", direction=strategy.long, qty=position_qty, limit=long_ladder_2_limit_price)-        strategy.entry(id="Long Ladder 3", direction=strategy.long, qty=position_qty, limit=long_ladder_3_limit_price, when=long_conditions_met)+        if long_conditions_met+            strategy.entry(id="Long Ladder 3", direction=strategy.long, qty=position_qty, limit=long_ladder_3_limit_price)-        strategy.entry(id="Long Ladder 4", direction=strategy.long, qty=position_qty, limit=long_ladder_4_limit_price, when=long_conditions_met)+        if long_conditions_met+            strategy.entry(id="Long Ladder 4", direction=strategy.long, qty=position_qty, limit=long_ladder_4_limit_price)    -    strategy.exit(id="Close Long Ladder 1", from_entry="Long Ladder 1", stop=long_trailing_stop, limit=long_trailing_stop, when=close_long_entries)+    if close_long_entries+        strategy.exit(id="Close Long Ladder 1", from_entry="Long Ladder 1", stop=long_trailing_stop, limit=long_trailing_stop)-    strategy.exit(id="Close Long Ladder 2", from_entry="Long Ladder 2", stop=long_trailing_stop, limit=long_trailing_stop, when=close_long_entries)+    if close_long_entries+        strategy.exit(id="Close Long Ladder 2", from_entry="Long Ladder 2", stop=long_trailing_stop, limit=long_trailing_stop)-    strategy.exit(id="Close Long Ladder 3", from_entry="Long Ladder 3", stop=long_trailing_stop, limit=long_trailing_stop, when=close_long_entries)+    if close_long_entries+        strategy.exit(id="Close Long Ladder 3", from_entry="Long Ladder 3", stop=long_trailing_stop, limit=long_trailing_stop)-    strategy.exit(id="Close Long Ladder 4", from_entry="Long Ladder 4", stop=long_trailing_stop, limit=long_trailing_stop, when=close_long_entries)+    if close_long_entries+        strategy.exit(id="Close Long Ladder 4", from_entry="Long Ladder 4", stop=long_trailing_stop, limit=long_trailing_stop)    -    strategy.cancel(id="Long Ladder 1", when=cancel_entries)+    if cancel_entries+        strategy.cancel(id="Long Ladder 1")-    strategy.cancel(id="Long Ladder 2", when=cancel_entries)+    if cancel_entries+        strategy.cancel(id="Long Ladder 2")-    strategy.cancel(id="Long Ladder 3", when=cancel_entries)+    if cancel_entries+        strategy.cancel(id="Long Ladder 3")-    strategy.cancel(id="Long Ladder 4", when=cancel_entries)+    if cancel_entries+        strategy.cancel(id="Long Ladder 4")else-    strategy.entry(id="Long", direction=strategy.long, qty=100, when=long_conditions_met)+    if long_conditions_met+        strategy.entry(id="Long", direction=strategy.long, qty=100)-    strategy.exit(id="Close Long", from_entry="Long", stop=long_stop_loss, limit=EMA_, when=close_long_entries)+    if close_long_entries+        strategy.exit(id="Close Long", from_entry="Long", stop=long_stop_loss, limit=EMA_)-    strategy.cancel(id="Long", when=cancel_entries)+    if cancel_entries+        strategy.cancel(id="Long")// Short Entry⋯    if ladderRungs == 2-        strategy.entry(id="Short Ladder 1", direction=strategy.short, qty=position_qty, limit=short_ladder_1_limit_price, when=short_conditions_met)+        if short_conditions_met+            strategy.entry(id="Short Ladder 1", direction=strategy.short, qty=position_qty, limit=short_ladder_1_limit_price)-        strategy.entry(id="Short Ladder 2", direction=strategy.short, qty=position_qty, limit=short_ladder_2_limit_price, when=short_conditions_met)+        if short_conditions_met+            strategy.entry(id="Short Ladder 2", direction=strategy.short, qty=position_qty, limit=short_ladder_2_limit_price)    else if ladderRungs == 3-        strategy.entry(id="Short Ladder 1", direction=strategy.short, qty=position_qty, limit=short_ladder_1_limit_price, when=short_conditions_met)+        if short_conditions_met+            strategy.entry(id="Short Ladder 1", direction=strategy.short, qty=position_qty, limit=short_ladder_1_limit_price)-        strategy.entry(id="Short Ladder 2", direction=strategy.short, qty=position_qty, limit=short_ladder_2_limit_price, when=short_conditions_met)+        if short_conditions_met+            strategy.entry(id="Short Ladder 2", direction=strategy.short, qty=position_qty, limit=short_ladder_2_limit_price)-        strategy.entry(id="Short Ladder 3", direction=strategy.short, qty=position_qty, limit=short_ladder_3_limit_price, when=short_conditions_met)+        if short_conditions_met+            strategy.entry(id="Short Ladder 3", direction=strategy.short, qty=position_qty, limit=short_ladder_3_limit_price)    else if ladderRungs == 4-        strategy.entry(id="Short Ladder 1", direction=strategy.short, qty=position_qty, limit=short_ladder_1_limit_price, when=short_conditions_met)+        if short_conditions_met+            strategy.entry(id="Short Ladder 1", direction=strategy.short, qty=position_qty, limit=short_ladder_1_limit_price)-        strategy.entry(id="Short Ladder 2", direction=strategy.short, qty=position_qty, limit=short_ladder_2_limit_price, when=short_conditions_met)+        if short_conditions_met+            strategy.entry(id="Short Ladder 2", direction=strategy.short, qty=position_qty, limit=short_ladder_2_limit_price)-        strategy.entry(id="Short Ladder 3", direction=strategy.short, qty=position_qty, limit=short_ladder_3_limit_price, when=short_conditions_met)+        if short_conditions_met+            strategy.entry(id="Short Ladder 3", direction=strategy.short, qty=position_qty, limit=short_ladder_3_limit_price)-        strategy.entry(id="Short Ladder 4", direction=strategy.short, qty=position_qty, limit=short_ladder_4_limit_price, when=short_conditions_met)+        if short_conditions_met+            strategy.entry(id="Short Ladder 4", direction=strategy.short, qty=position_qty, limit=short_ladder_4_limit_price)    -    strategy.exit(id="Close Short Ladder 1", from_entry="Short Ladder 1", stop=short_trailing_stop, limit=EMA_, when=close_short_entries)+    if close_short_entries+        strategy.exit(id="Close Short Ladder 1", from_entry="Short Ladder 1", stop=short_trailing_stop, limit=EMA_)-    strategy.exit(id="Close Short Ladder 2", from_entry="Short Ladder 2", stop=short_trailing_stop, limit=EMA_, when=close_short_entries)+    if close_short_entries+        strategy.exit(id="Close Short Ladder 2", from_entry="Short Ladder 2", stop=short_trailing_stop, limit=EMA_)-    strategy.exit(id="Close Short Ladder 3", from_entry="Short Ladder 3", stop=short_trailing_stop, limit=EMA_, when=close_short_entries)+    if close_short_entries+        strategy.exit(id="Close Short Ladder 3", from_entry="Short Ladder 3", stop=short_trailing_stop, limit=EMA_)-    strategy.exit(id="Close Short Ladder 4", from_entry="Short Ladder 4", stop=short_trailing_stop, limit=EMA_, when=close_short_entries)+    if close_short_entries+        strategy.exit(id="Close Short Ladder 4", from_entry="Short Ladder 4", stop=short_trailing_stop, limit=EMA_)    -    strategy.cancel(id="Short Ladder 1", when=cancel_entries)+    if cancel_entries+        strategy.cancel(id="Short Ladder 1")-    strategy.cancel(id="Short Ladder 2", when=cancel_entries)+    if cancel_entries+        strategy.cancel(id="Short Ladder 2")-    strategy.cancel(id="Short Ladder 3", when=cancel_entries)+    if cancel_entries+        strategy.cancel(id="Short Ladder 3")-    strategy.cancel(id="Short Ladder 4", when=cancel_entries)+    if cancel_entries+        strategy.cancel(id="Short Ladder 4")else-    strategy.entry(id="Short", direction=strategy.short, when=short_conditions_met)+    if short_conditions_met+        strategy.entry(id="Short", direction=strategy.short)-    strategy.exit(id="Close Short", from_entry="Short", limit=EMA_, when=close_short_entries)+    if close_short_entries+        strategy.exit(id="Close Short", from_entry="Short", limit=EMA_)-    strategy.cancel(id="Short", when=cancel_entries)+    if cancel_entries+        strategy.cancel(id="Short")

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: 230 before, 230 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.

⋯// © jordanfray-//@version=5+//@version=6strategy(title="EMA Mean Reversion Strategy", overlay=true, max_bars_back=5000, default_qty_type=strategy.percent_of_equity, default_qty_value=100,initial_capital=100000, commission_type=strategy.commission.percent, commission_value=0.05, backtest_fill_limits_assumption=2)⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 62 msWith a workaround 1,000 bars in 116 msNot measured
PineTSNot testedNot testedNot measured

Zeal Terminal's engine currently reads an order side written as direction = strategy.long, direction = strategy.short as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 112, 113, 115, 116, 117, 119 and more) so the engine trades the way the script says. 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.

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/// © jordanfray//@version=5strategy(title="EMA Mean Reversion Strategy", overlay=true, max_bars_back=5000, default_qty_type=strategy.percent_of_equity, default_qty_value=100,initial_capital=100000, commission_type=strategy.commission.percent, commission_value=0.05, backtest_fill_limits_assumption=2)// Indenting Classsindent_1 = " "indent_2 = "  "indent_3 = "   "indent_4 = "    "// TooltipslongEntryToolTip = "When the percentage that the price is away from the selected EMA reaches this point, a long postion will open."shortEntryToolTip = "When the percentage that the price is away from the selected EMA reaches this point, a short postion will open."closeEntryToolTip = "When the percentage that the price is away from the selected EMA reaches this point, open postion will close."ladderInToolTip = "Enable this to use the laddering settings below."cancelEntryToolTip = "When the percentage that the price is away from the selected EMA reaches this point, any unfilled entries will be canceled."// Group Titlesgroup_one_title = "EMA Settings"group_two_title = "Entry Settings"// Colorsblue = color.new(#00A5FF,0)lightBlue = color.new(#00A5FF,90)green = color.new(#2DBD85,0)gray_80 =  color.new(#7F7F7F,80)gray_60 =  color.new(#7F7F7F,60)gray_40 =  color.new(#7F7F7F,40)white = color.new(#ffffff,0)red = color.new(#E02A4A,0)transparent = color.new(#000000,100)// Strategy SettingsEMAtimeframe = input.timeframe(defval="", title="Timeframe", group=group_one_title)EMAlength = input.int(defval=200, minval=1, title="Length", group=group_one_title)EMAtype = input.string(defval="EMA", options = ["EMA", "SMA", "RMA", "WMA"], title="Type", group=group_one_title)EMAsource = input.source(defval=close, title="Source", group=group_one_title)openLongEntryAbove = input.float(defval=9, title="Long Position Entry Trigger", tooltip=longEntryToolTip, group=group_two_title)openEntryEntryAbove = input.float(defval=9, title="Short Position Entry Trigger", tooltip=shortEntryToolTip, group=group_two_title)closeEntryBelow = input.float(defval=1.0, title="Close Position Trigger", tooltip=closeEntryToolTip, group=group_two_title)cancelEntryBelow = input.float(defval=4, title="Cancel Unfilled Entries Trigger", tooltip=cancelEntryToolTip, group=group_two_title)enableLaddering = input.bool(defval=true, title="Ladder Into Positions", tooltip=ladderInToolTip, group=group_two_title)ladderRungs = input.int(defval=4, minval=2, maxval=4, step=1, title=indent_4+"Ladder Rungs", group=group_two_title)ladderStep = input.float(defval=.5, title=indent_4+"Ladder Step (%)", step=.1, group=group_two_title)/100stop_loss_val = input.float(defval=4.0, title="Stop Loss (%)", step=0.1, group=group_two_title)/100start_trailing_after = input.float(defval=1, title="Start Trailing After (%)", step=0.1, group=group_two_title)/100trail_behind = input.float(defval=1, title="Trail Behind (%)", step=0.1, group=group_two_title)/100// Calculate trailing stop valueslong_start_trailing_val = strategy.position_avg_price + (strategy.position_avg_price * start_trailing_after)long_trail_behind_val = close - (strategy.position_avg_price * trail_behind)long_stop_loss = strategy.position_avg_price * (1.0 - stop_loss_val)short_start_trailing_val = strategy.position_avg_price - (strategy.position_avg_price * start_trailing_after)short_trail_behind_val = close + (strategy.position_avg_price * trail_behind)short_stop_loss = strategy.position_avg_price * (1 + stop_loss_val)// Calulate EMAEMA = switch EMAtype    "EMA" => ta.ema(EMAsource, EMAlength)    "SMA" => ta.sma(EMAsource, EMAlength)    "RMA" => ta.rma(EMAsource, EMAlength)    "WMA" => ta.wma(EMAsource, EMAlength)    => naEMA_ = EMAtimeframe == timeframe.period ? EMA : request.security(syminfo.ticker, EMAtimeframe, EMA[1], lookahead = barmerge.lookahead_on)plot(EMA_, title="EMA", linewidth=2, color=blue, editable=true)EMA_cloud_upper_band_val = EMA_ + (EMA_ * openLongEntryAbove/100)EMA_cloud_lower_band_val = EMA_ - (EMA_ * openLongEntryAbove/100)EMA_cloud_upper_band = plot(EMA_cloud_upper_band_val, title="EMA Cloud Upper Band", color=blue)EMA_cloud_lower_band = plot(EMA_cloud_lower_band_val, title="EMA Cloud Upper Band", color=blue)fill(EMA_cloud_upper_band, EMA_cloud_lower_band, editable=false, color=lightBlue)distance_from_EMA = ((close - EMA_)/close)*100if distance_from_EMA < 0    distance_from_EMA := distance_from_EMA * -1// Calulate Ladder Entrieslong_ladder_1_limit_price = close - (close * 1 * ladderStep)long_ladder_2_limit_price = close - (close * 2 * ladderStep)long_ladder_3_limit_price = close - (close * 3 * ladderStep)long_ladder_4_limit_price = close - (close * 4 * ladderStep)short_ladder_1_limit_price = close + (close * 1 * ladderStep)short_ladder_2_limit_price = close + (close * 2 * ladderStep)short_ladder_3_limit_price = close + (close * 3 * ladderStep)short_ladder_4_limit_price = close + (close * 4 * ladderStep)var position_qty = strategy.equity/closeif enableLaddering    position_qty := (strategy.equity/close) / ladderRungselse    position_qty := strategy.equity/close    plot(position_qty, color=white)//plot(strategy.equity, color=green)// Entry Conditionscurrently_in_a_postion = strategy.position_size != 0currently_in_a_long_postion = strategy.position_size > 0currently_in_a_short_postion = strategy.position_size < 0average_price = strategy.position_avg_pricebars_since_entry = currently_in_a_postion ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) + 1 : 5long_run_up = ta.highest(high, bar_index == 0 ? 5000: bars_since_entry)long_run_up_line = plot(long_run_up, style=plot.style_stepline, editable=false, color=currently_in_a_long_postion ? green : transparent)start_trailing_long_entry = currently_in_a_long_postion and long_run_up > long_start_trailing_vallong_trailing_stop = start_trailing_long_entry ? long_run_up - (long_run_up * trail_behind) : long_stop_losslong_trailing_stop_line = plot(long_trailing_stop, style=plot.style_stepline, editable=false, color=currently_in_a_long_postion ? long_trailing_stop > strategy.position_avg_price ? green : red : transparent)short_run_up = ta.lowest(low, bar_index == 0 ? 5000: bars_since_entry)short_run_up_line = plot(short_run_up, style=plot.style_stepline, editable=false, color=currently_in_a_short_postion ? green : transparent)start_trailing_short_entry = currently_in_a_short_postion and short_run_up < short_start_trailing_valshort_trailing_stop = start_trailing_short_entry ? short_run_up + (short_run_up * trail_behind) : short_stop_lossshort_trailing_stop_line = plot(short_trailing_stop, style=plot.style_stepline, editable=false, color=currently_in_a_short_postion ? short_trailing_stop < strategy.position_avg_price ? green : red : transparent)long_conditions_met = distance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postionshort_conditions_met = distance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postionclose_long_entries = distance_from_EMA <= closeEntryBelow or close <= long_trailing_stopclose_short_entries = distance_from_EMA <= closeEntryBelow or close >= short_trailing_stopcancel_entries = distance_from_EMA <= cancelEntryBelowplotshape(long_conditions_met ? close : na, style=shape.diamond, title="Long Conditions Met" )plotshape(short_conditions_met ? close : na, style=shape.diamond, title="Short Conditions Met" )plot(average_price,style=plot.style_stepline, editable=false, color=currently_in_a_postion ? blue : transparent)// Long Entryif enableLaddering    if ladderRungs == 2        strategy.entry(id="Long Ladder 1", direction=strategy.long, qty=position_qty, limit=long_ladder_1_limit_price, when=long_conditions_met)        strategy.entry(id="Long Ladder 2", direction=strategy.long, qty=position_qty, limit=long_ladder_2_limit_price, when=long_conditions_met)    else if ladderRungs == 3        strategy.entry(id="Long Ladder 1", direction=strategy.long, qty=position_qty, limit=long_ladder_1_limit_price, when=long_conditions_met)        strategy.entry(id="Long Ladder 2", direction=strategy.long, qty=position_qty, limit=long_ladder_2_limit_price, when=long_conditions_met)        strategy.entry(id="Long Ladder 3", direction=strategy.long, qty=position_qty, limit=long_ladder_3_limit_price, when=long_conditions_met)    else if ladderRungs == 4        strategy.entry(id="Long Ladder 1", direction=strategy.long, qty=position_qty, limit=long_ladder_1_limit_price, when=long_conditions_met)        strategy.entry(id="Long Ladder 2", direction=strategy.long, qty=position_qty, limit=long_ladder_2_limit_price, when=long_conditions_met)        strategy.entry(id="Long Ladder 3", direction=strategy.long, qty=position_qty, limit=long_ladder_3_limit_price, when=long_conditions_met)        strategy.entry(id="Long Ladder 4", direction=strategy.long, qty=position_qty, limit=long_ladder_4_limit_price, when=long_conditions_met)        strategy.exit(id="Close Long Ladder 1", from_entry="Long Ladder 1", stop=long_trailing_stop, limit=long_trailing_stop, when=close_long_entries)    strategy.exit(id="Close Long Ladder 2", from_entry="Long Ladder 2", stop=long_trailing_stop, limit=long_trailing_stop, when=close_long_entries)    strategy.exit(id="Close Long Ladder 3", from_entry="Long Ladder 3", stop=long_trailing_stop, limit=long_trailing_stop, when=close_long_entries)    strategy.exit(id="Close Long Ladder 4", from_entry="Long Ladder 4", stop=long_trailing_stop, limit=long_trailing_stop, when=close_long_entries)        strategy.cancel(id="Long Ladder 1", when=cancel_entries)    strategy.cancel(id="Long Ladder 2", when=cancel_entries)    strategy.cancel(id="Long Ladder 3", when=cancel_entries)    strategy.cancel(id="Long Ladder 4", when=cancel_entries)else    strategy.entry(id="Long", direction=strategy.long, qty=100, when=long_conditions_met)    strategy.exit(id="Close Long", from_entry="Long", stop=long_stop_loss, limit=EMA_, when=close_long_entries)    strategy.cancel(id="Long", when=cancel_entries)// Short Entryif enableLaddering    if ladderRungs == 2        strategy.entry(id="Short Ladder 1", direction=strategy.short, qty=position_qty, limit=short_ladder_1_limit_price, when=short_conditions_met)        strategy.entry(id="Short Ladder 2", direction=strategy.short, qty=position_qty, limit=short_ladder_2_limit_price, when=short_conditions_met)    else if ladderRungs == 3        strategy.entry(id="Short Ladder 1", direction=strategy.short, qty=position_qty, limit=short_ladder_1_limit_price, when=short_conditions_met)        strategy.entry(id="Short Ladder 2", direction=strategy.short, qty=position_qty, limit=short_ladder_2_limit_price, when=short_conditions_met)        strategy.entry(id="Short Ladder 3", direction=strategy.short, qty=position_qty, limit=short_ladder_3_limit_price, when=short_conditions_met)    else if ladderRungs == 4        strategy.entry(id="Short Ladder 1", direction=strategy.short, qty=position_qty, limit=short_ladder_1_limit_price, when=short_conditions_met)        strategy.entry(id="Short Ladder 2", direction=strategy.short, qty=position_qty, limit=short_ladder_2_limit_price, when=short_conditions_met)        strategy.entry(id="Short Ladder 3", direction=strategy.short, qty=position_qty, limit=short_ladder_3_limit_price, when=short_conditions_met)        strategy.entry(id="Short Ladder 4", direction=strategy.short, qty=position_qty, limit=short_ladder_4_limit_price, when=short_conditions_met)        strategy.exit(id="Close Short Ladder 1", from_entry="Short Ladder 1", stop=short_trailing_stop, limit=EMA_, when=close_short_entries)    strategy.exit(id="Close Short Ladder 2", from_entry="Short Ladder 2", stop=short_trailing_stop, limit=EMA_, when=close_short_entries)    strategy.exit(id="Close Short Ladder 3", from_entry="Short Ladder 3", stop=short_trailing_stop, limit=EMA_, when=close_short_entries)    strategy.exit(id="Close Short Ladder 4", from_entry="Short Ladder 4", stop=short_trailing_stop, limit=EMA_, when=close_short_entries)        strategy.cancel(id="Short Ladder 1", when=cancel_entries)    strategy.cancel(id="Short Ladder 2", when=cancel_entries)    strategy.cancel(id="Short Ladder 3", when=cancel_entries)    strategy.cancel(id="Short Ladder 4", when=cancel_entries)else    strategy.entry(id="Short", direction=strategy.short, when=short_conditions_met)    strategy.exit(id="Close Short", from_entry="Short", limit=EMA_, when=close_short_entries)    strategy.cancel(id="Short", when=cancel_entries)