<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Source+Code+Pro:wght@400;600;700&family=Source+Sans+3:wght@400;500;600;700&display=swap">

Stochastic with a trend filter

The same Stochastic, but entries are allowed only in the direction of the larger trend: the robot buys when %K leaves the oversold zone while price is above a long average, and sells the mirror case. The filter skips counter-trend signals.

  • Stochastic
  • Moving Average
  • Intermediate

Choose a platform: MT5 shows the chart and Inputs screenshots from MetaTrader 5, MT4 shows the chart with entries and the Inputs screenshot from MetaTrader 4.

How the robot trades

When it buys

The Stochastic turns upward below the 20 level and price is above the moving average.

When it sells

The Stochastic turns downward above the 80 level and price is below the average.

When it exits

The Stochastic turns the other way.

Screenshot 1 of 5

EURUSD, H4. The robot indicators and the deals from the MT5 strategy tester: arrows are entries, crosses are exits, and the lines join an entry to its exit.

The conditions as constructor blocks

Open a buy

(Stoch_K ≥ Stoch_D)
AND
(Stoch_K_Prev < Stoch_D_Prev)
AND
(Stoch_K < Stoch_Low (20))
AND
(Close > MA_Trend)

Open a sell

(Stoch_K ≤ Stoch_D)
AND
(Stoch_K_Prev > Stoch_D_Prev)
AND
(Stoch_K > Stoch_High (80))
AND
(Close < MA_Trend)

Close a buy

(Stoch_K ≤ Stoch_D)
AND
(Stoch_K_Prev > Stoch_D_Prev)

Close a sell

(Stoch_K ≥ Stoch_D)
AND
(Stoch_K_Prev < Stoch_D_Prev)

Inputs

These inputs appear in the expert advisor settings in MetaTrader 5 or MetaTrader 4, and they are what the strategy tester goes through when it optimises the robot.

InputDefaultWhat it means
InpStoch_K_kPeriod5%K period: how many recent candles the position of price in the range is taken from.
InpStoch_K_dPeriod3%D period: the smoothing of the %K line.
InpStoch_K_slowing3Slowing: extra smoothing of %K to make the line calmer.
InpMA_Trend_period100Period of the filter average: the larger it is, the stricter the filter and the rarer the trades.
Stoch_Low20Low level: a buy is allowed only while %K is below it.
Stoch_High80High level: a sell is allowed only while %K is above it.
The inputs dialog of the Stochastic with a trend filter expert advisor in MetaTrader 5. Screenshot 1 of 2The inputs dialog of the Stochastic with a trend filter expert advisor in MetaTrader 5. Screenshot 2 of 2
This is how the expert's inputs look in MetaTrader 5

How to tune the robot to your style

  • Change the length of the average: 200 filters harder, 50 lets more trades through.
  • Move the zones: at 25 and 75 there will be more signals, at 15 and 85 fewer.
  • Check the robot on another timeframe in the tester: the trend filter behaves differently on H1 than on H4.

You can do all of this in the constructor without signing up. To save your changes and come back to them later, you need an account.

Risks

How a strategy behaved in the past does not predict its future results. Check the robot in the strategy tester and on a demo account first, and only then decide whether to trade it with real money. This robot is an example to adapt to your instrument, timeframe and risk rules.

Similar robots