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

 

Made small fixes, now everything is correct

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

double MaBuffer_0[];
double MaBuffer_1[];
double MaBuffer_2[];
double custom_close[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
   
   int Len=100;      //array size
   int MAPeriod=12;  //ma period
   
   
   ArrayResize(MaBuffer_0,Len);  //iMA
   ArrayResize(MaBuffer_1,Len);  //ExponentialMA
   ArrayResize(MaBuffer_2,Len);  //iMAOnArray
   ArrayResize(custom_close,Len);
   
   ZeroMemory(MaBuffer_0);
   ZeroMemory(MaBuffer_1);
   ZeroMemory(MaBuffer_2);
   
   CopyClose(_Symbol,_Period,0,Len,custom_close);
   
   
   
   
   //iMA
   for(int i=Len-1; i>=0; i--)
      MaBuffer_0[i]=iMA(_Symbol,_Period,MAPeriod,0,MODE_EMA,PRICE_CLOSE,i);
   
   
   
   
   //ExponentialMA
   MaBuffer_1[0]=custom_close[0]; //first value
   for(int i=0+1; i<Len; i++)
      MaBuffer_1[i]=ExponentialMA(i,MAPeriod,MaBuffer_1[i-1],custom_close);
   ArraySetAsSeries(MaBuffer_1,true);
   
   
   //iMAOnArray
   for(int i=0; i<Len; i++)
      MaBuffer_2[i]=iMAOnArray(custom_close,0,MAPeriod,0,MODE_EMA,i);
   
   
   
   
   
   
   //Print
   for(int i=Len-1; i>=0; i--)
     PrintFormat ("i=%d; iMA=%f;  ExponentialMA=%f; iMAOnArray=%f;", i,MaBuffer_0[i],MaBuffer_1[i],MaBuffer_2[i] );
   


//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  }
//+------------------------------------------------------------------+
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:

Made small fixes, now everything is correct

Awesome!! Can't thank you enough.

 
//+------------------------------------------------------------------+
//|                                                whereitwillbe.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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);
  }
//+------------------------------------------------------------------+

double MaBuffer_0[];
double MaBuffer_1[];
double MaBuffer_2[];
double custom_close[];
int Len;
int MAPeriod;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   Len=100;      //array size
   MAPeriod=12;  //ma period

   ArrayResize(MaBuffer_0,Len);  //iMA
   ArrayResize(MaBuffer_1,Len);  //ExponentialMA
   ArrayResize(MaBuffer_2,Len);  //iMAOnArray
   ArrayResize(custom_close,Len);

   ZeroMemory(MaBuffer_0);
   ZeroMemory(MaBuffer_1);
   ZeroMemory(MaBuffer_2);
   ZeroMemory(custom_close);

   CopyClose(_Symbol,_Period,0,Len,custom_close);
//ArraySetAsSeries(custom_close,true);

//iMA
   for(int i=Len-1; i>=0; i--)
      MaBuffer_0[i]=iMA(_Symbol,_Period,MAPeriod,0,MODE_EMA,PRICE_CLOSE,i);

//ExponentialMA

   for(int i=0+1; i<Len; i++)
     {
      MaBuffer_1[0]=custom_close[0]+0*Point; //first value
      MaBuffer_1[i]=ExponentialMA(i,MAPeriod,MaBuffer_1[i-1],custom_close);
      ArraySetAsSeries(MaBuffer_1,true);
     }

//iMAOnArray
   for(int i=0; i<Len; i++)
      MaBuffer_2[i]=iMAOnArray(custom_close,0,MAPeriod,0,MODE_EMA,i);

   Print("MaBuffer_2[0] // same as ema = ",MaBuffer_2[0]);
   Print("MaBuffer_1[99] // same as ema = ",MaBuffer_1[99]); 
   Print("MaBuffer_0[0] // same as ema = ",MaBuffer_0[0]);
   Print("custom_close[99] // same as close = ",custom_close[99]);
   Print("Close[0] = ",Close[0]);
   Print("correct ema = ",iMA(_Symbol,0,12,0,MODE_EMA,PRICE_CLOSE,0));
   Print("+--------------------------------------------------------------------------------+");
  }
//+------------------------------------------------------------------+


Accurate to ninth decimal place in EURUSD, accurate enough. Would like it to be perfect, however thats what I got. Any suggestions to make it completely accurate are more than welcome.

Still need find a way to be able to input my custom value correctly so as to see in the future what ema will be with bid + 10*Point etc. So far altering the highlighted line does nothing more than altering the resulting MaBuffer_1[99] by extremely small amounts. Our result is that if you change the start close in the highlighted line by 10*Point we only change the  MaBuffer_1[99] by a super small amount; at the 12th decimal super small change. Real changes of 10 Points in the market alter the 12 EMA a lot more than that.

What's going on? Seems like something with the reversing of the arrays as series or something like that.
 

check it

//+------------------------------------------------------------------+
//|                                                whereitwillbe.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
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);
  }
//+------------------------------------------------------------------+

double MaBuffer_0[];
double MaBuffer_1[];
double MaBuffer_2[];
double custom_close[];
int Len;
int MAPeriod;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Len=200;      //array size
   MAPeriod=12;  //ma period

   ArrayResize(MaBuffer_0,Len);  //iMA
   ArrayResize(MaBuffer_1,Len);  //ExponentialMA
   ArrayResize(MaBuffer_2,Len);  //iMAOnArray
   ArrayResize(custom_close,Len);

   ZeroMemory(MaBuffer_0);
   ZeroMemory(MaBuffer_1);
   ZeroMemory(MaBuffer_2);
   ZeroMemory(custom_close);
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   
   CopyClose(_Symbol,_Period,0,Len,custom_close);

//iMA
   for(int i=Len-1; i>=0; i--)
      MaBuffer_0[i]=iMA(_Symbol,_Period,MAPeriod,0,MODE_EMA,PRICE_CLOSE,i);

//ExponentialMA
   MaBuffer_1[0]=custom_close[0]; //first value
   for(int i=0+1; i<Len; i++)
     {
      MaBuffer_1[i]=ExponentialMA(i,MAPeriod,MaBuffer_1[i-1],custom_close);
     }

//iMAOnArray
   for(int i=0; i<Len; i++)
      MaBuffer_2[i]=iMAOnArray(custom_close,0,MAPeriod,0,MODE_EMA,i);



   
   int digits=10;
   string   comment= "Buff iMAOnArray = "+DoubleToStr(MaBuffer_2[0],digits)+"\n"+
                     "Buff ExponentialMA = "+DoubleToStr(MaBuffer_1[Len-1],digits)+"\n"+
                     "Buff iMA = "+DoubleToStr(MaBuffer_0[0],digits)+"\n"+
                     "custom_close[Len-1] = "+DoubleToStr(custom_close[Len-1],digits)+"\n"+
                     "Close[0] = "+DoubleToStr(Close[0],digits)+"\n"+
                     "correct ema = "+DoubleToStr(iMA(_Symbol,0,MAPeriod,0,MODE_EMA,PRICE_CLOSE,0),digits)
                     ;
   
   Comment(comment);
  
  }
//+------------------------------------------------------------------+
 

Please do not double post.

I have removed your other topic.

 
Taras Slobodyanik:

check it

WOW!!!!! Awesome!!!!!!! This is too good!!