BULLISH/BEARISH Bar counter code

 

Hi guys i`m having problem creating a bar counter code.Basically this EA would be a statistical tool for me and it would help me very much with my market research.It would count how many consecutive BEARISH bars appear before a BULLISH bar disrupts the sequence, and how many consecutive BULLISH bars appear before a BEARISH bar disrupts the sequence; and it would always display the maximum BEARISH consecutive and maximum BULLISH consecutive bars happening in the journal as a Print().

So imagine it like this:

BULL BULL BULL BEAR BULL BULL BULL BULL BEAR

maxbullishbars = 4

BEAR BEAR BEAR BULL BEAR BEAR BULL BEAR BEAR BEAR BEAR BEAR BULL

maxbearishbars = 5



It should look like this, but my code is messed up so i please need help correcting it:

int up=0;
int down=0;
int upmax=0;
int downmax=0;

Start()

   if(Close[1]<Open[1]){
   down++;
   if(Close[0]>Open[0]){
   if(down>downmax)
   downmax=down;
   down=0;
   }
   }
   
   if(Close[1]>Open[1]){
   up++;
   if(Close[0]<Open[0]){
   if(up>upmax)
   upmax=up;
   up=0;
   }
   }
 
 
 
 Print("BULLISHBARS MAX: " ,upmax);
 Print("BEARISHBARS MAX:" ,downmax);

For some reason when i end the test it returns values for upmax and downmax like 150, 200 and so,which is ridiculous since there cant be so many same coloured consecutive bars because of retracements so please correct my code.TY MUCH!!




 

how is it going through the bars?

this code simply checks the 0., and 1. bars' relation on every tick.

it seems your result is the number of ticks during which the 0. and 1. bars were bulls or bears.


you need a loop for example: for (int i=Bars-1;i>0;i--)

and check the open, close of bars [i] and [i-1] or [i+1], the way you prefer.

 
szgy74:

how is it going through the bars?

this code simply checks the 0., and 1. bars' relation on every tick.

it seems your result is the number of ticks during which the 0. and 1. bars were bulls or bears.


At every tick it checks the previous bars color, then if the current bar is different color then the consecutive sequence is broken so it saves that in the downmax or upmax variable.

Its not the number of ticks because they get zeroed after they were saved, so it should work but something different is causing the bad values.

  down=0;
   up=0;
szgy74:


you need a loop for example: for (int i=Bars-1;i>0;i--)

and check the open, close of bars [i] and [i-1] or [i+1], the way you prefer.

Why should i need that, if i use all Bars they that would slow it down very much,although it would be a solution but i guess not the optimal.But my solution should work too but for some reason it doesnt, and i cant figure out why.Why it gives huge values like 100 and 200 when it should be tops 20-30 because a retracement is inevitable.Also if the counter is set to 0 after a local maximum value why it doesnt work?

Look at the returned values


 
Proximus:

At every tick it checks the previous bars color, then if the current bar is different color then the consecutive sequence is broken so it saves that in the downmax or upmax variable.

It makes no sense to use bar 0, it can be bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, . . . it is only finally a bearish or bullish bar in the same way as bars 1, 2, 3, 4, 5, etc. when it is complete, when it is complete it is no longer bar 0 it is bar 1

The reason you get huge values is because you are counting bar 0 for each tick . . . instead count bar 1 each time a new bar 1 arrives . . .

 

How exactly do you want to have this result? Continuosly, increasing on the fly, or at once looking the past candles?

How about checking once per bar?

Another thing, when you compare Close[0] you may have several different results until the bar closes.

 
RaptorUK:

It makes no sense to use bar 0, it can be bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, . . . it is only finally a bearish or bullish bar in the same way as bars 1, 2, 3, 4, 5, etc. when it is complete, when it is complete it is no longer bar 0 it is bar 1

The reason you get huge values is because you are counting bar 0 for each tick . . . instead count bar 1 each time a new bar 1 arrives . . .


szgy74:

How exactly do you want to have this result? Continuosly, increasing on the fly, or at once looking the past candles?

How about checking once per bar?

Another thing, when you compare Close[0] you may have several different results until the bar closes.


Thought about that too,but unfortunately its still the same it get it that bar[0] can modify so i shifted them all back 1 bar but still the same wrong values...

Here is the new code, i made it clearer to visualize i hope you can spot the error.Ty!

int up=0;
int down=0;
int upmax=0;
int downmax=0;

Start()

/////////////////////////////////////////////////////
////////////////////////////////////////////////////
//*******************************BEARISH TEST

//START FIRST IF
   if(Close[2]<Open[2])
   {
      down++;
//START SECOND IF
      if(Close[1]>Open[1])
      {
//START THIRD IF      
         if(down>downmax)
            {
               downmax=down;
            }
//END THIRD IF              
         down=0;
      }
//END SECOND IF
   }
//END FIRST IF 

////////////////////////////////////////////////////  
//*******************************BULLISH TEST
  
//START FIRST IF
   if(Close[2]>Open[2])
   {
      up++;
//START SECOND IF
      if(Close[1]<Open[1])
      {
//START THIRD IF        
         if(up>upmax)
            {
               upmax=up;
            }
//END THIRD IF              
         up=0;
      }
//END SECOND IF      
   }
//END FIRST IF 
///////////////////PRINTOUT
 
 Print("MAX BULLISHBARS: " ,upmax);
 Print("MAX BEARISHBARS: " ,downmax);
   
/////////////////////////////////////////////////////


And here is the log:


Plus if you ask its not about the quality of the data, i have very high quality data so thats 100% not the problem.

 
RaptorUK:

It makes no sense to use bar 0, it can be bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, bearish, bullish, . . . it is only finally a bearish or bullish bar in the same way as bars 1, 2, 3, 4, 5, etc. when it is complete, when it is complete it is no longer bar 0 it is bar 1

The reason you get huge values is because you are counting bar 0 for each tick . . . instead count bar 1 each time a new bar 1 arrives . . .

Try this EA . . .

int up=0;
int down=0;
int upmax=0;
int downmax=0;

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {
   static datetime Bar1Time =0;


   if(Bar1Time < Time[1])    // new Bar
      {
      if(Close[1] < Open[1])
         {
         down++;
         if(down > downmax) downmax = down;  // set the new max value of down
         if(up > 0) up = 0;  // reset the up count because we have a down bar   
         }
   
      if(Close[1] > Open[1])
         {
         up++;
         if(up > upmax) upmax = up;  // set the new max value of up
         if(down > 0) down = 0;  // reset the down count because we have a up bar   
         }
      
      Bar1Time = Time[1];
      
      Comment("BULLISH BARS MAX: " ,upmax, " BEARISH BARS MAX: " ,downmax);
      
      Print("BULLISH BARS MAX: " ,upmax);
      Print("BEARISH BARS MAX:" ,downmax);
      }
 
   } 
 
Proximus:

Thought about that too,but unfortunately its still the same it get it that bar[0] can modify so i shifted them all back 1 bar but still the same wrong values...

Here is the new code, i made it clearer to visualize i hope you can spot the error.Ty!

You are still counting the same bars on each tick . . . do it once per bar.
 
RaptorUK:

Try this EA . . .


Oh yea now thats what i call a good bar counter it works perfectly, returns values around 10-20 so thats acceptable and i guess it works properly, anything above 20 is suspicious since retracements happen often.Ty much Raptor! Although i thought 1 tick is 1 bar on the specific TF but i guess i was mistakened.
 
Proximus:

Oh yea now thats what i call a good bar counter it works perfectly, returns values around 10-20 so thats acceptable and i guess it works properly, anything above 20 is suspicious since retracements happen often.Ty much Raptor! Although i thought 1 tick is 1 bar on the specific TF but i guess i was mistakened.
Run the Strategy Tester on every tick model slowly and count how many ticks there are for an M1 bar . . . or simply look at am M1 bar and look at the tick count (volume) for each bar, you can see it in the bottom right of the terminal next to the Close value . . .
 
RaptorUK:

Try this EA . . .


As

static datetime Bar1Time =0;

is within the Start() function, won't it reset every tick?

Reason: