Help needed with EMA indicator

 

Hi 

I am trying to code an indicator which returns 1 when EMA(period) is higher than the high of current bar and returns -1 when is less than tha current low

 I did some coding but its returning only ema value , not 1/-1

Can some body help please.

 

Thanks

neuron_sun 

 
neuron_sun:

Hi 

I am trying to code an indicator which returns 1 when EMA(period) is higher than the high of current bar and returns -1 when is less than tha current low

 I did some coding but its returning only ema value , not 1/-1

Can some body help please.

 

Thanks

neuron_sun 

 

Hi, can you publish this code portion to get the right point to correct?
 
neuron_sun:

Hi 

I am trying to code an indicator which returns 1 when EMA(period) is higher than the high of current bar and returns -1 when is less than tha current low

 I did some coding but its returning only ema value , not 1/-1

Can some body help please.

 

Thanks

neuron_sun 

 

Hi

Please post the code so we can help you. 

Regards

 
Helghast:

Hi

Please post the code so we can help you. 

Regards

 

Hi

This is my first code , so i don't know which value is taken by indicator_data 

Custom indicator name is ema_left 

basically I want an arrow when ema is greater than high and another arrow when its less than low 

 

input int      ema=3;
input ENUM_MA_METHOD meth=MODE_EMA;

double         buyBuffer[];
double         sellBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,buyBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,sellBuffer,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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[])
  {

//code to get ema value -- this I think is correct 

   ema_left=iMA(NULL,0,3,0,meth,PRICE_CLOSE);
 

   

// I need help with 

   if(ema_left>high[0])
   {

  // code to return buybuffer arrow  -- HELP needed

 

   return(1);
   }
   
   if(ema_left<low[0])
   {

   // code to return sell buffer arrow  HELP needed

 

   return(1);
   }

   

// Don't know which value to return 

   return(1);
  }

 

Thanks

neuron_sun 


 
neuron_sun:

 

Hi

This is my first code , so i don't know which value is taken by indicator_data 

Custom indicator name is ema_left 

basically I want an arrow when ema is greater than high and another arrow when its less than low 

 

input int      ema=3;
input ENUM_MA_METHOD meth=MODE_EMA;
double         buyBuffer[];
double         sellBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,buyBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,sellBuffer,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(0,PLOT_ARROW,159);
   PlotIndexSetInteger(1,PLOT_ARROW,159);
   
//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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[])
  {

//code to get ema value -- this I think is correct 

   ema_left=iMA(NULL,0,3,0,meth,PRICE_CLOSE);
 

   

// I need help with 

   if(ema_left>high[0])
   {

  // code to return buybuffer arrow  -- HELP needed

 

   return(1);
   }
   
   if(ema_left<low[0])
   {

   // code to return sell buffer arrow  HELP needed

 

   return(1);
   }

   

// Don't know which value to return 

   return(1);
  }

 

Thanks

neuron_sun 

Hi there!

To code this properly I give you some sugestions:

- declare an indicator handle below the buffers declaration, like this:

    int handle_ema_left;

- Initiate the handle in the OnInit() fuction, like this:

    handle_ema_left =  iMA(NULL,0,ema,0,meth,PRICE_CLOSE);

 - Because your using a discrete arrow output it's recommended to set an empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); 

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);  

- In the OnCalculate() fuction try this code and modify to your needs:

//--- check for rates total
      if(rates_total<ema+1)
      return(0); // not enough bars for calculation
//--- not all data may be calculated
   int calculated=BarsCalculated(handle_ema_left);
   if(calculated<rates_total)
     {
      Print("Not all data of ema_left is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
 
//--- we may not need all data

   int to_copy;
   double ema_left[];

   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }
//--- get ema_left buffer
   if(IsStopped()) return(0); //Checking for stop flag
   if(CopyBuffer(handle_ema_left,0,0,to_copy,ema_left)<=0)
     {
      Print("Getting EMA has failed! Error",GetLastError());
      return(0);
     }

//--- calculations
   int i,limit;
//--- first calculation or number of bars was changed
   if(prev_calculated<=ema+1)
     {
      for(i=0;i<ema+1;i++)
        {
         buyBuffer[i]=0.0;
         sellBuffer[i]=0.0;
        }
      limit=ema+1;
     }
   else limit=prev_calculated-1;
//--- main loop of calculations
   for(i=limit+1; i<rates_total && !IsStopped(); i++)
     {
      if(ema_left[i]>High[i] ) sellBuffer[i]=-1.0; //or any value you want beside 0.0

      else sellBuffer[i]=0.0;

       if(ema_left[i]<Low[i] ) buyBuffer[i]=1.0; //or any value you want beside 0.0

        else buyBuffer[i]=0.0;
     }
//--- return value of prev_calculated for next call
   return(rates_total);

Hope it helps!

Bye 

 
Helghast:

Hi there!

To code this properly I give you some sugestions:

- declare an indicator handle below the buffers declaration, like this:

    int handle_ema_left;

- Initiate the handle in the OnInit() fuction, like this:

    handle_ema_left =  iMA(NULL,0,ema,0,meth,PRICE_CLOSE);

 - Because your using a discrete arrow output it's recommended to set an empty value

   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); 

   PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0);  

- In the OnCalculate() fuction try this code and modify to your needs:

//--- check for rates total
      if(rates_total<ema+1)
      return(0); // not enough bars for calculation
//--- not all data may be calculated
   int calculated=BarsCalculated(handle_ema_left);
   if(calculated<rates_total)
     {
      Print("Not all data of ema_left is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
 
//--- we may not need all data

   int to_copy;
   double ema_left[];

   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }
//--- get ema_left buffer
   if(IsStopped()) return(0); //Checking for stop flag
   if(CopyBuffer(handle_ema_left,0,0,to_copy,ema_left)<=0)
     {
      Print("Getting EMA has failed! Error",GetLastError());
      return(0);
     }

//--- calculations
   int i,limit;
//--- first calculation or number of bars was changed
   if(prev_calculated<=ema+1)
     {
      for(i=0;i<ema+1;i++)
        {
         buyBuffer[i]=0.0;
         sellBuffer[i]=0.0;
        }
      limit=ema+1;
     }
   else limit=prev_calculated-1;
//--- main loop of calculations
   for(i=limit+1; i<rates_total && !IsStopped(); i++)
     {
      if(ema_left[i]>High[i] ) sellBuffer[i]=-1.0; //or any value you want beside 0.0

      else sellBuffer[i]=0.0;

       if(ema_left[i]<Low[i] ) buyBuffer[i]=1.0; //or any value you want beside 0.0

        else buyBuffer[i]=0.0;
     }
//--- return value of prev_calculated for next call
   return(rates_total);

Hope it helps!

Bye 

 Hi

It worked , thanks a ton!!!

neuron_sun 

 

 

Please use SRC button to post your code. It will make your codes much easier to read for other forumer.

 

Reason: