This function has one parameter, orderType
, which represents the order type you want to calculate the average price for (OP_BUY
or OP_SELL
). The function uses a for loop to iterate through all orders in the account, similar to the previous two functions. If the order type matches orderType
, the function sums the order values and the total volume of that type. Finally, it returns the average price by dividing the total value by the total volume. If the total volume is 0, the function returns 0.
// Function to calculate the average price for Buy and Sell orders // orderType = "OP_BUY" (Buy order) or "OP_SELL" (Sell order) double GetAveragePrice(string orderType ) { // Get the total number of orders in the account int totalOrders = OrdersTotal(); // If there are no orders, return 0 if (totalOrders < 1) { Print("No orders in the account."); return 0; } // Initialize total value and total volume variables double totalOrderPrice = 0; double totalOrderLots = 0; // Loop through all orders in the account for (int i = 0; i < totalOrders; i++) { // Select the order at position i if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; // If the current order type matches orderType if (OrderType() == orderType )//"OP_BUY", "OP_SELL" { // Calculate the total order value and total volume totalOrderPrice += OrderOpenPrice() * OrderLots(); totalOrderLots += OrderLots(); } } // If the total volume is 0, return 0 if (totalOrderLots == 0) { Print("No ", (orderType == OP_BUY ? "Buy" : "Sell"), " orders in the account."); return 0; } // Return the average price for the specified order type double averagePrice = totalOrderPrice / totalOrderLots; Print("The average price of ", (orderType == OP_BUY ? "Buy" : "Sell"), " orders is: ", DoubleToStr(averagePrice, Digits)); return averagePrice; }
This function is designed to calculate the average price of Buy or Sell orders in a trading account. Below is a detailed explanation of each part of the function:
- Input parameter: The function takes an integer parameter to determine the type of order to calculate the average price for (
OP_BUY
orOP_SELL
). - Retrieve the total number of orders: The function uses
OrdersTotal()
to get the total number of active orders in the account. - Check order count: If there are no orders, the function prints a message and returns 0.
- Initialize total value and total volume: These variables are used to calculate the average price.
- Loop through each order: A loop iterates through all orders in the account.
- Select and check order type: The function uses
OrderSelect()
to select an order by position and checks if its type matches the specified order type. - Calculate total value and volume: If the current order matches the specified type, the function accumulates its total value and volume.
- Check total volume: If the total volume is 0, meaning there are no matching orders, the function prints a message and returns 0.
- Return the average price: The function calculates the average price by dividing the total value by the total volume, prints the result, and returns the calculated value.
For each order in the account, this function calculates the total value and total volume of the specified order type. Then, it computes the average price by dividing the total value by the total volume. Finally, the function prints the calculated average price and returns it.