Help needed with the Custom Indocator

 

Hello again,

Once again I’ve come to ask for your help. I’ve spent several hours on this indicator and still can’t get it to work. It counts only two bars back and … and nothing. Could any of more experienced coders (that would probably mean most of the readers;) ) tell me what I’ve done wrong?? That is probably some small and stupid mistake, but I can’t figure it out.

//+------------------------------------------------------------------+
//|                                                      Maatrix.mq4 |
//|                                   Copyright © 2012, Lukasz Drozd |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2012, Lukasz Drozd"

//--- properties

#property indicator_separate_window
#property indicator_buffers 3

//--- external variables

extern int     MAPeriod=4;
extern int     MAMode=1;
extern int     MAAppPrice=1;
extern int     ATRPeriod=10;
extern double  ATRFactor=0.5;
extern int     MAATRRange=5;

//--- buffers

double MAATRMain[];
double MAATRUp[];
double MAATRDown[];
double MA[];
double MAHILO[];
double ATR[];
double MAATRFactor[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
   {
   string ShortName;
     
//--- indicator buffers properties
   IndicatorBuffers(7);
   SetIndexBuffer(0,MAATRMain);
   SetIndexStyle(0,DRAW_NONE);
   SetIndexBuffer(1,MAATRUp);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1,Green);
   SetIndexBuffer(2,MAATRDown);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1,Red);
   IndicatorDigits(Digits);
   
//--- 4 additional buufers used for calculations
   SetIndexBuffer(3,MA);
   SetIndexBuffer(4,MAHILO);
   SetIndexBuffer(5,ATR);
   SetIndexBuffer(6,MAATRFactor);
   
//--- name for DataWindow and indicator subwindow label
   ShortName="Maatrix ("+MAPeriod+","+ATRPeriod+")";
   IndicatorShortName(ShortName);
   SetIndexLabel(0,ShortName);
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);

   return(0);
   }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
   {
   int      counted_bars=IndicatorCounted();
   int      limit;
   double   current, prev;
   int      MAHighID, MALowID;
   double   MAHighValue, MALowValue;
   
//--- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
      limit=Bars-counted_bars;

//--- calculate internal buffers
   for(int i=limit-1; i>=0; i--)
      {
      MA[i]=iMA(NULL,0,MAPeriod,0,MAMode,MAAppPrice,i);
      ATR[i]=iATR(NULL,0,ATRPeriod,i);
      }
   for(i=limit-1; i>=0; i--)
      {
      MAHighID=ArrayMaximum(MA,MAATRRange,i);
      MAHighValue=MA[MAHighID];
      MALowID=ArrayMinimum(MA,MAATRRange,i);
      MALowValue=MA[MALowID];
      MAHILO[i]=MAHighValue-MALowValue;
      MAATRFactor[i]=ATR[i]*ATRFactor;
      }
   for(i=limit-1; i>=0; i--)
      {
      MAATRMain[i]=(MAHILO[i]/MAATRFactor[i]);
      bool up=false;
      current=MAATRMain[i];
      prev=MAATRMain[i+1];   
      if(current>prev) up=true;
      if(current<prev) up=false;
      if(!up)
         {
         MAATRDown[i]=current;
         MAATRUp[i]=0.0;
         }
      else
         {
         MAATRUp[i]=current;
         MAATRDown[i]=0.0;
         }
      }   
      
      
   return(0);
  }
//+------------------------------------------------------------------+

Once again thanx for all the help.

 
  1. From
    To
    bool up=false;
    current=MAATRMain[i];
    prev=MAATRMain[i+1];   
    if(current>prev) up=true;
    if(current<prev) up=false;
    if(!up)
     {
     MAATRDown[i]=current;
     MAATRUp[i]=0.0;
     }
    else
     {
     MAATRUp[i]=current;
     MAATRDown[i]=0.0;
     }
    
    current=MAATRMain[i];
    prev=MAATRMain[i+1];
    if(current>prev)
     {
     MAATRUp[i]=current+0.001;
     MAATRDown[i]=current;
     }
    else
     {
     MAATRDown[i]=current-0.001;
     MAATRUp[i]=current;
     }
    
    Will get you started.
  2. And if you change
    From
    To
       SetIndexLabel(1,NULL);
       SetIndexLabel(2,NULL);
    SetIndexLabel(1,"hu");
    SetIndexLabel(2,"hd");
    
    You can see the values in the data window (control-D)
 

Hi WHRoeder,

Thanks for your reply, but … ;)

1. 1. Of course I checked your code, but it changes nothing. Still the Indicator counts just two bars back and nothing more. Besides, I’m pretty sure this part of the code is correct. I have coded other indicator using the same logic and it runs smoothly. This part of the code is designed just to change the color of the histogram depending on the bar height compared to the previous. And so we get to point no.

2. 2. I see the Value of the indicator in data window, but only from MAATRMain Buffer. Buffers MAATRUp and MAATRDown and just for coloring histogram. MAATRUp has the same value as MAATRMain if its higher then previous (and the histogram bar is green), if its lower the value of Main Buffer is given to Down Buffer (and the histogram turns red).

Actually I’ve “borrowed” this solution from Awesome Oscillator ;)

Reason: