Help with code? - page 2

 

I forgot to add () and edit this mistake.

My I get some help how to loop through all closed orders and compare OrderCloseTime? 

 
int LastClosedTicket(){
   datetime last_closed = 0;           
   int last_ticket = -1;               
   
   for (int i=0; i<OrdersHistoryTotal(); i++) {  
      if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
           
      if (OrderType()<=1) {
         if (OrderCloseTime() > last_closed) {        
            last_closed = OrderCloseTime();         
            last_ticket = OrderTicket();
         }
      }
   }   
   return(last_ticket);
}


double LastClosedTP(){
   int last_ticket = LastClosedTicket();
   
   if (last_ticket > 0){
      if (OrderSelect(last_ticket,SELECT_BY_TICKET,MODE_HISTORY))
         return(OrderTakeProfit());
   }   
   return(0.0);
}

Is this correct code? 

May I put more info of this last order in same function, or I have to create new function for each info I want (TP, SL, OrderType,...)

 

Just look in the reference for OrdersTotal(), OrdersHistoryTotal() and OrderCloseTime().

datetime-type can be treated link ulong numbers: >, >= ==, !=, ...


 
int MagicNumber = 2910986;
int ticket;
double places;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){

   if (Digits==3 || Digits==5) places=10.0; else places=1.0;
   
   if(LastClosedType()==0 && Ask<LastClosedOpenPrice()){
      Type=4;
   }
   else if(LastClosedType()==0 && Ask>LastClosedOpenPrice()){
      Type=2;
   }
   else if(LastClosedType()==1 && Bid>LastClosedOpenPrice()){
      Type=5;
   }
   else if(LastClosedType()==1 && Bid<LastClosedOpenPrice()){
      Type=3;
   }
   
   int orders_num=OrdersTotal();
   if(orders_num<1){
      ticket=-1;
      while (ticket<0){   
         RefreshRates();
         ticket=OrderSend(Symbol(),Type,LastClosedLots(),LastClosedOpenPrice(),5*places,LastClosedSL(),LastClosedTP(),"GBPJPY",MagicNumber,0,clrNONE);           
         //Sleep(100);
      }
      if(ticket>0){
         //orders_num=orders_num+1;
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Pending order placed.");
      }
      else{
         Print("Error sending BUYSTOP order : ",GetLastError()); 
         //return(0); 
      }     
   }
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---
   
  }
//+------------------------------------------------------------------+
int LastClosedTicket(){
   datetime last_closed = 0;           
   int last_ticket = -1;               
   
   for (int i=0; i<OrdersHistoryTotal(); i++) {  
      if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
           
      if (OrderType()<=1) {
         if (OrderCloseTime() > last_closed) {        
            last_closed = OrderCloseTime();         
            last_ticket = OrderTicket();
         }
      }
   }   
   return(last_ticket);
}

double LastClosedTP(){
   int last_ticket = LastClosedTicket();
   
   if (last_ticket > 0){
      if (OrderSelect(last_ticket,SELECT_BY_TICKET,MODE_HISTORY))
         return(OrderTakeProfit());
   }   
   return(0.0);
}
   
double LastClosedSL(){
   int last_ticket = LastClosedTicket();
   
   if (last_ticket > 0){
      if (OrderSelect(last_ticket,SELECT_BY_TICKET,MODE_HISTORY))
         return(OrderStopLoss());
   }   
   return(0.0);
}
   
double LastClosedOpenPrice(){
   int last_ticket = LastClosedTicket();
   
   if (last_ticket > 0){
      if (OrderSelect(last_ticket,SELECT_BY_TICKET,MODE_HISTORY))
         return(OrderOpenPrice());
   }   
   return(0.0);
}
   
int LastClosedType(){
   int last_ticket = LastClosedTicket();
   
   if (last_ticket > 0){
      if (OrderSelect(last_ticket,SELECT_BY_TICKET,MODE_HISTORY))
         return(OrderType());
   }   
   return(0);
}

double LastClosedLots(){
   int last_ticket = LastClosedTicket();
   
   if (last_ticket > 0){
      if (OrderSelect(last_ticket,SELECT_BY_TICKET,MODE_HISTORY))
         return(OrderLots());
   }   
   return(0.0);
}
What do you think?
 
01005379: What do you think?
  1. A lot of duplicate codeI would code it thus:
    double LastClosedTP(){
       int last_ticket = LastClosedTicket();
      
       if (last_ticket > 0){
          if (OrderSelect(last_ticket,SELECT_BY_TICKET,MODE_HISTORY))
             return(OrderTakeProfit());
       }  
       return(0.0);
    }
    :
    double LastClosedLots(){
       int last_ticket = LastClosedTicket();
      
       if (last_ticket > 0){
          if (OrderSelect(last_ticket,SELECT_BY_TICKET,MODE_HISTORY))
             return(OrderLots());
       }  
       return(0.0);
    }
    Only the returned value differs. Mode is irrelevant when selecting by ticket.
    bool SelectLastClosed(){
       int last_ticket = LastClosedTicket();
       return OrderSelect(last_ticket,SELECT_BY_TICKET);
    }
    double LastClosedTP(){
       return SelectLastClosed() ? OrderTakeProfit() : 0.0;
    }
    :
    double LastClosedLots(){
       return SelectLastClosed() ? OrderLots()       : 0.0;
    }

  2. Your function doesn't filter by magic number or pair
    thus:
    int MagicNumber = 2910986;
    int LastClosedTicket(){
       datetime last_closed = 0;
       int last_ticket = -1;

       for (int i=0; i<OrdersHistoryTotal(); i++) {
          if (!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;

          if (OrderType()<=1) {
             if (OrderCloseTime() > last_closed) {
                last_closed = OrderCloseTime();
                last_ticket = OrderTicket();
             }
          }
       }
       return(last_ticket);
    }
    Don't hard code numbers. You must count down when closing/deleting in a position loop. Get in the habit of always counting down. Loops and Closing or Deleting Orders - MQL4 forum
    int MagicNumber = 2910986;
    int LastClosedTicket(){
       datetime last_closed = 0;
       int      last_ticket = -1;
       string   marketPair  = Symbol();
       for (int i=OrdersHistoryTotal() - 1; i>=0; --i) if(
          OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
          && OrderMagicNumber() == MagicNumber
          && OrderCloseTime() > last_closed
          && OrderIsOpen()
          && OrderSymbol() == marketPair
        ) {
          last_closed = OrderCloseTime();
          last_ticket = OrderTicket();
       }
       return(last_ticket);
    }
    bool OrderIsPending(void){ return OrderType() >= OP_BUYLIMIT; }
    bool OrderIsOpen(void){    return !OrderIsPending();          }
 

Thanks for the help!

I'm testing what I have done so far and for now it works as I want. I trade only one pair so there is no need to look for symbol.
I think all I need is to look at last trade and that is all. If I have 6 pending orders and two trades EA will do nothing because I will set it to work only if number of trades is < 8. When one trade will close it will open another pending order where last closed order was. Will test and see.

Thanks again!