Stop Losing in MT4 EA Close Opposite Trade Automatically

Many traders lose money not because their strategy is bad, but because their Expert Advisor keeps opening trades in both directions.

You open a Buy trade… and suddenly there is a Sell trade running at the same time.

This creates conflict in your strategy and slowly drains your account.

In this guide, you will learn how to fix this by making your EA automatically close the opposite trade before opening a new one.

Disclaimer

This content is for educational purposes only. Always test your strategy before using it on a live account.


Why This Problem Happens

By default, most EAs do not check if there is already an opposite trade running.

So when a new signal comes:

  • Buy signal → EA opens Buy
  • Sell signal → EA opens Sell (without closing Buy)

Now both trades are active at the same time.

This leads to:

  • Conflicting positions
  • Higher drawdown
  • Unpredictable results

The solution is simple. Before opening a new trade, your EA should first check and close any opposite trades.

Watch the Full Tutorial


The Logic You Need

Before placing a Buy trade:

  • Close all Sell trades

Before placing a Sell trade:

  • Close all Buy trades

This keeps your strategy clean and controlled.


Step 1: Loop Through Open Orders

 
for(int i = 0; i < OrdersTotal(); i++)
   {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      {
         if(OrderSymbol() == Symbol())
         {
            // Logic here
         }
      }
   }

This loop helps you go through all active trades for the current symbol.

Step 2: Close Opposite Trades

Example: If you want to open a Buy trade, first close all Sell trades.



if(OrderSelect(sellTicket, SELECT_BY_TICKET))
      {
         OrderClose(sellTicket, OrderLots(), Ask, 3, clrRed);
      }

Now your EA removes the conflict before opening a new position.

Step 3: Open Your New Trade

After closing opposite trades, you can safely open your new order.


OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, 0, 0, "Buy Trade", 2211, 0, clrBlue);

Now your EA only keeps one direction active at a time.

Common Mistakes

1. Not Filtering by Symbol

If you don’t check OrderSymbol(), your EA may close trades from other charts.

2. Wrong Price Used

Use Ask for Buy and Bid for Sell when closing orders.

3. Loop Direction Issue

Always loop from OrdersTotal() - 1 down to 0 to avoid skipping orders.

4. No Error Handling

You should always check if OrderClose() was successful.

Why This Improves Your Strategy

This small change makes a big difference:

  • No conflicting trades
  • Cleaner trade management
  • Better control over risk
  • More predictable results

What’s Next

In the next tutorials, you can improve this logic further by:

  • Adding stop loss and take profit
  • Using risk management rules
  • Limiting maximum open trades

Final Thoughts

If your EA is opening trades in both directions, it is not a strategy issue. It is a logic issue.

Fixing this will immediately improve your trading system.

If you want more MetaTrader automation tutorials, keep following Coding Reel.

Post a Comment

Share your thoughts ...

Previous Post Next Post