title: What is the correct way to set StopLoss and TakeProfit in OrderSend() in MetaTrader4 EA?
body: <p>I'm trying to figure out if there is a <strong>correct way</strong> to set the <em>Stop Loss</em> (SL) and Take Profit (TP) levels, when sending an order in an <em>Expert Advisor</em>, in MQL4 (Metatrader4). The functional template is: </p>
  
  <p><code>OrderSend( symbol, cmd, volume, price, slippage, stoploss, takeprofit, comment, magic, expiration, arrow_color);</code></p>
  
  <p>So naturally I've tried to do the following:</p>
  
  
  
  <pre class="lang-cpp prettyprint-override"><code>double dSL = Point*MM_SL;
  double dTP = Point*MM_TP;
  
  if (buy)  { cmd = OP_BUY;  price = Ask; SL = ND(Bid - dSL);  TP = ND(Ask + dTP); }
  if (sell) { cmd = OP_SELL; price = Bid; SL = ND(Ask + dSL);  TP = ND(Bid - dTP); }
  
  ticket = OrderSend(SYM, cmd, LOTS, price, SLIP, SL, TP, comment, magic, 0, Blue);
  </code></pre>
  
  <p>However, there are as many variations as there are scripts and EA's. So far I have come across these. </p>
  
  <p>In the <em>MQL4 Reference</em> in the <em>MetaEditor</em>, the documentation say to use: </p>
  
  <pre class="lang-cpp prettyprint-override"><code>OrderSend(Symbol(),OP_BUY,Lots,Ask,3,
    NormalizeDouble(Bid - StopLoss*Point,Digits),
    NormalizeDouble(Ask + TakeProfit*Point,Digits), 
    "My order #2",3,D'2005.10.10 12:30',Red); 
  </code></pre>
  
  <p>While in the "same" documentation <a href="https://docs.mql4.com/trading/ordersend" rel="noreferrer">online</a>, they use: </p>
  
  <pre class="lang-cpp prettyprint-override"><code>double stoploss = NormalizeDouble(Bid - minstoplevel*Point,Digits);
  double takeprofit = NormalizeDouble(Bid + minstoplevel*Point,Digits);
  int ticket=OrderSend(Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My order",16384,0,clrGreen);
  </code></pre>
  
  <p>And so it goes on with various flavors, <a href="https://www.forexboat.com/ordersend-error-130/" rel="noreferrer">here</a>, <a href="https://www.iexpertadvisor.com/metatrader-mql-course-module-3-opening-a-trade-using-ordersend/" rel="noreferrer">here</a> and <a href="http://mql4tradingautomation.com/mql4-submit-order/" rel="noreferrer">here</a>...</p>
  
  <p>Assuming we are interested in a <code>OP_BUY</code> and have the signs correct, we have the options for basing our SL and TP values on: </p>
  
  <pre class="lang-cpp prettyprint-override"><code>bid, bid 
  bid, ask
  ask, ask
  ask, bid
  </code></pre>
  
  <p><strong>So what is the correct way to set the SL and TP for a BUY?</strong></p>
  
  <p>(What are the advantages or disadvantages of using the various variations?)</p>
  
  <hr>
  
  <p><strong>EDIT</strong>: 2018-06-12</p>
  
  <p>Apart a few details, the answer is actually quite simple, although not obvious. Perhaps <strong>because MT4 only show <em>Bid</em> prices on the chart (by default)</strong> and not both <em>Ask</em> and <em>Bid</em>.</p>
  
  <p>So because: <code>Ask &gt; Bid</code> and <code>Ask - Bid = Slippage</code>, it doesn't matter which we choose as long as we know about the slippage. Then depending on what price you are following on the chart, you may wish to decide on using one over the other, adding or subtracting the Slippage accordingly.  </p>
  
  <p>So when you use the measure tool to get the <em>Pip</em> difference of currently shown prices, vs your "exact" SL/TP settings, you need to keep this in mind.</p>
  
  <p>So to avoid having to put the Slippage in my code above, I used the following for <strong><code>OP_BUY</code></strong>: <code>TP = ND(Bid + dTP);</code> (and the opposite for <strong><code>OP_SELL</code></strong>.) </p>