Download MetaTrader 5
To add comments, please log in or register
Download MQL5 library codes via MetaTrader. It is convenient!
AED AED
242
AED AED 2015.11.21 17:59 

Hi,

I'm trying to calculate the ima of an indicator, below code has been used for this purpose and it was working fine a couple months ago but not anymore after build 890. I basically take the indicator value to a double array, create a copy of it as indix_temp to change sequence by ArraySetAsSeries() function and implement the imaonarray() function.

When I trace the code;

1-) it takes the values correctly to indix[] array.

2-) ArraySetasSeries does not work after the second bar loop. Insided of this I write a for loop to change the sequence of values. This works good but I wonder why ArraySetasSeries does not work anymore?

3-) imaonarray() function does not return the correct values.


Could anyone comment why imaonarray()  does not work???

Thanks.



 for(int j=0;j<49;j++) {   
      indix[j]=iCustom(_Symbol,0,FileName_myindi,0,j);  
      indix_temp[j]=indix[j];
   }
   

   //ArraySetAsSeries(indix_temp,True);  --> this does not work anymore, working before??? I put the below line to replace the arraysetasseries()function, it works good.
   for(j=0;j<49;j++) indix_temp[j]=indix[48-j];


   
   for(j=0;j<=49;j++) {    
      indix_ima[j]=iMAOnArray(indix_temp,0,10,0,MODE_LWMA,j);
      }
whroeder1
13627
whroeder1 2015.11.21 19:32  
set array as series, THEN fill the array, use ima. You filled the array as a series [i]=icustom(i) but then you set it as series=true which flipped it backwards from the data.
AED AED
242
AED AED 2015.11.21 20:47  
WHRoeder:
set array as series, THEN fill the array, use ima. You filled the array as a series [i]=icustom(i) but then you set it as series=true which flipped it backwards from the data.

I want to take the ima of an array, so I need to use imaonarray() i guess.

And its says in the documatation that the array must be reversed. Plese see below information at the bottom.

The issue is that the same code was working in the previous builds. If anything has changed since then, I need to know it and it must be documented. My expert is opening stupid orders :-)



--------------------

https://docs.mql4.com/indicators/imaonarray

Note

Unlike iMA(...), the iMAOnArray() function does not take data by symbol name, timeframe, the applied price. The price data must be previously prepared. The indicator is calculated from left to right. To access to the array elements as to a series array (i.e., from right to left), one has to use the ArraySetAsSeries() function.

AED AED
242
AED AED 2015.11.22 12:22  

I solved the problem!

Both ArraySetAsSeries() and iMAonArray() functions does only take the initial (the first loop) values of the array. when the bar closes and new bar created they somehow still read the same vales. Strage but this is what I vae traced.

I've put ArrayInitilize(indix_temp,0) and ArrayInitilize(indix_ima,0) before the code above, now both functions working properly as before.

My issue has been solved however I wonder what has been changed in the new builds that I need to initilize the arrays before using these functions???

Keith Watford
Moderator
7976
Keith Watford 2015.11.23 12:27  

I have done some testing and can confirm that there is certainly something odd here.

It doesn't help that even when an array is successfully set as series, the function ArrayIsSeries() seems to return false.

I will do more testing as and when I have time 

AED AED
242
AED AED 2015.12.18 19:31  

Hi GumRai,

Do you have any results that you can share with us?

Carl Schreiber
5954
Carl Schreiber 2015.12.18 20:48  

2015.09.05 I (b840) send a request to the service desk, that arrays of struct couldn't set as series and I got this answer:

There has been a major bug for the arrays that were resized with reserve.

Fixed, please wait for updates.

I  have sent them as well my script showing this problem (may be it is still not working?) here is the code just as is:

//+------------------------------------------------------------------+
//|                                             test_OrderArrray.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
struct __StructTest{
   int idx;
};
__StructTest myStruct[];
double Norm[];
void OnStart()
  {
//---
   string cmt0="Primary  ",cmt2="Add sz+1 ",cmt1="Series     ";
   int i,sz = ArrayResize(myStruct,5,1000);
   ArrayResize(Norm,5,1000);
   for (i=0;i<sz;i++) {
      myStruct[i].idx = i; Norm[i] = i;
      cmt0 = cmt0+"   "+(string)i+"] "+(string)myStruct[i].idx+" = "+DoubleToString(Norm[i],1);
   }
   Comment(cmt0,"\n",cmt1);
   ArraySetAsSeries(Norm,true);
   bool ok = ArraySetAsSeries(myStruct,true);
   for (i=0;i<sz;i++) {
      myStruct[i].idx = i;
      cmt1 = cmt1+"   "+(string)i+"] "+(string)myStruct[i].idx+" = "+DoubleToString(Norm[i],1);
   }
   cmt1 = cmt1 + "  set: "+(ok?"ok":"Err: "+(string)_LastError)+"  isSerial: "+(ArrayGetAsSeries(myStruct)?"yes":"no");

   ArrayResize(Norm,sz+1,1000);
   ArrayResize(myStruct,sz+1,1000);
   myStruct[sz].idx = sz; 
   Norm[sz] = sz;
   myStruct[0].idx = 9; Norm[0] = 9;
   for (i=0;i<=sz;i++) {
      cmt2 = cmt2+"   "+(string)i+"] "+(string)myStruct[i].idx+" = "+DoubleToString(Norm[i],1);
   }
   cmt2 = cmt2 + "  set: "+(ok?"ok":"Err: "+(string)_LastError)+"  isSerial: "+(ArrayGetAsSeries(myStruct)?"yes":"no");
   Comment(cmt0,"\n",cmt1,"\n",cmt2);
    
   DebugBreak();   
  }
//+------------------------------------------------------------------+
AED AED
242
AED AED 2015.12.21 11:23  

Thanks for the feedback.

Doerk Hilger
1073
Doerk Hilger 2015.12.24 14:18  
Probably same issue: The function SimpleSMAOnBuffer() from the standard library (indicators) returns sometimes "Array out of range". It also deals with ArraySetAsSeries. 
Keith Watford
Moderator
7976
Keith Watford 2016.02.08 09:36  

I lost track of this thread.

What was confusing me is that ArrayIsSeries() will only return true for the predefined arrays in OnCalculate.

For other arrays you must use ArrayGetAsSeries().

As WHRoeder said, it is no good setting an array as series after populating the array if it is populated from a series array. It must be set as series first.

With a non series array,say with 100 elements iMAONArray will calculate from right to left, irrespective of the index number. It will treat element index 99 as 0.

If you then set that array as series, element 0 is now moved to the far right and given the value of element 99. I find this hard to explain, but the indexing is reversed ,not the values. It means that reading the element values from right to left is exactly the same, whether the array is as series or not.

So if a non series array is populated with values and the set as series, iMAOnArray will return the same value when the array is set as series or not.

I hope that this make sense. 

/
To add comments, please log in or register