title: how to add a stoploss in mql5
body: <p>I have a code that execute trading when 2 moving average crossover happen in mt5, the problem is I do not know how to code the stoploss which is the opposite of the crossover, let say i'm in a sell position with fast moving average is crossing below slow moving average , the position is kept open until new buy moving average crossover happen, then the sell position is closed with new buy position is open. anybody can help me?</p>
  <p>'''</p>
  <p>#include &lt;Trade/Trade.mqh&gt;</p>
  <p>CTrade trade;</p>
  <p>int OnInit() {
  Print(&quot;OnInit&quot;);</p>
  <p>return(INIT_SUCCEEDED);
  }</p>
  <p>void OnDeinit(const int reason){</p>
  <p>}</p>
  <p>void OnTick(){</p>
  <pre><code>  static datetime timestamp;
    datetime time = iTime(_Symbol, PERIOD_CURRENT, 0); 
    if(timestamp != time) {
       timestamp = time ;
    
  
    static int handleSlowMa = iMA(_Symbol,PERIOD_CURRENT, 200, 0, MODE_SMA, PRICE_CLOSE);
    double slowMaArray[];
    CopyBuffer(handleSlowMa, 0,1,2,slowMaArray);
    ArraySetAsSeries(slowMaArray, true);
    
    int handleFastMa = iMA(_Symbol,PERIOD_CURRENT, 20, 0, MODE_SMA, PRICE_CLOSE);
    double fastMaArray[];
    CopyBuffer(handleFastMa, 0,1,2,fastMaArray);
    ArraySetAsSeries(fastMaArray, true);
    
    if(fastMaArray[0] &gt; slowMaArray[0] &amp;&amp; fastMaArray[1] &lt; slowMaArray[1]) {
       Print(&quot;Fast Ma is now &gt; slow ma&quot;);
       double ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
       trade.Buy(0.1,_Symbol, ask, &quot;This is a buy&quot; );
     
    }
    
   
    if(fastMaArray[0] &lt; slowMaArray[0] &amp;&amp; fastMaArray[1] &gt; slowMaArray[1]) {
       Print(&quot;Fast Ma is now &lt; slow ma&quot;);
       double bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
       trade.Sell(0.1,_Symbol, bid,&quot;This is a sell&quot; );
    }
    
    Comment(&quot;\nslowMaArray[0]: &quot;,slowMaArray[0],
            &quot;\nSlowMaArray[1]: &quot;,slowMaArray[1],
            &quot;\nfastMaArray[0]: &quot;,fastMaArray[0],
            &quot;\nfastMaArray[1]: &quot;,fastMaArray[1]);
    
  </code></pre>
  <p>}</p>
  <p>}</p>
  <p>'''</p>