FOR Statement limitations under start function

 

friends,

I am facing a problem with for statement in mq4 file. In my mq4 file under the start function, there are four numbers of FOR statements. When I put another FOR statement for picking maximum and minimum prices by counting 36 bars or any number of bars as count (say k =36) from 00hrs to 9 hrs in M15 chart, I am getting the result for the maximum price only and the minimum price is always zero. There is no error in the code for picking maximum and minimum values as the same is copied from another working program. The integer k is defined earlier to start function. Any solution for this?

int start()
  {
  int l_hour_8 = Hour();
  int l_minute_12 = Minute();
   double min;
  double max;            
      
  if (l_hour_8 >= starthour && l_hour_8 < TradingHour){ //l_minute_12>=TradingMinute){
  for( k=0;k<= Quant_Bars-1;k++)
  {
   if(High[k]>max){
   max = High[k];
   }
 if(Low[k]<min){
    min = Low[k];
  }  
  }
 
  if(cnt2<2){
     for ( k = 1; k < barcount; k++){  
 bar3=High[k+2]-Low[k+2];
bar2=High[k+1]-Low[k+1];
bar1=High[k]-Low[k];  
if(bar2>bar1 && bar2>bar3){   
   if(NormalizeDouble(High[k+2],4)>=NormalizeDouble(High[k+1],4)){
   if(Low[k+1]>=Low[k]){
//------------------------
 

You declare max and min but don't define them (MQL4 will do this by default but it is bad practice to rely on this)

min need to be initialized to something big in order for the code to work.

You test the same bars every tick, which is wasteful. You would be better to test only once per bar.

 
dabbler:

You declare max and min but don't define them (MQL4 will do this by default but it is bad practice to rely on this)

min need to be initialized to something big in order for the code to work.

You test the same bars every tick, which is wasteful. You would be better to test only once per bar.


Thank you for giving me the solution. Now it is working correctly. When I initialised a big value to 4 it worked.

thank you

 
sunilkumarks46:

Thank you for giving me the solution. Now it is working correctly. When I initialised a big value to 4 it worked.

thank you

I'm glad, but you might want to think

BIGGER


GBPJPY is 131 for example.

XAUUSD is 1643.

There is no penalty for going bigger. Try something like 12345678.

 
        #define INF 0x6FFFFFFF // Not quite infinite, Jul 2029, or 1,879,048,191
 
WHRoeder:
0x6FFFFFFF

why such an odd number, why not simply
0x7FFFFFFF
which is the biggest positive 32 bit integer?
 

sunilkumarks46:

The integer k is defined earlier to start function.

Such loop variables should be defined local. Generally you should make everything always local unless you absolutely need it global and even then there often might be a solution to make many of these things local too.
 
7bit:
Such loop variables should be defined local. Generally you should make everything always local unless you absolutely need it global and even then there often might be a solution to make many of these things local too.


Noted your advice, thank you
 

I could see the results of my EA only for one day even though I select 30 days date range in mt4 strategy tester. I have to select each date in mt4 and run this Ea by clicking the start button for seeing results for 30 days. I am getting the result only for the first day even if I select 30 days range. I have used STATIC variables in my EA. Any solution for this?

double bar3,bar2,bar1;
 double TH3,BL3,TL3,BH3;
static int TL3bar,TH3bar;
bool longone = True;
bool shortone = True;
int k;
double poin;
int PipFactor =1;
 


int deinit()
  {
//----
   ObjectDelete("H3 Label");
  ObjectDelete("H3 Line");
ObjectDelete("L3 Label");
ObjectDelete("L3 Line");
//----
   return(0);
  }
  int Quant_Bars=25;                  
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
  int l_hour_8 = Hour();
  int l_minute_12 = Minute();
   static double min=2;
  static double max;            
      
  if (l_hour_8 >= starthour && l_hour_8 < TradingHour){ //l_minute_12>=TradingMinute){
  for( k=0;k<= Quant_Bars-1;k++)
  {




 
7bit:
why such an odd number, why not simply
which is the biggest positive 32 bit integer?
Because then you can't use it in calculations, what would result in sign changes. I had some code where I did a INF+SL or something like that. Result went negative so the IF (SL>0 & res < INF) was true, resulting an a failed orderModify.
 

Hello, i have a problem with the for statement to. iv put it in a function before the start(), but id like to return every value of the iteration. it only returns the result of the entry in the iteration i.e, if i would like it to run 7 times, it only gives me the result of the 7th time it loops, yet id like to return a result for every time I loop. is there a way to get the result?

thank you

Reason: