Another iMAOnArray problem

To add comments, please log in or register
sd59
636
sd59  

Hi Guys,

I am trying to extract MA values (with iMAOnArray) when I attach a 13 SMA to the OBV indicator by drag and drop using the First Indicator option:

My code is below and the param2 values are all zero. I have read many of the other posts about iMAOnArray and I think I have used it correctly here but would appreciate comments from anybody otherwise.

double param1[10], param2;

void OnStart()
  {
   //fill OBV array first
   for(int i=0;i<50;i++)
   {
    param1[i] = iOBV(NULL,0,PRICE_CLOSE,i);
    //Print(i,"   ",param1[i]);
   }
   ArraySetAsSeries(param1,True);
   for(i=0;i<50;i++)
   {
    param2 = iMAOnArray(param1,0,13,0,MODE_SMA,i);
    Print(i,"  ",param2);
   }
  }
William Roeder
20228
William Roeder  
You would know what the problem is, if you had used strict.

Always use strict. Fixing the warnings will save you hours of debugging.

sd59
636
sd59  
William Roeder:
You would know what the problem is, if you had used strict.

Always use strict. Fixing the warnings will save you hours of debugging.

I normally do, so thanks for the reminder.

I'm now getting values with the repaired code but regardless of whether I use ArraySetAsSeries or not the values appear in reverse order which is correct for standard use of iMAOnArray which calculates from left to right.

I understood that ArraySetAsSeries reverses the index order from right to left. Please can you suggest where I am going wrong. Thank you.

#property strict

double param1[50], param2;

void OnStart()
  {
   //fill OBV array first
   for(int i=0;i<50;i++)
   {
    param1[i] = iOBV(NULL,0,PRICE_CLOSE,i);
    //Print(i,"   ",param1[i]);
   }
   ArraySetAsSeries(param1,True);
   for(int i=0;i<50;i++)
   {
    param2 = iMAOnArray(param1,0,13,0,MODE_SMA,i);
    Print(i,"  ",DoubleToStr(param2,0));
   }
  }
Keith Watford
Moderator
20477
Keith Watford  
sd59:

I normally do, so thanks for the reminder.

I'm now getting values with the repaired code but regardless of whether I use ArraySetAsSeries or not the values appear in reverse order which is correct for standard use of iMAOnArray which calculates from left to right.

I understood that ArraySetAsSeries reverses the index order from right to left. Please can you suggest where I am going wrong. Thank you.

I don't believe that the documentation is very clear.

You should set the array as series before filling it.

void OnStart()
  {
   ArraySetAsSeries(param1,True);
   //fill OBV array first
   for(int i=0;i<50;i++)
   {
    param1[i] = iOBV(NULL,0,PRICE_CLOSE,i);
    //Print(i,"   ",param1[i]);
   }
   //ArraySetAsSeries(param1,True);
   for(int i=0;i<50;i++)
   {
    param2 = iMAOnArray(param1,0,13,0,MODE_SMA,i);
    Print(i,"  ",DoubleToStr(param2,0));
   }
//---
   
  }
sd59
636
sd59  
Keith Watford:

I don't believe that the documentation is very clear.

You should set the array as series before filling it.

Brilliant! Thank you.

sd59
636
sd59  

Just to continue this post on the same subject I'm having problems getting the correct values from iMAOnArray when I add (by drag and drop) a 50 SMA to a custom indicator I have.

I can confirm that the iCustom call does return the correct values of the indicator and I do get values returned by iMAOnArray except not matching the ones shown in the Data window.

I wanted to ask if my code is correct and any other suggestions please?

#property strict

double buy[], sell[], total[];
double average;

   
void OnStart()
{
 ArrayResize(buy,Bars-1,100);
 ArrayResize(sell,Bars-1,100);
 ArrayResize(total,Bars-1,100);
 ArraySetAsSeries(total,True);

 for(int i=0;i<Bars-1;i++)
 {
  buy[i] = iCustom(NULL,0,"SD VSA",0,i);
  sell[i] = iCustom(NULL,0,"SD VSA",1,i);
  total[i] = buy[i] + sell[i];
  Print(total[i]);
  }
  
 for(int i=0;i<Bars-1;i++){
 average = iMAOnArray(total,0,50,0,MODE_SMA,i);
 Print(average);}
 
}
William Roeder
20228
William Roeder  
Answered in #3 then you broke it.
sd59
636
sd59  
William Roeder:
Answered in #3 then you broke it.
Not very helpful - please don't respond unless you have a sensible contribution.
To add comments, please log in or register