Deleting pending orders

User avatar
traderuk
Trader
Posts: 18
Joined: Wed Mar 20, 2013 11:12 pm

Re: Deleting pending orders

Post by traderuk »

That works great and makes sense, thank you!
User avatar
traderuk
Trader
Posts: 18
Joined: Wed Mar 20, 2013 11:12 pm

Re: Deleting pending orders

Post by traderuk »

Any ideas why my original method wasn't working... to help with the learning? :)
dietcoke
Trader
Posts: 162
Joined: Tue Nov 15, 2011 9:59 pm

Re: Deleting pending orders

Post by dietcoke »

traderuk wrote:Any ideas why my original method wasn't working... to help with the learning? :)
this would work, except it will only ever close one order due to the positioning of the return(0) line

Code: Select all

//+------------------------------------------------------------------+
//| Close Pending Orders                                           |
//+------------------------------------------------------------------+
void ClosePendings()
{
int count=0;
int trade;
int trades=OrdersTotal();
   for(trade=trades-1;trade>=0;trade--)
   {
   OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()==OP_BUYSTOP || OP_SELLSTOP){
         OrderDelete(OrderTicket());
      }
      return(0);
   }
}

try...

Code: Select all

//+------------------------------------------------------------------+
//| Close Pending Orders                                           |
//+------------------------------------------------------------------+
void ClosePendings()
{
int count=0;
int trade;
int trades=OrdersTotal();
   for(trade=trades-1;trade>=0;trade--)
   {
   OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()==OP_BUYSTOP || OP_SELLSTOP){
         OrderDelete(OrderTicket());
      }
   }
   return(0);
}
Image
User avatar
traderuk
Trader
Posts: 18
Joined: Wed Mar 20, 2013 11:12 pm

Re: Deleting pending orders

Post by traderuk »

I can't thank you enough for your time!

I understand how the loop works and why we would move return to the outside of for loop, that makes sense.

From what I've read on mql4 docs I may be a bit confused about the return(0); actually does.

"A return(expression); operator terminates the current function execution with result transmission"

If for example we did return(-1); instead would this continuous loop, as it would return -1 and effectively set the condition trade=trades-1 on the next pass of the loop?

So the return(); effectively sets the value once the loop is made completed and is probably just a way to break out of the loop if I understand it right?

Thanks again
User avatar
traderuk
Trader
Posts: 18
Joined: Wed Mar 20, 2013 11:12 pm

Re: Deleting pending orders

Post by traderuk »

Think I understand it some more...

It turns out that in a function void it doesn't return anything so the return() wouldn't do anything. If I wanted to return something we'd use int or bool etc. ClosePendings :) The return generally just returns a value once the function completes an action.

Code: Select all

//+------------------------------------------------------------------+
//| Close Pending Orders                                           |
//+------------------------------------------------------------------+
void ClosePendings()
{
int count=0;
int trade;
int trades=OrdersTotal();
   for(trade=trades-1;trade>=0;trade--)
   {
   OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()==OP_BUYSTOP || OP_SELLSTOP){
         OrderDelete(OrderTicket());
      }
   }
   return(0);
}
phil_trade

Re: Deleting pending orders

Post by phil_trade »

traderuk wrote:Think I understand it some more...

It turns out that in a function void it doesn't return anything so the return() wouldn't do anything. If I wanted to return something we'd use int or bool etc. ClosePendings :) The return generally just returns a value once the function completes an action.

Code: Select all

//+------------------------------------------------------------------+
//| Close Pending Orders                                           |
//+------------------------------------------------------------------+
void ClosePendings()
{
int count=0;
int trade;
int trades=OrdersTotal();
   for(trade=trades-1;trade>=0;trade--)
   {
   OrderSelect(trade,SELECT_BY_POS,MODE_TRADES);

      if(OrderType()==OP_BUYSTOP || OP_SELLSTOP){
         OrderDelete(OrderTicket());
      }
   }
   return(0);
}
it's also better to use a bool function that return True or False depending if action required was executed

it's also better to test if OrderSelect was well executed

Code: Select all

bool ClosePendings()
{
int count=0;
int trade;
int trades=OrdersTotal();
bool MyResult = false;

for(trade=trades-1;trade>=0;trade--)
{

if not OrderSelect(trade,SELECT_BY_POS,MODE_TRADES) then MyResult := false;

      if(OrderType()==OP_BUYSTOP || OP_SELLSTOP)
     {
      OrderDelete(OrderTicket());
      MyResult := true;
      }
return(MyResult);
Post Reply

Return to “Coders Hangout”