How to beat Ordersend Error 130 in MetaTrader 4

ordersend-error-130

By popular demand, proven strategies on how to beat every algorithmic trader’s worst nightmare – Error 130

The OrderSend Error 130 appears in MetaTrader 4 when an Expert Advisor can’t execute a marker order as expected. Also known as the Invalid Stop (ERR_INVALID_STOPS) in MQL jargon, the Error 130 happens when the TakeProfit and StopLoss levels are set to close to the current market price.

Where does this error come from? What does it mean for your Expert Advisor? How can you find the part of your code that is causing the error? We tackle all this and more…

Video Tutorial

Alright! Let’s go ahead and send some orders with OrderSend. The video below is available if you prefer watching instead of reading

 

To start off, a formal definition from our friend, MQL4 Documentation:

ERR_INVALID_STOPS

That’s right! That is all you get from MetaQuotes. And the rest… Go figure!

Ordersend Error 130 is briefly mentioned in other sections of the documentation. However, there is no thorough guide to what “Invalid Stops” actually means and how to deal with this, perhaps, most common problem in Forex programming.

But not a worry! That’s why I have written this article. Let’s get through this together!

The silent killer

So… you launched your expert advisor and… nothing happens. No BUY orders, no SELL orders, no pending orders, not even error messages in the logs…. Just silence. You decide to wait a few hours / days / weeks, and nothing really changes – the charts go up and down, but you don’t see any profit. This can go on forever…

The real reason is simple – you’re actually getting ERR_INVALID_STOPS (which is the correct technical term for the issue), but you can’t see it. That’s because 130 is a silent killer. A cold-blooded murderer of your brain and inner calm 🙂

There is no way to pick up this error through expert advisor logs or even terminal logs. The only way to catch it is by adding the right failsafe mechanisms into your code. Here’s an example you can adapt to your code:
int ticket;
ticket = OrderSend("EURUSD", OP_BUY, 1.0, Ask, 10, StopLossLevel, TakeProfitLevel, "My 1st Order!");

if(ticket < 0)
{
Alert(“OrderSend Error: “, GetLastError());
}
else
{
Alert(“Order Sent Successfully, Ticket # is: ” + string(ticket));
}
What we are doing here is taking the ticket number and that OrderSend() returns and checking if it is less than zero. If yes, then that is a signal from MetaTrader 4 telling us that there was a problem with the request.

The error code is then printed out onto the screen using Alert() and the built-in GetLastError() function. This code will give a pop-up window like in the image up at the top of this article.

Note: you can use Print() instead of Alert() to redirect the message straight to the EA’s log instead of displaying it on the screen.

Core of Ordersend Error 130

Invalid stops is the real name for the culprit we are dealing with today. So what does invalid stops in MetaTrader 4 actually mean?

  • For a market order (BUY or SELL) invalid stops means that the StopLoss and/or TakeProfit you requested were not possible to set for your order. Therefore, since a request cannot be fulfilled only partially, the order was not executed at all
  • For a pending order (BUY STOP, BUY LIMIT, SELL STOP, or SELL LIMIT) invalid stops means that either (1) there were issues with the SL/TP (same as above) OR (2) the issue was with the entry price which you specified for the order itself

As we can see, the issue is always with one (or many) of the prices that your Forex Robot specified in its request to the trade server. Now that we know our enemy – let’s beat it!

1) StopLoss & TakeProfit are prices

There are several possible causes of ERR_INVALID_STOPS, and one of the more frequent ones among beginners is specifying the StopLoss and TakeProfit in pips rather than actual price levels. Like this:
OrderSend(EURUSD, OP_BUY, 0.1, 1.1606, 10, 20, 40);
This person tried to set a StopLoss of 20 pips and a TakeProfit of 40 pips. Big NO-NO….. The correct and only way of specifying your SL and TP is through price levels:
OrderSend(EURUSD, OP_BUY, 0.1, 1.1606, 10, 1.1585, 1.1645);
By the way, here we assumed that the current ASK price is 1.1606 and current BID price is 1.1605 (i.e. 1 pip spread).

2) 4-digits vs 5-digits

Another reason you could be getting ERR_INVALID_STOPS is if you are setting the input parameters of your EA in Pips (4-digit points) when the Robot is anticipating 5-digit points. Let’s look at an example:
extern int StopLoss = 20;
extern int TakeProfit = 40;

//…

OrderSend(EURUSD, OP_BUY, 0.1, Ask, 10, Bid-StopLoss*Point(), Bid+TakeProfit*Point());

This code will work fine on a 4-digit broker, however will fail on a 5-digit broker. The reason is that on a 4-digit broker, Point() equals to 0.0001, whereas on a 5-digit broker Point() equals to 0.00001.

Basically, with no additional adjustments, on a 5-digit broker the EA will be attempting to set the StopLoss and TakeProfit at only 2 and 4 pips away from the Bid price respectively!

That’s why in the case of a 5-digit broker you have to increase your StopLoss and TakeProfit parameters tenfold. Like this:
extern int StopLoss = 200;
extern int TakeProfit = 400;

//…

OrderSend(EURUSD, OP_BUY, 0.1, Ask, 10, Bid-StopLoss*Point(), Bid+TakeProfit*Point());
However, be careful! Some EA’s already have modules that will detect the number of digits after the decimal and will automatically adjust your input parameters for you. In these situations multiplying inputs by 10 can actually lead to erroneous performance.

Note: I plan on posting a separate article where we will discuss how to create our own modules to detect the number of digits after the decimal

3) ECN brokers

ECN accounts have their own specifics. One of them is – when trading through a ECN broker you will not be able to set a StopLoss and/or TakeProfit with your Market Order (BUY or SELL). If you try to do this – you will get Error 130.

However, of course, you do need to set a StopLoss (and maybe TakeProfit) for your order, and this must be done as soon as possible after the order has been executed. Try this code:
int MarketOrderSend(string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment, int magic)
{
int ticket;

ticket = OrderSend(symbol, cmd, volume, price, slippage, 0, 0, NULL, magic);
if(ticket <= 0) Alert(“OrderSend Error: “, GetLastError());
else
{
bool res = OrderModify(ticket, 0, stoploss, takeprofit, 0);
if(!res) { Alert(“OrderModify Error: “, GetLastError());
Alert(“IMPORTANT: ORDER #”, ticket, ” HAS NO STOPLOSS AND TAKEPROFIT”);}
}
return(ticket);
}
You can add this function to your code (at the very end) and then use it instead of OrderSend() in your main code. This function adds an extra step in the process of sending a Market Order.

First, it send the request to execute a market order stripping out the StopLoss and TakeProfit. Next, it modifies the newly opened market order by adding the desired SL and TP.

There is, of course, a risk that the order will be executed, but the modification will fail. However, in that case the function will promptly notify the trader that the StopLoss and TakeProfit have not been set.

Feel free to modify this function to suit your needs and trading style.

4) Stop-Levels

Stop-Levels are a mechanism for brokers to protect themselves from certain volatility and liquidity related risks. In simple terms, you will not be able to set your StopLoss or TakeProfit OR any pending order closer than a predetermined number of Pips to the current market price.

To find out what the Stop Level is for a specific currency pair you need to press CTRL+U on your keyboard, select the desired currency pair and click the “Properties” button as shown on the illustration below:

forex-symbol-properties

In this example the Stop Level for AUDUSD is 3 Pips. This means that you will not be able to set the StopLoss for your order closer than 3 Pips to the price at which the order will be opened.

This also means that any pending order will have to be set at least 3 Pips away from the current market price.

If you Robot tries to break these rules and set a StopLoss / TakeProfit or Pending Order within the Stop Level range, then it will get Error 130 “Invalid Stops”. So just be mindful of the Stop Level of the currency where your EA’s are trading – don’t specify excessively small StopLoss and TakeProfit parameters.

It is also worth noting that more exotic currency pairs can have much more significant Stop Levels. Fore example, for AUDNZD the Stop Level with the same broker as in the above example is 20 Pips. For GBPSEK (British Pound vs Swedish Krone) – it’s 100 Pips.

5) Normalization of doubles

With some brokers you will find that for an unknown reason the Ask and Bid prices are passed onto the trader with additional negligible digits after the decimal. For example:

Instead of 1.1606 the broker would give you 1.160600001

Now this phenomenon has no effect on manual trading, moreover since the MT4 terminal is hardwired to display a certain number of digits after the decimal point (either 4 or 5) – you will not be able to notice any difference at all!

However, these ‘negligible’ digits after the decimal can have a dramatic effect on Expert Advisors causing……… that’s right! Our old friend, OrderSend Error 130!

Here’s a strategy that I personally use to protect my Robots from this issue:
void OnTick()
{
//...
OrderSend(EURUSD, OP_BUY, 0.1, ND(Ask), 10, ND(Bid-StopLoss*Point()), ND(Bid+TakeProfit*Point()));

}

double ND(double val)
{
return(NormalizeDouble(val, Digits));
}
This neat little trick allows you to normalize (in simple terms – Round) any prices that you are inputting into the OrderSend() function. This way you cut off all ‘negligible’ digits after the decimal point.

Conclusion

Today we saw that there may be multiple (at least 5) causes to error 130. Though this is quite a few, the underlying issues are all trivial and can be corrected in a matter of minutes.

Therefore, Error 130 should not be feared! If you have encountered this culprit, it’s just a matter of going through the list above, finding the situation that applies to you and applying the prescribed solution.

Hope you found this article useful!

Let me know if you have any questions by using the comments section below.

Happy trading,

Kirill

P.S: if you liked what you read in this article, here you can find the full course:

Algorithmic Trading Course

What are you waiting for?

START LEARNING FOREX TODAY!

share This:
Muhammad Awais

8 Responses to “How to beat Ordersend Error 130 in MetaTrader 4”

February 10, 2015 at 11:02 am, TheXlearner said:

Thanks Kirill , These informations helped me to solve pending entry price error .

Reply

February 18, 2015 at 1:53 pm, Kirill said:

Anytime! Happy to help

Reply

April 03, 2015 at 11:03 pm, Daniel said:

Hello Kirill,

so I understand that you cannot set a SL and TP to your market order when trading with an ECN Broker.
Can you maybe explain why? What is the reason that you cannot do it?
And why can you do it with the rest of the brokers (Market Makers)?
Thank you!

Reply

April 08, 2015 at 11:31 am, Kirill said:

Hi Daniel,

In market execution (ECN) you are not guaranteed a certain price at which your order will be filled. Your request will be executed at the next available price, which can be 10 pips away or even 100 pips away from what you specified. That’s the main reason why the SL and TP cannot be set right away.

In instant execution, you are able to set the SL and TP right away because you are able to control the maximum slippage. If the price is too far away from what you want the order will not be executed.

You can read more about this here: http://www.forexful.com/articles/market-execution-vs-instant-execution-in-mt4

Hope this helps,

Cheers,

Kirill

Reply

April 11, 2015 at 9:40 pm, Daniel said:

Hi Kirill,

yes it has definitely helped.
Thank you for your answer and the link to the article.
Things are clear to me know.
Can you maybe also recommend a good broker which offers Market Execution?
As far as I understand Dealing Desk Brokers usually manipulate the price, so they are more dangerous than ECN Brokers which offer market execution.
Thank you!
Best regards,
Daniel.

Reply

April 12, 2015 at 1:54 am, Kirill said:

Hi Daniel,

I’m glad that I could help out! Here’s my review of Forex Brokers:

forexboat.com/brokers

RoboForex have a good ECN solution. Also, I am in the process of reassessing this list, so changes will come soon (probably May). In the meantime, feel free to join the discussions at the ForexBoat Forum:

http://forum.forexboat.com

It just launched last week, so can’t promise you will get a reply quickly. But with time people will share their experiences.

Talk soon!

Kirill

Reply

August 26, 2015 at 1:42 pm, Vincent said:

Hi Kirill,

Is the SimpleSystem v8.9 able to handle the Error 130 and contain all the codes mentioned above?

Regards,
Vincent

Reply

January 20, 2017 at 11:23 am, yassine said:

Hi,
Thank you Kirill for your post.It appears that my issue is the ECN broker.I tried your script but my Mt4 seems not to recognize the”MarketOrderSend”.Any advice?
Regards,
Yassine

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

What are you waiting for?

START LEARNING FOREX TODAY!

as seen on: