- Strategy
- Buys when the slope of a 20-bar linear regression of closes rises above 0.05 ATR per bar and the fit's R-squared is at least 0.50, and sells when the slope falls below -0.05 ATR per bar under the same condition. The stop is 1.5 x ATR and the target is 2R.
- Risk model
- 1% of balance per trade (configurable), stop-loss on every order, one position at a time
What it does
This EA fits a straight line through the last 20 closes and looks at two things: how steep the line is and how well it fits. When the slope becomes clearly positive and the closes line up well along it, the EA buys; when it becomes clearly negative with a good fit, it sells.
Measuring the slope in ATR units makes the threshold comparable across symbols and quiet or busy periods. Every trade has an ATR stop-loss, a reward-to-risk target and risk-based sizing.
How it works
On each new bar the EA fits an ordinary least-squares line to the closes of the last 20 completed bars, with time running from oldest to newest.
- Slope: the line's rise per bar, divided by ATR(14) to give "ATRs per bar". A value of 0.05 over 20 bars equals roughly one ATR of movement.
- R-squared: the share of the closes' variation explained by the line, from 0 (no fit) to 1 (a perfect straight line).
- Buy: the normalized slope is above 0.05 on the last closed bar and was at or below 0.05 on the bar before.
- Sell: the slope is below -0.05 and was at or above -0.05 on the bar before.
- Fit filter: R-squared on the signal bar must be at least 0.50; otherwise the signal is logged and skipped.
- Stop-loss: 1.5 x ATR(14). Target: 2R. Exit: by default a long closes when the slope drops below -0.05 (and the mirror for shorts).
How to use it
Regression slope is a statistical way of asking "is price moving steadily in one direction?". It suits liquid markets on H1 and H4 that trend smoothly. A higher R-squared requirement keeps the EA out of jagged, news-driven moves; a higher slope threshold demands faster trends.
If you are new to trend tools, the technical analysis basics lesson gives useful background.
Settings
| Input | Default | What it changes |
|---|---|---|
| Regression length | 20 | Closed bars used for the least-squares fit. |
| Minimum slope per bar (x ATR) | 0.05 | Slope threshold, measured in ATRs per bar. |
| Minimum R-squared | 0.50 | How well the straight line must fit the closes (0 to 1). |
| Stop-loss distance (x ATR) | 1.5 | Stop distance as a multiple of ATR(14). |
| Take-profit (R multiple) | 2.0 | Target as a multiple of the stop distance. |
| Exit on opposite slope | true | Closes when the slope crosses the threshold the other way. |
| Risk per trade (% of balance) | 1.0 | Share of balance risked between entry and stop. |
| Fixed lots | 0 | Fixed volume instead of risk %; 0 = risk-based. |
| Maximum spread (points) | 20 | Skips signals when the spread is wider; 0 = off. |
| Trading start / end hour | 7 / 20 | Server-time window for new trades; equal values = 24 hours. |
| Move stop to break-even | true | Stop to entry + 20 points once profit reaches 1R. |
| Use trailing stop | false | Trails the stop 2 x ATR behind price. |
| Magic number | 2611016 | Identifies this EA's trades. |
Limitations
- The fit looks backward, so the slope turns only after price has already moved.
- A single large bar can steepen the slope while lowering R-squared, so results depend on both settings.
- In ranges the slope hovers near zero and may cross the threshold back and forth.
- The ATR stop does not consider nearby support or resistance.
Before you run it live
Test on a demo account and in the MetaTrader 5 Strategy Tester over several years first. No backtest results are published for this EA, and any test describes the past only.
- Match the spread limit and trading hours to your broker's spreads, stop level and session times, and include commission and swap in tests.
- Start with 1% risk or less and check the lot sizes in the Experts log; the Position Size Calculator is a useful cross-check.
- Use a different magic number on each chart, and keep the terminal (or a VPS) running so stops are managed.
Review how often the R-squared filter rejects signals in the Experts log during a tester run.

