Inconsistent indicator

 

I wrote a custom indicator. It plots the history corectly but it is inconistent in real time. If i recompile it displays corectly until a few new ticks come in then it goes wrong. I looked at a lot of similar code and everyhing seems corect but it is still incosistent in real time. Any ideas on how to fix this will be apreciated. Here is the code:


#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Blue



extern int       Periods=5;
//---- buffers


double U_ShadowBuffer[];
double L_ShadowBuffer[];
double BodyBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,U_ShadowBuffer);
   SetIndexLabel(0, "Upper Shadow");
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,L_ShadowBuffer);
   SetIndexLabel(1, "Lower Shadow");
  
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,BodyBuffer);
   SetIndexLabel(2, "Body");


 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   int i, limit=Bars-counted_bars-1;
   
   
  for(i=limit; i>=0; i--)
   {
      U_ShadowBuffer[i]=High[i]-MathMax(Close[i],Open[i]);            
      L_ShadowBuffer[i]=MathMin(Close[i],Open[i])-Low[i];
      BodyBuffer[i]=MathMax(Close[i],Open[i])-MathMin(Close[i],Open[i]);
   }
  
   for(i=limit; i>=0; i--)
   {
      U_ShadowBuffer[i]=iMAOnArray(U_ShadowBuffer,0,Periods,0,MODE_SMA,i);
      L_ShadowBuffer[i]=iMAOnArray(L_ShadowBuffer,0,Periods,0,MODE_SMA,i);
      BodyBuffer[i]=iMAOnArray(BodyBuffer,0,Periods,0,MODE_SMA,i); 
   }

   return(0);
  }
//+------------------------------------------------------------------+
 

You are modifying the buffer that you use for input...

Use separate buffers for intermediate calculation and the final calculation for display.

USB[i] = =High[i]-MathMax(Close[i],Open[i]); // new buffer

and later create the display:

U_ShadowBuffer[i]=iMAOnArray(USB[i],0,Periods,0,MODE_SMA,i);

 
phy:

You are modifying the buffer that you use for input...

Use separate buffers for intermediate calculation and the final calculation for display.

USB[i] = =High[i]-MathMax(Close[i],Open[i]); // new buffer

and later create the display:

U_ShadowBuffer[i]=iMAOnArray(USB[i],0,Periods,0,MODE_SMA,i);

If that was the case then this code should work, but it provides the same inconsistent results:


#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Blue



extern int       Periods=5;
//---- buffers


double U_ShadowBuffer[];
double L_ShadowBuffer[];
double BodyBuffer[];

double U_Shadow[];
double L_Shadow[];
double Body[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,U_ShadowBuffer);
   SetIndexLabel(0, "Upper Shadow");
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,L_ShadowBuffer);
   SetIndexLabel(1, "Lower Shadow");
  
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,BodyBuffer);
   SetIndexLabel(2, "Body");

   ArraySetAsSeries(U_Shadow, true);
   ArraySetAsSeries(L_Shadow, true);
   ArraySetAsSeries(Body, true);

 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  ArrayResize(U_Shadow, Bars);
   ArrayResize(L_Shadow, Bars);
    ArrayResize(Body, Bars);
  
   
   int    counted_bars=IndicatorCounted();
   int i,    limit=Bars-counted_bars-1;
   
   
  for( i=0; i<limit; i++)
   {
   U_Shadow[i]=High[i]-MathMax(Close[i],Open[i]);            
   L_Shadow[i]=MathMin(Close[i],Open[i])-Low[i];
   Body[i]=MathMax(Close[i],Open[i])-MathMin(Close[i],Open[i]);
  }
  
 for(i=limit; i>=0; i--)
 {
 
 U_ShadowBuffer[i]=iMAOnArray(U_Shadow,0,Periods,0,MODE_SMA,i);
 L_ShadowBuffer[i]=iMAOnArray(L_Shadow,0,Periods,0,MODE_SMA,i);
 BodyBuffer[i]=iMAOnArray(Body,0,Periods,0,MODE_SMA,i); 
 
 }
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

I don't see a problem...

Try period = 1, it looks correct.

Maybe you are being confused by the effect of the MA.

Define "inconsistent"

.

.

I changed it a little, but it is the same...

.

   #property indicator_separate_window
   #property indicator_buffers 3
   #property indicator_color1 Lime
   #property indicator_color2 Red
   #property indicator_color3 PowderBlue
   
   
   
   extern int       Periods=1;
   //---- buffers
   
   
   double U_ShadowBuffer[];
   double L_ShadowBuffer[];
   double BodyBuffer[];
   
   double U_Shadow[];
   double L_Shadow[1];
   double Body[];
   //+------------------------------------------------------------------+
   //| Custom indicator initialization function                         |
   //+------------------------------------------------------------------+
   int init()
   {
      //---- indicators
      
      SetIndexStyle(0,DRAW_LINE);
      SetIndexBuffer(0,U_ShadowBuffer);
      SetIndexLabel(0, "Upper Shadow");
      
      SetIndexStyle(1,DRAW_LINE);
      SetIndexBuffer(1,L_ShadowBuffer);
      SetIndexLabel(1, "Lower Shadow");
      
      SetIndexStyle(2,DRAW_LINE);
      SetIndexBuffer(2,BodyBuffer);
      SetIndexLabel(2, "Body");
      
      ArraySetAsSeries(U_Shadow, true);
      ArraySetAsSeries(L_Shadow, true);
      ArraySetAsSeries(Body, true);
      
      
      
      //----
      return(0);
   }
   //+------------------------------------------------------------------+
   //| Custom indicator deinitialization function                       |
   //+------------------------------------------------------------------+
   int deinit()
   {
      //----
      
      //----
      return(0);
   }
   //+------------------------------------------------------------------+
   //| Custom indicator iteration function                              |
   //+------------------------------------------------------------------+
   int start()
   {
      ArrayResize(U_Shadow, Bars);
      ArrayResize(L_Shadow, Bars);
      ArrayResize(Body, Bars);
      
      
      int    counted_bars=IndicatorCounted();
      int i,    limit=Bars-counted_bars-1;
      
      //limit = Bars-100;
      Comment("Limit = ", limit);
      
      for( i=limit; i>=0; i--)
      {
         //U_Shadow[i]=High[i]-MathMax(Close[i],Open[i]);            
         //L_Shadow[i]=MathMin(Close[i],Open[i])-Low[i];
         //Body[i]=MathMax(Close[i],Open[i])-MathMin(Close[i],Open[i]);
         U_Shadow[i]=High[i]-MathMax(Close[i],Open[i]);            
         L_Shadow[i]=MathMin(Close[i],Open[i])-Low[i];
         Body[i]=MathAbs(Close[i]-Open[i]);
      }
      
      for(i=limit; i>=0; i--)
      {
         
         U_ShadowBuffer[i]=iMAOnArray(U_Shadow,0,Periods,0,MODE_SMA,i);
         L_ShadowBuffer[i]=iMAOnArray(L_Shadow,0,Periods,0,MODE_SMA,i);
         BodyBuffer[i]=iMAOnArray(Body,0,Periods,0,MODE_SMA,i); 
         
      }
      //----
      
      //----
      return(0);
   }
   //+------------------------------------------------------------------+
 
I know it looks corect with period 1 but that difeets the purpose of the indicator. By inconsistent i mean that if i atach the indicator to a 1 min chart and then a few bars later i atach the same exact indicator with the same settings they should show the same values, but they do not. Everithing seems corect to me in the code it just will not work. Can anyone help?
 
Ok.
 

I think this code works correctly. There is something going wrong with the arrays in the original.

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Red
#property indicator_color3 PowderBlue



extern int       Periods=5;
//---- buffers


double U_ShadowBuffer[];
double L_ShadowBuffer[];
double BodyBuffer[];

double U_Shadow[];
double L_Shadow[1];
double Body[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   
   IndicatorBuffers(6);
   
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,U_ShadowBuffer);
   SetIndexLabel(0, "Upper Shadow");
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,L_ShadowBuffer);
   SetIndexLabel(1, "Lower Shadow");
  
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,BodyBuffer);
   SetIndexLabel(2, "Body");

   //ArraySetAsSeries(U_Shadow, true);
   //ArraySetAsSeries(L_Shadow, true);
   //ArraySetAsSeries(Body, true);
   SetIndexBuffer(3,U_Shadow);
   SetIndexBuffer(4,L_Shadow);
   SetIndexBuffer(5,Body);


   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
  
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  //ArrayResize(U_Shadow, Bars);
  //ArrayResize(L_Shadow, Bars);
  //ArrayResize(Body, Bars);
  
   
   int i;
   int limit=MathMax(Bars-IndicatorCounted(), Periods+1);
   
   //limit = Bars-100;
   Comment("Limit = ", limit);
   
for( i=limit; i>=0; i--)
{
//U_Shadow[i]=High[i]-MathMax(Close[i],Open[i]);            
//L_Shadow[i]=MathMin(Close[i],Open[i])-Low[i];
//Body[i]=MathMax(Close[i],Open[i])-MathMin(Close[i],Open[i]);
U_Shadow[i]=High[i]-MathMax(Close[i],Open[i]);            
L_Shadow[i]=MathMin(Close[i],Open[i])-Low[i];
Body[i]=MathAbs(Close[i]-Open[i]);
}
  
for(i=limit; i>=0; i--)
{

U_ShadowBuffer[i]=iMAOnArray(U_Shadow,0,Periods,0,MODE_SMA,i);
L_ShadowBuffer[i]=iMAOnArray(L_Shadow,0,Periods,0,MODE_SMA,i);
BodyBuffer[i]=iMAOnArray(Body,0,Periods,0,MODE_SMA,i); 

}
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

It works. Im still buffled by why the othere verssion did not work corectly.

Thank you for your time and effort!!!

 

I don't know... have to play with arrays again.

Reason: