The idea
This is jordanfray's open-source script titled EMA Mean Reversion Strategy. The idea is straightforward: when price has moved far enough from a moving average, the script treats a move back toward that average as a reason to get in. Distance is measured as a percent gap between the close and the average. By default, a stretch beyond 9% with the close already below the average sets up a long, and the mirror case above the average sets up a short. Laddering is on by default, so the position is split into limit orders instead of being filled all at once at the market. The publication points to a separate EMA Price Distance Tracker indicator; the strategy itself already computes the distance in its source.
The author's publication chart is BINANCE:BTCUSDT on the daily timeframe, so this entry is filed under BTC. The script is not hard-coded to one symbol; it will load on other charts, but the write-up and the image follow that publication chart. The moving-average timeframe input is blank by default, which means the chart's own timeframe. On the author's daily chart, that is a 200-period average of daily closes.

How the indicators are calculated
- Average: EMA, SMA, RMA, or WMA, default EMA, length 200, source close. If Timeframe is left blank (the default), it is computed on the chart timeframe. If you set a different timeframe, the script pulls it with request.security and lookahead = barmerge.lookahead_on, which can use values from a higher-timeframe bar that has not closed yet. Know that before you change the setting.
- Distance: distance_from_EMA = abs((close − EMA_) / close × 100). The divisor is the close, not the average. The distance is always positive; side is decided by whether the close is below or above the average.
- The cloud bands on the chart are both drawn from Long Position Entry Trigger (default 9%). The short trigger is not used for the upper band.
- Ladder limits (on by default): from the current close, orders are placed 1 to 4 steps outward at Ladder Step (default 0.5%). Longs are limited 0.5%, 1.0%, 1.5%, and 2.0% below the close; shorts are placed the same way above it.
- The strategy uses 100% of equity in its declaration, a 0.05 percent commission, and backtest_fill_limits_assumption=2. With laddering on, each rung's size is (strategy.equity / close) / ladderRungs, so four filled rungs use about the full equity.
Entry rules
- Long: distance is greater than Long Position Entry Trigger (default 9%), the close is below the average, and there is no open position. By default the script places four limit orders named Long Ladder 1 through 4.
- Short: distance is greater than Short Position Entry Trigger (also 9% by default; the variable is named openEntryEntryAbove), the close is above the average, and there is no open position. Four Short Ladder limits are placed in the default setup.
- No new group of entries is placed while a position is already open. Unfilled limit orders are cancelled with strategy.cancel when distance shrinks to Cancel Unfilled Entries Trigger (default 4%) or less.
- If you turn Ladder Into Positions off, the long branch uses qty=100, i.e. a fixed 100 units, not a fraction of equity. The non-ladder short branch does not set qty, so it falls back to the strategy declaration. Laddering is on by default, and that is the mode that matches the author's chart.
- The source uses the older when= order form and does not wait for barstate.isconfirmed. Whether a limit is placed or filled can still change while the bar is open, and it also depends on how the tester fills limit orders.
Exits and position size
- The trailing stop starts as a fixed stop from the average entry price: average × (1 − 4%) for longs and average × (1 + 4%) for shorts, with 4% from the Stop Loss (%) input. Once the run-up high (long) or run-up low (short) since entry has moved 1% in the trade's favor, the stop starts trailing: 1% below that high for longs, or 1% above that low for shorts. Those 1% figures are Start Trailing After and Trail Behind.
- The close flags close_long_entries and close_short_entries fire when distance has shrunk to Close Position Trigger (default 1%) or less, or when the close has reached that trailing-stop price.
- In the default laddered mode, each long exit sets both stop and limit to long_trailing_stop. Each short exit sets stop=short_trailing_stop and limit=EMA_. The two sides are not symmetrical.
- With laddering off, the long exit uses stop=long_stop_loss and limit=EMA_. The short exit in that branch only sets limit=EMA_ and does not set a stop. Check the short-side exit before you turn laddering off.
- If all four default rungs fill, the notional is close to full equity. That is a large position; change it to a size you can accept before you use it.
Inputs (source defaults)
| Input | Default | Role |
|---|---|---|
| Timeframe | blank (chart timeframe) | Timeframe for the average. Blank uses the chart; a higher timeframe uses request.security with lookahead_on. |
| Length / Type / Source | 200 / EMA / close | Length, type, and source of the average. |
| Long Position Entry Trigger | 9 | Minimum percent stretch for a long, with the close below the average. Also used for the cloud width. |
| Short Position Entry Trigger | 9 | Minimum percent stretch for a short, with the close above the average. |
| Close Position Trigger | 1.0 | When distance falls to this percent or less, the script is ready to exit. |
| Cancel Unfilled Entries Trigger | 4 | When distance falls to this percent or less, unfilled limits are cancelled. |
| Ladder Into Positions | true | Whether to split the position into limit rungs. |
| Ladder Rungs / Ladder Step (%) | 4 / 0.5 | Number of rungs (2 to 4) and the gap from the close at each step. |
| Stop Loss (%) | 4.0 | Fixed stop from average entry price before trailing starts. |
| Start Trailing After (%) / Trail Behind (%) | 1 / 1 | Favorable move required to start trailing, and how far the trail stays behind the run-up. |
Things to know before using it
- The distance formula divides by the close. When price is far from the average, that percent is not the same as a percent gap measured from the average itself.
- Default size is heavy: four filled rungs come close to a full-equity position. With laddering off, the long branch uses a fixed 100 units, which is not the same math as the percent-of-equity declaration.
- Long and short exits are not symmetrical. In laddered mode the long limit is the trailing stop, while the short limit is the average. With laddering off, the short branch has no stop.
- If you move the average to a higher timeframe, lookahead_on can let it use data from a bar that has not closed, which may not match what you see after that bar settles.
- The script plots position size as a white line, along with the trailing stop and average entry. That is display only. Some variable names and comments are misspelled (for example postion, Calulate); that does not change the logic as written.
Installing it on TradingView
- Download the .pine file from Coinpred (active membership required, see below), or check the original page for a newer version.
- Open the Pine Editor, create a blank script, paste the full source, and save.
- Click Add to chart. To match the author image, start with BINANCE:BTCUSDT on the daily timeframe.
- Review the stretch thresholds, laddering, stop and trail settings, and the actual order size in the strategy properties, then inspect limit fills in the Strategy Tester.
- Past tester results do not guarantee future returns.
About backtest figures
Source, license and version
- Source: https://www.tradingview.com/script/Xt7AXMUD-EMA-Mean-Reversion-Strategy/
- Author: jordanfray. Source version 1.0 (Pine v5), scriptAccess=open_no_auth.
- License: Mozilla Public License 2.0. Redistribution must keep the copyright and license notices; modified files must remain under MPL 2.0. Coinpred ships the unmodified full source.
- Members can download the .pine file on this page to study the rules. Judge the risk yourself, and follow your venue's rules.



