Indicador para contar las barras del periodo M1 que se cruzan con el precio de apertura del periodo D1 - página 3

 
Zaldy:


Ese código puede dar valores, pero no da valores correctos.

Por favor, vea mis posts anteriores

 
GumRai:


Ese código puede dar valores, pero no da valores correctos.

Por favor, vea mis posts anteriores

Hola Gumrai, he compilado tu código más el código mágico de Qjol que adjunto aquí. Sin el código qjol la forma del gráfico es como un número 7 pero añadiendo el código qjol se comporta mejor. El valor como puedes ver en la imagen bajó a -1. El gráfico tiene la misma forma que la imagen anterior pero con un valor diferente. Por favor, aconsejen cómo mejorar esto. Gracias.
 
#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:


Ese código puede dar valores, pero no da valores correctos.

Por favor, vea mis mensajes anteriores


Hola GumRai,

Por favor, aconsejen la mejor manera de mejorar esto. Gracias.

 
Zaldy:


Hola GumRai,

Por favor, aconséjenos cómo podemos mejorar esto. Gracias.


Parece que ya has implementado mis sugerencias.

La línea indi cae a cero porque el buffer[0] no se calcula en el código. Tienes que decidir si quieres que este valor empiece en 0 y aumente a medida que avanza el día o quizás sólo hacer que buffer[0] == buffer[1], lo que lo haría parecer más ordenado.

 
Gracias a WHRoeder, qjol y GumRai por la gran ayuda en la codificación de mi indicador.
 
GumRai:


Parece que ya has implementado mis sugerencias.

La línea indi cae a cero porque el buffer[0] no se calcula en el código. Tienes que decidir si quieres que este valor comience en 0 y aumente a medida que avanza el día o tal vez sólo hacer que buffer[0] == buffer[1], lo que lo haría parecer más ordenado.


Hola GumRai, ¿Cómo puedo codificar para que empiece en 0 y aumente a medida que avanza el día? Por favor, ayuda. Gracias.
 
Zaldy:

Hola GumRai, ¿Cómo puedo codificar para que comience en 0 y aumente a medida que avanza el día? Por favor, ayúdame. Gracias.


He hecho algunos cambios en tu código



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);// 
  }
//--------------------------------------------------------------------
//+------------------------------------------------------------------+

Esto debería hacer lo que usted quiere

Tal y como está, no dará un valor para hoy

Si haces el cambio como en el texto resaltado, debería incluir un valor para hoy que empezará en 0 y aumentará a medida que el día avance

 

Por qué la trama está incompleta aquí. No se está llenando antes de enero de 2014. Por favor, aclare el problema. Este gráfico se hizo utilizando el código WHRoeder pero incluso el propuesto por GumRai también da la misma trama discontinua.

 
porque no tienes datos de M1 antes de esa fecha