Download MetaTrader 5
To add comments, please log in or register
Have you developed a profitable strategy? Launch your trading signal and make profit from it!
Dominic Gilbert
1632
Dominic Gilbert 2014.07.29 08:43 
I'm wanting to delete 3 orders and replace them if the below is true... I'm doing something wrong though as it is not deleting all 3 open pending orders and replacing them all with new "NewBuyOrder"... Can anyone help? Thanks!

     for(PositionIndex1 = TotalNumberOfOrders1 - 1; PositionIndex1 >= 0 ; PositionIndex1 --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
       {
       if( ! OrderSelect(PositionIndex1, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
         if( OrderMagicNumber() == MagicNumber1 && MagicNumber2 && MagicNumber3  // <-- does the Order's Magic Number match our EA's magic number ? 
         && OrderSymbol() == Symbol()           // <-- does the Order's Symbol match the Symbol our EA is working on ? 
         && ( OrderType() == OP_BUYSTOP ) ) 
                {  
                   if(BuyStopPrice - OrderStopLoss() > Point / 2. )

                        {
                        Stored_BuyPrice = OrderOpenPrice();
                        DeleteOrder = OrderDelete(OrderTicket());
                        if(DeleteOrder != TRUE) Print("Buy Delete Order Failed = ",GetLastError(), " On: ", OrderSymbol());
                        if(DeleteOrder==True)Print("Buy Order Deleted = ", OrderTicket(), " On: ", OrderSymbol());
                        }

                   if(OpenOrdersThisPair(Symbol())<3 && DeleteOrder==True && H1_Buy_Touch == "H1 Buy Touch" && LotSize_Buy - minlot > - Point)// If there are no open orders = place a new order.
                        {
                        int NewBuyOrder1 = OrderSend(Symbol(),OP_BUYSTOP,LotSize_Buy,Stored_BuyPrice,3,BuyStopPrice,btp1,NULL,MagicNumber1,0,Green);
                        int NewBuyOrder2 = OrderSend(Symbol(),OP_BUYSTOP,LotSize_Buy,Stored_BuyPrice,3,BuyStopPrice,btp2,NULL,MagicNumber2,0,Green);
                        int NewBuyOrder3 = OrderSend(Symbol(),OP_BUYSTOP,LotSize_Buy,Stored_BuyPrice,3,BuyStopPrice,btp3,NULL,MagicNumber3,0,Green);
                        }   
                 }      
         }
Simon Gniadkowski
Moderator
17963
Simon Gniadkowski 2014.07.29 08:59  
DomGilberto:
I'm wanting to delete 3 orders and replace them if the below is true... I'm doing something wrong though as it is not deleting all 3 open pending orders and replacing them all with new "NewBuyOrder"... Can anyone help? Thanks!


Take the OrderSend() out of the loop . . .  adding in new orders is,  I think,  confusing the situation.  Remember what you need to do inside the loop,  then do it outside the loop.
Dominic Gilbert
1632
Dominic Gilbert 2014.07.29 09:24  

Thanks RaptorUK.

 Hmmm... Hasn't done the trick. I'm placing Prints all over the place to figure it out with no luck so far. As I have 3 pending orders, will the above code (from what you can see) cut it in terms of deleting all 3 open orders? 

 

     for(PositionIndex1 = TotalNumberOfOrders1 - 1; PositionIndex1 >= 0 ; PositionIndex1 --)  //  <-- for loop to loop through all Orders . .   COUNT DOWN TO ZERO !
       {
       if( ! OrderSelect(PositionIndex1, SELECT_BY_POS, MODE_TRADES) ) continue;   // <-- if the OrderSelect fails advance the loop to the next PositionIndex
         if( OrderMagicNumber() == MagicNumber1 && MagicNumber2 && MagicNumber3  // <-- does the Order's Magic Number match our EA's magic number ? 
         && OrderSymbol() == Symbol()           // <-- does the Order's Symbol match the Symbol our EA is working on ? 
         && ( OrderType() == OP_BUYSTOP ) ) 
                {  
                   if(BuyStopPrice - OrderStopLoss() > Point / 2. )

                        {
                        Stored_BuyPrice = OrderOpenPrice();
                        DeleteOrder = OrderDelete(OrderTicket());
                        if(DeleteOrder != TRUE) Print("Buy Delete Order Failed = ",GetLastError(), " On: ", OrderSymbol());
                        if(DeleteOrder==True)Print("Buy Order Deleted = ", OrderTicket(), " On: ", OrderSymbol());
                        }
xavilin
48
xavilin 2014.07.29 10:34  
is this line correct?
 if( OrderMagicNumber() == MagicNumber1 && MagicNumber2 && MagicNumber3 
as it looks to me as if it was saying:
 if( OrderMagicNumber() == MagicNumber1) if( MagicNumber2) if( MagicNumber3) 

which are true/false, true, true

while you may want to say:

 if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
dunno if that's what's happening, hope this helps
Dominic Gilbert
1632
Dominic Gilbert 2014.07.29 10:47  
update: this has done something! Will report back in a minute.

 if( OrderMagicNumber() == MagicNumber1 || OrderMagicNumber()== MagicNumber2 || OrderMagicNumber()== MagicNumber3 )
Dominic Gilbert
1632
Dominic Gilbert 2014.07.29 11:41  
That's sorted it Xavilin - thank you very much for your help and time!!!
xavilin
48
xavilin 2014.07.29 13:50  
DomGilberto:
That's sorted it Xavilin - thank you very much for your help and time!!!

you're welcome, you can't imagine how amateur i am :D
/
To add comments, please log in or register