OrdersTotal()

 
Hi peeps,

Just a quick question, I am in the middle of writting an EA, where it places a buystop and a sellstop at the same.

At the moment, I want the EA to place a maxium of 2 pending orders, (buystop/sellstop) per currency. If I use the following code, will that only place 2 orders all together on all currencys, or place 2 orders per currency, if that makes sence?

 if (OrdersTotal() < 2) { 



For example, I trade gbp/usd and eur/usd. I want to place a buystop order and a sellstop sellstop on each of these currencys.

Thanks

Bob

 
Bob,

OrdersTotal returns the number of orders that you have open or pending for all currencies. You must iterate through the current orders to determine what trades you have on the current symbol. E.g.

    bool HaveBuyOrder = false, HaveSellOrder = false;

    for (int iter = OrdersTotal(); iter >= 0; iter--) {
        if (OrderSelect(iter, SELECT_BY_POS, MODE_TRADES)) {
            if (OrderSymbol() == Symbol() &&
              (OrderType() == OP_BUY || OrderType() == OP_BUYSTOP)) {
                HaveBuyOrder = true;
            } else if (OrderSymbol() == Symbol() &&
              (OrderType() == OP_SELL || OrderType() == OP_SELLSTOP)) {
                HaveSellOrder = true;
            }
        }
    }



You could write your own functions, HaveBuyOrder and HaveSellOrder, that return a boolean value using the above logic. This helps to simplify the main loop.

HTH,

Aaron.

 
Corrections have been made to above listing.
Reason: