Download MetaTrader 5

Count bars since price reach a certain point

To add comments, please log in or register
simoncs
321
simoncs 2015.03.03 06:51 

Hi

Am i supposed to now be posting this in the mql5 forum? (not very clear as there doesn't seem to be a dedicated mt4 section) 

 

I have tried to write a function that will count the number of bars when a certain criteria is met. In this case when price may have crossed above/below the 61fib line. I then hope to use this barcount variable as a restriction to trades taken eg where RetraceBarCount <2

PLease see image of what i am hoping to do. The red numbers refer to a count of each bar that is above the 61 fib. In this example there are 4 bars. I will reset this counter every time there is a new swing. 

 

 

and here is the code... at the moment when i run print statements, the bar count is either 1 or 0, but randomly. I can't see the logic for why the count unfortunately.

Is my code wrong or is there a better way to do it?

 

void RetraceCheck()
{
RefreshRates();
static datetime Time0;
int i;

//ExtDTOSCTF = 15mins as a timeframe for example
// code is based on price being either above/below the 50fib or the 61fib, which is an external variable

if(Time0==iTime(Symbol(),ExtDTOSCTF,0))
      return(0);           // if this is not a new bar then let's not do anything

   Time0=iTime(Symbol(),ExtDTOSCTF,0);


for(i = 10; i >=0; i--)

 

  
   if(Trend==UpTrend) //will be buy trades
      if(ExtEntryLine==50)

        {
         
            if((iLow(NULL,ExtDTOSCTF,i+1)<Fib50Line) && (iLow(NULL,ExtDTOSCTF,i+1)>Fib61Line)) //low of current bar is less than 50 greater than 61
            RetraceBarCount=0;
            
            if((iLow(NULL,ExtDTOSCTF,i)<Fib50Line) && (iLow(NULL,ExtDTOSCTF,i)>Fib61Line)) //low of current bar is less than 50 greater than 61   
               
               
              {
              RetraceBarCount++;
                           
              }
              
             
   
   

        }

   else if(ExtEntryLine==61)

     {
      
         if((iLow(NULL,ExtDTOSCTF,i+1)<Fib61Line) && (iLow(NULL,ExtDTOSCTF,i+1)>Fib73Line))//low of current bar is less than 61 greater than 73
         RetraceBarCount=0;
         
          if((iLow(NULL,ExtDTOSCTF,i)<Fib61Line) && (iLow(NULL,ExtDTOSCTF,i)>Fib73Line))//low of current bar is less than 61 greater than 73   
           
           {
           RetraceBarCount++;
           
           }

     }

   if(Trend==DownTrend) //will be sell trades
      if(ExtEntryLine==50)

        {
         
            if((iHigh(NULL,ExtDTOSCTF,i+1)>Fib50Line) && (iHigh(NULL,ExtDTOSCTF,i+1)<Fib61Line))//high of current bar is greater than 50 less than 61
            RetraceBarCount=0;
            
            if((iHigh(NULL,ExtDTOSCTF,i)>Fib50Line) && (iHigh(NULL,ExtDTOSCTF,i)<Fib61Line))//high of current bar is greater than 50 less than 61   
              
              {
              RetraceBarCount++;
               
              }

        }

   else if(ExtEntryLine==61)
     {
      
         if((iHigh(NULL,ExtDTOSCTF,i+1)>Fib61Line) && (iHigh(NULL,ExtDTOSCTF,i+1)<Fib73Line))//high of previous bar is greater than 61 less than 73
         RetraceBarCount=0;
         
         if((iHigh(NULL,ExtDTOSCTF,i)>Fib61Line) && (iHigh(NULL,ExtDTOSCTF,i)<Fib73Line))//high of previous bar is greater than 61 less than 73   
           
           {
           RetraceBarCount++;
            
           }

     }
     
     
     
    
     
     
   return(RetraceBarCount);


} 

 thanks

whroeder1
14737
whroeder1 2015.03.03 12:45  
simoncs: and here is the code... at the moment when i run print statements, the bar count is either 1 or 0, but randomly. I can't see the logic for why the count unfortunately.
for(i = 10; i >=0; i--)
Don't look at bar zero
simoncs
321
simoncs 2015.03.04 20:52  
WHRoeder:
Don't look at bar zero


thanks amended to i>0 and this improves things for most occurences but there are still a few that are wrong so will investigate those further.
simoncs
321
simoncs 2015.03.11 06:21  

hi 

i amended the code, but i still can't get the count to be correct. In this example i expect the count to be 3, but it is only 1.

i have tried changing the for loop to count up instead of down, but that made no difference.

The retracebarcount is only reset in the function,no where else so i can't understand why the count doesn't seem to work in this example. 

 

any ideas?

 

 

 

output from print statement shows the Retrace No as only 1, prior to the trade being taken.

 

 

 

  
void RetraceCheck()
{
RefreshRates();
static datetime Time0;
int i;

if(Time0==iTime(Symbol(),ExtDTOSCTF,0))
      return(0);           // if this is not a new bar then let's not do anything

   Time0=iTime(Symbol(),ExtDTOSCTF,0);


for(i = 0; i >0; i--) //amended from i>=0 to i>0

 

  
   if(Trend==UpTrend) //will be buy trades
      if(ExtEntryLine==50)

        {
         
            if((iLow(NULL,ExtDTOSCTF,i+1)> Fib50Line)) /* && (iLow(NULL,ExtDTOSCTF,i+1)>Fib61Line))*/ //low of previous  bar is greater than 50 greater than 61
            RetraceBarCount=0;
            
            if((iLow(NULL,ExtDTOSCTF,i)<Fib50Line) && (iLow(NULL,ExtDTOSCTF,i)>Fib61Line)) //low of current bar is less than 50 greater than 61   
               
              {
              RetraceBarCount++;
              }
              
             
   
   

        }

   else if(ExtEntryLine==61)

     {
      
         if((iLow(NULL,ExtDTOSCTF,i+1)>Fib61Line)) /* && (iLow(NULL,ExtDTOSCTF,i+1)>Fib73Line))*/ //low of previous bar is greater than 61 greater than 73
         RetraceBarCount=0;
         
          if((iLow(NULL,ExtDTOSCTF,i)<Fib61Line) && (iLow(NULL,ExtDTOSCTF,i)>Fib73Line))//low of current bar is less than 61 greater than 73   
            
           {
           RetraceBarCount++;
           
           
           }

     }

   if(Trend==DownTrend) //will be sell trades
      if(ExtEntryLine==50)

        {
         
            if((iHigh(NULL,ExtDTOSCTF,i+1)< Fib50Line)) /*&& (iHigh(NULL,ExtDTOSCTF,i+1)<Fib61Line))*/  //high of previous bar is less than 50 less than 61
            RetraceBarCount=0;
            
            if((iHigh(NULL,ExtDTOSCTF,i)>Fib50Line) && (iHigh(NULL,ExtDTOSCTF,i)<Fib61Line))//high of current bar is greater than 50 less than 61   
            
               
              {
              RetraceBarCount++;
              
              }

        }

   else if(ExtEntryLine==61)
     {
      
         if((iHigh(NULL,ExtDTOSCTF,i+1)< Fib61Line)) /*&& (iHigh(NULL,ExtDTOSCTF,i+1)<Fib73Line))*/ //high of previous bar is less than 61 less than 73
         RetraceBarCount=0;
         
         if((iHigh(NULL,ExtDTOSCTF,i)>Fib61Line) && (iHigh(NULL,ExtDTOSCTF,i)<Fib73Line))//high of current bar is greater than 61 less than 73   
            
           {
           RetraceBarCount++;
           
           }

     }
     
     
     
    
     
     
   return(RetraceBarCount);


}  
  
Carl Schreiber
6667
Carl Schreiber 2015.03.11 08:16  

You will never enter this loop:

for(i = 0; i >0; i--) //amended from i>=0 to i>0

But I think you wanted to write:

for(i =10; i >0; i--) //amended from i>=0 to i>0
simoncs
321
simoncs 2015.03.16 03:26  
gooly:

You will never enter this loop:

But I think you wanted to write:

thks - i was trying all sorts of options and forgot to change it back when copying the code.

i think i may have found the problem. the fib line was changing, but as the last numbers were identical didn't spot it until i tried a different example. still testing to find out if this is the reason though.

The reason the fib line changes is that it is related to the swings calc which repastes.

So what using the above code won't work as it only checks the previous bar to see if the condition is true, to then start the count.

So what i probably need to do is to count all the bars where the high is greater than 61fib, after the SwingDate0 variable. 

i think i need to use ibarshift to do this, or ihighest? how would i format this?

 

thks 

simoncs
321
simoncs 2015.04.14 08:27  

i am still having issues with this, and have tried all manner of code to resolve it.

The issue seems to be that as the zigzag repaints, the variables change.

what i actually need to achieve is each time the value of SwingDate[0] changes it looks back from the current bar and counts each bar that has crossed the Fib61 line since SwingDate[0]. Then this count can be reset/reevaluated each time the SwingDate[0] changes.

 

SwingDate[0] in the images above (in previous posts) would be the last bar in the previous swing ie Fib 0

the last code i tried was as below...

 

any suggestions on how i achieve the above, please? 

 

void RetraceCheck()
  {
   RefreshRates();
   static datetime Time0;
   int i;



   

      if(Time0==iTime(Symbol(),ExtZZSwingTF,0))
         return(0);           // if this is not a new bar then let's not do anything

   Time0=iTime(Symbol(),ExtZZSwingTF,0);

for(i=10; i>=0; i--) //amended from i>=0 to i>0
{
   if(i>=iBarShift(Symbol(),ExtZZSwingTF,SwingDate[0]))continue;

   if(Trend==UpTrend) //will be buy trades
      if(ExtEntryLine==50)

        {

         if((iLow(NULL,ExtZZSwingTF,i+1)>Fib50Line)) /* && (iLow(NULL,ExtZZSwingTF,i+1)>Fib61Line))*/ //low of current bar is less than 50 greater than 61
            RetraceBarCount=0;

         if((iLow(NULL,ExtZZSwingTF,i)<Fib50Line) && (iLow(NULL,ExtZZSwingTF,i)>Fib61Line)) //low of current bar is less than 50 greater than 61   

           {
            RetraceBarCount++;
           }



        }

   else if(ExtEntryLine==61)

     {

      if((iLow(NULL,ExtZZSwingTF,i+1)>Fib61Line)) /* && (iLow(NULL,ExtZZSwingTF,i+1)>Fib73Line))*/ //low of current bar is less than 61 greater than 73
         RetraceBarCount=0;

      if((iLow(NULL,ExtZZSwingTF,i)<Fib61Line) && (iLow(NULL,ExtZZSwingTF,i)>Fib73Line))//low of current bar is less than 61 greater than 73   

        {
         RetraceBarCount++;

        }

     }

   if(Trend==DownTrend) //will be sell trades
      if(ExtEntryLine==50)

        {

         if((iHigh(NULL,ExtZZSwingTF,i+1)<Fib50Line)) /*&& (iHigh(NULL,ExtZZSwingTF,i+1)<Fib61Line))*/  //high of current bar is greater than 50 less than 61
            RetraceBarCount=0;

         if((iHigh(NULL,ExtZZSwingTF,i)>Fib50Line) && (iHigh(NULL,ExtZZSwingTF,i)<Fib61Line))//high of current bar is greater than 50 less than 61   

           {
            RetraceBarCount++;

           }

        }

   else if(ExtEntryLine==61)
     {

      if((iHigh(NULL,ExtZZSwingTF,i+1)<Fib61Line)) /*&& (iHigh(NULL,ExtZZSwingTF,i+1)<Fib73Line))*/ //high of previous bar is greater than 61 less than 73
         RetraceBarCount=0;

      if((iHigh(NULL,ExtZZSwingTF,i)>Fib61Line) && (iHigh(NULL,ExtZZSwingTF,i)<Fib73Line))//high of previous bar is greater than 61 less than 73   

        {
         RetraceBarCount++;

        }

     }


}

   return(RetraceBarCount);


  }
simoncs
321
simoncs 2015.04.19 21:54  
bump.
still can't  find a solution
whroeder1
14737
whroeder1 2015.04.20 00:56  
simoncs:

The issue seems to be that as the zigzag repaints, the variables change.

what i actually need to achieve is each time the value of SwingDate[0] changes ...

any suggestions on how i achieve the above, please?
  1. ZigZag is known to repaint. Deal with it.
  2. You don't state what you actually need.
  3. Unless you can state it in concrete terms, nobody can help you.
simoncs
321
simoncs 2015.04.21 04:25  

sorry WHR,thought that was clear.

I am comfortable with the Zigzag repainting - it works fine in my EA.

The SwingDate[0] is one of the zigzag/swing points/dates. This therefore changes regularly. What i need to do is...

  

Each time Swingdate[0] changes - calculate the number of bars that have crossed the Fib61 line (ie higher or lower depending on trend direction),from the current bar back to the SwingDate[0]

So SwingDate[0]is the starting point, and count forward to the current bar. Increment a counter each time a bar crosses the Fib61line. The counter can be reset each time SwingDate[0] changes. 

I think i need a loop to do this, and increase the count each time a bar crosses the Fib61line, but i can't seem to get it to work consistently. I have tried lots of variation to my loop setup,as above but no luck so far.

despite having read all the help docs, loops and arrays always screw me. 

 

let me know if this isn't clear and i will try to put together a visual example.

 

thanks 

simoncs
321
simoncs 2015.04.29 01:30  
simoncs:

sorry WHR,thought that was clear.

I am comfortable with the Zigzag repainting - it works fine in my EA.

The SwingDate[0] is one of the zigzag/swing points/dates. This therefore changes regularly. What i need to do is...

  

Each time Swingdate[0] changes - calculate the number of bars that have crossed the Fib61 line (ie higher or lower depending on trend direction),from the current bar back to the SwingDate[0]

So SwingDate[0]is the starting point, and count forward to the current bar. Increment a counter each time a bar crosses the Fib61line. The counter can be reset each time SwingDate[0] changes. 

I think i need a loop to do this, and increase the count each time a bar crosses the Fib61line, but i can't seem to get it to work consistently. I have tried lots of variation to my loop setup,as above but no luck so far.

despite having read all the help docs, loops and arrays always screw me. 

 

let me know if this isn't clear and i will try to put together a visual example.

 

thanks 

bump
123
To add comments, please log in or register