Search code examples
mql4trading

How to make an Expert advisor to enter a trade at an exact price?


I made an EA, it is to enter trade at a particular price. But I notice it is not entering the trade at the exact price, it is entering the trade at price that is 3-5 fractional pips above the specified price. What I want is that the EA enters the trade at the exact price. Can someone help please?


Solution

  • How to make an Expert advisor to enter a trade at an exact price?

    Making a trade is a bilateral contract - it has a side that offers you a price ( mediated by your Broker ) and it has a side ( you, in person or represented by an automated code-driven trading-agent ), who accepts a price. All regulated by Terms and Conditions signed / accepted for doing this business.

    // ----------------------------------------------------------------------------
    // Rule#0: Prices move faster, than the QUOTE-message ever makes it to your CPU
    // --------------------------
       RefreshRates(); // A MUST DO AS-LATE-AS-POSSIBLE, before placing a tight slippage XTO
    // --------------- // A RE-TEST AS-FAST-AS-POSSIBLE if XTO conditions hold
       ...
    // --------------- // GO / NO-GO XTO, always using Normalized values
       ticket = OrderSend( Symbol(),
                           XTO_OrderTYPE,
                           NormalizeDouble( XTO_volume, LotDigits ),
                           NormalizeDouble( XTO_price,  Digits() ),
                           MaxSlippage, //---------------------------- BE CAREFULL ON THIS
                           0,           // [XTO_price_SL]
                           0,           // [XTO_price_TP]
                           ordername,
                           MagNumber,
                           0,
                           clr
                           );
    

    Time matters, prices move indeed fast. VPS may avoid, but yet a part

    Before we get to the type-of-Contract, which decides, how a trade is being executed on the Broker side ( a spot-Buy-Long uses other price-handling than a pending-BuyStop ), first, let's look at the network latency ( how long does the Top-of-the-Book [ToB] price hold, before it is changed and advertised from market to Broker and from Broker to your computer or to VPS computer ( even the best co-located VPS machines are a few hundred meters "farther" on the cable, and "behind" the Broker's machine, thus a few orders of magnitude slower in receiving ToB-QUOTE-updates, than your Broker Server is ).

    The ToB-prices hold for way less than 100 ms in steady markets on FX-Majors, yet there are ofter thousands of wild moves during fundamental events, where each [ms] may contain tens, if not hundreds and sometimes thousands ToB-price changes per [ms].

    enter image description here

    If you insist on having an exact price for a trade, you may use but pending-contracts, not the immediate-price executed orders ( often called at-Market ).

    Your Broker Terms and Conditions define the rules for doing this.

    Be aware, that even pending-orders may "acquire" a price-slippage, a difference between an "ordered"-price and actually "executed"-price, so again, Terms and Conditions will rule ( SNB flash-crash event was a Tsunami a few years ago, after many Brokers filed for bankruptcy and KMPG and other court-named supervising bodies were liquidating ashes from uncovered pendin-orders for many many years later ), so due Risk-Management is a must and never believe an optimistic assumption a trade-order will get filled at-an-exact-price. There are both technical and legally supported reasons, why this is not always obeyed even for the pending-orders.