Calculating to total number of lots open.

 
Hi

I wonder if somecould point me in the right direction, what I am trying to do is code a hedge. I am trying to work out a way of calulating the total number of lots which are open on a given symbol in a given direction at a given time, in order for me to place an opposite trade of equal ammount of lots to create a perfect hedge.

Any help would be most appreciated.
Thanks
Scouseman
 
Check out OrdersTotal(), OrderSelect(), OrderType(), OrderSymbol(), OrderOpenTime(), OrderLots(), etc. at https://docs.mql4.com/trading.

All you need is a loop which goes through all open orders picked by OrderSelect() and filters out all unsuitable orders.

double lots = 0.;
int i = OrdersTotal();
datetime tm;
while (i > 0)
{
    i--;
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    if (OrderSymbol() != Symbol() /* or whatever symbol */) continue;
    if (OrderType() != givenType) continue;
    tm = OrderOpenTime();    
    if (tm < intervalStart) continue;
    if (tm > intervalEnd) continue;
    lots += OrderLots();
}



Good luck,
mqlexpert {at} gmail {dot} com

 
Hi Irtron

Many Thanks for your help, it was most helpfull

Regards

Scouseman