My indicator does not display any data in MT4

 

Hi,


I have written the code below for my custom indicator. When I insert it into MT4, a separate window is added as intended, but no line or anything appears.


The custom indicator is supposed to compare EMAs, and if the EMAs are trending up, then it adds 1 to a running total. If the EMAs are trending down, then it reduces the running total by 1.


The idea being that it would be used similarly to an RSI, but using 50 EMAs per bar, rather than only 2.


Would really appreciate it if you could advise me as to why this is not displaying in MetaTrader... it all compiles without error.


Many Thanks in advance,


David

//+------------------------------------------------------------------+
//|                                                         DRSI.mq4 |
//|                                                   David Farrell. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "David Farrell."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_minimum -50
#property indicator_maximum 50
#property indicator_buffers 1
#property indicator_color1 Red
//---- buffers
int totalbuffer[];
//+------------------------------------------------------------------+
//| Indicator drawing/buffer settings                        |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   SetIndexBuffer(0,totalbuffer);
   SetIndexDrawBegin(0,totalbuffer);
//----
   return(0);
   }
//+------------------------------------------------------------------+
// Indicator logic begins...
//+------------------------------------------------------------------+
int start()
{
int total,n,Counted_bars,limit,fast,medium,slow,repeat;
total=0;
double EMAFast,EMAMedium,EMASlow;
EMAFast=iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,0);
EMAMedium=iMA(NULL,0,medium,0,MODE_EMA,PRICE_CLOSE,0);
EMASlow=iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,0);

int counted_bars=IndicatorCounted(); 
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- main loop
for(int i=0; i<limit; i++)
     {
   while (repeat<50)
   {    
   if (EMAFast>EMAMedium && EMAMedium>EMASlow) //if uptrend, add 1 to our running total
   {
   total++;
   }
   else if (EMAFast>EMAMedium && EMAMedium>EMASlow) //if downtrend, take 1 away from our running total
   {
   total--;
   }
   repeat++; //increment count, and all EMA periods.
   fast++;
   medium++; 
   slow++;
   totalbuffer[limit]=total; //display total
   }
  }
   return;
}
  
//+------------------------------------------------------------------+


 
If you want to change your iMa, you have to use EMAFast, EMAMedium and EMASlow as functions,

otherwise they won't change, when you change fast, medium and slow.

Or you have to change it yourself after you change variables.



while (repeat<50)
{

EMAFast=iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,0);
EMAMedium=iMA(NULL,0,medium,0,MODE_EMA,PRICE_CLOSE,0);
EMASlow=iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,0);

if (EMAFast>EMAMedium && EMAMedium>EMASlow) //if uptrend, add 1 to our running total
{
total++;
}
else if (EMAFast>EMAMedium && EMAMedium>EMASlow) //if downtrend, take 1 away from our running total
{
total--;
}
repeat++; //increment count, and all EMA periods.
fast++;
medium++;
slow++;
totalbuffer[limit]=total; //display total
}

 
Roger:
If you want to change your iMa, you have to use EMAFast, EMAMedium and EMASlow as functions,

otherwise they won't change, when you change fast, medium and slow.

Or you have to change it yourself after you change variables.


Hi Roger, many thanks for the reply, I have coded an EMA function, and called it using different variables as inputs for speed (fast, medium, slow). 

However I still get nothing in the MetaTrader window, all compiles without error. Any thoughts on how I can get a visual output from this!?


Thanks again,


David

//+------------------------------------------------------------------+
//|                                                         DRSI.mq4 |
//|                                                   David Farrell. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "David Farrell."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_minimum -50
#property indicator_maximum 50
#property indicator_buffers 1
#property indicator_color1 Red
//---- buffers
int totalbuffer[];
//+------------------------------------------------------------------+
//| Indicator drawing/buffer settings                        |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   SetIndexBuffer(0,totalbuffer);
   SetIndexDrawBegin(0,totalbuffer);
//----
   return(0);
   }
   
//---- Declare iMA Functions----------------------------
int EMACalc(int speed)
{
double EMA=iMA(NULL,0,speed,0,MODE_EMA,PRICE_CLOSE,0);
return(EMA);
}

//+------------------------------------------------------------------+
// Indicator logic begins...
//+------------------------------------------------------------------+
int start()
{
int total,n,Counted_bars,limit,FAST,MEDIUM,SLOW,repeat;
total=0;
FAST=2;
MEDIUM=3;
SLOW=4;
double FastEMA,SlowEMA,MediumEMA;
int counted_bars=IndicatorCounted(); 
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- main loop
for(int i=0; i<limit; i++)
     {
   while (repeat<50)
   {
   FastEMA=EMACalc(FAST);              //Call EMA function and assign result as variable
   MediumEMA=EMACalc(MEDIUM);
   SlowEMA=EMACalc(SLOW);
   
   if (FastEMA>MediumEMA && MediumEMA>SlowEMA) //if uptrend, add 1 to our running total
   {
   total++;
   }
   else if (FastEMA<MediumEMA && MediumEMA<SlowEMA) //if downtrend, take 1 away from our running total
   {
   total--;
   }
   repeat++; //increment count, and all EMA periods.
   FAST++;
   MEDIUM++; 
   SLOW++;
   totalbuffer[limit]=total; //display total
   }
  }
   return;
}
  
//+------------------------------------------------------------------+

 

Unfortunately I don't know your logic. I put lots Prints in your code, you can use its for visualization you want see.

//------------------------------------------------------------------+
//|                                                         DRSI.mq4 |
//|                                                   David Farrell. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "David Farrell."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_minimum -50
#property indicator_maximum 50
#property indicator_buffers 1
#property indicator_color1 Red
//---- buffers
double totalbuffer[];
//+------------------------------------------------------------------+
//| Indicator drawing/buffer settings                        |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   SetIndexBuffer(0,totalbuffer);
   SetIndexDrawBegin(0,4);
//----
   return(0);
   }
   
//---- Declare iMA Functions----------------------------
double EMACalc(int speed)
{
double EMA=iMA(NULL,0,speed,0,MODE_EMA,PRICE_CLOSE,0);
return(EMA);
}

//+------------------------------------------------------------------+
// Indicator logic begins...
//+------------------------------------------------------------------+
int start()
{
int total,n,Counted_bars,limit,FAST,MEDIUM,SLOW,repeat;
total=0;
FAST=2;
MEDIUM=3;
SLOW=4;
double FastEMA,SlowEMA,MediumEMA;
int counted_bars=IndicatorCounted(); 
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   //Print("first limit - ",limit);
//---- main loop
for(int i=0; i<limit; i++)
     {
      total=0;
      FAST=2;
      MEDIUM=3;
      SLOW=4;
      repeat=0;
   while (repeat<5)
      {
      //Print("i - ",i);
      FastEMA=EMACalc(FAST);              //Call EMA function and assign result as variable
      MediumEMA=EMACalc(MEDIUM);
      SlowEMA=EMACalc(SLOW);
      //Print("FastEMA - ",FastEMA," MediumEMA - ",MediumEMA," SlowEMA - ",SlowEMA);
      if (FastEMA>MediumEMA && MediumEMA>SlowEMA) //if uptrend, add 1 to our running total
         {
         total++;
         }
      else if (FastEMA<MediumEMA && MediumEMA<SlowEMA) //if downtrend, take 1 away from our running total
         {
         total--;
         }
      //Print("total - ",total);
      repeat++; //increment count, and all EMA periods.
      FAST++;
      MEDIUM++; 
      SLOW++;
      totalbuffer[i]=total; //display total
      //Print("totalbuf - ",totalbuffer[i]," limit - ",i);
      }
  
  }
   return(0);
}
  
//+------------------------------------------------------------------+
 

Hi Roger,


Had a bit of a breakthrough thanks to your help; the indicator is now displaying data for the current Bar in MT4, however for all previous bars it only displays "0". It's as if it is 'forgetting' the calculation when it moves onto a new bar.


What I would like this indicator to do:

Calculate the strength of the trend by checking how many of the EMAs between periods 2 until 50 are monotonic (linear). For each EMA that is monotonic (eg EMA2period >EMA3period>EMA4period or EMA2period >EMA3period>EMA4period)  +/- 1 to a running total. 


The closer to 50 for an uptrend, and the closer to -50 for a downtrend, the stronger the trend is.


Would really appreciate any further feedback anyone has for this getting this sorted out! 


Thanks in advance,


David

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
double Buf_0[];

extern int History=300;

int init()
{
IndicatorBuffers(1);
SetIndexBuffer(0,Buf_0);
SetIndexDrawBegin(0,Buf_0);
SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);
SetIndexLabel (0,"High Line");
return;
}

double EMACalc(int speed) //EMA Value calculator
{
double EMA;
EMA=iMA(NULL,0,speed,0,MODE_EMA,PRICE_CLOSE,0);
return(EMA);
}

int start()
{
int i,r,fast,medium,slow,total,trendstrength,Counted_bars;       //declare variables for logic

Counted_bars=IndicatorCounted();
i=Bars-Counted_bars-1;

if (i>History-1)                 // If there are too many bars...
i=History-1;

   while(i>=0)                //For every bar...
   {
   fast=2;
   medium=3;
   slow=4;
   trendstrength=0;
   
      while(r<=50) //Check how many of the 50 EMAs are monotonic
      {
      double EMAFast, EMAMedium, EMASlow;
      EMAFast=EMACalc(fast);
      EMAMedium=EMACalc(medium);
      EMASlow=EMACalc(slow);
      
         if (EMAFast>EMAMedium && EMAMedium>EMASlow) //if true, then more power to uptrend
         {
         trendstrength++;
         }
         else if (EMAFast<EMAMedium && EMAMedium<EMASlow) //if true then more power to downtrend
         {
         trendstrength--;
         }
      fast++;              //increment the EMA speeds for next loop
      medium++;
      slow++;
      r++;                 //increment repeat counter
      }
  
   Buf_0[i]=trendstrength;
   i--;
   }
return;
}
 

May be you want this picture:

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Blue
double Buf_0[];

extern int History=300;

int init()
{
IndicatorBuffers(1);
SetIndexBuffer(0,Buf_0);
SetIndexDrawBegin(0,1000);
SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,2);
SetIndexLabel (0,"High Line");
return;
}
/*
double EMACalc(int speed) //EMA Value calculator
{
double EMA;
EMA=iMA(NULL,0,speed,0,MODE_EMA,PRICE_CLOSE,0);
return(EMA);
}
*/
int start()
{
int i,r,fast,medium,slow,total,trendstrength,Counted_bars;       //declare variables for logic

Counted_bars=IndicatorCounted();
i=Bars-Counted_bars-1;

if (i>History-1)                 // If there are too many bars...
i=History-1;

   while(i>=0)                //For every bar...
   {
   fast=2;
   medium=3;
   slow=4;
   trendstrength=0;
   r=0;
      while(r<=50) //Check how many of the 50 EMAs are monotonic
      {
      double EMAFast, EMAMedium, EMASlow;
      EMAFast=iMA(NULL,0,fast,0,MODE_EMA,PRICE_CLOSE,i);
      EMAMedium=iMA(NULL,0,medium,0,MODE_EMA,PRICE_CLOSE,i);
      EMASlow=iMA(NULL,0,slow,0,MODE_EMA,PRICE_CLOSE,i);
      
         if (EMAFast>EMAMedium && EMAMedium>EMASlow) //if true, then more power to uptrend
         {
         trendstrength++;
         }
         else if (EMAFast<EMAMedium && EMAMedium<EMASlow) //if true then more power to downtrend
         {
         trendstrength--;
         }
      fast++;              //increment the EMA speeds for next loop
      medium++;
      slow++;
      r++;                 //increment repeat counter
      }
  
   Buf_0[i]=trendstrength;
   //Print("i - ",i," buf - ",Buf_0[i]);
   i--;
   }
return;
}
 
HAHA - You rock!! Thanks Roger, that's awesome.
Reason: