I’d like to create an EA or indicator that shows me the total lot position size of my sell trades. And seperately of my buy trades.
I found the script already, so that is easy. I put it in Meta-editor as EA, saved as MQ4 file and copied it to the folder: program files/MT4/expert. And now what? Sorry, this is my first time, what should I do now to check the total amount of lot size?
Below the script:
Introduction
It is useful to discover how many orders our EA has open and what type these open orders are, such as a buy or sell. It is thus useful to create a order counting function that can count the current number of open orders based on the order type.
MT4 Snippet
int OrdersTotalMagicOpen() {
int OrderCount = 0;
for (int l_pos_4 = OrdersTotal() - 1; l_pos_4 >= 0; l_pos_4--) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
if (OrderType() == OP_SELL || OrderType() == OP_BUY) OrderCount++;
}
return (OrderCount);
}
Explanation
We have named our order counting function OrdersTotalMagicOpen(). It will return an integer value of how many orders are currently opened on the specified chart symbol matching the magic number that we have passed as a function argument.
We start by declaring the OrderCount variable, the intial value of which is 0. We use the for operator to loop through the block of code, and the OrderSelect() function to examine the pool of currently open positions, checking for ones that match our order symbol and magic number.
If OrderMagicNumber() matches our MagicNumber, we can be confident that this order was placed by our EA. Note: uncertainty about EA identification would only arise if the user is running two EA’s on the same currency symbol with the same magic number.
If OrderMagicNumber() does not match, we use the != sign, which means “not equals”, the program can continue, that is, it discards unaffiliated trades and moves on to trades that match up.
We next see if the order matches the order types OP_BUY or (||) OP_SELL. OP_BUY is a constant that indicates a buy market order. To count other types, simply replace OP_BUY or OP_SELL with the appropriate order type constant.
If the order matches both our magic number and chart symbol and order type, the value of the OrderCount will be incremented by one. After we have looped through all the orders in the order pool, we return the value of OrderCount to the calling function.
MT4 Usage
if (OrdersTotalMagicOpen() > 0 && CloseOrders == true){ // Close all orders }
If there are orders opened by this EA, and the value of CloseOrders is true, then the code inside the braces will run, which will close all the open orders.
Counting only Buy or Sell Orders
Sometimes it is necessary to just count the open buy orders or open sell orders, in which case we would have to split out the above function into two.
Counting Buy Orders
int BuyTotalMagicOpen() {
int OrderCount = 0;
for (int l_pos_4 = OrdersTotal() - 1; l_pos_4 >= 0; l_pos_4--) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
if (OrderType() == OP_BUY) OrderCount++;
}
return (OrderCount);
}
Counting Sell Orders
int SellTotalMagicOpen() {
int OrderCount = 0;
for (int l_pos_4 = OrdersTotal() - 1; l_pos_4 >= 0; l_pos_4--) {
OrderSelect(l_pos_4, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
if (OrderType() == OP_SELL) OrderCount++;
}
return (OrderCount);
}