Mql4 How to count specific bars above/below specific High/Low for some days

To add comments, please log in or register
Mpower
24
Mpower  

Dear Coders,

I need your help.

By "Specific" I mean Time Window.

I would like to count BARS from 13:15 to 20:00 that are Higher and lower than the High/Low defined from 12:00 to 13:00 for every day.

I tried this:

double HH,LL,highest,lowest;

for(int i=500; i>0; i--){

if (TimeHour(iTime(NULL, 0, i)) == 20 && TimeMinute(iTime(NULL, 0, i)) == 00) {

highest= High[iHighest(Symbol(),PERIOD_M15, MODE_HIGH, 33, i)];
lowest = Low[iLowest(Symbol(),  PERIOD_M15, MODE_LOW, 33, i)];

string check=TimeToStr(iTime(Symbol(),PERIOD_M15,i),TIME_MINUTES);{

if(check>="13:15" && check<="20:00"){

if(iHigh(Symbol(),PERIOD_M15,i)>=highest) HH=HH+1;
if(iLow(Symbol(), PERIOD_M15,i)<=lowest)  LL=LL+1; 

}}}}

I attached a picture. 


Thanks a lot for your support,

Mpo

Mohamad Zulhairi Baba
17684
Mohamad Zulhairi Baba  

comparing string will never work like this.

string check=TimeToStr(iTime(Symbol(),PERIOD_M15,i),TIME_MINUTES);{

if(check>="13:15" && check<="20:00"){
  // --
}
}
Mpower
24
Mpower  
Mohamad Zulhairi Baba:

comparing string will never work like this.

Thank you Mohamad. I continue to search.
nicholi shen
2244
nicholi shen  

Your screenshot with markup is very nicely done. Here's a working example:


void OnTick(){
   int above, below;
   bool is_calc = above_below(
      above, below,
      D'12:00', D'13:00', D'20:00' 
   );
   if(is_calc)
      printf("above = %d, below = %d", above, below);
   
}

bool above_below( int &count_above,
                  int &count_below,
                  const datetime range_begin_time,
                  const datetime range_end_time,
                  const datetime check_end_time,
                  const ENUM_TIMEFRAMES timeframe=PERIOD_M15)
{
   count_above = 0;
   count_below = 0;
   if(TimeCurrent() < range_end_time + PeriodSeconds(timeframe))
      return false;
   MqlRates rates[];
   double highs[];
   double lows[];
   int total_highs = CopyHigh(_Symbol, timeframe, range_begin_time, range_end_time, highs);
   int total_lows  = CopyLow(_Symbol, timeframe, range_begin_time, range_end_time, lows);
   int total_rates = CopyRates(_Symbol, timeframe, 
      datetime(range_end_time + PeriodSeconds(timeframe)), check_end_time, rates
   );
   if(total_highs < 1 || total_lows < 1 || total_rates < 1)
      return false;
   double highest = highs[ArrayMaximum(highs)];
   double lowest  = lows[ArrayMinimum(lows)];
   
   for(int i=0; i<total_rates; i++){
      if(rates[i].high > highest)
         count_above++;
      if(rates[i].low < lowest)
         count_below++;
   }
   return true;
}
Mpower
24
Mpower  
nicholi shen:

Your screenshot with markup is very nicely done. Here's a working example:


Dear Nicholi,

It works very well ! Bravo and thank you for your help.

If you agree, I try by myself to find how to display Counted Bars for days before, if I can't, I come back to you with a screenshot.

Once again, thank you ;)

Mpo

Mpower
24
Mpower  
nicholi shen:

Your screenshot with markup is very nicely done. Here's a working example:


Dear Nicholi,

I did some tries with these lines of code but it displays only on the current day.

      ObjectCreate("Counted_Bars", OBJ_TEXT, 0, iTime(NULL, 0, i+20), highest);
      ObjectSetText("Counted_Bars", "High "+count_above+"  Low "+count_below, 16, "Verdana", DarkOrange);

In attachment the screenshot of what i am working on.



Thank you in advance for your help,

Mpo

Petr Nosek
1652
Petr Nosek  
mpower_321:
 

I did some tries with these lines of code but it displays only on the current day.

We don't see your whole code. But I guess you could have a problem with the objects name. The object name must be unique. For an example see below.

      ObjectCreate("Counted_Bars"+IntegerToString(count,5,'0'), OBJ_TEXT, 0, iTime(NULL, 0, i+20), highest);
      ObjectSetText("Counted_Bars"+IntegerToString(count,5,'0'), "High "+count_above+"  Low "+count_below, 16, "Verdana", DarkOrange);
      count++;
Mpower
24
Mpower  
Petr Nosek:

We don't see your whole code. But I guess you could have a problem with the objects name. The object name must be unique. For an example see below.

Dear Petr,

Sorry you are right.

I am using the code from Nicholi Shen (Post #3). I have inserted an "ObjectCreate" like that :

int init(){ObjectsDeleteAll(EMPTY, OBJ_RECTANGLE);ObjectsDeleteAll(EMPTY, OBJ_TEXT);return (0);}
int deinit(){return(0);}
void OnTick(){
   int above, below;
   bool is_calc = above_below(
      above, below,
      D'12:00', D'13:00', D'20:00' 
   );
   if(is_calc)
      Comment("Above = ",above,"   Below = ",below);
   
}

bool above_below( int &count_above,
                  int &count_below,
                  const datetime range_begin_time,
                  const datetime range_end_time,
                  const datetime check_end_time,
                  const ENUM_TIMEFRAMES timeframe=PERIOD_M15)
{
   count_above = 0;
   count_below = 0;
   if(TimeCurrent() < range_end_time + PeriodSeconds(timeframe))
      return false;
   MqlRates rates[];
   double highs[];
   double lows[];
   int total_highs = CopyHigh(_Symbol, timeframe, range_begin_time, range_end_time, highs);
   int total_lows  = CopyLow(_Symbol, timeframe, range_begin_time, range_end_time, lows);
   int total_rates = CopyRates(_Symbol, timeframe, 
      datetime(range_end_time + PeriodSeconds(timeframe)), check_end_time, rates
   );
   if(total_highs < 1 || total_lows < 1 || total_rates < 1)
      return false;
   double highest = highs[ArrayMaximum(highs)];
   double lowest  = lows[ArrayMinimum(lows)];
   
   for(int i=0; i<total_rates; i++){
      if(rates[i].high > highest)
         count_above++;
      if(rates[i].low < lowest)
         count_below++;
         

      ObjectCreate("Counted_Bars", OBJ_TEXT, 0, iTime(NULL, 0, i+20), highest);
      ObjectSetText("Counted_Bars", "Above "+count_above+"    Below "+count_below, 10, "Verdana", Lime);         
         
         
   }
   return true;
}


This is my result :



And I am working to get that, without RECTANGLES, and i used High and Low words instead of Above and Below, sorry.


Thank you for your help,

Mpo

Petr Nosek
1652
Petr Nosek  
mpower_321:

I understand what you want to achieve. It is not a difficult task much but I'm not sure you have skills enough. If so follow:

  • you should write the code as an indicator (not as an EA)
  • you have to count your conditions for every day (not only for last or current day)
  • you have to create a text object (with an unique name) for every day and fill it by values
  • you have to check the new period

Maybe I've forgotten something but these rules are crucial. If you don't feel you can do it you should ask somebody to write the code for you.

Mpower
24
Mpower  
Petr Nosek:

I understand what you want to achieve. It is not a difficult task much but I'm not sure you have skills enough. If so follow:

  • you should write the code as an indicator (not as an EA)
  • you have to count your conditions for every day (not only for last or current day)
  • you have to create a text object (with an unique name) for every day and fill it by values
  • you have to check the new period

Maybe I've forgotten something but these rules are crucial. If you don't feel you can do it you should ask somebody to write the code for you.

Thanks Petr for your help ;)

Hope somebody will help the code to be achieved.

For sure i do not have the skills for it  :p

Mpo

Petr Nosek
1652
Petr Nosek  
mpower_321:

Thanks Petr for your help ;)

Hope somebody will help the code to be achieved.

For sure i do not have the skills for it  :p

Mpo

You're welcome.

If you don't find somebody to help you let me know. The best way by PM because I won't read this topic anymore.

To add comments, please log in or register