Orderopen price restriction fails to restrict

 
void OpenLong(double Price){
  RefreshRates();
  if(MarketInfo(Symbol(),MODE_ASK)<=Price){
  OrderSend(Symbol(), OP_BUY, 0.10, MarketInfo(Symbol(),MODE_ASK), 0, 0, 0, "", 0, 0, Blue);}
  return(0);
  }

This is the code which i want to use to open a long, with restriction that the orderopenprice can be up to double "Price".

So OpenLong(1.5000) would mean, buy 0.10 lots if available for 1.5000 or less.

But, sometimes the order gets filled at a higher price, say 1.5001 or 1.5002. Even with 0 slippage allowed.

How is this possible and, how can i solve this?

Thanks!

 
After RefreshRates(); try adding a variable called iAsk or Whatever. Check if iAsk<=Price and then in OrderSend() use iAsk instead and see if that helps.
 
ubzen:
After RefreshRates(); try adding a variable called iAsk or Whatever. Check if iAsk<=Price and then in OrderSend() use iAsk instead and see if that helps.

Thanks. I've corrected it to this and am going to test it now.
void OpenLong(double Price){
  RefreshRates(); iAsk = MarketInfo(Symbol(),MODE_ASK);
  if(iAsk<=Price){
  OrderSend(Symbol(), OP_BUY, 0.10, iAsk, 0, 0, 0, "", 0, 0, Blue);}
  return(0);
  }
 
OK, so i've just tested it, but unfortunatly it doesn't work. Do you or maybe someone else knows what i am doing wrong here?

I think your (ubzen) solution is good, but just seems that MT4 is making a mess of it?
 
  1. captaincrunchy:
    So OpenLong(1.5000) would mean, buy 0.10 lots if available for 1.5000 or less.
    The code says to open a buy at 1.5 +/- 0 (no slippage) If the market is not exactly at 1.5 order fails. If you want 1.5 or less use a OP_BUYLIMIT.
  2. Always test return codes
    int ticket = OrderSend(...
    if (ticket < 0) Alert("OrderSend failed: ", GetLastError());

  3. Why use
    MarketInfo(Symbol(),MODE_ASK)
    and not just the simpler Ask. iAsk = Ask;

  4. Unless your EA is looping (not returning from start) or opening multiple orders, the Refresh is unnecessary.
 
WHRoeder:
  1. captaincrunchy:
    So OpenLong(1.5000) would mean, buy 0.10 lots if available for 1.5000 or less.
    The code says to open a buy at 1.5 +/- 0 (no slippage) If the market is not exactly at 1.5 order fails. If you want 1.5 or less use a OP_BUYLIMIT.
  2. Always test return codes
  3. Why use and not just the simpler Ask. iAsk = Ask;

  4. Unless your EA is looping (not returning from start) or opening multiple orders, the Refresh is unnecessary.


Thanks, WHRoeder.

1. It's in my strategy to open direct marketorders, my edge is a few pips per trade. Too small to use limit orders. But if i get slippage it will kill my profit.

2. Thanks, will do that.

3. Cause i use it with more pairs all being traded from one EA, one chart.

4. Yes it's looping.

Actually i still don't know why it wouldn't work. Seems that MT4 always allows some slippage (but always negative it seems?)?