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
- 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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| EMA Settings | |||
| Timeframe | | timeframe | EMA_ |
| Length | 200 | int · min 1 | EMA |
| Type | EMA | string · options ["EMA", "SMA", "RMA", "WMA"] | EMA |
| Source | close | source | EMA |
| 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. | 9 | float | EMA_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. | 9 | float | short_conditions_met |
| Close Position TriggerWhen the percentage that the price is away from the selected EMA reaches this point, open postion will close. | 1.0 | float | close_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. | 4 | float | cancel_entries |
| Ladder Into PositionsEnable this to use the laddering settings below. | true | bool | exit 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" | 4 | int · min 2 · max 4 · step 1 | position_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 (%)" | .5 | float · step .1 | long_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.0 | float · step 0.1 | long_stop_loss short_stop_loss |
| Start Trailing After (%) | 1 | float · step 0.1 | long_start_trailing_val short_start_trailing_val |
| Trail Behind (%) | 1 | float · step 0.1 | long_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 Type
EMA · line 52 - Highesthigh, bar_index == 0 ? 5000: bars_since_entry
long_run_up · line 91 - Lowestlow, bar_index == 0 ? 5000: bars_since_entry
short_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
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long Ladder 1 long | long_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postion | On | 112 |
| Open Long Ladder 2 long | long_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postion | On | 113 |
| Open Long Ladder 1 long | long_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postion | On | 115 |
| Open Long Ladder 2 long | long_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postion | On | 116 |
| Open Long Ladder 3 long | long_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postion | On | 117 |
| Open Long Ladder 1 long | long_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postion | On | 119 |
| Open Long Ladder 2 long | long_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postion | On | 120 |
| Open Long Ladder 3 long | long_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postion | On | 121 |
| Open Long Ladder 4 long | long_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postion | On | 122 |
| Exit Close Long Ladder 1 stop and limit | close_long_entriesdistance_from_EMA <= closeEntryBelow or close <= long_trailing_stop | On “Ladder Into Positions” | 124 |
| Exit Close Long Ladder 2 stop and limit | close_long_entriesdistance_from_EMA <= closeEntryBelow or close <= long_trailing_stop | On “Ladder Into Positions” | 125 |
| Exit Close Long Ladder 3 stop and limit | close_long_entriesdistance_from_EMA <= closeEntryBelow or close <= long_trailing_stop | On “Ladder Into Positions” | 126 |
| Exit Close Long Ladder 4 stop and limit | close_long_entriesdistance_from_EMA <= closeEntryBelow or close <= long_trailing_stop | On “Ladder Into Positions” | 127 |
| cancel Long Ladder 1 | cancel_entriesdistance_from_EMA <= cancelEntryBelow | On “Ladder Into Positions” | 129 |
| cancel Long Ladder 2 | cancel_entriesdistance_from_EMA <= cancelEntryBelow | On “Ladder Into Positions” | 130 |
| cancel Long Ladder 3 | cancel_entriesdistance_from_EMA <= cancelEntryBelow | On “Ladder Into Positions” | 131 |
| cancel Long Ladder 4 | cancel_entriesdistance_from_EMA <= cancelEntryBelow | On “Ladder Into Positions” | 132 |
| Open Long long | long_conditions_metdistance_from_EMA > openLongEntryAbove and close < EMA_ and not currently_in_a_postion | On “Ladder Into Positions” | 134 |
| Exit Close Long stop and limit | close_long_entriesdistance_from_EMA <= closeEntryBelow or close <= long_trailing_stop | On “Ladder Into Positions” | 135 |
| cancel Long | cancel_entriesdistance_from_EMA <= cancelEntryBelow | On “Ladder Into Positions” | 136 |
| Open Short Ladder 1 short | short_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postion | On | 140 |
| Open Short Ladder 2 short | short_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postion | On | 141 |
| Open Short Ladder 1 short | short_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postion | On | 143 |
| Open Short Ladder 2 short | short_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postion | On | 144 |
| Open Short Ladder 3 short | short_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postion | On | 145 |
| Open Short Ladder 1 short | short_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postion | On | 147 |
| Open Short Ladder 2 short | short_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postion | On | 148 |
| Open Short Ladder 3 short | short_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postion | On | 149 |
| Open Short Ladder 4 short | short_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postion | On | 150 |
| Exit Close Short Ladder 1 stop and limit | close_short_entriesdistance_from_EMA <= closeEntryBelow or close >= short_trailing_stop | On “Ladder Into Positions” | 152 |
| Exit Close Short Ladder 2 stop and limit | close_short_entriesdistance_from_EMA <= closeEntryBelow or close >= short_trailing_stop | On “Ladder Into Positions” | 153 |
| Exit Close Short Ladder 3 stop and limit | close_short_entriesdistance_from_EMA <= closeEntryBelow or close >= short_trailing_stop | On “Ladder Into Positions” | 154 |
| Exit Close Short Ladder 4 stop and limit | close_short_entriesdistance_from_EMA <= closeEntryBelow or close >= short_trailing_stop | On “Ladder Into Positions” | 155 |
| cancel Short Ladder 1 | cancel_entriesdistance_from_EMA <= cancelEntryBelow | On “Ladder Into Positions” | 157 |
| cancel Short Ladder 2 | cancel_entriesdistance_from_EMA <= cancelEntryBelow | On “Ladder Into Positions” | 158 |
| cancel Short Ladder 3 | cancel_entriesdistance_from_EMA <= cancelEntryBelow | On “Ladder Into Positions” | 159 |
| cancel Short Ladder 4 | cancel_entriesdistance_from_EMA <= cancelEntryBelow | On “Ladder Into Positions” | 160 |
| Open Short short | short_conditions_metdistance_from_EMA > openEntryEntryAbove and close > EMA_ and not currently_in_a_postion | On “Ladder Into Positions” | 162 |
| Exit Close Short limit | close_short_entriesdistance_from_EMA <= closeEntryBelow or close >= short_trailing_stop | On “Ladder Into Positions” | 163 |
| cancel Short | cancel_entriesdistance_from_EMA <= cancelEntryBelow | On “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.
- 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.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +4.7% | +102.5% | 230 | 40% | 1.11 | 8.9% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.3% | +5.5% | 46 | 35% | 0.91 | 3.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +6.0% | +9.8% | 334 | 41% | 1.07 | 20.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.5% | +8.4% | 56 | 45% | 1.06 | 5.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −18.7% | +16.7% | 326 | 34% | 0.80 | 21.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −2.5% | +21.5% | 68 | 43% | 0.84 | 9.6% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +4.2% | +148.3% | 195 | 42% | 1.10 | 11.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.7% | +17.1% | 30 | 40% | 0.84 | 3.1% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 86 | 36% | −$3,881.08 |
| Sideways market | 59 | 42% | $818.67 |
| Falling market | 85 | 41% | $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.
The first 150 of 230 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 6 Sep 2024 55,899.10 | 7 Sep 2024 53,963.00 | 1 | −$886.02 |
| long | 8 Sep 2024 53,890.10 | 10 Sep 2024 57,042.00 | 2 | $1,416.64 |
| short | 20 Oct 2024 68,719.90 | 23 Oct 2024 67,426.00 | 3 | $450.55 |
| short | 28 Oct 2024 69,061.80 | 28 Oct 2024 69,061.80 | 0 | −$25.38 |
| short | 29 Oct 2024 70,312.00 | 4 Nov 2024 68,776.00 | 6 | $529.04 |
| short | 6 Nov 2024 69,718.90 | 7 Nov 2024 75,572.00 | 1 | −$2,167.21 |
| short | 8 Nov 2024 76,237.20 | 11 Nov 2024 80,370.00 | 3 | −$1,378.37 |
| short | 12 Nov 2024 89,091.20 | 13 Nov 2024 87,952.00 | 1 | $290.21 |
| short | 13 Nov 2024 89,534.50 | 13 Nov 2024 89,534.50 | 0 | −$24.73 |
| short | 14 Nov 2024 90,827.10 | 16 Nov 2024 91,032.10 | 2 | −$80.39 |
| short | 16 Nov 2024 91,279.00 | 16 Nov 2024 91,279.00 | 0 | −$24.80 |
| short | 17 Nov 2024 91,039.90 | 18 Nov 2024 89,856.00 | 1 | $296.03 |
| short | 18 Nov 2024 91,492.80 | 18 Nov 2024 91,492.80 | 0 | −$24.77 |
| short | 19 Nov 2024 90,916.40 | 22 Nov 2024 98,317.10 | 3 | −$2,037.61 |
| short | 24 Nov 2024 98,160.80 | 25 Nov 2024 97,900.10 | 1 | $40.11 |
| short | 25 Nov 2024 98,649.10 | 25 Nov 2024 98,649.10 | 0 | −$24.32 |
| short | 26 Nov 2024 93,475.10 | 27 Nov 2024 91,965.20 | 1 | $367.02 |
| short | 27 Nov 2024 93,940.10 | 27 Nov 2024 93,940.10 | 0 | −$24.33 |
| short | 28 Nov 2024 96,342.40 | 29 Nov 2024 95,644.00 | 1 | $151.91 |
| short | 29 Nov 2024 96,821.80 | 29 Nov 2024 96,821.80 | 0 | −$24.41 |
| short | 1 Dec 2024 96,890.00 | 2 Dec 2024 97,185.20 | 1 | −$98.46 |
| short | 2 Dec 2024 97,372.10 | 2 Dec 2024 97,372.10 | 0 | −$24.45 |
| short | 4 Dec 2024 96,328.90 | 5 Dec 2024 98,587.30 | 1 | −$594.16 |
| short | 6 Dec 2024 97,430.40 | 7 Dec 2024 99,740.80 | 1 | −$597.03 |
| short | 8 Dec 2024 100,331.00 | 9 Dec 2024 101,110.00 | 1 | −$210.27 |
| short | 10 Dec 2024 97,762.90 | 11 Dec 2024 96,593.00 | 1 | $262.71 |
| short | 11 Dec 2024 98,249.20 | 11 Dec 2024 98,249.20 | 0 | −$24.06 |
| short | 12 Dec 2024 101,631.00 | 14 Dec 2024 101,424.00 | 2 | $24.77 |
| short | 14 Dec 2024 102,136.00 | 14 Dec 2024 102,136.00 | 0 | −$24.12 |
| short | 15 Dec 2024 101,927.00 | 17 Dec 2024 106,059.00 | 2 | −$997.43 |
| short | 19 Dec 2024 100,705.00 | 20 Dec 2024 97,461.90 | 1 | $741.57 |
| short | 21 Dec 2024 98,294.50 | 24 Dec 2024 94,881.50 | 3 | $807.68 |
| short | 24 Dec 2024 98,783.50 | 24 Dec 2024 98,783.50 | 0 | −$24.06 |
| short | 25 Dec 2024 99,156.90 | 26 Dec 2024 99,429.60 | 1 | −$90.55 |
| short | 26 Dec 2024 99,650.20 | 26 Dec 2024 99,650.20 | 0 | −$24.26 |
| short | 27 Dec 2024 96,270.60 | 29 Dec 2024 95,300.00 | 2 | $219.05 |
| short | 30 Dec 2024 94,206.90 | 31 Dec 2024 92,792.10 | 1 | $338.89 |
| short | 31 Dec 2024 94,675.60 | 31 Dec 2024 94,675.60 | 0 | −$24.28 |
| short | 1 Jan 2025 94,043.90 | 2 Jan 2025 94,591.80 | 1 | −$165.54 |
| short | 3 Jan 2025 97,469.70 | 4 Jan 2025 98,174.20 | 1 | −$199.18 |
| short | 5 Jan 2025 98,711.60 | 6 Jan 2025 98,363.60 | 1 | $61.03 |
| short | 6 Jan 2025 99,202.70 | 6 Jan 2025 99,202.70 | 0 | −$24.27 |
| short | 10 Jan 2025 93,015.30 | 14 Jan 2025 94,536.10 | 4 | −$419.36 |
| short | 15 Jan 2025 97,043.70 | 18 Jan 2025 104,077.00 | 3 | −$1,768.31 |
| short | 19 Jan 2025 105,079.00 | 20 Jan 2025 101,332.00 | 1 | $818.77 |
| short | 20 Jan 2025 105,602.00 | 20 Jan 2025 105,602.00 | 0 | −$23.73 |
| short | 21 Jan 2025 102,771.00 | 22 Jan 2025 106,144.00 | 1 | −$805.49 |
| short | 23 Jan 2025 104,225.00 | 24 Jan 2025 103,910.00 | 1 | $47.74 |
| short | 24 Jan 2025 104,744.00 | 24 Jan 2025 104,744.00 | 0 | −$23.72 |
| short | 26 Jan 2025 105,271.00 | 28 Jan 2025 102,083.00 | 2 | $691.76 |
| short | 29 Jan 2025 101,842.00 | 3 Feb 2025 97,700.60 | 5 | $944.00 |
| short | 3 Feb 2025 102,349.00 | 3 Feb 2025 102,349.00 | 0 | −$23.90 |
| short | 5 Feb 2025 98,251.90 | 10 Feb 2025 96,462.80 | 5 | $413.56 |
| short | 11 Feb 2025 97,918.00 | 13 Feb 2025 97,870.00 | 2 | −$12.30 |
| short | 14 Feb 2025 97,091.20 | 19 Feb 2025 95,671.70 | 5 | $328.66 |
| short | 20 Feb 2025 97,127.60 | 22 Feb 2025 96,182.00 | 2 | $211.53 |
| long | 11 Mar 2025 78,202.90 | 13 Mar 2025 83,680.10 | 2 | $1,656.94 |
| long | 7 Apr 2025 78,037.90 | 8 Apr 2025 79,163.20 | 1 | $327.63 |
| long | 8 Apr 2025 77,645.70 | 8 Apr 2025 77,645.70 | 0 | −$24.30 |
| long | 9 Apr 2025 75,940.80 | 10 Apr 2025 82,615.20 | 1 | $2,127.72 |
| short | 25 Apr 2025 94,450.40 | 26 Apr 2025 94,638.70 | 1 | −$75.71 |
| short | 26 Apr 2025 94,920.30 | 26 Apr 2025 94,920.30 | 0 | −$25.41 |
| short | 27 Apr 2025 95,101.10 | 29 Apr 2025 95,011.20 | 2 | −$1.35 |
| short | 1 May 2025 95,574.30 | 1 May 2025 95,574.30 | 0 | −$25.38 |
| short | 2 May 2025 96,972.40 | 6 May 2025 94,733.70 | 4 | $557.93 |
| short | 7 May 2025 97,318.20 | 8 May 2025 97,030.50 | 1 | $49.71 |
| short | 8 May 2025 97,802.40 | 8 May 2025 97,802.40 | 0 | −$25.52 |
| short | 9 May 2025 103,778.00 | 11 May 2025 104,810.00 | 2 | −$277.96 |
| short | 12 May 2025 104,639.00 | 13 May 2025 102,791.00 | 1 | $421.98 |
| short | 15 May 2025 104,025.00 | 16 May 2025 103,764.00 | 1 | $38.57 |
| short | 16 May 2025 104,543.00 | 16 May 2025 104,543.00 | 0 | −$25.56 |
| short | 18 May 2025 103,642.00 | 20 May 2025 105,574.00 | 2 | −$499.66 |
| short | 21 May 2025 107,384.00 | 22 May 2025 109,644.00 | 1 | −$558.16 |
| short | 24 May 2025 107,855.00 | 26 May 2025 109,004.00 | 2 | −$293.50 |
| short | 27 May 2025 109,982.00 | 28 May 2025 108,938.00 | 1 | $213.19 |
| short | 29 May 2025 108,321.00 | 1 Jun 2025 104,592.00 | 3 | $840.99 |
| short | 3 Jun 2025 106,387.00 | 6 Jun 2025 101,509.00 | 3 | $1,138.14 |
| short | 7 Jun 2025 104,810.00 | 10 Jun 2025 110,263.00 | 3 | −$1,360.62 |
| short | 15 Jun 2025 105,942.00 | 16 Jun 2025 105,594.00 | 1 | $57.78 |
| short | 16 Jun 2025 106,469.00 | 16 Jun 2025 106,469.00 | 0 | −$25.43 |
| short | 17 Jun 2025 107,329.00 | 18 Jun 2025 104,551.00 | 1 | $630.01 |
| short | 20 Jun 2025 105,182.00 | 22 Jun 2025 102,120.00 | 2 | $716.35 |
| short | 23 Jun 2025 105,705.00 | 23 Jun 2025 105,705.00 | 0 | −$25.60 |
| short | 24 Jun 2025 105,861.00 | 25 Jun 2025 106,083.00 | 1 | −$79.55 |
| short | 25 Jun 2025 106,387.00 | 25 Jun 2025 106,387.00 | 0 | −$25.77 |
| short | 26 Jun 2025 107,877.00 | 30 Jun 2025 108,357.00 | 4 | −$139.57 |
| short | 30 Jun 2025 108,414.00 | 30 Jun 2025 108,414.00 | 0 | −$25.74 |
| short | 2 Jul 2025 106,210.00 | 3 Jul 2025 108,850.00 | 1 | −$661.63 |
| short | 6 Jul 2025 108,739.00 | 9 Jul 2025 108,923.00 | 3 | −$68.40 |
| short | 9 Jul 2025 109,280.00 | 9 Jul 2025 109,280.00 | 0 | −$25.54 |
| short | 10 Jul 2025 111,790.00 | 11 Jul 2025 116,010.00 | 1 | −$984.13 |
| short | 12 Jul 2025 118,115.00 | 14 Jul 2025 119,087.00 | 2 | −$231.98 |
| short | 16 Jul 2025 118,347.00 | 17 Jul 2025 118,630.00 | 1 | −$85.20 |
| short | 17 Jul 2025 118,936.00 | 17 Jul 2025 118,936.00 | 0 | −$25.21 |
| short | 18 Jul 2025 119,773.00 | 23 Jul 2025 119,954.00 | 5 | −$62.93 |
| short | 24 Jul 2025 119,350.00 | 25 Jul 2025 118,341.00 | 1 | $186.69 |
| short | 26 Jul 2025 118,202.00 | 31 Jul 2025 117,840.00 | 5 | $51.80 |
| short | 31 Jul 2025 118,790.00 | 31 Jul 2025 118,790.00 | 0 | −$25.21 |
| short | 2 Aug 2025 113,864.00 | 4 Aug 2025 114,209.00 | 2 | −$101.02 |
| short | 4 Aug 2025 114,431.00 | 4 Aug 2025 114,431.00 | 0 | −$25.22 |
| short | 6 Aug 2025 114,700.00 | 7 Aug 2025 114,992.00 | 1 | −$88.86 |
| short | 7 Aug 2025 115,271.00 | 7 Aug 2025 115,271.00 | 0 | −$25.18 |
| short | 9 Aug 2025 117,258.00 | 14 Aug 2025 123,306.00 | 5 | −$1,316.76 |
| short | 15 Aug 2025 118,887.00 | 19 Aug 2025 116,227.00 | 4 | $528.10 |
| short | 22 Aug 2025 114,843.00 | 23 Aug 2025 116,936.00 | 1 | −$477.70 |
| short | 12 Sep 2025 116,060.00 | 13 Sep 2025 116,029.00 | 1 | −$18.17 |
| short | 15 Sep 2025 116,498.00 | 17 Sep 2025 116,789.00 | 2 | −$86.47 |
| short | 17 Sep 2025 117,077.00 | 17 Sep 2025 117,077.00 | 0 | −$24.83 |
| short | 18 Sep 2025 117,030.00 | 25 Sep 2025 113,307.00 | 7 | $760.78 |
| short | 2 Oct 2025 119,188.00 | 7 Oct 2025 124,659.00 | 5 | −$1,166.96 |
| short | 8 Oct 2025 121,940.00 | 10 Oct 2025 121,662.00 | 2 | $31.32 |
| short | 10 Oct 2025 122,546.00 | 10 Oct 2025 122,546.00 | 0 | −$24.70 |
| long | 16 Nov 2025 95,118.30 | 17 Nov 2025 94,261.40 | 1 | −$243.42 |
| long | 18 Nov 2025 91,754.10 | 20 Nov 2025 91,555.00 | 2 | −$76.92 |
| long | 20 Nov 2025 91,293.00 | 20 Nov 2025 91,293.00 | 0 | −$24.15 |
| long | 21 Nov 2025 86,204.00 | 22 Nov 2025 85,129.40 | 1 | −$326.38 |
| long | 24 Nov 2025 86,395.90 | 25 Nov 2025 88,300.00 | 1 | $508.22 |
| long | 26 Nov 2025 86,933.10 | 29 Nov 2025 90,890.70 | 3 | $1,081.13 |
| long | 30 Nov 2025 90,348.40 | 1 Dec 2025 90,360.00 | 1 | −$21.41 |
| long | 1 Dec 2025 89,894.40 | 1 Dec 2025 89,894.40 | 0 | −$24.44 |
| long | 4 Dec 2025 92,962.80 | 5 Dec 2025 92,078.10 | 1 | −$258.09 |
| long | 7 Dec 2025 88,790.60 | 8 Dec 2025 90,395.30 | 1 | $417.85 |
| long | 9 Dec 2025 90,181.20 | 10 Dec 2025 92,678.80 | 1 | $656.14 |
| long | 11 Dec 2025 91,555.30 | 12 Dec 2025 92,513.40 | 1 | $234.16 |
| long | 12 Dec 2025 91,095.20 | 12 Dec 2025 91,095.20 | 0 | −$24.63 |
| long | 13 Dec 2025 89,817.10 | 19 Dec 2025 85,516.40 | 6 | −$1,212.01 |
| long | 21 Dec 2025 87,919.10 | 23 Dec 2025 88,620.80 | 2 | $170.98 |
| long | 23 Dec 2025 87,477.30 | 23 Dec 2025 87,477.30 | 0 | −$24.38 |
| long | 24 Dec 2025 87,048.60 | 26 Dec 2025 87,225.30 | 2 | $25.25 |
| long | 28 Dec 2025 87,437.60 | 30 Dec 2025 87,237.10 | 2 | −$80.81 |
| long | 30 Dec 2025 86,998.20 | 30 Dec 2025 86,998.20 | 0 | −$24.42 |
| long | 31 Dec 2025 88,043.10 | 1 Jan 2026 87,648.20 | 1 | −$134.44 |
| long | 1 Jan 2026 87,600.60 | 1 Jan 2026 87,600.60 | 0 | −$24.40 |
| long | 2 Jan 2026 88,394.80 | 3 Jan 2026 89,995.10 | 1 | $418.52 |
| long | 7 Jan 2026 91,072.10 | 8 Jan 2026 91,364.20 | 1 | $54.23 |
| long | 8 Jan 2026 90,614.40 | 8 Jan 2026 90,614.40 | 0 | −$24.46 |
| long | 9 Jan 2026 90,644.50 | 10 Jan 2026 90,641.30 | 1 | −$25.47 |
| long | 12 Jan 2026 90,558.60 | 13 Jan 2026 91,296.20 | 1 | $175.58 |
| long | 21 Jan 2026 87,985.50 | 22 Jan 2026 89,454.70 | 1 | $386.46 |
| long | 23 Jan 2026 89,111.90 | 24 Jan 2026 89,600.30 | 1 | $110.72 |
| long | 25 Jan 2026 88,779.20 | 29 Jan 2026 89,300.00 | 4 | $120.39 |
| long | 29 Jan 2026 88,333.10 | 29 Jan 2026 88,333.10 | 0 | −$24.63 |
| long | 30 Jan 2026 84,226.90 | 1 Feb 2026 78,741.10 | 2 | −$1,637.82 |
| long | 2 Feb 2026 76,583.40 | 4 Feb 2026 75,770.20 | 2 | −$283.01 |
| long | 5 Feb 2026 72,800.00 | 6 Feb 2026 62,909.90 | 1 | −$3,323.98 |
| long | 7 Feb 2026 70,227.40 | 8 Feb 2026 69,289.40 | 1 | −$336.84 |
| long | 9 Feb 2026 69,978.70 | 10 Feb 2026 70,138.00 | 1 | $29.82 |
| long | 10 Feb 2026 69,627.10 | 10 Feb 2026 69,627.10 | 0 | −$23.27 |
| long | 11 Feb 2026 68,497.10 | 12 Feb 2026 67,082.50 | 1 | −$506.22 |
| long | 13 Feb 2026 65,940.80 | 15 Feb 2026 69,822.90 | 2 | $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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| EMA Mean Reversion Strategy this page | — | v5 strategy | +4.7% | 230 | 8.9% |
| MACD Willy Strategy by PtGambler | rma sma ema highest lowest wma | v5 strategy | No trades on this market | ||
| Bagheri IG Ether by Bagheri_IG | ema highest lowest rma sma | v4 strategy | +0.0% | 3 | 0.0% |
| Bagheri IG Ether v2 by Bagheri_IG | ema highest lowest rma sma | v4 strategy | −0.0% | 2 | 0.0% |
| Swing Multi Moving Averages Crypto and Stocks Strategy by exlux99 | ema wma highest lowest sma | v5 strategy | +60.2% | 22 | 13.1% |
| VCP pivot buy by perrycc007 | sma ema rma wma lowest highest | v4 strategy | does 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.
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
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 62 ms | With a workaround 1,000 bars in 116 ms | Not measured |
| PineTS | Not tested | Not tested | Not 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)