Lesson 16: Latest Lots Function to Retrieve the Most Recent Lots in MT4

Lesson 16: Latest Lots Function to Retrieve the Most Recent Lots in MT4
To retrieve the lots of the most recent order, you can use the OrdersTotal() function to get the number of currently open orders, then use a loop to get information about each order and find the most recent one. For example, to get the lots of the most recent order, you can use the following code:

double LayLotMoiNhat(string type)
{
   double Lotmoinhat = 0;
   for(int i = 0; i < OrdersTotal(); i++)//>
    {
      if(OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
     if(OrderSymbol()==Symbol() && OrderMagicNumber()== MagicNumber)
         {
            if(OrderType()==OP_BUY && type== "OP_BUY")   Lotmoinhat = OrderLots();
            if(OrderType()==OP_SELL && type== "OP_SELL") Lotmoinhat = OrderLots();
         }
    }
   return Lotmoinhat ;
}

The GetLatestLots function is used to retrieve the lot size of the most recently opened order of the specified order type (OP_BUY or OP_SELL). Below is a detailed description of this function:

  1. Input Parameter:
    • type: A string specifying the type of order you want to retrieve the lot size for. It can be “OP_BUY” or “OP_SELL”.
  2. Local Variable:
    • double latestLot = 0: This variable is initialized with a default value of 0. It will store the lot size of the most recently opened order of the specified type.
  3. Loop through open orders:
    • The for loop iterates through all open orders using the OrdersTotal() function to get the total number of current orders.
    • For each order, use the OrderSelect() function to select the current order by position (SELECT_BY_POS) in the order list.
    • Then, check if the selected order has the same symbol (OrderSymbol()) and Magic number (OrderMagicNumber()) as the symbol and Magic number specified in the function.
    • Next, check if the order type is OP_BUY and the passed-in type is also “OP_BUY”, or if the order type is OP_SELL and type is “OP_SELL”, then retrieve the lot size using the OrderLots() function and assign it to latestLot.
  4. Return the lot size of the latest order:
    • At the end of the loop, the function returns the lot size of the most recently opened order of the specified type. If no matching order is found, the function returns the default value of 0.

Note that you should make sure to call this function with the appropriate order type and check the result before using the returned lot size in your trading strategy.

Leave a Reply

Your email address will not be published. Required fields are marked *