Help: Indicator not showing correctly on chart

 

Hi,

I've written an indicator to test out the trigger for my EA . But something is wrong with it as it seems that my indicator buffer are only stuck at 1 level.

Hope some kind souls could help to point out the problem in this Indicator.

//+------------------------------------------------------------------+
//|                                         trendrojak indicator.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Red
//--- buffers
int ExtMapBuffer1[];
int ExtMapBuffer2[];
double ExtMapBuffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtMapBuffer3);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit,   counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
for(int i=0; i<limit; i++){
ExtMapBuffer1[i]=mafilter ();
ExtMapBuffer2[i]=trendstrength ();
ExtMapBuffer3[i]=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,0);
}
//----
   
//----
   return(0);
  }

int mafilter () {

double ma5,ma10,ma30;
ma5= iMA (NULL,0,5,0,MODE_EMA,PRICE_MEDIAN,0);
ma10= iMA (NULL,0,10,0,MODE_EMA,PRICE_MEDIAN,0);
ma30= iMA (NULL,0,30,0,MODE_EMA,PRICE_MEDIAN,0);
   if (ma5 >ma30 && ma10>ma30) {
      return(1); //trend intact
      } //end if 
   else { 
      return (0);//trend change
      } //end else
} //end mafilter 

int trendstrength() {
// iADX( string symbol, int timeframe, int period, int applied_price, int mode, int shift) 

if (iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,0) > 25){
   if (iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,1) >iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,2)) {
   return (1); //trend picking up strength
   }//end if
}//endif
else {
return (0);
}//end else
}//end trendstrength

//+------------------------------------------------------------------+
 
You seem to be calculating values for ma5, ma10, ma30 and your iADX stuff always using bar 0 . . . even when processing the historical bars . . . it might work for the current bar but not for any bars to the left.
 
RaptorUK:
You seem to be calculating values for ma5, ma10, ma30 and your iADX stuff always using bar 0 . . . even when processing the historical bars . . . it might work for the current bar but not for any bars to the left.


Hi,

Yes I'm calculating whether ma 5, > ma 10 > ma30. if true, gives a 1, else return 0. I wanted the indicator to show me a one or zero . but it seems to be stucked at one.

Same case as with the iADX..shouldn't the chart be updated with every tick?

 
praetorian:


Hi,

Yes I'm calculating whether ma 5, > ma 10 > ma30. if true, gives a 1, else return 0. I wanted the indicator to show me a one or zero . but it seems to be stucked at one.

Same case as with the iADX..shouldn't the chart be updated with every tick?

You are always calculating for bar 0 . . .. what about all the other bars ? like, 1, 2, 3 etc ?
 

Should it look like this ?

 
RaptorUK:

Should it look like this ?


not exactly i've modified the code and what you're showing is extmapbuffer3 right?

I wrote that to test my code.. What i really want is for it to show the state of my mafilter and trend-strength

It should show either 1 or 0.. attached is modified code

//+------------------------------------------------------------------+
//|                                         trendrojak indicator.mq4 |
//|                      Copyright © 2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 Blue
#property indicator_color3 Red
//--- buffers
int ExtMapBuffer1[];
int ExtMapBuffer2[];
double ExtMapBuffer3[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,ExtMapBuffer3);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int limit,   counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
for(int i=0; i<limit; i++){
ExtMapBuffer1[i]=mafilter (i);
ExtMapBuffer2[i]=trendstrength (i);
//ExtMapBuffer3[i]=iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,i);
Print ("mafilter state is ",mafilter (i));
}
//----
   
//----
   return(0);
  }

int mafilter (int shiftma) {

double ma5,ma10,ma30;
ma5= iMA (NULL,0,5,0,MODE_EMA,PRICE_MEDIAN,shiftma);
ma10= iMA (NULL,0,10,0,MODE_EMA,PRICE_MEDIAN,shiftma);
ma30= iMA (NULL,0,30,0,MODE_EMA,PRICE_MEDIAN,shiftma);
//Print ("ma5 is " ,ma5); 
   if (ma5 >ma30 && ma10>ma30) {
      return(1); //trend intact
      } //end if 
   else { 
      return (0);//trend change
      } //end else
} //end mafilter 

int trendstrength(int shift) {
// iADX( string symbol, int timeframe, int period, int applied_price, int mode, int shift) 

if (iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,shift) > 25){
   if (iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,shift) >iADX(NULL,0,14,PRICE_CLOSE,MODE_MAIN,shift-1)) {
   return (1); //trend picking up strength
   }//end if
}//endif
else {
return (0);
}//end else
}//end trendstrength

//+------------------------------------------------------------------+
 
praetorian:


not exactly i've modified the code and what you're showing is extmapbuffer3 right?

I wrote that to test my code.. What i really want is for it to show the state of my mafilter and trend-strength

Yep, I figured that out, I suspect that the reason neither are shown is because neither are ever anything other than 0 . . . I've not tried to find out if that is true.
 

How about this . . .

 

Oh yes raptor!

Looks actually like what I'm trying to achieve. DOn't mind sharing with me how you did it?

 
praetorian:

Oh yes raptor!

Looks actually like what I'm trying to achieve. DOn't mind sharing with me how you did it?

Happy to share, it's your code after all ;-)

I made the buffers doubles not ints . . . and made the 2 functions return doubles also.

 
The official response can be seen in https://www.mql5.com/en/forum/111489
Reason: