How to Add Stop Loss and Take Profit in MT4 EA


If your Expert Advisor opens trades without Stop Loss or Take Profit, your account can become very risky.

This is one of the most common mistakes beginners make while creating automated trading systems in MetaTrader 4.

In this tutorial, you will learn how to properly add Stop Loss and Take Profit in your MT4 Expert Advisor using MQL4.

Disclaimer
This tutorial is for educational purposes only. Always test your EA on a demo account before using it on a live account.


Why Stop Loss and Take Profit Matter

Without Stop Loss:

  • Your losses can grow very large
  • Your EA may keep holding losing trades
  • Your account becomes exposed to market volatility

Without Take Profit:

  • Your profits may disappear during reversals
  • Your EA has no clear exit strategy

A good EA always manages risk properly.


Understanding OrderSend Parameters

In MT4, trades are usually opened using the OrderSend() function.

Here is a simple Buy order example:

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

Notice these two values:

 0, // Stop Loss 0, // Take Profit 

Right now, both are disabled.

Adding Stop Loss and Take Profit

Let’s say:

  • Stop Loss = 50 pips
  • Take Profit = 100 pips

For a Buy trade:

 double stopLoss = Ask - (50 * Point); double takeProfit = Ask + (100 * Point); 

Now place them inside OrderSend().

 OrderSend( Symbol(), OP_BUY, 0.1, Ask, 3, stopLoss, takeProfit, "Buy Trade", 0, 0, clrBlue ); 

Your EA will now automatically close trades when those levels are reached.


For Sell Trades

Sell orders work in the opposite direction.

 double stopLoss = Bid + (50 * Point); double takeProfit = Bid - (100 * Point); 

Then:

 OrderSend( Symbol(), OP_SELL, 0.1, Bid, 3, stopLoss, takeProfit, "Sell Trade", 0, 0, clrRed ); 


Important Point About 5-Digit Brokers

Some brokers use 5-digit pricing.

For example:

  • EURUSD → 1.12345

In that case, using only Point may give incorrect pip calculations.

A safer approach is:

 double pip = Point;

if(Digits == 5 || Digits == 3)
{
pip = Point * 10;
}

Then use:

 double stopLoss = Ask - (50 * pip); 
double takeProfit = Ask + (100 * pip);


Common Mistakes

1. Using Wrong Direction

For Buy:

  • Stop Loss goes below entry
  • Take Profit goes above entry

For Sell:

  • Stop Loss goes above entry
  • Take Profit goes below entry

2. Stop Loss Too Close

Some brokers reject orders if Stop Loss is too near the current price.

You can check minimum stop level using:

 MarketInfo(Symbol(), MODE_STOPLEVEL) 

3. Ignoring Error Handling

Always check if the order was opened successfully.

 int ticket = OrderSend(...);

if(ticket < 0)
{
Print("Order failed: ", GetLastError());
}


Complete Buy Example

 double pip = Point;

if(Digits == 5 || Digits == 3)
{
pip = Point * 10;
}

double stopLoss = Ask - (50 * pip);
double takeProfit = Ask + (100 * pip);

int ticket = OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, stopLoss, takeProfit, "Buy Trade", 0,0,clrBlue);

if(ticket < 0)
{
Print("Order failed: ", GetLastError());
}


Final Thoughts

Adding Stop Loss and Take Profit is one of the most important parts of EA development.

Even a simple strategy becomes much safer when proper risk management is added.

Start simple, test your EA properly, and improve it step by step.


More MT4 EA Tutorials

  • How to Prevent Multiple Trades in MT4 EA
  • EX4 vs MQ4 Explained
  • How to Automatically Close Opposite Trades
  • Why Your EA Is Not Trading

Follow Coding Reel for more MetaTrader automation tutorials and real coding sessions.

Post a Comment

Share your thoughts ...

Previous Post Next Post