How to add buying & selling conditions into an expert advisor

Adding in a buy or sell condition into an Expert Advisor is about as easy as it was to read this sentence.

MQL4 pretty much tells you exactly what to input, so its not to difficult.

Step 1: How to create a simple buy or sell condition

Here is a simple template to use as an example:

if(buy condition goes here)


int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"MY TRADE NAME",0,0,Blue);


Now, if the above looks confusing to you, that is fairly normal. Let's go over it and you will see how easy it really is.

So, first we have the "if". This starts off our condition part. We are saying if this happens, open a trade...

For example we could use an RSI indicator like so... if(RSI>70){YOUR ORDERSEND CODE HERE}... OR maybe you want to open a trade after 5 bars close greater than they opened, like so:

if(Close[1]>Open[1] && Close[2]>Open[2] && Close[3]>Open[3] && Close[4]>Open[4] && Close[5]>Open[5])

int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"MY TRADE NAME",0,0,Blue);


Basically to create any conditions, for buying or selling, you simply start with "if".

The reason we put "int" before openbuy=Ordersend... is because the OrderSend function returns an integer(A number without a decimal). The integer returned is the trades ticket number.

Step 2: Different ideas for creating buying or selling conditions

Here are a few different ideas to help you on your way with creating buy and sell conditions...

Open,Close,High and Low

This one is used commonly. For example if you are dealing with a moving average indicator, you may want your autotrader to open a buy trade when the last bar close is greater than the moving average line.

To do this, you simply need to say:

double MovAv50 = iMA(NULL, 0, 50, 0, MODE_EMA, PRICE_CLOSE, 1);
if(Close[1]>MovAv){OPEN BUY CODE HERE}

Perhaps, you only want it to open, on its first crossing over of the moving average, instead. Using the above code, your auto-trader with automatically open another trade as soon as the last trade has closed, if the close of that bar is still above the moving average.

To fix this we need to add in an additional condition. This time we are going to say:

if(Close[1] > MOVINGAVERAGE && Open[1] < MOVINGAVERAGE) 


This way, we ensure that, only the times when the open of the bar was less than the moving average and the close is greater than the moving average, do we open a buy trade.

You can use this method with several options. We will focus on Open, Close, High & Low. All of these can be used and with any bar number you want. The number represents the bar number. Close[1] is the close of the last bar and Close[2] is the close of 2 bars ago, while High[100] is the high of the bar 100 bars ago and so on.


You can easily use indicators as buy conditions or sell conditions.

Lets pretend you have an MACD indicator, an RSI indicator and an Envelopes indicator(ENVUPPER and ENVLOWER).

Now lets say we only want to open a sell when the MACD IS BELOW ZERO, RSI IS LESS THAN 30 and the last Close was below the lower Envelope line...

Here is what we would do, after our "if"...

if((MACD < 0.0)&&(RSI < 30)&&(Close[1] < ENVLOWER)) {SELL OPEN CODE HERE}


Now, what if you want to have an order open if the ask or bid hits a certain price?

This can be accomplished by simply going if(Bid < 1234.56) {SELL ORDER OPEN CODE HERE}

TIP:If you are create a condition like this it would be most useful to add in two extern doubles at the top of the page and a true or false option(defaulted on false), to turn on from the expert advisor(otherwise your trader will constantly be placing new trades as soon as trades close):

extern double buystrikeprice=12345.67;

extern double sellstrikeprice=12345.76;

bool buytradesallowed=false;


Now you would just use the externs in your condition: if((Bid < sellstrikeprice)&&(selltradesallowed==true)){SELL OPEN CODE HERE}

This will give you all of these options customizable outside of meta-editor. When you want to prepare for a buy trade just plug in the strike price and click your buytradesallowed to "true".

These are the main types of closing conditions you will need. There are nearly an unlimited amount of conditions you can create, so use your imagination and start making some auto-traders. If you have questions, go to our Q and A page.


Now you should have a basic idea of how to start writing in some opening and closing conditions for your trading with expert advisors on MT4 in the MQL4 language.

After you have made your conditions such as, if(trade conditions here) you have the next part of the code, that actually sends the trade order. It is called the OrderSend function.

Place this right after your conditions, within squiggly brackets {}.

For example:

if(trade conditions here)

{int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,0,0,"WHATEVER NAME YOU WANT HERE",0,0,Red);}


{int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,0,0,0,"WHATEVER NAME YOU WANT HERE",0,0,Blue);}

As you can see the only difference is that on a buy you use OP_BUY and Bid. On a sell, you use OP_SELL and Ask.

NEXT UP --->

How to add closing conditions into an Expert Advisor