imaonarray previous indicator's data iMA giving me wrong results

 

Hello


I'm new to mql4 coding so please assist me in understanding why the results from the iMA() are correct

yet the iMAOnArray() is giving me wrong results compared to the ones i have on the charts.


Please review my code and assist me in getting the results as expected.


Code attached.


extern int Base_Period         = 13;

extern int Signal_Period       = 3;



  double Base_Open1[]; 

  double Base_Close1[]; 

  double Signal_Open1[];  

  double Signal_Close1[];



int OnInit()

  {



   return(INIT_SUCCEEDED);

  }



void OnDeinit(const int reason)

  {



  }



void OnTick()

  {

 //-----------------------------------------------------------------------------------

  ArrayResize(Base_Open1,1000,100);

  ArrayResize(Base_Close1,1000,100);  

  ArrayResize(Signal_Open1,1000,100);

  ArrayResize(Signal_Close1,1000,100);

   

  ArrayInitialize(Base_Open1,0);

  ArrayInitialize(Base_Close1,0);  

  ArrayInitialize(Signal_Open1,0);

  ArrayInitialize(Signal_Close1,0);

   

  ArraySetAsSeries (Base_Open1,true);

  ArraySetAsSeries (Base_Close1,true);   

  ArraySetAsSeries (Signal_Open1,true);

  ArraySetAsSeries (Signal_Close1,true);

  

//-----------------------------------------------------------------------------------------------------------

 for(int i=0;i<3;i++)

 {

 Base_Open1[i]  = iMA(_Symbol,PERIOD_CURRENT,Base_Period,0,MODE_EMA,PRICE_OPEN,i);     // Base_Period is set at int=13

 Base_Close1[i] = iMA(_Symbol,PERIOD_CURRENT,Base_Period,0,MODE_EMA,PRICE_CLOSE,i);    // Base_Period is set at int=13

 Signal_Open1[i] = iMAOnArray(Base_Open1,0,Signal_Period,0,MODE_SMMA,i);               // Signal_Period is at int=3 looking for previous indicator's data

 Signal_Close1[i] = iMAOnArray(Base_Close1,0,Signal_Period,0,MODE_SMMA,i);             // Signal_Period is at int=3 looking for previous indicator's data

 

}



 

 double PID_MA_Open1  =  Signal_Open1[1];            //Previous indicator's data results Signal_Open1[1] bar 1

 double PID_MA_Open2  =  Signal_Open1[2];            //Previous indicator's data results Signal_Open1[2] bar 2

 double PID_MA_Close1 =  Signal_Close1[1];           //Previous indicator's data results Signal_Close1[1] bar 1

 double PID_MA_Close2 =  Signal_Close1[2];           //Previous indicator's data results Signal_Close1[2] bar 2



   Print("PID_MA_Open1    " +DoubleToString(PID_MA_Open1)); //the results are not the same as on the chart

   Print("PID_MA_Open2    " +DoubleToString(PID_MA_Open2)); //the results on the chart

   Print("PID_MA_Close1   " +DoubleToString(PID_MA_Close1)); //the results on the chart

   Print("PID_MA_Close2   " +DoubleToString(PID_MA_Close2)); //the results on the chart

   

   

   

   

/*   Results:

2020.09.27 10:56:14.743 PID MA #Bitcoin,M1: PID_MA_Close2   3537.47692345

2020.09.27 10:56:14.743 PID MA #Bitcoin,M1: PID_MA_Close1   3537.73260105

2020.09.27 10:56:14.743 PID MA #Bitcoin,M1: PID_MA_Open2    3537.00266591

2020.09.27 10:56:14.743 PID MA #Bitcoin,M1: PID_MA_Open1    3537.32609459

*/

//these results are wrong based on the visual results from the chart can  anyone please help me address this problem.   

  }

//+---------------------------------------------------------------------------------+

Visualize a Strategy in the MetaTrader 5 Tester
Visualize a Strategy in the MetaTrader 5 Tester
  • www.mql5.com
We all know the saying "Better to see once than hear a hundred times". You can read various books about Paris or Venice, but based on the mental images you wouldn't have the same feelings as on the evening walk in these fabulous cities. The advantage of visualization can easily be projected on any aspect of our lives, including work in the...
 
Please edit your post and
use the code button (Alt+S) when pasting code

I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
 
Keith Watford:
Please edit your post and
use the code button (Alt+S) when pasting code

I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
Noted and thank you.
 

You can't get iMAonArray unless the array that you are using it on has at least enough elements filled.

ie  iMAonArray [3] will only have iMA[3] to work with as  iMA[4]  and   iMA[5] are both zero.

You must fill the necessary elements of the iMA array first and THEN use  iMAonArray.

ie. 2 for loops.