Loop to count consecutive bull or bear bars

 
int Consecutive(int Count)
  {
//---

   int      BB = 0;

   for(int i = 1; i <= Count; i++)
   {
      if(iClose(_Symbol, PERIOD_H1, i) - iOpen(_Symbol, PERIOD_H1, i) > 0)
      {
         BB++;
      }
      else
      {
         BB--;
      }
   }

//---
   return(BB);
  }

Besides using loop to count consecutive bull or bear bar, is there any more efficient methods?

 
Dua Yong Rew:

Besides using loop to count consecutive bull or bear bar, is there any more efficient methods?

You can use an array to keep track of consecutive bullish//bearish bars and update it based on current bar direction.
 
Dua Yong Rew:

Besides using loop to count consecutive bull or bear bar, is there any more efficient methods?

just compare open below close or close below open
 

Hi

I don’t think there are other methods, maybe there is some indicator available from some sellers, but this loop you showed us is not best solution. Since you return a summed value, so when you have 3 bullish candles and 2 bearish you get “1” – but that does not mean that you have 1 bullish candle. You would have to break the loop as soon as the direction is changed and return the current number of consecutive candles. Unless this is what you want and need to get the summary number of “potential” bullish/bearish candle within recent chosen number of candles.

Best Regards

 

Read the documentation CAREFULLY!

https://www.mql5.com/en/docs/series/iclose

"Note

The function always returns actual data. For this purpose it performs a request to the timeseries for the specified symbol/period during each call. This means that if there is no ready data during the first function call, some time may be taken to prepare the result.

The function does not store previous calls results, and there is no local cache for quick value return."

Documentation on MQL5: Timeseries and Indicators Access / iClose
Documentation on MQL5: Timeseries and Indicators Access / iClose
  • www.mql5.com
Returns the Close price of the bar (indicated by the 'shift' parameter) on the corresponding chart. Parameters symbol [in]  The symbol name of...
 
Flavio Javier Jarabeck #:

Read the documentation CAREFULLY!

https://www.mql5.com/en/docs/series/iclose

"Note

The function always returns actual data. For this purpose it performs a request to the timeseries for the specified symbol/period during each call. This means that if there is no ready data during the first function call, some time may be taken to prepare the result.

The function does not store previous calls results, and there is no local cache for quick value return."

 I'm pretty sure his function is working for him, so what's the big deal there?


As regard to a more efficient way, I'm not sure, I think this function is efficient enough unless you want to manage the counting of consecutive bull or bear bars in an indicator instead, and then use iCustom to call that indicator in the EA.

 
Conor Mcnamara #:

 I'm pretty sure his function is working for him, so what's the big deal there?


As regard to a more efficient way, I'm not sure, I think this function is efficient enough unless you want to manage the counting of consecutive bull or bear bars in an indicator instead, and then use iCustom to call that indicator in the EA.

All "i" functions don't have readily available data... you must be very carefull with your code...

The code is being wrongly used...




Again, it is all there in the DOCUMENTATION.

 
Flavio Javier Jarabeck #:

All "i" functions don't have readily available data... you must be very carefull with your code...

The code is being wrongly used...




Again, it is all there in the DOCUMENTATION.


No it isn't.

You can use 'i' without issues in that function call like he's doing. As long as it doesn't return an error, then it will shift to each historic bar defined by the index variable.
The only thing he can't get is the close price of the unfinished bar which is defined by index zero. But he most likely doesn't need that one.
 
Conor Mcnamara #: No it isn't.

Yes it is.

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

On MT5: Unless the current chart is that specific pair/TF, you must synchronize the terminal Data from the Server before accessing candle/indicator values.
          Error 4806 while using CopyBuffer() - Expert Advisors and Automated Trading - MQL5 programming forum #10 (2020)
          Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum (2019)
          Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
          Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum #2 (2018)
          SymbolInfoInteger doesn't work - Symbols - General - MQL5 programming forum (2019)

 
Well you don't need to use copybuffer for iClose or iOpen in mql5. If you try his function you would see it works. It works because his loop starts at 1.
 

I rest my case...

ALL "i" functions have this delay. You MUST create an according code to deal with these delays... Otherwise you could be in trouble, using something that it isn't avalable yet...

But, whatever... I will not discuss things that are clearly stated in the DOCUMENTATION.

The OP now have enough guidance to redo the code... that by the way it is poorly coded... for someone that has 11+ years of experience and several products in the Marketplace.

Live Long and Prosper!

;)