Indicator Offset And Indexing Problems.

 

Hi,

I am trying to create an indicator which subtracts one MA from another MA, then plots an MA of the result and plots a single period rate of change of the result.

The two inital MAs are of equal length (let the length be X) and are separated by a window period (let this be W, please note that X isn't necessarily equal to W).

The length of the resultant MA is equal to R.  

I am new to coding but trying to learn. I can't seem to get the array indexing right, so that the two lines (the resultant MA and the ROC) are in phase .

 i.e.

 

MA_2=iMA(NULL,0,X,0,MODE_SMA,PRICE_CLOSE,(i+A));
MA_1=iMA(NULL,0,X,0,MODE_SMA,PRICE_CLOSE,(i+B));

Line_1[i]=iMA(NULL,0,R,0,MODE_SMA,(MA_2 - MA_1),(i+C));






ROC (ROC of 1 period required)

ROC_2=Line_1[i+D];
ROC_1=Line_1[i+E];



Line_2[i]=(ROC_2 - ROC_1)

If the window period between the two inital MAs = W

If the length of the two MAs = X

And the length of the resultant MA = R 

then what are the variables A,B,C,D,E?

 

Is this the best way to code this indicator? Should I instead let

Line_1[]=MA_2-MA_1

And then use iMAOnArray to get the resultant MA?

 

Thanks. 

 

Yes, you should collect the data first and use iMAOnArray()

   int i, indicator_counted;
   
   //--- subtracts one MA from another MA
   for (i = Bars - indicator_counted; i >= 0; i--)
      {
      MA_2 = iMA(NULL,0,X,0,MODE_SMA,PRICE_CLOSE,(i+A));
      MA_1 = iMA(NULL,0,X,0,MODE_SMA,PRICE_CLOSE,(i+B));
      Data [i] = MA_2 - MA_1;
      }
   
   //--- MA of the result of "subtracts one MA from another MA"
   for (i = Bars - indicator_counted; i >= 0; i--)
      {
      MA [i] = iMAOnArray (Data,0, R,0,MODE_SMA, i);
      }

   
   indicator_counted = IndicatorCounted() - 1;  
Reason: