How to give an indicator my own data? - page 2

 
Anthony Garot:

Well, EMA is just a formula that takes three inputs and spits out an output.

This macro can simplify your code, but in essence, it's the same formula you have above.

I use this formula all the time.

There is a library of MQL5 routines, one of which should also fit your needs.

https://www.mql5.com/en/code/77

But yes, if you want to create an indicator that does EMA on your terms, I would probably start with the free indicator from MetaQuotes and tweak it until it fits your needs.

What do I do with #define?
 
iMAOnArray(), iMA()

// P == Previous EMA value, i.e. EMA of previous day's price
// C == Current value (e.g. close price)
// L == Time period, e.g. a 10 day moving average
// Example: MaBuffer0[i]=EMA(MaBuffer0[i-1], dCurrValue, g_maPeriod);
#define EMA(P, C, L) ((P) + (2.0/((L)+1.0))*((C)-(P)))

 EMA(P, C, L) ((P) + (2.0/((L)+1.0))*((C)-(P)))

EMA()

So ima on array or ima both the same?

C is where I input the Custom Value so I can place Bid + 1 or Bid +2 or Bid +3 or bid +4 etc?


I generate 2 Arrays? Why go through all this, Im already at

   if(Bars <= MA_Period12) return(0);
   ExtCountedBars12=IndicatorCounted();
   if(ExtCountedBars12 < 0) return(-1);
   if(ExtCountedBars12>0) ExtCountedBars12--;
//----
   ema12_function();
   double pr12;
   pr12=2.0/(MA_Period12+1);
   int pos12=Bars-2;
   if(ExtCountedBars12>2) pos12=Bars-ExtCountedBars12-1;
//---- main calculation loop
   while(pos12>=0)
     {
      if(pos12==Bars-2)
         ExtMapBuffer12[pos12+1]=(Open[pos12+1]+Close[pos12+1])/2;
      ExtMapBuffer12[pos12]=Bid*pr12+ExtMapBuffer12[pos12+1]*(1-pr12);
      pos12--;
      ema12=ExtMapBuffer12[pos12];
      Print ("GetPrice12(pos12) = ",GetPrice12(pos12));
      Print("Bid = " , Bid);
      Print("ema12 = ",ema12);
      Print("default ema 12 = ",iMA(_Symbol,PERIOD_M1,12,0,MODE_EMA,PRICE_CLOSE,0));
      //   Print("default ema 26 = ", iMA(_Symbol,PERIOD_M1,26,0,MODE_EMA,PRICE_CLOSE,0));
      return(0);
     }
  }
//----
void ema12_function()
  {
   double pr12;
   pr12=2.0/(MA_Period12+1);
   int pos12=Bars-2;
   if(ExtCountedBars12>2) pos12=Bars-ExtCountedBars12-1;
//---- main calculation loop
   while(pos12>=0)
     {
      if(pos12==Bars-2)
         ExtMapBuffer12[pos12+1]=(Open[pos12+1]+Close[pos12+1])/2;
      ExtMapBuffer12[pos12]=GetPrice12(pos12)*pr12+ExtMapBuffer12[pos12+1]*(1-pr12);
      pos12--;
     }
  }
//+------------------------------------------------------------------+
double GetPrice12(int shift12)
  {
   double price12;
   price12=Close[shift12];
   return(price12);
  }
//+------------------------------------------------------------------+
 
Is there anywhere there where I can place a custom value at all? Straight into source looks perfect. No way to clean that up a bit and get it to work?
         ExtMapBuffer12[pos12+1]=(Open[pos12+1]+Close[pos12+1])/2;
      ExtMapBuffer12[pos12]=Bid*pr12+ExtMapBuffer12[pos12+1]*(1-pr12);
      pos12--;

Is perfect, Cant accept the Bid custom values to compute the thing.. How have it be able to be accurate with custom value instead of Bid? Build an array with the current Bid first?

Call  iMAOnArray instead of Bid? What if I got a custom value like Bid + 1?
 

See the file \MQL4\Include\MovingAverages.mqh

//+------------------------------------------------------------------+
//| Exponential Moving Average                                       |
//+------------------------------------------------------------------+
double ExponentialMA(const int position,const int period,const double prev_value,const double &price[])
  {
//---
   double result=0.0;
//--- calculate value
   if(period>0)
     {
      double pr=2.0/(period+1.0);
      result=price[position]*pr+prev_value*(1-pr);
     }
//---
   return(result);
  }
 
Taras Slobodyanik:

See the file \MQL4\Include\MovingAverages.mqh

double ExponentialMA(const int position,const int period,const double prev_value,const double &price[])
  {
//---
   double result=0.0;
//--- calculate value
   if(period>0)
     {
      double pr=2.0/(period+1.0);
      result=price[position]*pr+prev_value*(1-pr);
     }
//---
   return(result);
  }

What do I place custom price data? For Price to EMA conversion?

Dont really know what to put in any of those.

MaBuffer0[i]=EMA(MaBuffer0[i-1], dCurrValue, g_maPeriod);

EMA(MaBuffer0[i-1] ////////// This is just the array with all of the previous closes for the past 12 periods? dCurrValue is where I put custom data of whatever experiment I want to do with Bid + X ? gMAPerdiod Ive no clue what about yet. 
 
Anthony Garot:

Well, EMA is just a formula that takes three inputs and spits out an output.

This macro can simplify your code, but in essence, it's the same formula you have above.

I use this formula all the time.

There is a library of MQL5 routines, one of which should also fit your needs.

https://www.mql5.com/en/code/77

But yes, if you want to create an indicator that does EMA on your terms, I would probably start with the free indicator from MetaQuotes and tweak it until it fits your needs.

Dont really know what to put in any of those.

MaBuffer0[i]=EMA(MaBuffer0[i-1], dCurrValue, g_maPeriod);

EMA(MaBuffer0[i-1] ////////// This is just the array with all of the previous closes for the past 12 periods? dCurrValue is where I put custom data of whatever experiment I want to do with Bid + X ? gMAPerdiod Ive no clue what about yet. 
 
nadiawicket:

What do I place custom price data? For Price to EMA conversion?

Dont really know what to put in any of those.


MaBuffer0[i]=ExponentialMA ( i, g_maPeriod, MaBuffer0[i-1], yourPriceBuff);        //MaBuffer0[i-1] (or [i+1]) = prev_value 


double ExponentialMA(const int position,const int period,const double prev_value,const double &price[])
  {
//---
   double result=0.0;
//--- calculate value
   if(period>0)
     {
      double pr=2.0/(period+1.0);
      result=price[position]*pr+prev_value*(1-pr);
     }
//---
   return(result);
  }
 
Taras Slobodyanik:
Couldn't touch MQL in the past 24 or so hours, I'm here again and wondering if I just need to generate an array with the altered bid price as if it was the first close of the array that it checks the previous one until i...Period ?

Well anyway I'm now more than happy to read about your method.This shouldn't be THAT impossible.

Anyway just to make clear I hope you and anyone reading understand that I want to put completely custom number in the first close so that I can anticipate in the future what the EMA's are going to be when price is current bud + 1*Digit or any other custom bid price for the first close.

Again thanks. I think I'm on the right path, its just a matter of generating an array that has the first value of the iClose "customized".

 
//+------------------------------------------------------------------+
//|                                                          EMA.mq4 |
//|                                         Copyright © 2010. |
//|                                                 b-market@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010."
#property link      "b-market@mail.ru"

#property indicator_chart_window
#property indicator_buffers 1

double MA_Period12=12;
int MA_Shift12 = 0;
int SetPrice12 = 0;
double ExtMapBuffer12[];
double ema12;
int ExtCountedBars12=0;
double ema12_calculation;
//+------------------------------------------------------------------+
int init()
  {
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   SetIndexShift(0,MA_Shift12);
   SetIndexBuffer(0,ExtMapBuffer12);
  }
//+------------------------------------------------------------------+
int start()
  {
   if(Bars <= MA_Period12) return(0);
   ExtCountedBars12=IndicatorCounted();
   if(ExtCountedBars12 < 0) return(-1);
   if(ExtCountedBars12>0) ExtCountedBars12--;
//----
   ema12_function();
   double pr12;
   pr12=2.0/(MA_Period12+1);
   int pos12=Bars-2;
   if(ExtCountedBars12>2) pos12=Bars-ExtCountedBars12-1;
//---- main calculation loop
   while(pos12>=0)
     {
      if(pos12==Bars-2)
         ExtMapBuffer12[pos12+1]=(Open[pos12+1]+Close[pos12+1])/2;
      ExtMapBuffer12[pos12]=GetPrice12(pos12)*pr12+ExtMapBuffer12[pos12+1]*(1-pr12);
      pos12--;
      ema12_calculation=(ExtMapBuffer12[pos12]);
      ema12=NormalizeDouble(ema12_calculation,Digits);
      Print("ema12 = ",ema12);
      return(0);
     }
  }
//----
void ema12_function()
  {
   double pr12;
   pr12=2.0/(MA_Period12+1);
   int pos12=Bars-2;
   if(ExtCountedBars12>2) pos12=Bars-ExtCountedBars12-1;
//---- main calculation loop
   while(pos12>=0)
     {
      if(pos12==Bars-2)
         ExtMapBuffer12[pos12+1]=(Open[pos12+1]+Close[pos12+1])/2;
      ExtMapBuffer12[pos12]=GetPrice12(pos12)*pr12+ExtMapBuffer12[pos12+1]*(1-pr12);
      pos12--;
     }
  }
//+------------------------------------------------------------------+
double GetPrice12(int shift12)
  {
   double price12;
   price12=Close[shift12];
   return(price12);
  }
//+------------------------------------------------------------------+

Looks like I just need to put my 'customized first close array' insted of using the standard "Close" array at

 price12=Close[shift12];
 
//+------------------------------------------------------------------+
//|                                                          EMA.mq4 |
//|                                         Copyright © 2010, LeMan. |
//|                                                 b-market@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, LeMAn."
#property link      "b-market@mail.ru"

#property indicator_chart_window
#property indicator_buffers 1

double MA_Period12=12;
int MA_Shift12 = 0;
int SetPrice12 = 0;
double ExtMapBuffer12[];
double ema12;
int ExtCountedBars12=0;
double ema12_calculation;
double custom_close[13];
double custom_open[13];
double default_ema;
//+------------------------------------------------------------------+
int init()
  {
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
   SetIndexShift(0,MA_Shift12);
   SetIndexBuffer(0,ExtMapBuffer12);
  }
//+------------------------------------------------------------------+
int start()
  {
  
  default_ema=iMA(_Symbol,PERIOD_M1,12,0,MODE_EMA,PRICE_CLOSE,0);
   
   custom_close[0]=Close[0]+0*Point;
   custom_close[1]=Close[1];
   custom_close[2]=Close[2];
   custom_close[3]=Close[3];
   custom_close[4]=Close[4];
   custom_close[5]=Close[5];
   custom_close[6]=Close[6];
   custom_close[7]=Close[7];
   custom_close[8]=Close[8];
   custom_close[9]=Close[9];
   custom_close[10]=Close[10];
   custom_close[11]=Close[11];
   custom_close[12]=Close[12];
   
   custom_open[0]=Open[0]+0*Point;
   custom_open[1]=Open[1];
   custom_open[2]=Open[2];
   custom_open[3]=Open[3];
   custom_open[4]=Open[4];
   custom_open[5]=Open[5];
   custom_open[6]=Open[6];
   custom_open[7]=Open[7];
   custom_open[8]=Open[8];
   custom_open[9]=Open[9];
   custom_open[10]=Open[10];
   custom_open[11]=Open[11];
   custom_open[12]=Open[12];
   
   
   
   if(Bars <= MA_Period12) return(0);
   ExtCountedBars12=IndicatorCounted();
   if(ExtCountedBars12 < 0) return(-1);
   if(ExtCountedBars12>0) ExtCountedBars12--;
//----
   ema12_function();
   double pr12;
   pr12=2.0/(MA_Period12+1);
   int pos12=Bars-2;
   if(ExtCountedBars12>2) pos12=Bars-ExtCountedBars12-1;
//---- main calculation loop
   while(pos12>=0)
     {
      if(pos12==Bars-2)
         ExtMapBuffer12[pos12+1]=(custom_open[pos12+1]+custom_close[pos12+1])/2;
      ExtMapBuffer12[pos12]=GetPrice12(pos12)*pr12+ExtMapBuffer12[pos12+1]*(1-pr12);
      pos12--;
      ema12_calculation=(ExtMapBuffer12[pos12]);
      ema12=NormalizeDouble(ema12_calculation,Digits+1);
      Print("!default_ema  = ", default_ema , "!!!!ema12 = ", ema12);
      return(0);
     }
  }
//----
void ema12_function()
  {
   double pr12;
   pr12=2.0/(MA_Period12+1);
   int pos12=Bars-2;
   if(ExtCountedBars12>2) pos12=Bars-ExtCountedBars12-1;
//---- main calculation loop
   while(pos12>=0)
     {
      if(pos12==Bars-2)
         ExtMapBuffer12[pos12+1]=(custom_open[pos12+1]+custom_close[pos12+1])/2;
      ExtMapBuffer12[pos12]=GetPrice12(pos12)*pr12+ExtMapBuffer12[pos12+1]*(1-pr12);
      pos12--;
     }
  }
//+------------------------------------------------------------------+
double GetPrice12(int shift12)
  {
   double price12;
   price12=custom_close[shift12];
   return(price12);
  }
//+------------------------------------------------------------------+
Still off a lot, any suggestions on this code?