Indicator to count Bars of M1 Period that crisscross Open price of D1 Period

 

Greetings! I need help on this indicator. Been trying different method to count number of Bars in M1 Period that came across the level of Open price in the D1 Period. I attached my attempt to code it but unable to run it successfully. Been trying hard on this but I hit the wall so now I ask the help of kind gentlemen here for help!

Here's what I need for this indicator:

1. When ever a Bar in M1 Period price came across the price of Open in D1 Period it will be counted. There are 1440 bars in M1 Period for each D1 Period so every 1440 bars will be tested.

2. For each D1 Bar the total count/D1 Bar will be summed up and averaged by a certain value. Say the average is 30, this 30 is the number of D1 bars.

//+------------------------------------------------------------------+
//|                                                          XXX.mq4 |
//|                                              Copyright 2014, XXX |
//|                                                              XXX |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, XXX"
#property link      "XXX"
#property version   "1.00"
#property indicator_separate_window    // Indicator is drawn in the main window
#property indicator_buffers 1       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
//#property indicator_color2 Red      // Color of the 2nd line
 int count;  // Bar index
extern int Aver_Bars=30;             // number of bars for calculation
//extern double lots= 0.1; 
//extern int Pnt= 10; // Point multiplier for the pips.
double Buf_0[], Buf_1[];             // Declaring indicator arrays
//THIS INDICATOR IS TO COUNT NUMBER OF M1 BARS WHICH HIGH[I] IS HIGHER AND LOW[I] IS LOWER THAN OPEN[1440].
//--------------------------------------------------------------------
int init()                          // Special function init()
  {
//--------------------------------------------------------------------
   SetIndexBuffer(0,Buf_0);         // Assigning an array to a buffer
   SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);// Line style
//--------------------------------------------------------------------
   SetIndexBuffer(1,Buf_1);         // Assigning an array to a buffer
   //SetIndexStyle (1,DRAW_LINE,STYLE_DOT,1);// Line style
//--------------------------------------------------------------------
   return(0);                          // Exit the special funct.init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
  if (Crosses()) count++;
   int i, n,                           // Bar index
       Counted_bars; 
       Buf_1[i]= count;               // Number of counted bars
   double   Sum_H;  
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
      Sum_H=0;                      // Nulling at loop beginning
    
      for(n=i;n<=i+Aver_Bars-1;n++) // Loop of summing values
     {
      Buf_0[i]=Buf_1[n];             // Value of 0 buffer on i bar
    
      i--;                          // Calculating index of the next bar
     }
  }   
//--------------------------------------------------------------------
   return(0);                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------

bool Crosses()
   {
   int i;
   double H=iHigh(NULL,PERIOD_M1,i);
   double L=iLow(NULL,PERIOD_M1,i);
   double O=iOpen(NULL,PERIOD_D1,i);
 
       if (O > L && O < H)
         
  return(true);

else
return(EMPTY_VALUE);
   }

3. Then the average value will be plotted as the indicator.

 
PLEASE HELP ANYONE! IS THIS POSSIBLE AT ALL?
 

I can't figure out from your code what you are trying to do but in 2 places you do not give variable i a value. You need to address this.

int start()                         // Special function start()
  {
  if (Crosses()) count++;
   int i, n,                           // Bar index
       Counted_bars; 
       Buf_1[i]= count;               // Number of counted bars
   double   Sum_H;  
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                      // Loop for uncounted bars
     {
      Sum_H=0;                      // Nulling at loop beginning
    
      for(n=i;n<=i+Aver_Bars-1;n++) // Loop of summing values
     {
      Buf_0[i]=Buf_1[n];             // Value of 0 buffer on i bar
    
      i--;                          // Calculating index of the next bar
     }
  }   
//--------------------------------------------------------------------
   return(0);                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------

bool Crosses()
   {
   int i;
   double H=iHigh(NULL,PERIOD_M1,i);
   double L=iLow(NULL,PERIOD_M1,i);
   double O=iOpen(NULL,PERIOD_D1,i);
 
       if (O > L && O < H)
         
  return(true);

else
return(EMPTY_VALUE);
   }
 

Sorry to be blunt, but your code shows that you are trying to achieve something far beyond your capability

Example

bool Crosses()
   {
   int i;
   double H=iHigh(NULL,PERIOD_M1,i);
   double L=iLow(NULL,PERIOD_M1,i);
   double O=iOpen(NULL,PERIOD_D1,i);
 
       if (O > L && O < H)
         
  return(true);

else
return(EMPTY_VALUE);

Let's say that i = 1 for example

double O=iOpen(NULL,PERIOD_D1,1);

This will return yesterday's open price

   double H=iHigh(NULL,PERIOD_M1,1);
   double L=iLow(NULL,PERIOD_M1,1);

These 2 will return values for the last closed 1 minute bar.


So you are comparing the values from 1 minute ago to yesterday's open.

If i = 2, you will be comparing values from 2 minutes ago to the open 2 days ago

You must see that this cannot be what you want.

 
GumRai:

Sorry to be blunt, but your code shows that you are trying to achieve something far beyond your capability

Example

Let's say that i = 1 for example

This will return yesterday's open price

These 2 will return values for the last closed 1 minute bar.


So you are comparing the values from 1 minute ago to yesterday's open.

If i = 2, you will be comparing values from 2 minutes ago to the open 2 days ago

You must see that this cannot be what you want.


Hi GumRai,

Thanks for taking time helping me. You are right, I am just a beginner and eagerly trying hard to learn coding EA. Here is what I am trying to do:

1. Compare PERIOD_D1 Open to every minute PERIOD_M1 bar on it's own D1 bar, so there are 1440 M1 bars to compare with the D1 bar. Then count the M1 bars that satisfy the condition if (O > L && O < H).

2. Store the counted bars in an array then proceed again to the next D1 bar and repeat the cycle again.

3. Counted bars will be plotted by the indicator on a daily chart. So if say the M1 bars that satisfy the statements is 5 this 5 will be plotted as the value of the daily Bar1. Then say for the next daily bar the M1 bar counted is 10 then it will be plotted again on Bar1 as the previous bar moved to Bar2.

Kindly show me how best to code this seemed simple statement that i struggle with. Let me know is you need further explanation. and big thanks.

 
Zaldy:


Hi GumRai,

Thanks for taking time helping me. You are right, I am just a beginner and eagerly trying hard to learn coding EA. Here is what I am trying to do:

1. Compare PERIOD_D1 Open to every minute PERIOD_M1 bar on it's own D1 bar, so there are 1440 M1 bars to compare with the D1 bar. Then count the M1 bars that satisfy the condition if (O > L && O < H).

2. Store the counted bars in an array then proceed again to the next D1 bar and repeat the cycle again.

3. Counted bars will be plotted by the indicator on a daily chart. So if say the M1 bars that satisfy the statements is 5 this 5 will be plotted as the value of the daily Bar1. Then say for the next daily bar the M1 bar counted is 10 then it will be plotted again on Bar1 as the previous bar moved to Bar2.

Kindly show me how best to code this seemed simple statement that i struggle with. Let me know is you need further explanation. and big thanks.


I will be happy to help you, but I will not write the code as this would not help you to learm.

First, you have to realise that 1440 M1 bars will not always correspond to a 1 day period. If there are no ticks during a bar, that bar will be missing from history and there may be less than you expect.

So you need the datetime value for the open of the D1 candle. Can you do this? Try it and post your code

 

not so fast

i do know what u r trying to do & ur code (if (O > L && O < H)) doesn't gonna get u what u want

look @ the chart attached



the arrow facing up is the todays opening

& the arrow facing right is the first bar matching to your code (if (O > L && O < H)) so count = 1

but if you pay attention the bar closed above the today opening so, actually count supposed to be = 2 (at least (might be a few more crosses that you can't c only in tick chart))

but in ur code count = only 1

 
qjol:

not so fast

i do know what u r trying to do & ur code (if (O > L && O < H)) doesn't gonna get u what u want

look @ the chart attached



the arrow facing up is the todays opening

& the arrow facing right is the first bar matching to your code (if (O > L && O < H)) so count = 1

but if you pay attention the bar closed above the today opening so, actually count supposed to be = 2 (at least (might be a few more crosses that you can't c only in tick chart))

but in ur code count = only 1


From how I understand it, he wants to count the bars that straddle the opening price - I may be wrong though :)
 
  1. Zaldy: PLEASE HELP ANYONE! IS THIS POSSIBLE AT ALL?
    Don't SHOUT at us. Off course it's possible.
  2. Integers and booleans are convertible. False == 0, true = non-zero.
    return(true); else return(EMPTY_VALUE);
    return(true); else return(2147483647);
    return(true); else return(true);
  3. You are also dealing with three timeframes, D1, chart, M1. You must convert.
    Not compiled or tested.
       Counted_bars=IndicatorCounted(); // Number of counted bars
       for(iCht = Bars - 1 - Counted_bars; iCht >= 0; iCht--){ // Chart bars
          int      iD1    = iBarShift(NULL, PERIOD_D1, Time[iCht];
          double   openD1 = iOpen(NULL, PERIOD_D1, iD1);
          int      iM1Beg = iBarShift(NULL, PERIOD_M1, Time[iCht],
                   iM1End = -1;
          if(iCht > 0) iM1End = iBarShift(NULL, PERIOD_M1, Time[iCht-1];
          for(Buf_0[i] = 0; iM1Beg > iM1End; iM1Beg--){
             double hM1 = iHigh(NULL, PERIOD_M1, iM1Beg),
                    lM1 =  iLow(NULL, PERIOD_M1, iM1Beg);
    // count Bars of M1 Period that crisscross Open price of D1 Period 
            if( hM1 >= openD1 && openD1 >= lM1) Buf_0[iCht]++; 
          }
       }
    
    Not compiled or tested.
 
qjol:

not so fast

i do know what u r trying to do & ur code (if (O > L && O < H)) doesn't gonna get u what u want

look @ the chart attached



the arrow facing up is the todays opening

& the arrow facing right is the first bar matching to your code (if (O > L && O < H)) so count = 1

but if you pay attention the bar closed above the today opening so, actually count supposed to be = 2 (at least (might be a few more crosses that you can't c only in tick chart))

but in ur code count = only 1


Hi Qjol, Yes the arrow facing right is the first count that I want. Those that criss-cross the D1 Open only is counted. Thanks again for your efforts.
 
WHRoeder:
  1. Zaldy: PLEASE HELP ANYONE! IS THIS POSSIBLE AT ALL?
    Don't SHOUT at us. Off course it's possible.
  2. Integers and booleans are convertible. False == 0, true = non-zero.
  3. You are also dealing with three timeframes, D1, chart, M1. You must convert.
    Not compiled or tested.
    Not compiled or tested.

Thanks WHRoeder and sorry for using caps here! I will try your suggestions and come back here for the result.
Reason: