reopen a closed order

 
Hello, I'm trying to learn the code. But for the following problem I have found nothing in the documentation:
How can I start the same buy-limit order again after it was closed?
for example: buy_limit EUR / USD opened at 1.35, tp closed at 1.3510. Now I want that this order is always opened at 1.35 and closed at 1.3510.

I would be very grateful if you could help me.
thank you
 

You use a magic number to identify trades from this particular EA (not necessary if you only ever have 1 trade open at a time, but still a good idea)

Use OrdersTotal() and OrderSect() with MODE_TRADES to check open orders and check that any open trades have the magic number

If no open trades with the magic number, check that the current Ask price is higher than the limit buy entry +Stop Level.

Place the order.

 

hello,

thanks for the answers. this with the magic number is a good idea, but can you give me an short example.

i thank you.

 

Here's a short example

bool open_trade = false;
  for(int x=OrdersTotal()-1;x>=0;x--)
    {
     if(OrderSelect(x, SELECT_BY_POS, MODE_TRADES) )
       {
       if(OrderMagicNumber()==MagicNumber)  
          open_trade=true ;
       }
       else
          open_trade=true ;
    }
    
  //insert code to check that limit entry is not too close and below current ask.
  if (open_trade == false)
     int ticket = OrderSend(Symbol(), OP_BUYLIMIT, lots, price,sl,tp,0,MagicNumber,0,color);
  if( ticket == -1)
     {
     int ERR = GetLastError() ;
     Print("Order send failed. Error number is ",ERR );
     }

Note that if there is an open order with the magic number, bool open_trade will be true. If an order select fails for whatever reason it will also be true. This is because if an order select fails, then you can't be sure that there is not an open order.

 

Thanks for your answer. I've tried it, but it was unsuccessful. Below I have copied the code. This code is to simply explain better what I want. There are several orders are specially processed so that whenever an order is closed, the same will be opened



  if(order1==0)
   {
   tiket1 = OrderSend(Symbol(),OP_SELLLIMIT,mylot,prise1,3,0,Bid-my_profit*Point,NULL,0011,0,CLR_NONE );
   
      if(tiket1==0)
         {
         Print("tiket1=",tiket1);
         if(OrderSelect(tiket1,SELECT_BY_TICKET)==true)
            {
            order1=1;
            Sleep(500000);
            int MagicNumber=0011;
            }
         }
   return(0);
   }
  RefreshRates();
  
  if (order1==1)
   { 
   OrderSelect(SELECT_BY_POS, MODE_TRADES);
      {
      if(OrderMagicNumber()==MagicNumber)
         {
          order1=1; 
         }
       
      if(OrderMagicNumber()!=MagicNumber)
         {
         order1=0;
         }
      return(0);   
      }
    return(0);   
   }




This code not work well. I would be very grateful if you could help me.

Thanks

 
if(order1==0)
   {
   tiket1 = OrderSend(Symbol(),OP_SELLLIMIT,mylot,prise1,3,0,Bid-my_profit*Point,NULL,0011,0,CLR_NONE );
   
      if(tiket1==0)      // IF THE ORDERSEND SUCCEEDS, ticket1 WILL BE MORE THAN 0, SO NONE OF THE FOLLOWING CODE WILL BE EXECUTED
                         // order1 will still == 0 and MagicNumber WILL NOT BE ASSIGNED A VALUE
         {
         Print("tiket1=",tiket1);
         if(OrderSelect(tiket1,SELECT_BY_TICKET)==true)
            {
            order1=1;
            Sleep(500000);  //WHY SLEEP FOR MORE THAN 8 MINUTES?
            int MagicNumber=0011;
            }
         }
   return(0);
   }
  RefreshRates();
  
  if (order1==1)
   { 
   OrderSelect(SELECT_BY_POS, MODE_TRADES);  //YOU ARE NOT GIVING THE INDEX NUMBER TO SELECT A TRADE
      {
      if(OrderMagicNumber()==MagicNumber)    //MagicNumber HAS NOT BEEN ASSIGNED A VALUE
         {
          order1=1; 
         }
       
      if(OrderMagicNumber()!=MagicNumber))    //MagicNumber HAS NOT BEEN ASSIGNED A VALUE
         {
         order1=0;
         }
      return(0);   
      }
    return(0);   
   }
 

hello,
thanks for the quick response. following the code with the changes, but he order permanently sell-limit .
I ask for help.
thank you

 
//---- input parameters
extern double my_profit=100.00;
extern double my_stop=1000;
extern double shag=100.00;
extern double mylot=0.01;
extern double entry_price=0.8270;
double MagicNumber1=0011;

int cnt;
int trade1;
 
  int order1,order2,order3,order4,order5,order6,order7,order8,order9,order10,SELL,BUY;
  
  int tiket1,tiket2,tiket3,tiket4,tiket5,tiket6,tiket7,tiket8,tiket9,tiket10;
 
  
int init()
  {
order1=0;
order2=0;
order3=0;
order4=0;
order5=0;
order6=0;
order7=0;
order8=0;
order9=0;
order10=0;
 
tiket1=0;
tiket2=0;
tiket3=0;
tiket4=0;
tiket5=0;
tiket6=0;
tiket7=0;
tiket8=0;
tiket9=0;
tiket10=0;

    return(0);
  }
int deinit()
  {
  
   return(0);
  }
int start()
  {

  int total=OrdersTotal();
  int pos;
 
    
    
   double prise1=entry_price+shag*1*Point;            
   double prise3=entry_price+shag*2*Point;    
   double prise5=entry_price+shag*3*Point;    
   double prise7=entry_price+shag*4*Point;    
   double prise9=entry_price+shag*5*Point;    
  

 
               
   double prise2=entry_price-shag*1*Point;            
   double prise4=entry_price-shag*2*Point;    
   double prise6=entry_price-shag*3*Point;    
   double prise8=entry_price-shag*4*Point;    
   double prise10=entry_price-shag*5*Point;   

   
 
//-----------------------------------------------------------------------------------

  if(order1==0)
   {
   tiket1 = OrderSend(Symbol(),OP_SELLLIMIT,mylot,prise1,3,0,Bid-my_profit*Point,NULL,0011,0,CLR_NONE );
   
      if(tiket1>0)
         {
         Print("tiket1=",tiket1);
         if(OrderSelect(tiket1,SELECT_BY_TICKET)==true)
            {
            order1=1;
            Sleep(5000);
           int MagicNumber1=0011;
            }
         }
   return(0);
   }
  RefreshRates();
  
  if (order1==1)
   { 
   OrderSelect(pos,SELECT_BY_POS, MODE_TRADES);
      {
      if(OrderMagicNumber()==MagicNumber1)
         {
          order1=1; 
         }
       
      if(OrderMagicNumber()!=MagicNumber1)
         {
         order1=0;
         }
      return(0);   
      }
    return(0);   
   }
   

   
return(0);   
}
 
if(order1==0)
   {
   tiket1 = OrderSend(Symbol(),OP_SELLLIMIT,mylot,prise1,3,0,Bid-my_profit*Point,NULL,0011,0,CLR_NONE );
   
      if(tiket1<0)
         {
         Print("tiket1=",tiket1);
         if(OrderSelect(tiket1,SELECT_BY_TICKET)==true)
            {
            order1=1;
            Sleep(5000);
           int MagicNumber1=0011;
            }
         }
   return(0);
   }

order1 is set to 0 in init() so order will be sent.

If order send is successful tiket1 will be bigger than 0, so the value of order1 will not be changed.

So all this piece of code does is to send an order and if that order is successful, it will do nothing else and then exit the start() function.

Next tick order1 will still ==0 and it will just open another order and continue to do the same every tick

 
//---- input parameters
extern double my_profit=100.00;
extern double my_stop=1000;
extern double shag=100.00;
extern double mylot=0.01;
extern double entry_price=0.8270;
double MagicNumber1=0011;

int cnt;
int trade1;
 
  int order1,order2,order3,order4,order5,order6,order7,order8,order9,order10,SELL,BUY;
  
  int tiket1,tiket2,tiket3,tiket4,tiket5,tiket6,tiket7,tiket8,tiket9,tiket10;
 
  
int init()
  {
order1=0;
order2=0;
order3=0;
order4=0;
order5=0;
order6=0;
order7=0;
order8=0;
order9=0;
order10=0;
 
tiket1=0;
tiket2=0;
tiket3=0;
tiket4=0;
tiket5=0;
tiket6=0;
tiket7=0;
tiket8=0;
tiket9=0;
tiket10=0;

    return(0);
  }
int deinit()
  {
  
   return(0);
  }
int start()
  {

  int total=OrdersTotal();
  int pos;
 
    
    
   double prise1=entry_price+shag*1*Point;            
   double prise3=entry_price+shag*2*Point;    
   double prise5=entry_price+shag*3*Point;    
   double prise7=entry_price+shag*4*Point;    
   double prise9=entry_price+shag*5*Point;    
  

 
               
   double prise2=entry_price-shag*1*Point;            
   double prise4=entry_price-shag*2*Point;    
   double prise6=entry_price-shag*3*Point;    
   double prise8=entry_price-shag*4*Point;    
   double prise10=entry_price-shag*5*Point;   

   
 
//-----------------------------------------------------------------------------------

  if(order1==0)
   {
   tiket1 = OrderSend(Symbol(),OP_SELLLIMIT,mylot,prise1,3,0,Bid-my_profit*Point,NULL,0011,0,CLR_NONE );
   
      if(tiket1>0)
         {
         Print("tiket1=",tiket1);
         if(OrderSelect(tiket1,SELECT_BY_TICKET)==true)
            {
            order1=1;
            Sleep(5000);
           int MagicNumber1=0011;
            }
         }
   return(0);
   }
  RefreshRates();
   
   }
   

   
return(0);

if i let run this code without the last part, one order will ececuted. how i can restart this order, that after closing a new order is activated. i want to do this thenn also with the next 9 orders according the number of orders. can you help me please?

thanks

 
polywerk:

if i let run this code without the last part, one order will ececuted. how i can restart this order, that after closing a new order is activated. i want to do this thenn also with the next 9 orders according the number of orders. can you help me please?

thanks


It is not totally clear what you want to do. I'm guessing that you intend to open alternate limit buys and limit sells.

It is important that you try to follow how a code will work.

For instance, it appears that you intend to open a Limit buy after the limit sell has executed and hit TP

TP appears to be 100 points and so price is likely to be below or very close to the entry level of the 2nd trade. That would mean that a Limit buy order will fail. You have to think about how the EA can check for such conditions and what you would require the EA to do.

double MagicNumber1=0011;

//make this 

extern double MagicNumber1=0011;

//This will allow you to input different MN if running the EA on more than 1 chart
//then use the variable name in the order send

tiket1 = OrderSend(Symbol(),OP_SELLLIMIT,mylot,prise1,3,0,Bid-my_profit*Point,NULL,MagicNumber1,0,CLR_NONE );


// Looking at the order send, you use Bid when calculating TP, surely you intended to use prise1??

if(tiket1>0)
         {
         Print("tiket1=",tiket1);
         if(OrderSelect(tiket1,SELECT_BY_TICKET)==true)  //No need to select the order as you do nothing with it
                                                         //You have already checked that the Order send was succesful
            {
            order1=1;
            Sleep(5000);
           int MagicNumber1=0011;        // Not necessary as you are just reassigning the same value to the variable.
            }
         }

This is all you need, though I don't understand, why the sleep?

if(tiket1>0)
         {
         Print("tiket1=",tiket1);
         order1=1;
         Sleep(5000);
         }

You have a stop loss value in you extern parameters but you don't use it in the order send. Very risky trading without any SL at all.

Seems to me that you could be using arrays in this code to simplify it.

I suggest that you read relevant sections in the Book (Link at top of page) and study and practice working with arrays.

Reason: