2

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);
SUKHMANJIT Singh
  • 21
  • 1
  • 1
  • 2
  • 1
    Bad tp value? Also, 20 cents looks like a very tight stop for XAUUSD. You are likely hitting the stop level (MODE_STOPLEVEL) with that. – Enivid Feb 23 '21 at 18:24

3 Answers3

2

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

Amr Sakr
  • 181
  • 1
  • 4
  • I also checked with 0.0001 and 0.01 instead of Point, but not working – SUKHMANJIT Singh Feb 23 '21 at 08:21
  • 1
    you have to check the minimum stop level allowed by the broker and make the distance of the stops higher than this stop level you can know the stop level by calling MarketInfo(Symbol(),MODE_STOPLEVEL) – Amr Sakr Feb 24 '21 at 04:59
1

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!

Diego
  • 11
  • 2
0

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.

PaulB
  • 1,262
  • 1
  • 6
  • 17