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

 
Zaldy:


That code may give values, but it does not give correct values.

Please see my earlier posts

 
GumRai:


That code may give values, but it does not give correct values.

Please see my earlier posts

Hi Gumrai, I compiled your code plus Qjol magic code I attached here. Without the qjol code the shape of the plot is like a number 7 but adding qjol code it behave better. The value as you can see in the image dropped to -1. The plot has the same shape as the previous image here but different value. Please advise how to improve this. Thanks.
 
#property indicator_separate_window    // Indicator is drawn in the main window
#property indicator_buffers 1       // Number of buffers
#property indicator_color1 Red     // Color of the 1st line
//#property indicator_color2 Yellow      // Color of the 2nd line
 
//extern int Multiplier=2; 
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_SOLID,2);// Line style
//--------------------------------------------------------------------
   return(0);                          // Exit the special funct.init()
  }
//--------------------------------------------------------------------
int start()                         // Special function start()
  {
  
  
     int  i=0, iCht, Counted_bars; 
      
   
//--------------------------------------------------------------------
  Counted_bars=IndicatorCounted(); // Number of counted bars
   for(iCht = Bars - 1 - Counted_bars; iCht >= 0; iCht--){ // Chart bars
      int      iD1    = iBarShift(Symbol(), PERIOD_D1, Time[iCht]); 
    Buf_0[iCht] = EMPTY;  //  <<<<<<<<<<<< This is the magic code that gjol inserted and it worked.      
      //----find the datetime of iD1. ie the Time[]
      datetime daystarttime = iTime(Symbol(),PERIOD_D1,iD1);
      
      double   openD1 = iOpen(Symbol(), PERIOD_D1, iD1);
      
      //----find BarShift of daystarttime on current chart and take the nearest following bar if it doesn't exist
      int iM1Beg = iBarShift(Symbol(),PERIOD_M1,daystarttime,true);
      if(iM1Beg<0)
         iM1Beg = iBarShift(Symbol(),PERIOD_M1,daystarttime,false)+1;
      
      //----find BarShift for end of day on current chart and take the nearest following bar if it doesn't exist
      if(iD1>0)
         {
         datetime nextday = iTime(Symbol(),PERIOD_D1,iD1-1);
         int iM1End = iBarShift(Symbol(),PERIOD_M1,nextday-1,true);
         if(iM1End<0)
         
            iM1End = iBarShift(Symbol(),PERIOD_M1,nextday,false);
      
               
       
         for(Buf_0[i] = 0; iM1Beg > iM1End; iM1Beg--){
                double hM1 = iHigh(Symbol(), PERIOD_M1, iM1Beg),
                lM1 =  iLow(Symbol(), PERIOD_M1, iM1Beg);
// count Bars of M1 Period that crisscross Open price of D1 Period 
         if( hM1 >= openD1 && openD1 >= lM1) Buf_0[iCht]++;
         } 
         }
         }
//--------------------------------------------------------------------
   return(0);// 
  }
//--------------------------------------------------------------------
 
GumRai:


That code may give values, but it does not give correct values.

Please see my earlier posts


Hi GumRai,

Please advise how best we can improve this. Thanks.

 
Zaldy:


Hi GumRai,

Please advise how best we can improve this. Thanks.


Looks like you have already implemented my suggestions.

The indi line falls to zero because the buffer[0] is not calculated in the code. You have to decide whether you want this value to start at 0 and increase as the day goes on or maybe just make buffer[0] == buffer[1], which would make it look tidier.

 
Thank you WHRoeder, qjol and GumRai for the big help coding my indicator.
 
GumRai:


Looks like you have already implemented my suggestions.

The indi line falls to zero because the buffer[0] is not calculated in the code. You have to decide whether you want this value to start at 0 and increase as the day goes on or maybe just make buffer[0] == buffer[1], which would make it look tidier.


Hi GumRai, How can I code to start at 0 and increase as the day progress? Please help. Thanks.
 
Zaldy:

Hi GumRai, How can I code to start at 0 and increase as the day progress? Please help. Thanks.


I've made some changes to your code



int start()                         // Special function start()
  {
  
  
  int  i, iCht, Counted_bars, limit, iD1, iM1Beg, iM1End, counter ; 
  datetime daystarttime, nextday ;   
  double hM1, lM1, openD1 ; 
//--------------------------------------------------------------------
  Counted_bars=IndicatorCounted(); // Number of counted bars
  limit = Bars - 1 - Counted_bars;
  if(limit > Bars-100)   
      limit = Bars-100;
  for(iCht = limit; iCht >= 0; iCht--){ // Chart bars
      iD1 = iBarShift(Symbol(), PERIOD_D1, Time[iCht]); //---Barshift on daily chart
      daystarttime = iTime(Symbol(),PERIOD_D1,iD1); //--Find datetime value for start of the day      
      openD1 = iOpen(Symbol(), PERIOD_D1, iD1);
      
      //----find BarShift of daystarttime on current chart and take the nearest following bar if it doesn't exist
      iM1Beg = iBarShift(Symbol(),PERIOD_M1,daystarttime,true);
      if(iM1Beg<0)
         iM1Beg = iBarShift(Symbol(),PERIOD_M1,daystarttime,false)+1;
      
      //----find BarShift for end of day on current chart and take the nearest following bar if it doesn't exist
      if(iD1>0)  //-- Change to iD1>=0 to show running total for the current day
         {
         nextday = iTime(Symbol(),PERIOD_D1,iD1-1);
         iM1End = iBarShift(Symbol(),PERIOD_M1,nextday-1,true);
         if(iM1End<0)         
            iM1End = iBarShift(Symbol(),PERIOD_M1,nextday,false);
       
         counter = 0;
         for(i= iM1Beg; i >= iM1End; i--){
            hM1 = iHigh(Symbol(), PERIOD_M1, i);
            lM1 =  iLow(Symbol(), PERIOD_M1, i);
            // count Bars of M1 Period that crisscross Open price of D1 Period 
            if( hM1 >= openD1 && openD1 >= lM1)
               counter++ ;
         }
         Buf_0[iCht] = counter; 
         }
         }
//--------------------------------------------------------------------
   return(0);// 
  }
//--------------------------------------------------------------------
//+------------------------------------------------------------------+

This should do what you want

As it is it will not give a value for today

If you make the change as in the highlighted text, it should then include a value for today that will start at 0 and increase as the day progress

 

Why the plot is incomplete here. It is not filling before Jan 2014. Please advise the problem. This chart was done using WHRoeder code but even the one proposed by GumRai also gives the same discontinued plot.

 
because u dont have M1 data before that date