Skip to content
Home/Articles/MQL5 and automation
MQL5 and automation

SmartVision EA: a two-timeframe trend robot, from signal to code

A stochastic for the moment to enter, two timeframes for the direction, pivots for the stop and the target. How SmartVision EA decides, what its code does well, and what to know before testing it.

SmartVision EA is a trend-following robot whose source code is shared for free. It is interesting for two reasons. It shows how to make a robot decide on several timeframes at once, and it runs a series of checks before every order (volume, margin, stop distance) that free robots rarely include.

This article follows the path of a decision, from the signal to the order, with the matching code excerpts. It also points out the limits to know before running it, even on demo.

The decision in three layers

SmartVision only takes a position when three conditions are met:

  1. A signal on the chart timeframe: the stochastic leaves an extreme area while price touches a moving average.
  2. A confirmed direction on two higher timeframes, H4 and H1 by default: the robot only buys if the consensus is bullish, and only sells if it is bearish.
  3. Valid levels: the stop and the target, taken from pivots, must respect the broker's minimum distances.

If one of the three is missing, the robot does nothing and writes it in the MetaTrader log.

Layer 1: the signal, stochastic and 100 average

MQL5
// buy: the stochastic moves back above 20 and the low touches the average
if(stochDATA[0] > get_DN_L() && stochDATA[1] < get_DN_L()
      && getLow(0) < maDATA[0] && signalTimeBuy != currBarTime0) {

The stochastic is set to 8, 3, 3 (simple smoothing, computed on highs and lows), with levels at 20 and 80. The moving average is a 100-period simple one. A buy is signalled when the stochastic moves back above 20 and the low of the candle goes below the 100 average: the robot buys a pullback to the average, when the downward push runs out of steam. The sell is the mirror image, around 80.

The static variable signalTimeBuy prevents signalling twice on the same candle.

Layer 2: the direction, on two timeframes

This is the most interesting part of the code. For each timeframe (H4 and H1 by default), the robot combines two readings.

The first one reads the averages and the RSI. Over the last three candles (ConfirmBars), it counts those where price is above the 200 exponential average, the 50 above the 200 and the RSI above 55 (and the reverse for a downtrend, with an RSI below 45). If most candles point the same way, the trend is kept.

MQL5
if(closep[b]>ma_slow[b] && ma_fast[b]>ma_slow[b] && rsi[b]>RSI_threshold_up) up++;

The second one reads the structure. It compares the high and the low of the last five candles (SwingWindow) with those of the five before. Higher high and higher low: bullish. The reverse: bearish.

MQL5
if(high1 > high2 && low1 > low2) return  1;   // rising highs and lows
if(high1 < high2 && low1 < low2) return -1;   // falling highs and lows

When both readings agree, the trend is called strong; when only one decides, it is weak.

The final consensus, in the tendance() function, follows simple rules:

  • both timeframes point the same way: follow that direction;
  • they disagree: the stronger one wins, if the other is weak;
  • only one has a trend, the other is neutral: follow the one that has a trend;
  • otherwise: no trade.

This breakdown has one merit: every rule is readable, and the panel on the chart shows the H4 trend, the H1 trend and the consensus live. You can see why the robot accepts or refuses a signal.

Layer 3: the stop and target come from pivots

MQL5
PivotPoint  = (h + l + c) / 3.0;
Resistance2 = PivotPoint + (h - l);
Support2    = PivotPoint - (h - l);

Classic pivots are computed on the last closed H4 candle. For a buy, the stop goes on the second support (S2) and the target on the second resistance (R2). For a sell, the reverse.

An important consequence: the stop distance and the reward-to-risk ratio change with every trade. They depend on the range of the last H4 candle and on where price sits relative to the pivot. A buy taken near S2 gets a very short stop and a distant target; a buy taken near R2 gets the opposite. If price is already below S2, the stop would sit above price: the level check then refuses the order.

The checks before every order

Before sending an order, the robot checks four things:

  1. The volume is brought between the symbol's minimum and maximum, and rounded to the allowed step.
  2. The free margin is enough (OrderCalcMargin).
  3. The total volume open on the symbol stays within the broker's limit.
  4. The stop and the target respect the broker's minimum distance (stop level) and freeze zone (freeze level).
MQL5
if(sl > 0 && bid - sl < stopPts - buf) return false;   // stop too close

The robot also keeps only one position per direction, filtered by symbol and magic number. It therefore leaves your other positions alone. That is exactly what should be done, and a good base for your own robots.

The progressive break-even

Off by default (FonctionBE = false), it moves the stop up in four steps, as a percentage of the distance to the target:

Price has coveredThe stop moves to
30% of the targetentry price
50%entry + 10% of the target
70%entry + 30%
90%entry + 60%

The stop never moves back. This protects trades that have moved well; it also cuts some trades that would have reached the target after a pullback. Test both settings.

What to know before testing it

The lot is fixed. Initial_lot is 0.01 and depends neither on the balance nor on the stop distance. With stops that change on every trade, the money at risk also changes on every trade. For a constant risk, compute the lot from the stop distance: that is the subject of our article on position sizing.

Testing must be done tick by tick. Since the signal is read on the current candle, an "open prices only" test does not reproduce the real behaviour. Use Every tick based on real ticks, over a period that includes both trends and ranges.

No news filter and no maximum daily loss. The robot can enter just before a major announcement. If you run it on a live account, switch it off during major announcements, or add these filters to the code.

The first signal comes quickly. The robot can take a trade from the first candle after it starts, if the three layers are validated. It is not a flaw, but do not start it "just to see" on a live account.

Going further in the code

Three simple improvements, if you want to practise:

  • read the stochastic on closed candles (indexes 1 and 2) for a signal that no longer moves;
  • compute the lot from a percentage of the balance and the distance to S2 or R2;
  • add a daily loss limit that suspends the robot until the next day.

The takeaway

SmartVision EA is not a profit machine: it is a cleanly built trend robot, whose every decision can be read in the code and on the panel. Its two-timeframe logic, its pre-order checks and its magic number filtering make it an excellent base to study. Its limits, the fixed lot and the signal on the forming candle, can be fixed in a few lines. Test it tick by tick, over varied periods, before any other decision.

A question, a disagreement, your own experience?

The forum is the right place to discuss it with other traders.

Discuss it on the forum
The tool this article is about