Popular results

Advertisements
$ £ ¥
¥ £ $

OrderSend Error 130 — What to Do?

The expert advisors that work on one broker can stop working on another; the problem with them often lies in the OrderSend Error 130. If you see Error 130 in the log of the Experts or Journal tabs in your MetaTrader platform when your expert advisor should be opening a position, then that means that the stop-loss or take-profit levels are set too close to the current market price. In the MQL4 documentation, this error is called ERR_INVALID_STOPS (Invalid stops). Some Forex brokers set the minimum distance between the current price and the stop-loss/take-profit levels to prevent scalping or abusing the quote delays. That isn't a real problem for the majority of expert advisors that aren't used for scalping. To prevent this error from occurring, you need to change the expert advisor's code.

Example of OrderSend Error 130 in MetaTrader 4

First, you might want to know what the minimum stop level is set in your broker's MetaTrader server. Adding this line of code will output the current minimum stop level for the currency pair of the chart where you run the EA:

Print("StopLevel = ", (int)MarketInfo(Symbol(), MODE_STOPLEVEL));
Example of StopLevel Output in MetaTrader 4

One thing you should be wary of is that a stop level value of zero doesn't mean that your broker doesn't set any minimum stop distance. It could also mean that the broker uses some external system for dynamic management of their stop level. In this case, it may be variable and undetectable via MQL4.

Market orders

When opening a market order, you won't be able to set a stop-loss or take-profit level that is closer than MarketInfo(Symbol(), MODE_STOPLEVEL) to the current price.

What Stop Level Means for Buy Order's Stop-Loss and Take-Profit - Simple Chart What Stop Level Means for Sell Order's Stop-Loss and Take-Profit - Simple Chart

MQL4 solution to OrderSend Error 130 with market orders

If your EA calculates stops and take-profits dynamically, below is the solution to prevent OrderSend Error 130 from occurring.

Declare a global variable for the minimum stop level; e.g.:

int StopLevel;

In the OnInit() function (or init() in older versions of MT4) of your expert advisor, define the minimum stop level:

StopLevel = MarketInfo(Symbol(), MODE_STOPLEVEL);

Next time your stop-loss or take-profit in points is calculated, just make sure that they aren't less than StopLevel:

if (SL < StopLevel) SL = StopLevel;
if (TP < StopLevel) TP = StopLevel;

To check with actual stop-loss and take-profit price levels, a difference between them and the current Bid price for Buy orders or a difference between them and the current Ask price for Sell orders should be checked.

For Buy orders:

if (Bid - StopLoss < StopLevel * _Point) StopLoss = Bid - StopLevel * _Point;
if (TakeProfit - Bid < StopLevel * _Point) TakeProfit = Bid + StopLevel * _Point;

For Sell orders:

if (StopLoss - Ask < StopLevel * _Point) StopLoss = Ask + StopLevel * _Point;
if (Ask - TakeProfit < StopLevel * _Point) TakeProfit = Ask - StopLevel * _Point;

Don't forget to refresh the current market rates with a call to the RefreshRates() function before adding the SL/TP distance to the current market rates or before comparing calculated stop-loss and take-profit levels to the current market rates.

 

Some brokers (ECN ones) don't allow expert advisors to set stop-loss or take-profit levels on market orders in the OrderSend() function even if it is greater than their MODE_STOPLEVEL value. In this case, you will have to change your EA to send market orders without SL and TP and then use OrderModify() function to set stop-loss and take-profit for the open position. Alternatively, you can also switch the EA to using pending orders only.

Pending orders

For pending orders (stop or limit), MetaTrader 4 offers the following restrictions in regards to stop level:

Buy Limit — the distances between the current Ask and the Entry, between the Entry and the Stop-Loss, and between the Entry and Take-Profit should all be greater or equal to the stop level.

What Is Stop Level When Placing Buy Limit Pending Order - Relation to Entry, SL, and TP

Sell Limit — the distances between the current Bid and the Entry, between the Entry and the Stop-Loss, and between the Entry and Take-Profit should all be greater or equal to the stop level.

What Is Stop Level When Placing Sell Limit Pending Order - Relation to Entry, SL, and TP

Buy Stop — the distances between the current Ask and the Entry, between the Entry and the Stop-Loss, and between the Entry and Take-Profit should all be greater or equal to the stop level. Actually, the conditions are the same conditions as for the Buy Limit order, but the Entry level is located above the current Ask in Buy Stop.

What Is Stop Level When Placing Buy Stop Pending Order - Relation to Entry, SL, and TP

Sell Stop — the distances between the current Bid and the Entry, between the Entry and the Stop-Loss, and between the Entry and Take-Profit should all be greater or equal to the stop level. Actually, the conditions are the same conditions as for the Sell Limit order, but the Entry level is located below the current Bid in Sell Stop.

What Is Stop Level When Placing Sell Stop Pending Order - Relation to Entry, SL, and TP

MQL4 solution to OrderSend Error 130 with pending orders

Here are examples of MQL4 code checks to make sure your Entry, Stop-Loss, and Take-Profit levels for MT4 pending orders comply with the broker's stop level restriction.

For Buy Limit orders:

if (Ask - Entry < StopLevel * _Point) Entry = Ask - StopLevel * _Point;
if (Entry - StopLoss < StopLevel * _Point) StopLoss = Entry - StopLevel * _Point;
if (TakeProfit - Entry < StopLevel * _Point) TakeProfit = Entry + StopLevel * _Point;

For Sell Limit orders:

if (Entry - Bid < StopLevel * _Point) Entry = Bid + StopLevel * _Point;
if (StopLoss - Entry < StopLevel * _Point) StopLoss = Entry + StopLevel * _Point;
if (Entry - TakeProfit < StopLevel * _Point) TakeProfit = Entry - StopLevel * _Point;

For Buy Stop orders:

if (Entry - Ask < StopLevel * _Point) Entry = Ask + StopLevel * _Point;
if (Entry - StopLoss < StopLevel * _Point) StopLoss = Entry - StopLevel * _Point;
if (TakeProfit - Entry < StopLevel * _Point) TakeProfit = Entry + StopLevel * _Point;

For Sell Stop orders:

if (Bid - Entry < StopLevel * _Point) Entry = Bid - StopLevel * _Point;
if (StopLoss - Entry < StopLevel * _Point) StopLoss = Entry + StopLevel * _Point;
if (Entry - TakeProfit < StopLevel * _Point) TakeProfit = Entry - StopLevel * _Point;

Summary

This should help in the majority of cases when you see OrderSend Error 130 in your MetaTrader 4 Experts tab.

You discuss your personal struggles with OrderSend Error 130 problem on our forum if you are having trouble solving this issue on your own.