Simple code needed for EA (modify two open orders) - page 2

 
kwng111:


Sorry kwng your posting looks to me not the solution for madmax3

You don't have to modify trades in a funtion( ) called void CloseAll()
Also you have to select there OrderSymbol ( ) and OrderMagicNumber( )

and in this part of the program

        }
        for (int i =1; i < OrdersTotal(); i++) {
    if(OrderSelect(i+1, SELECT_BY_POS, MODE_TRADES)) {
        OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit+20)*Point),0,Blue);
    }
}

pendingtrades will be still get modified

 

Here is the revised code for the whole EA:

//+------------------------------------------------------------------+
//|                                                  TimeBasedEA.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
//changed by:       "forex4capital@yahoo.ca"
//changed again by: madmax3

// Time frame: M5 and higher

extern int     MagicNumber = 20080122;
extern double DistancefromAsk;
extern double DistancefromBid;
extern double  TakeProfit  = 28;
extern double  StopLoss    = 55;
extern double  Lots        = 0.1;
extern int     StartHour   = 2300;      // Open Trade time
extern bool    OpenBuy     = true;
extern bool    OpenSell    = true;
extern int     NumBuys     = 1;
extern int     NumSells    = 1;
extern int     Slippage    = 2;

//+------------------------------------------------------------------+
//|                        S T A R T                                 |
//+------------------------------------------------------------------+
int start()
  {
   int cnt, ticket, total;
      if (TimeDayOfWeek(TimeCurrent())==5 && TimeCurrent()>=StrToTime("22:59")) { CloseAll(); return(0); }
   int ct;
//-------------------------------------+
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
//-------------------------------------+

//-------------------------------------+
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
//-------------------------------------+

   ct = Hour() * 100 + Minute();
   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
      // check for long position (BUY) possibility
      if(ct == StartHour && Close[1]>Open[1] && OpenBuy)
      //if(ct == StartHour && High[1]<Open[0] && OpenBuy)
        {
         for ( cnt = 0; cnt < NumBuys; cnt++)
         {
           ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+(DistancefromAsk*Point),Slippage,Bid-(StopLoss*Point),Ask+(TakeProfit*Point),"",MagicNumber,TimeCurrent()+39600,CLR_NONE);
           ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-(DistancefromBid*Point),Slippage,Ask+(StopLoss*Point),Bid-(TakeProfit*Point),"",MagicNumber,TimeCurrent()+39600,CLR_NONE); 
           if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }

          
           else Print("Error opening BUY order : ",GetLastError()); 
           

         }
         return; 
        }
      // check for short position (SELL) possibility
      if(ct == StartHour && Close[1]<Open[1] && OpenSell)
      //if(ct == StartHour && Low[1]>Open[0] && OpenSell)
        {
         for ( cnt = 0; cnt < NumSells; cnt++)
         {
           ticket=OrderSend(Symbol(),OP_SELLSTOP,Lots,Bid-(DistancefromAsk*Point),Slippage,Ask+(StopLoss*Point),Bid-(TakeProfit*Point),"",MagicNumber,TimeCurrent()+39600,CLR_NONE);
           ticket=OrderSend(Symbol(),OP_BUYSTOP,Lots,Ask+(DistancefromBid*Point),Slippage,Bid-(StopLoss*Point),Ask+(TakeProfit*Point),"",MagicNumber,TimeCurrent()+39600,CLR_NONE);
           if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
           else Print("Error opening SELL order : ",GetLastError());
         } 
         return; 
        
    
}

     }
 
     for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber                 // my magic number
    &&  OrderSymbol()       == "EURUSD"                // and my pair.
    ){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit+20)*Point),0,Blue);
      if (!OrderSelect(iPos, SELECT_BY_POS))
   Alert("OrderSelect failed: ", GetLastError());}


   return(0);
  }
  
  
  
  void CloseAll()
{
   for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if (OrderMagicNumber()!=MagicNumber) continue;
         
      //
      //
      //
      //
      //
         
      if (OrderType()==OP_BUY || OrderType()==OP_SELL)
      {
         for(int c=0; c<3; c++)
         {
            RefreshRates();
            if (OrderType()==OP_BUY)
                  { double cp = Bid;}  
            else  {        cp = Ask;}
               
            OrderClose(OrderTicket(),OrderLots(),cp,0,Yellow);
               int err=GetLastError();
               if(err==4 || err==136 || err==137 || err==138 || err==146)
               {
                  Sleep(5000); continue;
               }  
               break;                     
         }
         break;
      }
   }





}

// the end.

The CloseAll() function is there to close the trades at a specified time if they go on for so long.

This part of the code is the revised part of what I'm trying to figure out:
(as you can see the MagicNumber and Symbol are there, however only the pending orders are still being modified)

     for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber                 // my magic number
    &&  OrderSymbol()       == "EURUSD"                // and my pair.
    ){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit+20)*Point),0,Blue);
      if (!OrderSelect(iPos, SELECT_BY_POS))
   Alert("OrderSelect failed: ", GetLastError());}
 
deVries:
        }
        for (int i =1; i < OrdersTotal(); i++) {
    if(OrderSelect(i+1, SELECT_BY_POS, MODE_TRADES)) {
        OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit+20)*Point),0,Blue);
    }
}

for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)
   {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
int total=OrdersTotal();
 if(total>1)

You were told that the select will not work yesterday. And that you must test return codes and about total

If you're not going to bother to read the posts and correct your code, we are not going to bother to try and help you.

 
WHRoeder:

You were told that the select will not work yesterday. And that you must test return codes and about total

If you're not going to bother to read the posts and correct your code, we are not going to bother to try and help you.


I have done both of the above, you are referring to the wrong person methinks. I am the original poster not deVries and I have changed the code to this (as posted before):

   for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber                 // my magic number
    &&  OrderSymbol()       == "EURUSD"                // and my pair.
    ){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit+20)*Point),0,Blue);
      if (!OrderSelect(iPos, SELECT_BY_POS))
   Alert("OrderSelect failed: ", GetLastError());}

The problem now is that it modifies pending orders while I want it to only modify open orders and only when two open orders from the same symbol are open and running at the same time. I have tried different number combinations and OrderSelect numbers but I'm still stumped on this.

 
madmax3:


I have done both of the above, you are referring to the wrong person methinks. I am the original poster not deVries and I have changed the code to this (as posted before):

The problem now is that it modifies pending orders while I want it to only modify open orders and only when two open orders from the same symbol are open and running at the same time. I have tried different number combinations and OrderSelect numbers but I'm still stumped on this.

So you need to loop through your open orders, check the Symbol and MagicNumber as you have above . . . but you also need to check for OrderType() <= OP_SELL . . . count the orders that match, if it is 2 . . . then loop again and OrderModify().

The only way you are going to learn is to read the Documentation about each function and get a grasp of how they work . . . e.g. OrderType()

 

I have tried a variation of different codes but still can't get it to work exactly. It modifies one of the open trades when two are open and it keep on modifying it, I only need it to be modified once and I need both trades modified.

     for(int iPos = OrdersTotal()-1; iPos >= 1 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber                 // my magic number
    &&  OrderSymbol()       == "EURUSD"                // and my pair.
    && (OrderType() <= OP_SELL)
    
    ){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit+20)*Point),0,Blue);
    
   if (OrderSelect(iPos, SELECT_BY_POS))
    if (OrderType() <= OP_BUY)
      if (!OrderSelect(iPos, SELECT_BY_POS))
      {OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit-20)*Point),0,Blue);}
      if (!OrderSelect(iPos, SELECT_BY_POS))
   Alert("OrderSelect failed: ", GetLastError());}


   return(0);
  }
     for(int iPos = OrdersTotal()-1; iPos >= 1 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber                 // my magic number
    &&  OrderSymbol()       == "EURUSD"                // and my pair.
    && (OrderType() <= OP_SELL)
    && (OrderType() <=OP_BUY)
    ){OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit+20)*Point),0,Blue);}
    
   if  (OrderSelect(iPos-1, SELECT_BY_POS))
    {OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+((TakeProfit-20)*Point),0,Blue);



   return(0);
  }}

Any suggestions as to what I should do? I have read up on the documentation btw.

 
What is the difference before the order is modified and after it is modified ? what changes ?
 

First two orders are set, then one of them (a sell in this case when I tested the specific date) gets executed. Later on in the day the second order (which is still there) gets executed, and this trade is a buy.
The sell is order 1 and the buy is order 2. The buy (order 2) has it's TakeProfit modified by 16.3 pips (I'm not sure why it's not adding 20 pips), then the TakeProfit is increased by 0.2 pips in the same minute, and this keeps on happening, the TakeProfit is continuously modified throughout the day, sometimes increased, sometimes decreased, not sure why this is happening.

Edit: I think the 16.3 pips and further changes are also influenced by the spread?

 
OK, can we go back to the beginning . . . . I know you are trying to modify two orders . . . but what exactly are you trying to do ? tailing SL ? something else ? what are you trying to achieve ?
 
madmax3 2012.03.09 15:06
WHRoeder:

You were told that the select will not work yesterday. And that you must test return codes and about total

If you're not going to bother to read the posts and correct your code, we are not going to bother to try and help you.


I have done both of the above, you are referring to the wrong person methinks

I was referring to your directly previous post

madmax3 2012.03.09 14:52
Here is the revised code for the whole EA:
   total=OrdersTotal();
   if(total<1) 
Which still shows the problems I stated.