MQL4 - automated forex trading   /  

Forum

Market & Pending Order Control

Back to topics list To post a new topic, please log in or register

avatar
44
Superion 2007.09.27 08:50 

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

article

Interview with Vladimir Lekhovitser (PrizmaL)

As to our EA, PrizmaL, it belongs to the family of PH (Pips-Hunter) strategies.


avatar
2462
phy 2007.09.27 09:22 

"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


avatar
44
Superion 2007.09.27 09:36 

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


avatar
2462
phy 2007.09.27 09:37 
See reply above... fat fingered the entry earlier.

avatar
44
Superion 2007.09.27 12:50 

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?


avatar
2462
phy 2007.09.27 17:12 

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


avatar
44
Superion 2007.09.28 15:00 

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!


avatar
2462
phy 2007.09.28 15:24 

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.


avatar
44
Superion 2007.09.28 17:06 

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...


avatar
2462
phy 2007.09.28 17:57 

Do you zero the OpenBuyOrders/OpenSellOrders counters before looping?


avatar
44
Superion 2007.09.28 21:00 

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?

Back to topics list  

To add comments, please log in or register