Using better code for identifying session

 

Hello 

I haven an EA that runs 24 hours a day and takes into account the 3 main timezone session.

Here is what I usE to identify the current time zone and so it is then used to find the start bar of the session:

 

//+------------------------------------------------------------------+
//|       Identify current session/crossover                         |
//+------------------------------------------------------------------+
int Brokentime(){  
   wst=0;wsti=0;      
        if(DayOfWeek()==0){
        ds = iTime(Symbol(),PERIOD_D1,1)+(Asia_ET*3600);dr = 0;
        }
//+------------------------------------------------------------------+
        if(DayOfWeek()==1){
        if(TimeHour(TimeCurrent())>=0 && TimeHour(TimeCurrent())<London_ST){
        ds = iTime(Symbol(),PERIOD_D1,2)+(Asia_ET*3600);dr = 0; 
        }
        if(TimeHour(TimeCurrent())>=London_ST && TimeHour(TimeCurrent())<Asia_ET){
   dr = iTime(Symbol(),PERIOD_D1,2)+(Asia_ET*3600);
   ds = iTime(Symbol(),PERIOD_D1,2)+(London_ET*3600);
   }
   if(TimeHour(TimeCurrent())>=Asia_ET && TimeHour(TimeCurrent())<NewYork_ST){  
   ds = iTime(Symbol(),PERIOD_D1,2)+(London_ET*3600);dr = 0;
        }
        if(TimeHour(TimeCurrent())>=NewYork_ST && TimeHour(TimeCurrent())<London_ET){
   ds = iTime(Symbol(),PERIOD_D1,2)+((Asia_ST+1)*3600);
   dr = iTime(Symbol(),PERIOD_D1,2)+(London_ET*3600);
        }
        if(TimeHour(TimeCurrent())>=London_ET && TimeHour(TimeCurrent())<Asia_ST){
   ds = iTime(Symbol(),PERIOD_D1,2)+((Asia_ST+1)*3600);dr = 0;
        }
        if(TimeHour(TimeCurrent())>=Asia_ST){
        ds = iTime(Symbol(),PERIOD_D1,0)+(Asia_ET*3600);dr = 0;
        }
}
//+------------------------------------------------------------------+
        if(DayOfWeek()>=2){
        if(TimeHour(TimeCurrent())>=0 && TimeHour(TimeCurrent())<London_ST){
        ds = iTime(Symbol(),PERIOD_D1,1)+(Asia_ET*3600);dr = 0;
        }
        if(TimeHour(TimeCurrent())>=London_ST && TimeHour(TimeCurrent())<Asia_ET){
   dr = iTime(Symbol(),PERIOD_D1,1)+(Asia_ET*3600);
   ds = iTime(Symbol(),PERIOD_D1,1)+(London_ET*3600);
   }
   if(TimeHour(TimeCurrent())>=Asia_ET && TimeHour(TimeCurrent())<NewYork_ST){  
   ds = iTime(Symbol(),PERIOD_D1,1)+(London_ET*3600);dr = 0;
        }
        if(TimeHour(TimeCurrent())>=NewYork_ST && TimeHour(TimeCurrent())<London_ET){
   ds = iTime(Symbol(),PERIOD_D1,1)+((Asia_ST+1)*3600);
   dr = iTime(Symbol(),PERIOD_D1,1)+(London_ET*3600);
   }
        if(TimeHour(TimeCurrent())>=London_ET && TimeHour(TimeCurrent())<Asia_ST){
   ds = iTime(Symbol(),PERIOD_D1,1)+((Asia_ST+1)*3600);dr = 0;
        }
        if(TimeHour(TimeCurrent())>=Asia_ST){
        ds = iTime(Symbol(),PERIOD_D1,0)+(Asia_ET*3600);dr = 0;}
  }
      if(t>GlobalVariableGet(pivot_d)){GlobalVariableDel(pivot_d);}
}

Is there a way to do this thats uses more efficient coding?

Bearing in mind I still quite a novice so examples would be useful

 

Thanks

 

Antony  

 
tonyjms2005:

Hello 

I haven an EA that runs 24 hours a day and takes into account the 3 main timezone session.

Here is what I usE to identify the current time zone and so it is then used to find the start bar of the session:

 

Is there a way to do this thats uses more efficient coding?

Bearing in mind I still quite a novice so examples would be useful

You cold certainly improve the readability of your code . . . .  ds, dr, wt, wsti  . . .   what do these variables stand for ?  why not give them meaningful names that don't have to be guessed at ?   I also find your braces layout hard to follow . . .  I have my own style that I am used to but I don't see much consistency in what you have done, for example the last line that starts,  if(t>Global . . .   I can't quickly see which block of code it belongs to. 
 

Hi

Thanks for the reply, I have reattached the "cleared up" version of the code:

 

extern    int       London_ST                       = 8;// 7 FOR SUMMER
extern    int       London_ET                       = 17;// 16 FOR SUMMER
extern    int       NewYork_ST                      = 13;// 12 FOR SUMMER
extern    int       Asia_ST                         = 22;//21 FOR SUMMER
extern    int       Asia_ET                         = 9;//8 FOR SUMMER
//+------------------------------------------------------------------+  
int Brokentime(){  
        if(DayOfWeek()==0){
        daystarttime= iTime(Symbol(),PERIOD_D1,1)+(Asia_ET*3600);
        }
//+------------------------------------------------------------------+
        if(DayOfWeek()==1){
        if(TimeHour(TimeCurrent())>=0 && TimeHour(TimeCurrent())<London_ST){
        daystarttime = iTime(Symbol(),PERIOD_D1,2)+(Asia_ET*3600);
        }
        if(TimeHour(TimeCurrent())>=London_ST && TimeHour(TimeCurrent())<Asia_ET){
  	daystarttime = iTime(Symbol(),PERIOD_D1,2)+(Asia_ET*3600);
   	}
   	if(TimeHour(TimeCurrent())>=Asia_ET && TimeHour(TimeCurrent())<NewYork_ST){  
  	daystarttime = iTime(Symbol(),PERIOD_D1,2)+(London_ET*3600);
        }
        if(TimeHour(TimeCurrent())>=NewYork_ST && TimeHour(TimeCurrent())<London_ET){
  	daystarttime = iTime(Symbol(),PERIOD_D1,2)+((Asia_ST-1)*3600);
        }
        if(TimeHour(TimeCurrent())>=London_ET && TimeHour(TimeCurrent())<Asia_ST){
 	daystarttime = iTime(Symbol(),PERIOD_D1,2)+((Asia_ST-1)*3600);
        }
        if(TimeHour(TimeCurrent())>=Asia_ST){
        daystarttime = iTime(Symbol(),PERIOD_D1,0)+(Asia_ET*3600);
        }
}
//+------------------------------------------------------------------+
        if(DayOfWeek()>=2){
        if(TimeHour(TimeCurrent())>=0 && TimeHour(TimeCurrent())<London_ST){
        daystarttime = iTime(Symbol(),PERIOD_D1,1)+(Asia_ET*3600);
        }
        if(TimeHour(TimeCurrent())>=London_ST && TimeHour(TimeCurrent())<Asia_ET){
  	 daystarttime = iTime(Symbol(),PERIOD_D1,1)+(London_ET*3600);
 	 }
  	if(TimeHour(TimeCurrent())>=Asia_ET && TimeHour(TimeCurrent())<NewYork_ST){  
 	daystarttime = iTime(Symbol(),PERIOD_D1,1)+(London_ET*3600);
        }
        if(TimeHour(TimeCurrent())>=NewYork_ST && TimeHour(TimeCurrent())<London_ET){
        daystarttime = iTime(Symbol(),PERIOD_D1,1)+(Asia_ST*3600);
  	}
        if(TimeHour(TimeCurrent())>=London_ET && TimeHour(TimeCurrent())<Asia_ST){
   	daystarttime = iTime(Symbol(),PERIOD_D1,1)+(Asia_ST*3600);
        }
        if(TimeHour(TimeCurrent())>=Asia_ST){
        daystarttime = iTime(Symbol(),PERIOD_D1,0)+(Asia_ET*3600);}
  }
}

Thanks again

Antony 

 
tonyjms2005:

Hi

Thanks for the reply, I have reattached the "cleared up" version of the code:


Thank you for making the effort,  I think it is much easier to read now :-)
 

No problem, I was thinking of using arrays?

 
I prefer Whitesmith style
Reason: