MQL4 BASICS - CREATE YOUR OWN AUTOTRADING EXPERT ADVISORS FOR FREE

How to add closing conditions into an expert advisor

Now you are going to learn how to use closing conditions to close your trades, instead of just mere stoploss and takeprofit settings.

Let's pretend you want to close all open Buy trades for a certain symbol, when the last 3 bars in a row have gone up in price... How would we code this?

What if you want to close all open buy trades, when the last bar was higher than 71 on the RSI indicator... How would you code that?

This is what you will learn on this page.

Step 1: How to close a trade

The first thing you need to know is how to select an open trade

It is extremely easy to close a trade, but first you need to select it using the following code:

  

if(OrdersTotal() > 0)
        {
         for(int i = OrdersTotal() - 1; i >= 0; i--)  // Start from the last order and move backwards
           {
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderSymbol() == Symbol()) //Search for an open trade that matches the symbol of the open chart... If the open chart is EURUSD, then the EA will only search for open EURUSD trades..
              {
              
              (TRADE CLOSING CRITERIA HERE)
              
              }
              }
              }
              
              
              

You just need to identify the trade you want to close... If you are using magic numbers, you can narrow it down even more, if not then by symbol, like in the above code is enough.

For example, if you have a buy trade opened under the name MagicNumber1, you will write, as a part of your condition, that if the open trade has MagicNumber1 connected to it and is a buy trade, continue on to close the trade.

Like so:


/////////////////////////////////////////

int MagicNumber1=123456; //this would be somewhere in your code previously and would have also been used when opening the trade we are now searching for using the magic number

/////////////////////////////////////////

if(OrderMagicNumber()==MagicNumber1 && OrderType() == OP_BUY && (MORE CONDITIONS HERE)) {CLOSE TRADE CODE HERE}

The code that closes an open trade

To perform the actual function of closing a trade, you simply call upon the OrderCLose function. Just like with the OrderSend function, the MetaEditor will autofill what you need. We will show you a code snippet anyway though.

For example, to close a buy trade that uses MagicNumber1 and closes after 3 bullish candles close in a row:


    
if(OrderMagicNumber()==MagicNumber1 && OrderType() == OP_BUY && Close[1]>Open[1]  && Close[2]>Open[2] && Close[3]>Open[3])

{

int BuyClose=OrderClose(OrderTicket(), OrderLots(), Bid, 0, Blue);

}

If you were closing a sell trade, you would replace Bid, with Ask and give the int "BuyClose" a different name like "SellClose" and search for sell trades using "OrderType() == OP_SELL".

If you don't have a magicnumber in use and just want to close a trade based on its symbol matching the open chart, you would replace the OrderMagicNumber()==MagicNumber1 with: OrderSymbol()==Symbol() .


Closing Conditions

Closing conditions work exactly the same as opening conditions as explained fully in How to add in buying and selling conditions

Please refer here to learn how to create many types of different conditions, using bars, indicators and price changes.

NEXT UP --->

How to prevent an expert advisor from opening more than one trade at a time