Popular Posts

Editor'S Choice - 2019

MQL4: Writing an Advisor for Trading Given Patterns


Surely many of you often noticed that periodically the charts of a particular currency pair repeat their movements. In fact, in most cases - this is just a temporary phenomenon, which will not work to make enough money. But what if there are such reliable price movements and patterns that we are not aware of? Today we will write a fairly simple adviser that will help us with market research.

What will we write?

Any research is always based on a specific hypothesis. First, we put forward it, and then we prove or disprove it.

Today we will write a simple adviser that will work on daily charts. The basis for the formation of the entrance will be a candlestick pattern linked to the day of the week. Our hypothesis is this: if we see one bearish candle, and then bullish, say, on Wednesday, then on Thursday the candle will also be bullish. If we see a bullish candle, and then a bearish candle, then the next day will close even lower. I will illustrate this in the picture:

As you can see, the pattern is very simple, and it is unlikely that it really works. Nevertheless, such anomalies do exist on the market, on which you can easily earn small, but real money from year to year.

The design of our advisor

The adviser will be very simple. He will check the current day of the week with the days on which trading is allowed. Then he checks to see if there are any open orders. After that, it will be checked whether the specified pattern is present, and if it is detected, it will open a deal. The position will be closed the next day.

Therefore, we will divide the body of the adviser into several parts. The first part is the filter of the day of the week:

bool TimeFilter () {

if (! MondayTrade && DayOfWeek () == 1) return (false);

if (! TuesdayTrade && DayOfWeek () == 2) return (false);

if (! WednesdayTrade && DayOfWeek () == 3) return (false);

if (! ThursdayTrade && DayOfWeek () == 4) return (false);

if (! FridayTrade && DayOfWeek () == 5) return (false);

return (true);


It will return false if the current day of the week is prohibited in the settings. The second block generates a signal:

int Signal () {

if (! TimeFilter ()) return -5;

double Close1 = iClose (_Symbol, signal_period, 1);

double Open1 = iOpen (_Symbol, signal_period, 1);

double Close2 = iClose (_Symbol, signal_period, 2);

double Open2 = iOpen (_Symbol, signal_period, 2);

if (Close2> Open2 && Close1 <Open1) {

return OP_SELL;


if (Close2 Open1) {

return OP_BUY;


return -5;


As you can see, we are just looking for the pattern we need. And you can rewrite this method to search for any pattern.

All that remains is to write a method for opening and closing a position. And the method signaling that the current position is already open 1 bar, and it’s time to close it:

bool ExitTime (int dir) {

for (int i = OrdersTotal (); i> = 0; i-) {

if (! OrderSelect (i, SELECT_BY_POS, MODE_TRADES)) {



if (OrderSymbol ()! = Symbol () || OrderMagicNumber ()! = magic) {



if (TimeCurrent () - OrderOpenTime ()> 60 * exit_bars * exit_period) {

return (true);



return (false);


That, in fact, is all. You can see the full code of the adviser in the video tutorial.


As I already said, the adviser, like the pattern itself, is very simple. But in the markets, “simple” - does not always mean bad and not profitable. Even such simple things, to our surprise, can turn out to be workers, for which I like this market. This is a market of breathtaking contrasts. While manual traders trade blindly in general, without any confirmation of the statistical significance of their systems, newcomers invest the last money in grids and martins, algorithmic traders invent more sophisticated scalpers with the number of lines of code giving odds to “War and Peace”, sometimes very simple but profitable things lie on the surface, but nobody sees them. There is some very sophisticated irony in this, don’t you?

The results do not shine, of course, but the pattern was taken at random, and it is extremely simple. I am sure that if you correct it a little, then you can achieve better results.


The idea of ​​a pattern with two candles of opposite colors and set to continue the movement is a very simple example. This takes into account neither the form, nor the volatility, nor the ratio of the size of the shadows to the size of the candle body itself. You can come up with your own patterns and test them - as you know, maybe you will find really valuable and very profitable opportunities.

Watch the video: MQL4 Programming - Candle Stick patterns part 1 (December 2019).

Leave Your Comment