Place limit order as tight to market as possible

 

Hello community,

when trying to place a Buy Limit order programmatically, i get error 4765.

It turned out, that the limit price is too close to market.

It seems there is no information about an offset accepted by the broker for such orders.


This is my workaround code, which simply repeatedly steps down the price by 1 point an replaces the order until the specific error disappears.

But i am not happy with that solution for obvious reasons:

  // Lower upoint offset until price accepted            
               while(IsStopped() == false && counter > 0)
               {                  
                  price=alignedBid(-UPointOffset); //-this.spreadInt());                            
                  if(td.BuyLimit(volume,price,_symbol,sl,tp,0,0,comment) == false && GetLastError() == 4756)
                  {
                     UPointOffset += 1;
                     ResetLastError();
		     --counter;
                  }
                  else
                     break;                  
               }

alignedBid returns the current bid and UPointOffset is a positive integer as point multiplier. So alignedBid(-x) returns bid - x* Point(). td is of type CTrade. The code works and does the job. But there is no good break condition and it puts some milliseconds on runtime until order placements succeeds. In my tests with Bitcoin on FxPro, it turned out, that an offset of ~ 16000 points was required for the order being accepted and it tooks6 milliseconds in addition.

My questions: Is there a more smarter and safe method to accomplish this?
Thank you

 
Check for SYMBOL_TRADE_STOPS_LEVEL. It defines the minimum distance to place stop orders. If I'm not mistaken, it should work for placing tp/sl. Also check for SYMBOL_TRADE_FREEZE_LEVEL.
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Symbol Properties
  • www.mql5.com
To obtain the current market information there are several functions: SymbolInfoInteger() , SymbolInfoDouble() and SymbolInfoString() . The first...
 

Also check freeze level:

if(entry>Bid+SYMBOL_TRADE_FREEZE_LEVEL*_Point)
   trade.SellLimit(...);
Type of order/position
Activation price
Check
Buy Limit order
 Ask
Ask-OpenPrice >= SYMBOL_TRADE_FREEZE_LEVEL
Buy Stop order  Ask OpenPrice-Ask >= SYMBOL_TRADE_FREEZE_LEVEL
Sell Limit order  Bid OpenPrice-Bid >= SYMBOL_TRADE_FREEZE_LEVEL
Sell Stop order  Bid Bid-OpenPrice >= SYMBOL_TRADE_FREEZE_LEVEL
Buy position
 Bid TakeProfit-Bid >= SYMBOL_TRADE_FREEZE_LEVEL
Bid-StopLoss >= SYMBOL_TRADE_FREEZE_LEVEL
Sell position
 Ask Ask-TakeProfit >= SYMBOL_TRADE_FREEZE_LEVEL
StopLoss-Ask >= SYMBOL_TRADE_FREEZE_LEVEL
 
Emanuel Cavalcante Amorim Filho #:
SYMBOL_TRADE_STOPS_LEVEL

Hello Emanuel, thank you for help, this seems to work.

Reason: