calculate open orders and close

 

Hi,

I have problem to write code that is monitoring open orders and if max orders is equal number of open orders, than it should close all open orders.


I will define max orders

maxordes =4;

than:

if (openorders == maxorders)

{ close all open orders ( all 4)}


can me anybody help?

I tried with orders total and cycles, but all is not working properly


Thanx in forward

 

Use this to find open & pending orders:

OrdersTotal()
Returns market and pending orders count.

Then step through the orders using a for loop (counting down) checking which are open (not pending) if that number equals 4, step through them once again using another loop and close them using:

bool OrderClose(int ticket, double lots, double price, int slippage, color Color=CLR_NONE)
Closes opened order. If the function succeeds, the return value is true. If the function fails, the return value is false. To get the detailed error information, call GetLastError().

If you need specific help with your code . . post it. If you do please use the SRC button . .

 

But .... OrdersTotal return ALL open Orders, from all Charts, manually and by EA opened.

If you need only 4 Orders from current chart you need to loop throug and count (Symbol, Magic Number,...)

 

yes, I tried this, but doesn`t work...

can anybody take a look?


//

int nOrderCount=0;

for (int j=OrdersTotal()-1 ; j>=0 ; j--)

{

if (!OrderSelect(i,SELECT_BY_POS)) continue;

if (OrderType() == OP_BUY && OP_SELL)

if (OrderMagicNumber() == magic)

nOrderCount++;

}

if (nOrderCount >= MaxTrades)

{

for(i=OrdersTotal()-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS);

if(OrderMagicNumber()==magic)

{

type = OrderType();

result = false;

switch(type)

{

//Close opened long positions

case OP_BUY : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,Red);

break;

//Close opened short positions

case OP_SELL : result = OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,LimeGreen);

}

if(result == false)

{

Sleep(0);

}

}

}

Print ("close max trades");

}