Download MetaTrader 5

Close open position with expiry from pending

To add comments, please log in or register
Cristian Baciu
676
Cristian Baciu  

Hello, i have next code and as title says i would like to have the expiry from pending to be used for the open position.

eg.: I open pending at 2:15 with expiry at 3:15. If position will be open at 3:14 i would like to be close at 3:15.

void ClosePosition()
{
   int ticket = OrderTicket();

   {
      if(OrderSelect(ticket,SELECT_BY_TICKET))
      {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()== 0)
         {
            
               if(OrderType()==OP_BUYLIMIT)
               {
                  int a=OrderOpenTime();
               }
               if(OrderType()==OP_SELLLIMIT)
               { 
                  int b=OrderOpenTime();
               }
               
               
               if((OrderType()==OP_BUY)&&(TimeCurrent() - a > maxDuration * ( 60) ))
                  {
                     OrderClose(OrderTicket(),OrderLots(),Bid,2,Red);
                  }
                  
               if((OrderType()==OP_SELL)&&(TimeCurrent() - b > maxDuration * ( 60) )) 
                  { 
                     OrderClose(OrderTicket(),OrderLots(),Ask,2,Red);
                  }
                  
                
          }
      }
   }
}

I tried to work with this but have no success, any idea about what should i change in my code or if there is anything right in the flow of code?

Thanks,

Keith Watford
Moderator
10566
Keith Watford  
void ClosePosition()
{
   int ticket = OrderTicket();
   int total = OrdersTotal();
   for(int i=total-1;i>=0;i--)
   {
      if(OrderSelect(ticket,SELECT_BY_TICKET))
      {
         if(OrderSymbol()==Symbol() && OrderMagicNumber()== 0)
         {
            
               if(OrderType()==OP_BUYLIMIT)
               {
                  int a=OrderOpenTime();
               }
               if(OrderType()==OP_SELLLIMIT)
               { 
                  int b=OrderOpenTime();
               }
               
               
               if((OrderType()==OP_BUY)&&(TimeCurrent() - a > maxDuration * ( 60) ))
                  {
                     OrderClose(OrderTicket(),OrderLots(),Bid,2,Red);
                  }
                  
               if((OrderType()==OP_SELL)&&(TimeCurrent() - b > maxDuration * ( 60) )) 
                  { 
                     OrderClose(OrderTicket(),OrderLots(),Ask,2,Red);
                  }
                  
                
          }
      }
   }
}

 

 Why do you have the loop? you don't use i at all.

Where does the variable ticket get its value from? 

SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI  
Must be some sort of rookie mistake.
Cristian Baciu
676
Cristian Baciu  
GumRai:

 

 Why do you have the loop? you don't use i at all.

Where does the variable ticket get its value from? 

I forget to delete some lines, i tried with Select_by_pos earlier but it won`t work because the position won`t be the same. The loop line is deleted.

Ticket should be get from a file? As i read from topics, it is easier to use select_by_pos but this won't work in my case. Do you have any ideas how to deal with it? (i am not sure if i include the ordersend in code i will be able to get it from there).

Or should i call as a pair, because i will have only one open order from each currencies. So i start a loop for pos and symb for each currencies to search for pendings, and save the orderopentime than i should make another loop for pos for each order opened and compare current time with the orderopentime from pendings? It is something that i tried up before, but i should include the loops now. It will works better like this maybe?

Thank you for your time

whroeder1
15573
whroeder1  
  1. OrderClose(OrderTicket(),OrderLots(),Bid,2,Red);
    
    
    Check your return codes (OrderSelect) What are Function return values ? How do I use them ? - MQL4 forum and Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
  2. If this doesn't execute:
    if(OrderType()==OP_BUYLIMIT){
      int a=OrderOpenTime();
    }
    
    What does this do?
    if((OrderType()==OP_BUY)&&(TimeCurrent() - a > maxDuration * ( 60) ))
        {
           OrderClose(OrderTicket(),OrderLots(),Bid,2,Red);
        }
    
Keith Watford
Moderator
10566
Keith Watford  

My post was intended to make you think about your code and rectify the first mistakes that you have made.

Then we can talk about other mistakes such as variable a is given a value if the order is a buy limit, then you try to use the variable if the order is a buy. This cannot work, because if the order is a buy, no value will be assigned to a

EDIT: took me a while to write my post and William has already pointed out the mistake in the code regarding variable a

To add comments, please log in or register