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 ERR_INVALID_STOPS
(Invalid stops). Some Forex brokers set the minimum distance between the current price and the
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));
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 MarketInfo(Symbol(), MODE_STOPLEVEL)
to the current price.
MQL4 solution to OrderSend Error 130 with market orders
If your EA calculates stops and
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 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 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
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.
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.
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.
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.
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.