Indicator buffers and shift - page 3

 

ok thanks, I'll be interested to know if you attach it to a chart does the Alerts give you that same 2147483647 value for lma (buffer 5) as I was getting.

 
Yes it does. See & use the amended part in previous attached code. It's because you initialized them with Buf_n[] values which at that moment has EMPTY_VALUE. All index buffers (the ones you assigned with SetIndexBuffer) are automatically resized and re-initialized with EMPTY_VALUE. This value is not mysterious let alone a bug! Search for it.
 

ok but have a look at this I just made another indicator to test and identify the problem there are two versions the first version does the same thing as I did previously in my indicator without the calculation for the buffers 6 and 7 then output three ways of getting the value.

first from iMA second from a double like I did in my indicator and third directly from the buffer, this works fine all three match like they should

then i added the calculation to create buffers 6 and 7 from the values of buffers 4 and 5, this replicated the error !! now the values directly from iMA are fine, the values directly from the buffer[0] are fine. BUT the values taken from the doubles created from the buffers are returning 2147483647

this must mean that to create a buffer value from another buffer value will cause some kind of error so you were right in your first diagnosis when you said you thought my code for calculating buffer 6 and 7 was the culprit here is the test indicator with the error causing code commented out:

run that as it is it works, include the code that is commented out and the alerts will return 214783647 intermittently but only on the alert from the doubles ma_0a etc

//+------------------------------------------------------------------+
//|                                                     tester 5.mq4 |
//|                                                        ©SDC 2010 |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "©SDC 2010"

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 Yellow
#property indicator_color4 DodgerBlue
#property indicator_color5 Magenta
#property indicator_color6 Magenta
#property indicator_color7 Magenta
#property indicator_color8 Magenta
#property indicator_style1 STYLE_DOT
#property indicator_style2 STYLE_DOT
#property indicator_style3 STYLE_DOT
#property indicator_style4 STYLE_DOT
#property indicator_style5 STYLE_SOLID
#property indicator_style6 STYLE_SOLID
#property indicator_style7 STYLE_DASHDOT
#property indicator_style8 STYLE_DASHDOT
double Buf_0[],Buf_1[],Buf_2[],Buf_3[],Buf_4[],Buf_5[],Buf_6[],Buf_7[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexBuffer(0,Buf_0);
   SetIndexBuffer(1,Buf_1);
   SetIndexBuffer(2,Buf_2);
   SetIndexBuffer(3,Buf_3);
   SetIndexBuffer(4,Buf_4);
   SetIndexBuffer(5,Buf_5);
   SetIndexBuffer(6,Buf_6);
   SetIndexBuffer(7,Buf_7);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted(),
          i=Bars-counted_bars-1;
   double ma_0= iMA(NULL,0,3,0,MODE_LWMA,PRICE_OPEN,1),  //regular way for ma values
          ma_1= iMA(NULL,0,3,0,MODE_LWMA,PRICE_CLOSE,1),
          ma_2= iMA(NULL,0,3,0,MODE_LWMA,PRICE_MEDIAN,1),
          ma_3= iMA(NULL,0,5,0,MODE_LWMA,PRICE_WEIGHTED,1),
          ma_4= iMA(NULL,0,3,0,MODE_LWMA,PRICE_HIGH,1),
          ma_5= iMA(NULL,0,3,0,MODE_LWMA,PRICE_LOW,1),
          
          ma_0a=Buf_0[0],   //doubles for ma values from the buffers
          ma_1a=Buf_1[0],
          ma_2a=Buf_2[0],
          ma_3a=Buf_3[0],
          ma_4a=Buf_4[0],
          ma_5a=Buf_5[0],
          ma_6a=Buf_6[0],
          ma_7a=Buf_7[0],
          adj=0;
          
          
//----ma's   
   while(i>=0)
    {Buf_0[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_OPEN,i+1);
     Buf_1[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_CLOSE,i+1);
     Buf_2[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_MEDIAN,i+1);
     Buf_3[i]=iMA(NULL,0,5,0,MODE_LWMA,PRICE_WEIGHTED,i+1);
     Buf_4[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_HIGH,i+1);
     Buf_5[i]=iMA(NULL,0,3,0,MODE_LWMA,PRICE_LOW,i+1);
     //adj=(Buf_4[i]-Buf_5[i])/3;
     //Buf_6[i]=Buf_4[i]-adj; //higher band
     //Buf_7[i]=Buf_5[i]+adj;
     i--;
    }
   
   Alert("ma_0= ",ma_0,"  ma_0a= ",ma_0a,"  Buf_0[0]= ",Buf_0[0]);
   Alert("ma_1= ",ma_1,"  ma_1a= ",ma_1a,"  Buf_1[0]= ",Buf_1[0]); 
   Alert("ma_2= ",ma_2,"  ma_2a= ",ma_2a,"  Buf_2[0]= ",Buf_2[0]); 
   Alert("ma_3= ",ma_3,"  ma_3a= ",ma_3a,"  Buf_3[0]= ",Buf_3[0]); 
   Alert("ma_4= ",ma_4,"  ma_4a= ",ma_4a,"  Buf_4[0]= ",Buf_4[0]); 
   Alert("ma_5= ",ma_5,"  ma_5a= ",ma_5a,"  Buf_5[0]= ",Buf_5[0]);
   //Alert("ma_6a= ",ma_6a,"  Buf_6[0]= ",Buf_6[0]);
   //Alert("ma_7a= ",ma_6a,"  Buf_7[0]= ",Buf_6[0]); 
    
    
//----
   return(0);
  }
//+--------------------------+
 

anyways thanks for all your help with this cameofx, I know now to be careful when initializing doubles to be used with indicator buffers and/or performing calculations on indicator buffer values

First thing I did this moring is I modifed my code as you prescribed in the earlier post and it works perfectly except for I didnt change this code:

     adj=(Buf_4[i]-Buf_5[i])/3;
     Buf_6[i]=Buf_4[i]-adj; //higher band
     Buf_7[i]=Buf_5[i]+adj;

to this

     adj=(Ma_H[i+1]-Ma_L[i+1])/3;                                                                          
     Ma_HB[i+1]=Ma_H[i+1]-adj; //higher band
     Ma_LB[i+1]=Ma_L[i+1]+adj; //lower band
     i--;
because i believe if i had done that, i would be calculating the current value of buffers 6 and 7 from bar [+1] of buffers 4 and 5 instead of the current one. The current values of buffers 4 and 5 are already shifted by 1 so calculated from the previous bar, so I want to use those current value for the values of 6 and 7
 
SDC:

anyways thanks for all your help with this cameofx, I know now to be careful when initializing doubles to be used with indicator buffers and/or performing calculations on indicator buffer values

First thing I did this moring is I modifed my code as you prescribed in the earlier post and it works perfectly except for I didnt change this code:

to this

because i believe if i had done that, i would be calculating the current value of buffers 6 and 7 from bar [+1] of buffers 4 and 5 instead of the current one. The current values of buffers 4 and 5 are already shifted by 1 so calculated from the previous bar, so I want to use those current value for the values of 6 and 7

Hi SDC,

I read this tread but I'm not exactly sure I understand what you are trying to do, but here is a suggestion to try.

Here is your main code section for the MA's:

 double ma_0= iMA(NULL,0,3,0,MODE_LWMA,PRICE_OPEN,1),  //regular way for ma values
          ma_1= iMA(NULL,0,3,0,MODE_LWMA,PRICE_CLOSE,1),
          ma_2= iMA(NULL,0,3,0,MODE_LWMA,PRICE_MEDIAN,1),
          ma_3= iMA(NULL,0,5,0,MODE_LWMA,PRICE_WEIGHTED,1),
          ma_4= iMA(NULL,0,3,0,MODE_LWMA,PRICE_HIGH,1),
          ma_5= iMA(NULL,0,3,0,MODE_LWMA,PRICE_LOW,1),

The MA code is set up to read the PAST bar - for example - (...PRICE_LOW,1)

Try changing the past bar to the CURRENT bar - (...PRICE_LOW,0)

This may place the arrows on the current bar?

Hope this helps,

Robert

 
cosmicbeing:

Hi SDC,

I read this tread but I'm not exactly sure I understand what you are trying to do, but here is a suggestion to try.

Here is your main code section for the MA's:

The MA code is set up to read the PAST bar - for example - (...PRICE_LOW,1)

Try changing the past bar to the CURRENT bar - (...PRICE_LOW,0)

This may place the arrows on the current bar?

Hope this helps,

Robert

I needed the MA (...PRICE_LOW,1) because i wanted the line to be created from the previous bar data so the line wouldnt reposition during formation of current bar

The problem did not turn out to be the shift, that was fine it turned out to be the varible which I had initialized with indicator line data that I was using as a reference for the position of the arrows was returning empty value, as cameofx pointed out, I had initialized variables with the drawing buffer data before the buffer was filled, like this:

var0=Buf_0[0]; var1=Buf_1[0] etc.

This caused an intermittent error when those variables were used as a reference point for the arrows and other things too, i was expecting those variables to contain the same data as buffer[0], but one of them ended up intermittently returning the data as EMPTY_VALUE which is a value of 2147483647

I changed the code to initialize the variables to nothing, then assign them to hold the buffer[0] data after the buffers were filled and the error ceased to occur. I still dont really understand why this error was intermittent though.

Reason: