Indicator displaying relation between two EMA.

 

Hello 

I put 2 EMAs on the chart, EMA 50 and EMA 100. I am preparing indicator to indicate by value when EMA 50 is below EMA 100. 

I wrote the code attached below. It almost works but I noticed that it shows line only when corosover of EMAs is visible on the chart. When there isn't any crossover visible indicator line dissapear.

Do you have any idea how to solve this ?

//+------------------------------------------------------------------+
//|                                                           W4.mq4 |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"

#property indicator_separate_window
#property indicator_buffers 3

double W0[],e50[],e100[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,W0);
   SetIndexDrawBegin(0,1);
   SetIndexStyle(0,DRAW_LINE,EMPTY,EMPTY,clrYellowGreen);
   ArraySetAsSeries(W0,true);

   ArrayResize(e50,Bars);
   ArrayResize(e100,Bars);
   ArraySetAsSeries(e50,true);
   ArraySetAsSeries(e100,true);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
    for(int i=1; i<rates_total; i++)
     {
      e50[i]=iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,i);
      e100[i]=iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,i);
      if(e50[i]<e100[i])
         W0[i]=1.0;
      else
         W0[i]=0.0;   
      }
   
  }
//+------------------------------------------------------------------+
Files:
W4.mq4  3 kb
 
geskam:

Hello 

I put 2 EMAs on the chart, EMA 50 and EMA 100. I am preparing indicator to indicate by value when EMA 50 is below EMA 100. 

I wrote the code attached below. It almost works but I noticed that it shows line only when corosover of EMAs is visible on the chart. When there isn't any crossover visible indicator line dissapear.

Do you have any idea how to solve this ?

It looks like you want the indicator line to show when it is 0.0:

else
   W0[i]=0.0;

Most likely, 0.0 is an empty value for your indicator, which the terminal does not display. Try to explicitly set an empty value other than 0.0 in OnInit:

SetIndexEmptyValue(0, EMPTY_VALUE);
 
geskam:

Hello 

I put 2 EMAs on the chart, EMA 50 and EMA 100. I am preparing indicator to indicate by value when EMA 50 is below EMA 100. 

I wrote the code attached below. It almost works but I noticed that it shows line only when corosover of EMAs is visible on the chart. When there isn't any crossover visible indicator line dissapear.

Do you have any idea how to solve this ?

And where is #property strict? Why did you remove it?

(this is not relevant to your question)

 
geskam: Hello
  1. Why did you post your MT4 question in the MT5 Indicators section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

 

I added #property strict and now I get error "not all control paths return a value.

What next? 

//+------------------------------------------------------------------+
//|                                                           W4.mq4 |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 3

double W0[],e50[],e100[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0,W0);
   SetIndexDrawBegin(0,1);
   SetIndexStyle(0,DRAW_LINE,EMPTY,EMPTY,clrYellowGreen);
   ArraySetAsSeries(W0,true);

   ArrayResize(e50,Bars);
   ArrayResize(e100,Bars);
   ArraySetAsSeries(e50,true);
   ArraySetAsSeries(e100,true);

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
    for(int i=1; i<rates_total; i++)
     {
      e50[i]=iMA(NULL,0,50,0,MODE_EMA,PRICE_CLOSE,i);
      e100[i]=iMA(NULL,0,100,0,MODE_EMA,PRICE_CLOSE,i);
      if(e50[i]<e100[i])
         W0[i]=1.0;
      else
         W0[i]=0.0;  
      }
  }
 
geskam #:

I added #property strict and now I get error "not all control paths return a value.

What next? 

  1. Always use strict. Fixing the warnings will save you hours of debugging, but you must understand the differences.

  2. Fix your broken code.

    int OnCalculate(const int rates_total,
                    const int prev_calculated,
                    const datetime &time[],
                    const double &open[],
                    const double &high[],
                    const double &low[],
                    const double &close[],
                    const long &tick_volume[],
                    const long &volume[],
                    const int &spread[])
      {
        for(int i=1; i<rates_total; i++){ … }
        <<<<<<< What int are your returning?
      }
  3.    for(int i=1; i<rates_total; i++)
    You are recalculating all bars every tick. Your maximum lookback is 100 for the EMA(100)
              How to do your lookbacks correctly #9#14 & #19 (2016)

  4.    ArrayResize(e50,Bars);
       ArrayResize(e100,Bars);
    As soon as a new bar starts, your arrays are too small and your code crashes. You never refer to any other value so no need to for the arrays, just use two variables.
Reason: