Market & Pending Order Control

 

Looking for code to count market orders without counting pending orders. OrdersTotal() - well does what its suppose to do and counts Market + Pending orders then outputs ithe total, not what I'm after.

Also I'm looking for two close functions that closes only market orders and the second only pending orders. This is what I have but executes quite slow. ..

//Market Orders Close if (CloseAllTrades == TRUE) {
for (g_pos_128 = 0; g_pos_128 < OrdersTotal(); g_pos_128++) {
if (OrderSelect(g_pos_128, SELECT_BY_POS, MODE_TRADES) == FALSE) Print("OrderSelect failed:", GetLastError());
else {
if (OrderType() == OP_BUY) OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);
if (OrderType() == OP_SELL) OrderClose(OrderTicket(), OrderLots(), Ask, 3, Red);
}
}
}

//Pending Orders Close
if (CloseAllOrders == TRUE) {
for (g_pos_128 = 0; g_pos_128 < OrdersTotal(); g_pos_128++) {
if (OrderSelect(g_pos_128, SELECT_BY_POS, MODE_TRADES) == FALSE) Print("OrderSelect failed:", GetLastError());
else OrderDelete(OrderTicket());
}
}

Thanks,

-S

 

"Looking for code to count market orders without counting pending orders. OrdersTotal() - well does what its suppose to do and counts Market + Pending orders then outputs ithe total, not what I'm after."

Loop through the Open and Pending orders and count the ones that are open and pending.

OP_BUY - buying position,
OP_SELL - selling position,
OP_BUYLIMIT - buy limit pending position,
OP_BUYSTOP - buy stop pending position,
OP_SELLLIMIT - sell limit pending position,
OP_SELLSTOP - sell stop pending position

"Also I'm looking for two close functions that closes only market orders and the second only pending orders. "

Again, you have to distingusigh between order types. Add more code.

"but executes quite slow..."

It takes time to close orders, mostly transit time to the server, server processing, then transi time to return the result.

I believe the code sends one close order, then waits for response. Synchronous operation.

Put some debugging code in to see where the tme lag is. GetTickCount() will give you access to a millisecond counter.

Find out how long it takes to close each order:

int startCount;

if(OrderType() == OPBUY){

startCount = GetTickCount();

OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue);

Print("Time to close = "+GetTickCount() - startCount+" milliseconds");

}

Also, you should loop like this:

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

NOT like this:

for(i = 0; i < OrdersTotal(); i++)

If you loop up from 0 to OrdersTotal() when an order is closed the list of orders changes

A B C D Four Orders in OrdersTotal()

0 1 2 3

Order a,b,c, and d

if you close A you then have

B C D Now three orders in the list

0 1 2

Your loop will be at 1, looking at order C ( B gets skipped).

You Close C

B D

0 1

Your Loop is at 2, the loop completes, but two orders remeain.

The correct way picks orders to close off the end of the list.

Alternatively, loop through the list, pick an order to close, then loop through the changed list again, and pick another to close

 

phy thanks for the reponse/s and I should have been more clearer, that Pending Order Close takes a quite a long time. However Market Order Close seems to react almost instantly once sent but as you send depends on broker execution.

Any ideas how I can get a count of Makert Orders versus Pending Order as totals? And not combined? Cheers!

-S

 
See reply above... fat fingered the entry earlier.
 

WOW phy appreciate your contribution. You've saved me alot of research time. It's just coming to 1 week since attempting to program in MQL. Forums and resources like this one I'm surprised how quickly one can come up to speed.

How can I shout you a beer?

 

Come down to the bar, surely you'll recognize my grumpy face.

 

Yeah that face looks familiar? I do hope you are having much success with MT4 trading.

Getting into Array's at the moment. Complex at first but possibilities seem endless. ..

This is the simple code that I came up with to calculate open market orders only. ..

//Count Open Market Orders
MarketOrders = 0;
for(cnt = OrdersTotal()-1; cnt >= 0; cnt--)
//for(cnt=0;cnt<OrdersTotal();cnt++) //DO NOT LOOP THIS WAY - thanks phy
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
{
if (OrderType()==OP_BUY)
{
OpenBuyOrders++;
}
if (OrderType()==OP_SELL)
{
OpenSellOrders++;
}
MarketOrders = OpenBuyOrders + OpenSellOrders;
}
}

I changed the for statement as per you recommendation and works great. Cheers!

 

Did that take care of the "speed" problem?

Before, you may have been waiting several ticks (executions of the loop) to get multiple closes executed, in addition to the normal wait on the server to respond.

 

Mmmm something has happened as its looping through everytime the price moves and adds++ 1 Open Market Orders each time. Trying to locate the issue but not having much luck, i'll find it eventually.

Strange as it was working fine yesterday? The speed was much better after implementing your changes...

 

Do you zero the OpenBuyOrders/OpenSellOrders counters before looping?

 

Okay I figured it out... first is to zeroize the counters as you've stated. Second I moved the looping declarations (cnt; MarketOrders;) to the top which caused the doubling effect as described above. Moved the decs to within the int start() section before the 'for' statement and all is working well. So some declarations must be definied within the int start() section as I've seen in sample code but don't understand why?

Instead I've used one of the sample arrays from within this site and to capture open and pending orders information. Then call the function when needed. You see for my fist EA I've gone and based it off a grid based system which makes order management a little more of a challenge. Getting there slowy thanks to your help and researching...

I believe I'll have a couple more questions and hope you done mind?

Reason: