why mql4 show error 130 when we use Stoploss in OrderSend function
why mql4 show error 130 when we use Stoploss in OrderSend function
Loading saved threads...
SUKHMANJIT Singh · External communityPost link
External question — Stack Overflow Stack Exchange
Author: SUKHMANJIT Singh
Original post: https://stackoverflow.com/questions/66328697
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
I am trying to create a EA in mql4, but in OrderSend function, when i use some value instead of zero it show ordersend error 130. Please help to solve this problem
Code line is
int order = OrderSend("XAUUSD",OP_SELL,0.01,Bid,3,Bid+20*0.01,tp,"",0,0,Red);
Quote
Report
Amr Sakr · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Amr Sakr
Original post: https://stackoverflow.com/a/66328911
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
error number 130 means Invalid stops.
so that means there is a problem with the stops you set with the ordersend function.
I suggest you set it like that:
int order = OrderSend("XAUUSD",OP_SELL,0.01,Bid,3,Bid+20*Point,tp,"",0,0,Red);
so you could use Point instead of hard coding it.
and to check what is the error number means. I think you could refer to:
https://book.mql4.com/appendix/errors
Quote
Report
Diego · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Diego
Original post: https://stackoverflow.com/a/70667245
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
You should know that there exists a minimum Stop Loss Size (mSLS) given in pips. "mSLS" changes with the currency and broker. So, you need to put in the OnInit() procedure of your EA a variable to get it:
int mSLS = MarketInfo(symbol,MODE_STOPLEVEL);
The distance (in pips) from your Order Open Price (OOP) and the Stop-Loss Price (SLP) can not be smaller than mSLS value.
I will try to explain a general algorithm I use for opening orders in my EAs, and then apply the constrain on Stop-Loss level (at step 3):
Step 1. I introduce a flag (f) for the type of operation I will open, being:
f = 1 for Buy, and
f = -1 for Sell
You know that there are mql4 constants OP_SELL=1 and OP_BUY=0 (
https://docs.mql4.com/constants/tradingconstants/orderproperties
).
Once I have defined f, I set my operation type variable to
int OP_TYPE = int(0.5((1+f)*OP_BUY+(1-f)*OP_SELL));
that takes value OP_TYPE=OP_BUY when f=1, while OP_TYPE=OP_SELL when f=-1.
NOTE: Regarding the color of the orders I put them in an array
color COL[2]= {clrBlue,clrRed};
then, having OP_TYPE, I set
color COLOR=COL[OP_TYPE];
Step 2. Similarly, I set the Order Open Price as
double OOP = int(0.5*((1+f)*Ask+(1-f)*Bid));
which takes value OOP=Ask when f=1, while OOP=Bid when f=-1.
Step 3. Then, given my desired Stop Loss in pips (an external POSITIVE parameter of my EA, I named sl) I make sure that sl > SLS. In other words, I check
if (sl <= mSLS) // I set my sl as the minimum allowed
{
sl = 1 + mSLS;
}
Step 4. Then I calculate the Stop-Loss Price of the order as
double SLP = OOP - f * sl * Point;
Step 5. Given my desired Take Profit in pips (an external POSITIVE parameter of my EA, I named tp) I calculate the Take-Profit Price (TPP) of the order as
double TPP = OOP + f * tp * Point;
OBSERVATION:
I can not affirm, but, according to mql4 documentation, the minimum distance rule between the stop-loss limit prices and the open price also applies to the take profit limit price. In this case, a "tp" check-up needs to be done, similar to that of the sl check-up, above. that is, before calculating TPP it must be executed the control lines below
if (tp <= mSLS) // I set my tp as the minimum allowed
{
tp = 1 + mSLS;
}
Step 5. I call for order opening with a given lot size (ls) and slippage (slip) on the operating currency pair (from where I get the Ask and Bid values)
float ls = 0.01;
int slip = 3; //(pips)
int order = OrderSend(Symbol(),OP_TYPE,ls,OOP,slip,SLP,TPP,"",0,0,COLOR);
Note that with these few lines it is easy to build a function that opens orders of any type under your command, in any currency pair you are operating, without receiving error message 130, passing to the function only 3 parameters: f, sl and tp.
It is worth including in the test phase of your EA a warning when the sl is corrected for being less than the allowed, this will allow you to increase its value so that it does not violate the stop-loss minimum value rule, while you have more control about the risk of its operations. Remember that the "sl" parameter defines how much you will lose if the order fails because the asset price ended up varying too much in the opposite direction to what was expected.
I hope I could help!
Quote
Report
PaulB · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: PaulB
Original post: https://stackoverflow.com/a/70670634
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
Whilst the other two answers are not necessarily wrong (and I will not go over the ground they have already covered), for completeness of answers, they fail to mention that for some brokers (specifically ECN brokers) you must open your order first, without setting a stop loss or take profit. Once the order is opened, use
OrderModify()
to set you stop loss and/or take profit.
Quote
Report
30-Dimensions- · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: 30-Dimensions-
Original post: https://stackoverflow.com/a/78147786
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
**
How to prevent OrderSend 130, and the Explanation of why it happens:
**
What is OrderSend 130?
1.
First, lets understand why error 130 even happens. OrderSend 130 means
ERR_INVALID_STOPS
"Invalid Stops"
[SEE:
Error Codes Mql4 Book
. Don't confuse the error to mean invalid
"Buy Stop"
or invalid
"Sell Stop"
as those are considered
"Pending Orders"
by definition and your
take profit
and
stop loss
are
"Stop Orders"
[SEE:
Order Characteristics Mql4
. By
"Invalid Stops"
, it means an invalid
"Take Profit (Stop Order)"
was set and or an invalid
"Stop Loss (Stop Order)"
was set
in the OrderSend function parameters which violates the trade requirements of your broker's "Stop Level".
What is "Stop Level"?
2.
Stop Level is the minimum distance in price at which you're programmatically/manually allowed to set a
(take profit)or(stop loss)
respectively. Should you place just one of these two parameters to a price that is floating
in a price range lesser than the Stop Level
, it induces OrderSend Error 130 everytime.
How do I find the Stop Level?
3.
You can accomplish this by calling the
"MarketInfoDouble()"
function which accepts two arguments: the
Symbol()
, and the constant,
MODE_STOPLEVEL
[SEE:
Trade Requirements Mql4
and
[SEE
MarketInfo() Identifiers Mql4
. I personally place this call into the "
OnInit()
",and typecast my call to this function as the (int) type to have it output a whole number easily readable as pips: [Copy & Paste:
int slv=(int)MarketInfo(Symbol(),MODE_STOPLEVEL);
Then Print it out by placing this code directly under the code to view the stop level set specifically by your broker:
Print("THE_BROKER'S_STOP_LEVEL_IS"," ",slv);
The output will be:
PRINT OUTPUT OF BROKER'S STOP LEVEL
As seen in this example, the
Stop Level
for my specific broker is
"10 pips"
.
This means that if I set a "Take Profit" or "Stop Loss" anywhere
under 10 pips
inside of the OrderSend() function, it'll induce an
OrderSend 130 error
.
4.
Ok, But How do I fix it?
You never want to "fix it". Rather, you always want to "prevent it", so that's what we'll do using a simple buy order as an example:
A.
Declare local or global variables to obtain the
symbol point
and
symbol ask
(All buy orders open at Ask price) and
stop level
as such or in your own syntax preference:
double spt = SymbolInfoDouble(Symbol(),SYMBOL_POINT);
double ask = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
double slv = MarketInfo(Symbol(),MODE_STOPLEVEL);
Warning!
Do not use: Uppercase
Ask
when declaring the variable: (ask)
B.
Declare local or global variables as order variables of the "double" or "int" type
(preferably double)
stop loss
,
take profit
, and
open price
and initialize them all to zero as such or in your own syntax preference:
double d_sl =0;
double d_tp =0;
double d_op =0;
C.
We now combine these variables in a way that
incorporates the stop level
ahead of time
prior to even placing them into the OrderSend() function. We'll use an integer value to represent a
45 pip take profit
and
25 pip stop loss
for the sake of this example:
1ST: the entry price:
d_op = ask;
2ND: the take profit:
d_tp = ask+slv*spt+45*10*spt;
3RD: the stop loss:
d_sl = ask-slv*spt-25*10*spt;
D.
END RESULT IS:
BUY ORDER WITH 45 PIP TAKE PROFIT AND 25 PIP STOP LOSS
CONCLUSION:
(a).
Declaring the stop level then combining them into the variables using this method "prevents" OrderSend 130 from occurring.
(b.)
Always declare a variable using the SymbolInfoDouble() function with the constant "SYMBOL_POINT" as it is more effective than using MarketInfo() "MODE_POINT" because it directly retrieves the point of the symbol your EA is placing the trade on quicker.
(c.)
After combining the variables you simply place
d_tp
,
d_op
, and
d_sl
into the OrderSend() function as its
"price"
,
"take profit"
, and
"stop loss"
arguments/parameters. May this thread assist you.
FIN.
if(!OrderSend(Symbol(),OP_BUY,d_vl,d_op,I_SET_SLIPPAGE,d_sl,d_tp,NULL,I_MAGIC_NUMBER,0,clrNONE))
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: SUKHMANJIT Singh Source score (net votes, not local likes): 3 Original post: https://stackoverflow.com/questions/66328697 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I am trying to create a EA in mql4, but in OrderSend function, when i use some value instead of zero it show ordersend error 130. Please help to solve this problem Code line is int order = OrderSend("XAUUSD",OP_SELL,0.01,Bid,3,Bid+20*0.01,tp,"",0,0,Red);
Checking account access…