To calculate the total Lots currently on MT4, you can use the OrdersTotal
function to get the total number of open orders on the account and then loop through each order to retrieve the lots
value of each order and sum them up. For example:
double GetTotalLots(string type) { double totalLots =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") { totalLots +=OrderLots(); } if(OrderType()==OP_SELL && type== "OP_SELL") { totalLots +=OrderLots(); } } } return(totalLots ); }
The GetTotalLots
function is used to calculate the total lot size of open orders that match the specified currency pair and Magic Number, for a specific order type (OP_BUY or OP_SELL). Below is a detailed explanation of the function:
- Input parameter:
type
: A string that specifies the order type for which you want to calculate the total lot size (OP_BUY or OP_SELL).
- Local variable:
double totalLots = 0
: This variable is initialized with a default value of 0. It will store the total lot size of orders that meet the condition.
- Loop through open orders:
- The
for
loop runs through all open orders using theOrdersTotal()
function to get the current total number of 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 specified in the function. - Next, check if the order type is OP_BUY and the input
type
is “OP_BUY”, or if the order type is OP_SELL and thetype
is “OP_SELL”, then add the lot size of the order to thetotalLots
variable.
- The
- Return total lot size:
- When the loop finishes, the function will return the total lot size of matching open orders. If no orders are found or match the specified type, the function will return 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 total lot size in your trading strategy.[block_content id=”3041″]