Download MetaTrader 5

Closing a position

To add comments, please log in or register
Share your programs. That will improve your rating.
Florent
183
Florent 2016.04.02 11:40 

Hello,

I would like to know if this code is correct to close an opened position with lot size = LOT :

What I did is that I removed SL and TP arameter so that it close at the latest bid price.

Thank you

//--- Putting all together
   if(Sell_Condition_1)
      {
      // any opened Buy position?
      if(Buy_opened)
        {
         Alert("We already have a Buy Position!!!");
         ZeroMemory(mrequest);
         mrequest.action=TRADE_ACTION_DEAL;                                // immediate order execution
         mrequest.price = NormalizeDouble(latest_price.bid,_Digits);           // latest Bid price
         //mrequest.sl = NormalizeDouble(latest_price.bid + STP *_Point,_Digits); // Stop Loss
         //mrequest.tp = NormalizeDouble(latest_price.bid - TKP *_Point,_Digits); // Take Profit
         mrequest.symbol = _Symbol;                                          // currency pair
         mrequest.volume = Lot;                                              // number of lots to trade
         mrequest.magic = EA_Magic;                                          // Order Magic Number
         mrequest.type= ORDER_TYPE_SELL;                                     // Sell Order
         mrequest.type_filling = ORDER_FILLING_FOK;                          // Order execution type
         mrequest.deviation=100;                                             // Deviation from current price
         //--- send order
         OrderSend(mrequest,mresult);
         // get the result code
         if(mresult.retcode==10009 || mresult.retcode==10008) //Request is completed or order placed
           {
            Alert("SELL.sl ", mrequest.sl - mrequest.price);
            Alert("A Sell order has been successfully placed with Ticket#:",mresult.order,"!!");
           }
         else
           {
            Alert("The Sell order request could not be completed -error:",GetLastError());
            ResetLastError();
            return;
           }
        }
     }
   return;
  }

Florent
183
Florent 2016.04.04 14:11  
Works like a charm
To add comments, please log in or register