need help. can somebody please find out why this EA is not working?

 
hello.
i don't understand why this EA dont work as it should.
he open 1 order but never closes it, also he don't open a second order.
in journal he says that the ticket in the OrderClose() function is not valid. but i close with Orderclose(orderticket().........)

the idea is based on three slopes.
2 for direction and 1 as trigger.

//+------------------------------------------------------------------+
//|                                                   SlopingJoe.mq4 |
//|                                                                Z |
//|                                                                Z |
//+------------------------------------------------------------------+
#property copyright "Z"
#property link      "Z"

int period1=80;
int period2=240;
int period3=960;
int phase1=0;
int phase2=0;
int phase3=0;


double slope.period[3];
double slope.phase[3];
double slope.values[3];
double slope.values.old[3];

int init()
  {
   slope.period[0]=period1;
   slope.period[1]=period2;
   slope.period[2]=period3;   
   slope.phase[0]=phase1;
   slope.phase[1]=phase2;
   slope.phase[2]=phase3;   
   return(0);
  }

int deinit()
  {

   return(0);
  }

datetime update=0;
datetime lasttrade=0;
int start()
  {
   int i=0;
   if(Time[0]!=update){
      update=Time[0];
      for(i=0;i<3;i++){
         slope.values.old[i]=slope.values[i];
      }
   }else{
      for(i=0;i<3;i++){
         double tmp=0;
         tmp=iCustom(Symbol(),Period(),"JMASlope",slope.period[i],slope.phase[i],0,0);
         if(tmp==0){
            tmp=iCustom(Symbol(),Period(),"JMASlope",slope.period[i],slope.phase[i],1,0);         
         }
         slope.values[i]=tmp;
         
      }
   }
   if(lasttrade!=Time[0]){
      if(slope.values[0]>0 &&slope.values.old[0]<=0){
         if(slope.values[1]>0 && slope.values[2]>0){
               OrderSend(Symbol(),OP_BUY,NormalizeDouble(AccountBalance()/10000,2),Ask,0,0,0,"",0,0,CLR_NONE);
               lasttrade=Time[0];
         }
      }else{
         if(OrdersTotal()>0)
         for(i=OrdersTotal();i>0;i--){
            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            if(OrderType()==OP_BUY){
                  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,CLR_NONE);
                  lasttrade=Time[0];
            }
         }
      } 

      if(slope.values[0]<0 &&slope.values.old[0]>=0){
         if(slope.values[1]<0 && slope.values[2]<0){
               OrderSend(Symbol(),OP_SELL,NormalizeDouble(AccountBalance()/10000,2),Bid,0,0,0,"",0,0,CLR_NONE);
               lasttrade=Time[0];
         }
      }else{
         if(OrdersTotal()>0)
         for(i=OrdersTotal();i>0;i--){
            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
            Alert(OrderTicket());
            if(OrderType()==OP_SELL){
                  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,CLR_NONE);
                  lasttrade=Time[0];
            }
         }
      } 
   }
   return(0);
  }
i know coding is bad, but this should only be a concept.
Files:
jmaslope.mq4  13 kb
 
zzuegg:
in journal he says that the ticket in the OrderClose() function is not valid. but i close with Orderclose(orderticket().........)
This specific problem might be due to:
         for(i=OrdersTotal();i>0;i--){
            OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
The order pool starts from position 0 and you have to check if selection is successful:
         for(i=OrdersTotal()-1;i>=0;i--){
            if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
 
if(OrdersTotal()>0)
         for(i=OrdersTotal();i>0;i--){
The if statement is redundant as the for statement should do nothing if there are no open orders.
OrserSelect indexes start at zero. or combine in {one group}
    for(int index = OrdersTotal() - 1; index >= 0; index--) if (
        OrderSelect(index, SELECT_BY_POS)    // Only my orders w/
    &&  OrderMagicNumber()  == MagicNumber   // my magic number
    &&  OrderSymbol()       == Symbol() ) {  // and period and symbol
second
if(tmp==0){
  tmp=iCustom(Symbol(),Period(),"JMASlope",slope.period[i],slope.phase[i],1,0);
Does that indicator provide two arrays AND uses zero as not valid? EMPTY_VALUE >> 0 or you mean 0,1.
 
WHRoeder:
The if statement is redundant as the for statement should do nothing if there are no open orders.
OrserSelect indexes start at zero. or combine in {one group} second Does that indicator provide two arrays AND uses zero as not valid? EMPTY_VALUE >> 0 or you mean 0,1.


hello,
orderclosing now work. thanks for that.

yes, the indicator provides two arrays. i testet both, EMPTY_VALUE and 0. both give the same result. i tryed outputing the result of both iCustom() functions, but due to double-normalizing in the alert function the results are always 0.000 - 0.0000. i think i am going to code the indicator on myself.