Download MetaTrader 5

Doubt with fix indicator

To add comments, please log in or register
butterfly5
102
butterfly5  

Hi all,

i am learning mql4 and for this i think that i must code and understand the logic. Well, i am trying to code the searching of previous high from actual+1 bar.


int start()
  {
   int  i;
   int  counthigh;
   
   
   
  
   for(i = 1000; i >=0; i--)
      {
      
       if ( iHigh(NULL,PERIOD_H4,1) > iHigh(NULL,PERIOD_H4,i+2) )   
          {
          counthigh++;
          }                                                                                  
          
       if ( iHigh(NULL,PERIOD_H4,1) <= iHigh(NULL,PERIOD_H4,i+2))  
          {
          counthigh=0;
          }

       }

       return(0);
    }

This is the heart of the code. I understand that the code is not correct because i have referenced "1" and i must be referenced each H4 previous bar. When i have referenced this bar, then i have to searchthe previous high in i+2, i+3,...

Then, my question:

i must referenced this bar with

int timeFrameIndex = iBarShift(NULL, PERIOD_H4, iTime(NULL,PERIOD_H4,i), true);

Well i have the index of H4 bar but now, i dont understand how i have to continue. Can someone explain for understand or other code for understand better? I dont want mix apples with oranges.

The propose of code is learn and understand operations in mql4.

Thank you very much!!

butterfly5
102
butterfly5  
butterfly5:

Hi all,

i am learning mql4 and for this i think that i must code and understand the logic. Well, i am trying to code the searching of previous high from actual+1 bar.


This is the heart of the code. I understand that the code is not correct because i have referenced "1" and i must be referenced each H4 previous bar. When i have referenced this bar, then i have to searchthe previous high in i+2, i+3,...

Then, my question:

i must referenced this bar with

Well i have the index of H4 bar but now, i dont understand how i have to continue. Can someone explain for understand or other code for understand better? I dont want mix apples with oranges.

The propose of code is learn and understand operations in mql4.

Thank you very much!!


Any help please??
whroeder1
15902
whroeder1  
butterfly5: i have referenced "1" and i must be referenced each H4 previous bar. i must referenced this bar with
int timeFrameIndex = iBarShift(NULL, PERIOD_H4, iTime(NULL,PERIOD_H4,i), true);
  1. Help you with what? You've already answered your own question.
  2. Don't mix apples and oranges.
butterfly5
102
butterfly5  
whroeder1:
  1. Help you with what? You've already answered your own question.
  2. Don't mix apples and oranges.


Sorry whroeder1, i complete with this code but now i am blocked because i dont understand the function of time. For reference to one bar, i have to know the index of this bar and later other loop. I agree with you that i am mixing apples and Oranges.

#property copyright "Copyright © 2017"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

static datetime CurBar;
double Arrow[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
     SetIndexBuffer(0,Arrow);
     SetIndexStyle(0,DRAW_ARROW);
     SetIndexArrow(0,218);
  
  
  
     IndicatorDigits(Digits+0);
     return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   Comment("");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int  i,TimeframeIndex,j,countHighHugePeriod,counthigh;
   
                      
   for ( i=1000;i>=0;i--)
  
         {
            
           
           
                       
           TimeframeIndex=iBarShift(NULL,PERIOD_H4,iTime(NULL,PERIOD_H4,i),true);
          
           
           CurBar = iTime(NULL,PERIOD_H4,TimeframeIndex);
           
           
           if(CurBar != iTime(NULL,PERIOD_H4,TimeframeIndex))
               {
              
              
              
                for ( j=100 ;j>1 ;j--)
                
                   {
                   
                     if ( iHigh(NULL,PERIOD_H4,TimeframeIndex+1) > iHigh(NULL,PERIOD_H4,TimeframeIndex+j) )   
                       {
                        counthigh++;
                       }                                                                                  
          
                     if ( iHigh(NULL,PERIOD_H4,TimeframeIndex+1) <= iHigh(NULL,PERIOD_H4,TimeframeIndex+j))  
                       {
                        counthigh=0;
                       }
           
                    countHighHugePeriod = counthigh+1;
                    
                    if(countHighHugePeriod==3)
                      {
                      
                       Arrow[TimeframeIndex+1]=iHigh(NULL, PERIOD_H4,TimeframeIndex+1);
                       
                      }  
            
                   }
           
              }                      
           
           }       
                                                  
                 
   
   return(0);
  }
Thank you very much!
butterfly5
102
butterfly5  
butterfly5:


Sorry whroeder1, i complete with this code but now i am blocked because i dont understand the function of time. For reference to one bar, i have to know the index of this bar and later other loop. I agree with you that i am mixing apples and Oranges.

Thank you very much!


Sorry again whroeder,

i read the posts of your link, but is not the same. They use the easy way with iHighest, but i want iterate searching the previous high for learn to use well loops. Maybe my logic is wrong. The line :

if(countHighHugePeriod==3)
                      {
                      
                       Arrow[TimeframeIndex+1]=iHigh(NULL, PERIOD_H4,TimeframeIndex+1);
                       
                      }  

is only for prove the code.

Thank you very much!!

butterfly5
102
butterfly5  

Hi all again,

i made this code but with some mystake. I dont understand very well function of to see when a bar close with static datetime. Is the logic correct?

Please help!!

#property copyright "Copyright © 2017"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red

//static datetime CurBar;
double Arrow[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
     SetIndexBuffer(0,Arrow);
     SetIndexStyle(0,DRAW_ARROW);
     SetIndexArrow(0,218);
  
  
  
     IndicatorDigits(Digits+0);
     return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   Comment("");
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int  i,TimeframeIndex,j,countHighHugePeriod,counthigh;
   
                      
   for ( i=1000;i>=0;i--)
  
         {
            
           
           static datetime lastBar;
           
           datetime CurBar = iTime(NULL, PERIOD_H4,i);
                       
           
           if(lastBar != CurBar)
               {
              
              lastBar=  CurBar;
              
                for ( j=100 ;j>1 ;j--)
                
                   {
                   
                     if ( iHigh(NULL,PERIOD_H4,i+1) > iHigh(NULL,PERIOD_H4,j+1) )   // contador si el high del bar anterior al actual es mayor que los anteriores
                       {
                        counthigh++;
                       }                                                                                  
          
                     if ( iHigh(NULL,PERIOD_H4,i+1) <= iHigh(NULL,PERIOD_H4,j+1))  // Cuando el high del bar anterior es menor, entonces inicializamos el contador
                       {
                        counthigh=0;
                       }
           
                    countHighHugePeriod = counthigh+1;
                    
                    
                    if(countHighHugePeriod==3)
                      {
                      
                       Arrow[i+1]=iHigh(NULL, PERIOD_H4,i+1);
                       
                      }  
            
                   }
           
              }
           
                                
           
           }       
                  
                  
                                  
                 
   
   return(0);
  }
whroeder1
15902
whroeder1  
butterfly5: i made this code but with some mystake. I dont understand very well function of to see when a bar close with static datetime. Is the logic correct?
for ( i=1000;i>=0;i--){
   static datetime lastBar;
   datetime CurBar = iTime(NULL, PERIOD_H4,i);
   if(lastBar != CurBar){

  1. The logic of that new bar code is correct except that it is inside a loop. That if statement will always be true, the time of a bar is always different from the next..
  2. This is an indicator. There is no need for new bar code. Do it like any other indicator. See How to do your lookbacks correctly.
  3. Convert your i to H4 index for all your H4 calls. apples and oranges.
butterfly5
102
butterfly5  
whroeder1:
  1. The logic of that new bar code is correct except that it is inside a loop. That if statement will always be true, the time of a bar is always different from the next..
  2. This is an indicator. There is no need for new bar code. Do it like any other indicator. See How to do your lookbacks correctly.
  3. Convert your i to H4 index for all your H4 calls. apples and oranges.


Hi again whroeder1,

i was studying your links and i made this code but some mystake. When i prove variable "t", i always have the value of 0:

int start()
  {
   int  j,countHighper,counthigh;
   
       int LookBack = 100;
       int counted=IndicatorCounted();
  
       int t = Bars - 1 - MathMax(LookBack, counted);
               
       for(int iBar = t; iBar >= 0; iBar--)
          {
                
            int iTF = iBarShift(Symbol(), PERIOD_H4, iTime(NULL,PERIOD_H4,iBar));
                
             for ( j=100 ;j>2 ;j--)
                 {
                   
                   if ( iHigh(NULL,PERIOD_H4,iTF+1) > iHigh(NULL,PERIOD_H4,iTF+j) )   
                       {
                         counthigh++;
                       }                                                                                  
          
                   if ( iHigh(NULL,PERIOD_H4,iTF+1) <= iHigh(NULL,PERIOD_H4,iTF+j))  
                       {
                          counthigh=0;
                       }
                         
           
                    countHighper = counthigh+1;
                    
                    if(countHighper==3)
                      {
                      
                       Arrow[iTF+1]=iHigh(NULL, PERIOD_H4,iTF+1);
                       
                      }  
                   }
              }
                                
   return(0);
  }

Well, i use iBar like current bar of period H4, then i calculate the index of that bar with "iTF". Later i have other loop for iterate fixing the iTF+1 bar. I think the logic of indicator is correct but some mystake have me pulling hair.

Thank you very much for your patience, i am learning more!

whroeder1
15902
whroeder1  
butterfly5:


When i prove variable "t", i always have the value of 0:

Well, i use iBar like current bar of period H4, then i calculate the index of that bar with "iTF". Later i have other loop for iterate fixing the iTF+1 bar. I think the logic of indicator is correct but some mystake have me pulling hair.

  1. int t = Bars - 1 - MathMax(LookBack, counted);
    The first time through the loop, counted will be zero and you do all bars. After that it will be zero until a new bar starts.
  2.             int iTF = iBarShift(Symbol(), PERIOD_H4, iTime(NULL,PERIOD_H4,iBar));
                 for ( j=100 ;j>1 ;j--){                   
                       if ( iHigh(NULL,PERIOD_H4,iTF+1) > iHigh(NULL,PERIOD_H4,iTF+j) )  
    First you convert iBar to iTF for your H4 calls, good. But then you are looping iTF+100 to iTF. No where do you check if you have iTF+100 bars on the H4. Your lookback is not 100. It is 100 on the H4, 400 on the H1, 800 on the M30, etc.
           int LookBack = 100 * Period_H4 / _Period;
    

  3. Arrow[iTF]=iHigh(NULL, PERIOD_H4,iTF);
    Your buffer (Arrow) is the current chart. iTF is the H4. You are mixing apples and oranges.
  4. if ( iHigh(NULL,PERIOD_H4,iTF+1) <= iHigh(NULL,PERIOD_H4,iTF+j))  
    Why is there a +1? iTF is the H4 shift for bar iBar. That is the bar you should be looking at because that is the bar you will draw the arrow on.
  5. for ( j=100 ;j>2 ;j--){
       if ( iHigh(NULL,PERIOD_H4,iTF+1) > iHigh(NULL,PERIOD_H4,iTF+j) )   counthigh++;
       if ( iHigh(NULL,PERIOD_H4,iTF+1) <= iHigh(NULL,PERIOD_H4,iTF+j))   counthigh=0;
       if(countHigh==2)
    Why are you looking back 100 bars when you only want to draw an arrow then the current H4 is higher then the previous two?
  6. Where do you ever set the count to zero before the loop.
butterfly5
102
butterfly5  
whroeder1:
  1. The first time through the loop, counted will be zero and you do all bars. After that it will be zero until a new bar starts.
  2. First you convert iBar to iTF for your H4 calls, good. But then you are looping iTF+100 to iTF. No where do you check if you have iTF+100 bars on the H4. Your lookback is not 100. It is 100 on the H4, 400 on the H1, 800 on the M30, etc.

  3. Your buffer (Arrow) is the current chart. iTF is the H4. You are mixing apples and oranges.
  4. Why is there a +1? iTF is the H4 shift for bar iBar. That is the bar you should be looking at because that is the bar you will draw the arrow on.
  5. Why are you looking back 100 bars when you only want to draw an arrow then the current H4 is higher then the previous two?
  6. Where do you ever set the count to zero before the loop.


Thank you very much whroeder1, i understand value of "t", the conversion of lookBack to timeframe, the Arrow Buffer. I use "countHigh==3" for prove that really draw arrow in chart. So 100 bars back is random number that i put for see the iteration. I initialize count to 0 before loops... 

int start()
  {
   int  j,countHighHugePeriod=0,counthigh=0;
   
       int LookBack = 100 * PERIOD_H4 / _Period;
       int counted=IndicatorCounted();
  
       int t = Bars - 1 - MathMax(LookBack, counted);
               
       for(int iBar=t; iBar >= 0; iBar--)
          {
                
            int iTF = iBarShift(Symbol(), PERIOD_H4, iTime(NULL,PERIOD_H4,iBar));
                
             for ( j=100 ;j>2 ;j--)
                 {
             
                  if ( iHigh(NULL,PERIOD_H4,iTF) > iHigh(NULL,PERIOD_H4,iTF+j) )   counthigh++;
                  if ( iHigh(NULL,PERIOD_H4,iTF) <= iHigh(NULL,PERIOD_H4,iTF+j))   counthigh=0;
                
                         
                    countHighHugePeriod = counthigh+1;
                    
                    if(countHighHugePeriod==3)
                      {
                      
                       Arrow[iBar]=iHigh(NULL, PERIOD_H4,iTF);
                       
                      }  
                   }
              }
                                
   return(0);
  }

Sorry if i delay in answer but i am reading and studying your links. I want understand all the code! Thank you very much and awesome

To add comments, please log in or register