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

 
Lakshan Perera:

if you want to give custom values for EMA (for any MA for that matter) use iMAOnArray()

https://docs.mql4.com/indicators/imaonarray
//+------------------------------------------------------------------+
//|                                                 ima_on_array.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 custom_close[13];
double ima_on_array_data;
double default_ema;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

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

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   default_ema=iMA(_Symbol,PERIOD_M1,12,0,MODE_EMA,PRICE_CLOSE,0);

   custom_close[0]=Close[0];
   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];

//ima_on_array_data=iMAOnArray(Close,0,12,0,MODE_EMA,0);
   ima_on_array_data=iMAOnArray(custom_close,0,12,0,MODE_EMA,0);
   Print("ima on array adata = ",ima_on_array_data);
   Print("default_ema = ",default_ema);
  }
//+------------------------------------------------------------------+

Why does that code not work?

 
//+------------------------------------------------------------------+
//|                                                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[13];
int x;
double custom_close[13];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

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

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

   custom_close[0]=Close[0];
   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];

   x=0;
   MaBuffer[x]=ExponentialMA(x,12,MaBuffer[x-1],custom_close);        //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);
  }
//+------------------------------------------------------------------+

If anyone knows why this code does not work please let me know.. .

Bah just stumbled upon https://book.mql4.com/samples/icustom it looks like buffers are the issue, didn't really get that before posting. 

Missed that page. Thanks for helping me out hopefully ill sort this out soon

Creation of Custom Indicators - Simple Programs in MQL4 - MQL4 Tutorial
Creation of Custom Indicators - Simple Programs in MQL4 - MQL4 Tutorial
  • book.mql4.com
When creating a trading strategy a developer often faces the necessity to draw graphically in a security window a certain dependence calculated by a user (programmer). For this purpose MQL4 offers the possibility of creating custom indicators. Custom Indicator is an application program coded in MQL4; it is basically intended for graphical...
 
nadiawicket:

Why does that code not work?

Set the array as series.
 
Lakshan Perera:
Set the array as series.
//+------------------------------------------------------------------+
//|                                                          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 custom_close[];
double default_ema;
double ima_on_array_data;
//+------------------------------------------------------------------+
int init()
  {
   int draw_begin;
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//----
   SetIndexDrawBegin(0,draw_begin);
//----
   SetIndexBuffer(0,custom_close);
//----
   return(0);
  }
//+------------------------------------------------------------------+
int start()
  {

//
   ArraySetAsSeries(custom_close,true);

   custom_close[0]=Close[0];
   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];

   ima_on_array_data=iMAOnArray(custom_close,0,12,0,MODE_EMA,0); 
   default_ema=iMA(_Symbol,PERIOD_M1,12,0,MODE_EMA,PRICE_CLOSE,0);
   
   Print("ima on array adata = ",ima_on_array_data);
   Print("default_ema = ",default_ema);



//----
   
  }

still way off

 
nadiawicket:

still way off

did you try it on 1 min chart?, because you have set the normal iMA() to 1 min.. and the value of Close[] gives the close prices of the current time frame

 
Lakshan Perera:

did you try it on 1 min chart?, because you have set the normal iMA() to 1 min.. and the value of Close[] gives the close prices of the current time frame

1m frame , yes ..
 
nadiawicket:
1m frame , yes ..

what are the values you get?

 
Lakshan Perera:

what are the values you get?

heres a screen

imaonarray is name of indicator on my computer.. .same exact code as above. .

Files:
imaonarray.png  40 kb
 
//+------------------------------------------------------------------+
//|                                                          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 ema12;
int ExtCountedBars12=0;
double ema12_calculation;
double custom_close[];
double custom_open[];
double default_ema;
//+------------------------------------------------------------------+
int init()
  {
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));

   SetIndexBuffer(0,custom_close);
   SetIndexShift(0,MA_Shift12);


   SetIndexBuffer(1,custom_open);
      SetIndexShift(1,MA_Shift12);

  }
//+------------------------------------------------------------------+
int start()
  {

   default_ema=iMA(_Symbol,PERIOD_M1,12,0,MODE_EMA,PRICE_CLOSE,0);

   custom_close[0]=Close[0];
   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];
   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)
         custom_close[pos12+1]=(Open[pos12+1]+custom_close[pos12+1])/2;
      custom_close[pos12]=GetPrice12(pos12)*pr12+custom_close[pos12+1]*(1-pr12);
      pos12--;
      ema12_calculation=(custom_close[pos12]);
      ema12=NormalizeDouble(ema12_calculation,Digits+1);
      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)
         custom_close[pos12+1]=(Open[pos12+1]+custom_close[pos12+1])/2;
      custom_close[pos12]=GetPrice12(pos12)*pr12+custom_close[pos12+1]*(1-pr12);
      pos12--;
     }
  }
//+------------------------------------------------------------------+
double GetPrice12(int shift12)
  {
   double price12;
   price12=custom_close[shift12];
   return(price12);
  }
//+------------------------------------------------------------------+

Why oh why does this previous code not just work, its got the buffered array and everything. Looks like I need to assign a value for every bar counted or something like that. .


Following MQL source doesn't set as series at all and works however I need to change the first value to a custom one so I can see in the future:

//+------------------------------------------------------------------+
//|                                                          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[];
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);
    
   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("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]=(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);
  }
//+------------------------------------------------------------------+
So Im just running these 2 side by side trying to replicate the exact value of the normal one (second one posted),  still no luck. Looks like its the bars counted thing
 
nadiawicket:

heres a screen

imaonarray is name of indicator on my computer.. .same exact code as above. .

instead of copying the each Close value, try using this

ArrayCopy(custom_close,Close,0,0,WHOLE_ARRAY);

Or just use

CopyClose(Symbol(),PERIOD_M1,0,iBars(Symbol(),PERIOD_M1),custom_close);
Reason: