Lesson 15: Function to Get the Latest Price (Latest Price MT4) Among Current Orders

Bài 15: Hàm Lấy giá mới nhất (Latest Price MT4) trong tổng lệnh hiện tại
Hello! To get the latest order entry price among n orders in MT4, you can use the OrderSelect() function to select the most recently opened order. Then, use OrderOpenPrice() to get its entry price. Here’s a simple example:

double GiaMoiNhat(string type)
{
   double GiaVaoLenh = 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")   GiaVaoLenh = OrderOpenPrice();
            if(OrderType()==OP_SELL && type== "OP_SELL") GiaVaoLenh = OrderOpenPrice();
         }
    }
   return GiaVaoLenh;
}

The GiaMoiNhat function is used to retrieve the latest open price of a specified order type (OP_BUY or OP_SELL). Here’s a detailed explanation of the function:

  1. Input Parameter:

    • type: A string specifying the order type you want to retrieve. It can be "OP_BUY" or "OP_SELL".

  2. Local Variable:

    • double GiaVaoLenh = 0: This variable is initialized to 0 and will hold the latest entry price of the specified order type.

  3. Loop Through Open Orders:

    • The for loop iterates through all open orders using OrdersTotal() to get the total number.

    • Each order is selected by position using OrderSelect().

    • It then checks if the selected order matches the current symbol (OrderSymbol()) and the specified Magic Number (OrderMagicNumber()).

    • Next, it checks if the order type matches the type parameter passed into the function. If it matches, the function retrieves the order’s entry price via OrderOpenPrice() and stores it in GiaVaoLenh.

  4. Return the Latest Open Price:

    • After completing the loop, the function returns the latest entry price for the specified order type. If no matching order is found, the function returns the default value 0.

Note: Be sure to call this function with the correct order type and validate the returned value before using it in your trading strategy.

Leave a Reply

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