Forex
Google

Go Back   Forex Trading > Discussion Areas > Metatrader 4
Forex Forum Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-09-2007, 04:23 PM
Forex Optimist's Avatar
Forex Optimist Forex Optimist is offline
Member
 
Join Date: Mar 2007
Posts: 38
Forex Optimist is on a distinguished road
MT4 coding help please: cancelling pending orders

Can anybody help? All I need to do is cancel a pending order if its counterpart is triggered. Example: BuyStop & SellStop placed. The Buy gets triggered, need to cancel the SellStop. I don't see why my code below doesn't work?

Thanks if anyone can help.

Code:
for(int z=OrdersTotal()-1;z>=0;z--) 
      {
      OrderSelect(z,SELECT_BY_POS);
      if(OrderSymbol()==Symbol()) 
         {
         if (OrderType()==OP_BUY)
            {
            OrderSelect(z,SELECT_BY_POS);
            if(OrderSymbol()==Symbol())
               {
               if (OrderType()==OP_SELLSTOP)
                  {
                  OrderDelete(OrderTicket(),DarkGoldenrod);
                  return(0);
                  }//if sellstop
                 // else Print("Failed due to error ",GetLastError());
               }//if symbol
            }//if buy
         }//if symbol

      if(OrderSymbol()==Symbol()) 
         {
         if (OrderType()==OP_SELL)
            {
            OrderSelect(z,SELECT_BY_POS);
            if(OrderSymbol()==Symbol())
               {
               if (OrderType()==OP_BUYSTOP)
                  {
                  OrderDelete(OrderTicket(),DarkGoldenrod);
                  return(0);
                  }//if buystop
                 // else Print("Failed due to error ",GetLastError());
               }//if symbol
            }//if sell
         }//if symbol
      }//end loop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2007, 04:45 PM
Forex Optimist's Avatar
Forex Optimist Forex Optimist is offline
Member
 
Join Date: Mar 2007
Posts: 38
Forex Optimist is on a distinguished road
I think I may have found the problem thanks to the thread "Need help" by Antone - need to test it, if anyone can confirm this may work :

Code:
  for(int z=OrdersTotal()-1;z>=0;z--) 
      {
      OrderSelect(z, SELECT_BY_POS, MODE_TRADES);
      if(OrderSymbol()==Symbol()) 
         {
         if (OrderType()==OP_BUY)
            {
            OrderSelect(z, SELECT_BY_POS, MODE_TRADES);
            if(OrderSymbol()==Symbol())
               {
               if (OrderType()==OP_SELLSTOP)
                  {
                  OrderSelect(z, SELECT_BY_POS, MODE_TRADES);
                  OrderDelete(OrderTicket(),DarkGoldenrod);
                  return(0);
                  }//if sellstop
                 // else Print("Failed due to error ",GetLastError());
               }//if symbol
            }//if buy
         }//if symbol

      if(OrderSymbol()==Symbol()) 
         {
         if (OrderType()==OP_SELL)
            {
            OrderSelect(z, SELECT_BY_POS, MODE_TRADES);
            if(OrderSymbol()==Symbol())
               {
               if (OrderType()==OP_BUYSTOP)
                  {
                  OrderSelect(z, SELECT_BY_POS, MODE_TRADES);
                  OrderDelete(OrderTicket(),DarkGoldenrod);
                  return(0);
                  }//if buystop
                 // else Print("Failed due to error ",GetLastError());
               }//if symbol
            }//if sell
         }//if symbol
      }//end loop
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-09-2007, 10:46 PM
ralph.ronnquist's Avatar
ralph.ronnquist ralph.ronnquist is offline
Senior Member
 
Join Date: Oct 2006
Posts: 280
ralph.ronnquist is on a distinguished road
You'll need to scan the orders twice: first to discover whether or not there is the OP_BUY or OP_SELL trades that you want should cause canceling of pending orders. The second loop is to carry out the canceling. Something like the following:
PHP Code:
bool has_sell false;
bool has_buy false;

for (
int i OrdersTotal()-1>= 0i-- ) {
    if ( ! 
OrderSelectiSELECT_BY_POS ) )
        continue;
    
has_sell |= OrderType() == OP_SELL;
    
has_buy |= OrderType() == OP_BUY;
}

for ( 
OrdersTotal()-1>= 0i-- ) {
    if ( ! 
OrderSelectiSELECT_BY_POS ) )
        continue;
    if ( 
has_sell && OrderType() == OP_BUYSTOP )
        
OrderDeleteOrderTicket(), DarkGoldenrod );
    if ( 
has_buy && OrderType() == OP_SELLSTOP )
        
OrderDeleteOrderTicket(), DarkGoldenrod );

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-10-2007, 07:37 PM
Forex Optimist's Avatar
Forex Optimist Forex Optimist is offline
Member
 
Join Date: Mar 2007
Posts: 38
Forex Optimist is on a distinguished road
Thanks Ralph. Didn't know |=, what does that signify?

You're right, my problem was not re-scanning through the orders. Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


Similar Threads
Thread Thread Starter Forum Replies Last Post
MT4 pending orders fx_geezer Metatrader 4 5 06-15-2007 09:36 PM
modify orders and close pending orders bkgridley Questions 8 05-23-2007 09:49 PM
Closing Pending Orders bkgridley Expert Advisors - Metatrader 4 1 05-11-2007 05:47 AM
how to delete a pending orders Altern8 Metatrader 4 1 01-02-2006 02:10 PM
Pending orders TheExponential Questions 4 11-25-2005 12:36 AM


All times are GMT. The time now is 08:45 AM.