Error code if not enough liquidity to fullfill order?

 

If my EA tries to send and order, and the order is rejected because there is not enough liquidity in the markets at that time (usually large orders), is there any way of knowing that that's the reason for the order being rejected?

Because if I know that that's the reason, I can simply code my EA to try again and just lower the amount of lots. However if I don't know that, then it will just keep trying and failing...

I asked my broker about this, and they said in such cases the only error that is returned is "off quotes". But of course that gives me no "clue" as to if the order was rejected due to lack of liquidity.

Any ideas on how to know this?

I thought maybe try 2 times and if keep getting the same "off quotes" error, just try and reduce lot size then. But that's kind of "sloppy" I guess? I'd rather know for a fact that that's the reason, so I can tell my EA to act accordingly right away.


Thanks!

 
nanquan:

If my EA tries to send and order, and the order is rejected because there is not enough liquidity in the markets at that time (usually large orders), is there any way of knowing that that's the reason for the order being rejected?

Because if I know that that's the reason, I can simply code my EA to try again and just lower the amount of lots. However if I don't know that, then it will just keep trying and failing...

I asked my broker about this, and they said in such cases the only error that is returned is "off quotes". But of course that gives me no "clue" as to if the order was rejected due to lack of liquidity.

Any ideas on how to know this?

I thought maybe try 2 times and if keep getting the same "off quotes" error, just try and reduce lot size then. But that's kind of "sloppy" I guess? I'd rather know for a fact that that's the reason, so I can tell my EA to act accordingly right away.


Thanks!

When you write the EA or CI or Script, alway add on top of your code with this 

#include <stdlib.mqh>

Then on Print() (or Alert()) add this :

int error = GetLatError();
Print ("Error number ",error," = ",ErrorDescription(error) );

And you will find the error for not enough liquidity as error number 134 (not enough money), this description can be found in MetaEditor's libraries's stdlib.mq4.

 

Better than retrying and lowering the lot size basing on the "Not Enough Money" error received, you should check beforehand if the account equity is enough for open the lot-size your EA wants to open. For this you simply add this check to your lot-size calculation at the final stage (so before submitting it to OrderSend() ):


if (MarketInfo(Symbol(), MODE_MARGINREQUIRED)!=0) FinalLots=MathMin(YourInputLotSize,AccountEquity()/MarketInfo(Symbol(), MODE_MARGINREQUIRED)*0.85); //add little safety buffer of 0.85 here instead of using the fully available margin
FinalLots=MathCeil(FinalLots / MarketInfo(Symbol(),MODE_LOTSTEP)) * MarketInfo(Symbol(),MODE_LOTSTEP); //check for lot-stepping and lot-digits
FinalLots=MathMax(FinalLots, MarketInfo(Symbol(),MODE_MINLOT)); //check for brokers min and max lot-size
FinalLots=MathMin(FinalLots, MarketInfo(Symbol(),MODE_MAXLOT));
 
geektrader:

Better than retrying and lowering the lot size basing on the "Not Enough Money" error received, you should check beforehand if the account equity is enough for open the lot-size your EA wants to open. For this you simply add this check to your lot-size calculation at the final stage (so before submitting it to OrderSend() ):


Yeah, I agree with that, better to avoid error that dealing after an error.

You should also calculate the loss from the SL target and a margin call calculation. 

 
Yep. Hope he does this already, calculating lot-size without taking SL and TickValue into account is not a good idea.
 

Guys I fear you misunderstand my question. I am not talking about a situation where I'm trying to open an order and my account doesn't have enough Equity/Free Margin!

I am talking about trying to open an order where the Order itself is so large (e.g. $50 Million) that there is not enough liquidity on the markets to fulfill that order.

So for example I'm trying to open order BUY at 500 lots (=$50 Million), and there is no one on the other side currently selling who is able to fulfill that order, and because of that the order gets rejected.

When that is the case, is there any way of knowing that that's the reason by way of the error code returned or not?

Like I said, my broker said that in such a case it just returns error "Off Quotes" (error 136) which is non specific.

Thanks!

 
nanquan:

Guys I fear you misunderstand my question. I am not talking about a situation where I'm trying to open an order and my account doesn't have enough Equity/Free Margin!

I am talking about trying to open an order where the Order itself is so large (e.g. $50 Million) that there is not enough liquidity on the markets to fulfill that order.

So for example I'm trying to open order BUY at 500 lots (=$50 Million), and there is no one on the other side currently selling who is able to fulfill that order, and because of that the order gets rejected.

When that is the case, is there any way of knowing that that's the reason by way of the error code returned or not?

Like I said, my broker said that in such a case it just returns error "Off Quotes" (error 136) which is non specific.

Thanks!

Humm.. That's why brokers have Maximum_Lots. Consider institutional brokers and platforms if you have money like that ;)
 
nanquan:

Guys I fear you misunderstand my question. I am not talking about a situation where I'm trying to open an order and my account doesn't have enough Equity/Free Margin!

I am talking about trying to open an order where the Order itself is so large (e.g. $50 Million) that there is not enough liquidity on the markets to fulfill that order.

So for example I'm trying to open order BUY at 500 lots (=$50 Million), and there is no one on the other side currently selling who is able to fulfill that order, and because of that the order gets rejected.

When that is the case, is there any way of knowing that that's the reason by way of the error code returned or not?

Like I said, my broker said that in such a case it just returns error "Off Quotes" (error 136) which is non specific.

Thanks!

Oh, that. That is correct - off quotes. No body selling/buying at that price. You should have a bigger slippage in the first place, or get a true RefreshRates() and change the buying/selling price to a new price.

BTW, compare to other market, liquidity in forex is big enough, so if you frequently get "off quotes", or trade time out, either do what I have suggested, or ask the broker who is their liquidity provider (which actually does not help at all) or change your account or broker to ECN one.

 
Yes, if you meant that there is noone available to take the opposite side of your trade because it is so large, there is no way to know this. You´ll only get OffQuotes / ReQuotes or slippage. No specific error message for that.