MQL4 BASICS - CREATE YOUR OWN AUTOTRADING EXPERT ADVISORS FOR FREE

Preventing Multiple Orders for the Same Symbol

One of the common issues traders face when automating their strategies with Expert Advisors (EAs) is the risk of sending multiple orders unintentionally. This can be due to a programming oversight or misinterpretation of market conditions. However, with MQL4, you can implement checks to prevent such occurrences.

1. Using OrderCount Function

To prevent multiple trades on the same symbol, you can create a function that counts the number of open orders for a particular symbol:

int OrdersForSymbol(string symbol) { int count = 0; for(int i=0; i

2. Implementing the Check Before Sending Orders

Before sending a new order, use the function to check the count of active orders for the symbol:

if(OrdersForSymbol(Symbol()) == 0) { // Send your order }

3. Differentiating Between Buy and Sell Orders

If you want to distinguish between Buy and Sell orders for the same symbol, you can expand the OrdersForSymbol function:

int OrdersForSymbol(string symbol, int cmd) { int count = 0; for(int i=0; i

Then, to check for specific order types:

if(OrdersForSymbol(Symbol(), OP_BUY) == 0) { // Send your Buy order } if(OrdersForSymbol(Symbol(), OP_SELL) == 0) { // Send your Sell order }

4. Sample EA with the Full Function

Here's a basic representation of how you can integrate the above functions into a simple Expert Advisor:


// Define Expert Advisor properties
input double lotSize = 0.1;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
    return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}

//+------------------------------------------------------------------+
//| Function to count the number of orders for a specific symbol     |
//+------------------------------------------------------------------+
int OrdersForSymbol(string symbol, int cmd) 
{
    int count = 0;
    for(int i = 0; i < OrdersTotal(); i++) 
    {
        if(OrderSelect(i, SELECT_BY_POS) && OrderSymbol() == symbol && OrderType() == cmd) 
        {
            count++;
        }
    }
    return count;
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
    // Sample condition for Buy Order
    if (Close[1] > Open[1] && OrdersForSymbol(Symbol(), OP_BUY) == 0) 
    {
        OrderSend(Symbol(), OP_BUY, lotSize, Ask, 3, 0, 0, "Sample Buy Order", 0, 0, Green);
    }

    // Sample condition for Sell Order
    if (Close[1] < Open[1] && OrdersForSymbol(Symbol(), OP_SELL) == 0) 
    {
        OrderSend(Symbol(), OP_SELL, lotSize, Bid, 3, 0, 0, "Sample Sell Order", 0, 0, Red);
    }
}

Conclusion

Ensuring that your EA operates as intended is crucial for effective trading. By implementing these checks, you can prevent unintentional multiple orders and ensure that your trading logic is accurately represented in the live market.

Try copy and pasting this code into a blank Expert Advisor and run a test on the strategy tester. You will see that it opens 2 trades only. One sell trade, after 2 bearish candles in a row and one buy trade, after 2 bullish candles in a row.

It can no longer open more trades, until one of the current trades have closed.

Click here to return to MQL4 Basics Directory