stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Deleting pending orders
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=2605
Page 2 of 2
Author:  traderuk [ Tue Jun 25, 2013 3:20 pm ]
Post subject:  Re: Deleting pending orders

That works great and makes sense, thank you!
Author:  traderuk [ Tue Jun 25, 2013 6:27 pm ]
Post subject:  Re: Deleting pending orders

Any ideas why my original method wasn't working... to help with the learning? :)
Author:  dietcoke [ Wed Jun 26, 2013 8:25 am ]
Post subject:  Re: Deleting pending orders

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);
}
Author:  traderuk [ Wed Jun 26, 2013 10:51 am ]
Post subject:  Re: Deleting pending orders

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
Author:  traderuk [ Thu Jun 27, 2013 9:18 am ]
Post subject:  Re: Deleting pending orders

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);
}
Author:  phil_trade [ Thu Jun 27, 2013 1:37 pm ]
Post subject:  Re: Deleting pending orders

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);
All times are UTC Page 2 of 2