Algo trading problem

 
Hey there! 

I have a problem with algo trading. Specifically with this command:

 trade.Buy(volume, _Symbol, UpperLimit + 5) 

When command executes I sometimes get the price of (UpperLimit + 5 + 7), which should not be possible. At the moment of this transaction my spread is 2.
From my understanding, BUY command setup in this way should make a trade at a price that is lower than ( UpperLimit + 5).

Best regards,

 

You seem to be placing a market order not a true limit order. This operates as "buy at market price" with a maximum deviation or slippage tolerance rather than a hard price limit. This is why it appears (but not actually) you're getting the upper limit plus spread, but it's really just a market order with 5 pips allowed for spread.

To do a hard price limit you'd add price validation.

// Place a BUY LIMIT order instead
OrderSend(_Symbol, OP_BUYLIMIT, volume, UpperLimit + 5, 3, 0, 0);

// Add Price Validation Before Market Orders:
mql4RefreshRates();
if (Ask <= UpperLimit + 5) {
    OrderSend(_Symbol, OP_BUY, volume, Ask, 3, 0, 0);
}

// Implement Strict Slippage Control:
mql4double maxPrice = UpperLimit + 5;
RefreshRates();
if (Ask <= maxPrice) {
    int slippage = (int)((maxPrice - Ask) / Point);
    OrderSend(_Symbol, OP_BUY, volume, Ask, slippage, 0, 0);
}

Note that this would be for MetaTrader 4. You left out like the most important bit of information known to man when asking this sort of thing: what version of MetaTrader you're using. MT5 would be different.

 
James McKnight #:

You seem to be placing a market order not a true limit order. This operates as "buy at market price" with a maximum deviation or slippage tolerance rather than a hard price limit. This is why it appears (but not actually) you're getting the upper limit plus spread, but it's really just a market order with 5 pips allowed for spread.

To do a hard price limit you'd add price validation.

Note that this would be for MetaTrader 4. You left out like the most important bit of information known to man when asking this sort of thing: what version of MetaTrader you're using. MT5 would be different.

Hey there. I am using MT5 can you help me with that?
 
..
 
Karlo Karlicc #:
..

Karlo, do not boost your post. If no one is answering, that means no one is interested and your next step is Freelance section 

NOTE: Traders and coders are working for free:

  • if it is interesting for them personally, or
  • if it is interesting for many members on this forum.

Freelance section of the forum should be used in most of the cases.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2025.05.30
  • www.mql5.com
The largest freelance service with MQL5 application developers