Stochastic Crossover EA MT5 — No Code

Build a Stochastic K/D crossover EA: parameters 14/3/80/20, Cross conditions, all UI condition types, export MQL5, Every tick backtest.

📖 10 min read

📝 1,953 words

🏷️ MQL5 and Expert Advisors

Share this article:

Stochastic Crossover EA for MT5 — No Code

Can I build a Stochastic crossover EA for MT5 without coding?

Yes. Add Stochastic on Step 3 — Entry rules in AlfaTactix Strategy Builder. Buy rule: Stoch crosses above Stoch %D ( %K crosses above %D ). Sell rule: crosses below (or switch on Sell mirrors Buy). Keep %K length 14, %K smoothing 3, %D length 3, Overbought level 80, Oversold level 20. Add a Stop loss in Step 5, press Save strategy in Step 7, then Get the code, compile once, backtest H1 EURUSD with Every tick in the Strategy Tester.

Disclosure: Educational build guide — not financial advice. Backtest and Demo do not guarantee live profits.

Related guides: Stochastic Indicator (academy) · RSI Overbought/Oversold EA — No Code · Build EA Without Coding · Backtest in Strategy Tester · EA Strategy Library


What Is a Stochastic Crossover EA?

A Stochastic crossover Expert Advisor automates entries when the %K and %D lines interact:

SignalRule (this guide)
Buy%K crosses above %D — Stoch crosses above Stoch %D
Sell%K crosses below %D — Stoch crosses below Stoch %D

Alternative: cross out of the oversold or overbought zone (the Level tab: Oversold level / Overbought level) — shown below.

The EA runs on OnTick() [1]. Strategy Builder exports iStochastic [2] logic — you do not hand-write CopyBuffer calls.


Classic Settings and When It Works

SettingStarterNotes
%K length14%K lookback
%K smoothing3Extra %K smoothing
%D length3%D smoothing
Overbought level80Zone cap
Oversold level20Zone floor
Trading timeframe (Step 2)H1M15 = more signals; H4 = fewer
SymbolEURUSDTight spread helps

Works best: Range-bound or pullback markets after a move into oversold/overbought.

Struggles in: Strong trends — Stochastic can stay pegged high/low. Combine with trend filter (MA Crossover EA) or session filter.

Theory: Stochastic Indicator guide.


Stochastic Parameters in Strategy Builder

SettingRangeDefault
%K length1 – 20014
%K smoothing1 – 503
%D length1 – 503
Overbought level50 – 10080
Oversold level0 – 5020

The indicator is added from the picker in Step 3. The timeframe it reads is the one you set in Step 2 (Timeframes): pick H1 there as the trading timeframe.


Stochastic Condition Types Explained

Step 3 has a Buy rule and a Sell rule. Each line reads like a sentence: Buy when [What to watch] [How they compare] [Compare with]. The Stochastic lines are Stoch (%K) and Stoch %D.

Part of the lineChoice
What to watchStoch
How they comparecrosses above / crosses below
Compare withStoch %D (Indicator tab), or a level (Level tab), or a number (Number tab)

Starter recipe: Buy when Stoch crosses above Stoch %D; Sell when Stoch crosses below Stoch %D.

Zone exit variant: Buy when Stoch crosses above its Oversold level (20); Sell when Stoch crosses below its Overbought level (80). Pick the level in the Level tab.


2. Is above / is below

How they compare also offers is above, is below, is at or above and is at or below, with a number in Compare with (Number tab).

Use case: “Only trade when Stoch is above 50” as a trend filter, added as a second line in an All of these group.


3. Divergence

How they compare: diverges from price. The form asks for the divergence type (regular or hidden, bullish or bearish), the swing points (left and right bars, 5 and 5 to start) and the distance between swings (5 to 60 bars to start).


4. Rising / falling

is rising / is falling, with Over how many bars and Minimum change.


5. Time window

On a line, Time window offers Happened within the last N bars and Has been true for N bars in a row. The second is not offered with crosses.


Build the Stochastic EA Step by Step

Step 1 — Information and platform

Name: Stochastic K/D H1 EURUSD

Step 2 — Timeframes

Add H1 as the trading timeframe.

Step 3 — Entry rules

  1. Buy rule → under What to watch, pick Stochastic (Indicator tab).
  2. Settings: %K length 14, %K smoothing 3, %D length 3, Overbought level 80, Oversold level 20.
  3. Line: Stoch crosses above Stoch %D.
  4. Sell rule → switch on Sell mirrors Buy, or write Stoch crosses below Stoch %D yourself.

Optional AND: add a line in an All of these group: Stoch is above 20 (already left the oversold zone).

Step 4 — Filters

Session or spread — Advanced EA MTF & Filters.

Steps 5 and 6 — Exits, risk and money management

Step 5: Stop loss, optional Take profit. Step 6: Position size. EA Risk Management.

Step 7 — Review and save

Fix anything in the Check list, then Save strategy and Get the code.


Stochastic and MA Confluence: Add a Trend Filter

A Stochastic-only EA buys the moment %K crosses %D, even while price is still below the slow average; an MA-only EA enters late and whipsaws in chop. Confluence means two independent rules must both be true before the EA opens a trade: Stochastic times the momentum, a moving-average cross confirms the trend.

SignalBuy rule / Sell rule in Step 3 (group: All of these)
BuyStoch crosses above Stoch %D, and SMA(10) crosses above SMA(20)
SellStoch crosses below Stoch %D, and SMA(10) crosses below SMA(20) — or switch on Sell mirrors Buy

How to build it: in the Buy rule, add a group set to All of these. Keep the Stochastic line from the step-by-step above. For the second line, pick a moving average with Average type SMA and Length 10 under What to watch, crosses above, and in Compare with a second moving average, SMA with Length 20. There is no single "cross period" setting: the cross is two averages compared.

ChoiceEffect
SMA(10) crosses above SMA(20)Fires on the cross itself — fewer entries; the starting point
Close is above SMA(200)Stays true while price is above the average — more entries; use it only if the cross proves too rare
Happened within the last N bars on the Stochastic lineLets the two crosses land a few bars apart instead of on the same bar

Exit: many confluence EAs leave on the opposite cross — in Step 5 use Exit by signal and build the condition like an entry rule.

Keep it to two rules. Each extra AND condition cuts trades and makes the result easier to curve-fit. If you need three OR branches to justify a trade, simplify before you optimize. Run the confluence version and the Stochastic-only version over the same dates in the Strategy Tester [3]: confluence should reduce the trade count. In hand-written MQL5 the trend line is one iMA [4] handle per average, read on closed bars like the iStochastic lines. The same AND pattern with other indicators: MACD + RSI Confluence.


Export, Compile, and Backtest

  1. Step 7 → Get the code → MetaTrader 5 code → .mq5.
  2. MetaEditor F7.
  3. Strategy Tester: Every tick, 6–12 months.
  4. Demo before live.

10016: Invalid Stops.


Improvements: Filters and Risk

ProblemLever
Over-trading in trendAND with MA trend filter
Late entriesTighter zones (75/25) or Happened within the last N bars on the cross line
Whipsaw in rangeA session filter in Step 4 (Market sessions) or an All of these rule with a second line

The stochastic %K/%D cross: history and rules

Who and when: George C. Lane, in the late 1950s.

George C. Lane developed the stochastic oscillator in the late 1950s. It shows where the latest close sits inside the high-low range of the last bars, 14 by default, on a scale from 0 to 100. The main line is called %K; %D is a short average of it. Readings above 80 are called overbought and readings below 20 oversold.

The classic signal joins the two ideas: a buy when %K crosses above %D in the oversold area, and a sell when %K crosses below %D in the overbought area. The cross shows the turn; the area says that the turn starts from a stretched level.

The rule as the source states it

A buy signal when a rising %K crosses above %D in the oversold region (below 20); a sell signal when a falling %K crosses below %D in the overbought region (above 80). [5] [6]

Our version

Stochastic (14, 3, 3) on 1-hour bars of a currency pair. Buy when %K crosses above %D while %K is below 20. Sell short when %K crosses below %D while %K is above 80. An opposite signal closes the trade and reverses it.

How ours differs

  • "In the oversold region" is read as "%K is below 20 on the bar of the cross".
  • Like every library file, ours also adds the builder's default stop, position size and account limits (Steps 5 and 6). You can change them in the form.

Can be built on the Free plan. Open in the builder →

Being well known says nothing about how a strategy will do. Test it on your own data.

Next Steps

Whipsaw test: Backtest Stochastic-only for three months, then add your planned session filter in the live demo and compare trade count — if trades drop by half and drawdown improves, keep the filter; if trades vanish, tune OB/OS before giving up on the indicator.

References: MQL5 iStochastic [2] · MetaTrader 5 Strategy Tester [3] · MQL5 iMA [4].

Frequently Asked Questions

Yes. Add Stochastic in Step 3 (Entry rules) of Strategy Builder. In the Buy rule, choose Stoch crosses above Stoch %D; in the Sell rule, crosses below (or switch on Sell mirrors Buy). Keep %K length 14, %K smoothing 3, %D length 3, Overbought level 80 and Oversold level 20, add a stop loss in Step 5, save in Step 7, press Get the code, and backtest with Every tick.

Classic starter: %K length 14, %K smoothing 3, %D length 3, Overbought level 80 and Oversold level 20 on H1 forex majors. A rule that uses the Level tab reads these two levels from the indicator, so the entry rule and the indicator lines stay aligned.

K/D cross catches momentum shifts inside the oscillator. Crossing the Oversold level or the Overbought level catches zone exits. Many beginners use Stoch crosses above Stoch %D with the default 80/20 zones — see the Step 3 rules in this guide.

In Step 3, make the Buy rule an All of these group with two lines: Stoch crosses above Stoch %D, and SMA(10) crosses above SMA(20). Switch on Sell mirrors Buy for the sell side. The MA cross confirms the trend, so the EA takes fewer Stochastic signals against it; Close is above SMA(200) is a looser alternative that gives more entries.

No. Stochastic mean-reversion and crossover systems struggle in strong trends. Educational guide only — backtest, Demo, then live with risk you can afford.

See the Stochastic indicator guide at /academy/indicators/stochastic for %K, %D, and zone logic, then return here for the no-code EA workflow.

George C. Lane, in the late 1950s. Stochastic (14, 3, 3) on 1-hour bars of a currency pair. Buy when %K crosses above %D while %K is below 20. Sell short when %K crosses below %D while %K is above 80. An opposite signal closes the trade and reverses it. Being well known says nothing about how a strategy will do. Test it on your own data.