moving average on another indicator question

 

Hello forum

After applying a moving average to another indicator in the separate chart window and seeing the value of this.

I now want to calculate the value of that moving average of the other indicator to use in an idea I am working on.

But I need help with a couple of newbie questions.

What is meant by previous indicators data as opposed to 1st indicators data?

If I wanted to calculate the average of whatever the indicator in the separate chart window, is that "applying the moving average to the 1st indicators data or previous?"

Secondly from the code base I understand that I would need to use iMAOnArray to calculate the moving average of the other indicator

iMAOnArray( double array[], int total, int period, int ma_shift, int ma_method, int shift)

I've never done this before and this question will likely betray my lack of understanding

But to calculate the double array[] part of iMAOnArray

Can I simply define a buffer as being equal to the values of the indicator that I want to average and insert that buffer into the iMAOnArray equation.

I suppose I am saying can I substitute double array[] with BufferIndicatorToBeAverage=iForce for example

As I say I have never tried this before, so finding it a bit confusing.

Any help appreciated

thank you



 
pullend:

Hello forum

After applying a moving average to another indicator in the separate chart window and seeing the value of this.

I now want to calculate the value of that moving average of the other indicator to use in an idea I am working on.

But I need help with a couple of newbie questions.

You will find information and examples if you search the forum: search for imaonarray on this forum sort by date
 
RaptorUK:
You will find information and examples if you search the forum: search for imaonarray on this forum sort by date


Thanks RaptorUK, I actually had a good look through forum before posting question, but have done again.

Am now satisfied how to treat the double array[]......(I think....I will post the results)

But an explanation of "applying the moving average to the 1st indicators data or previous" still escapes me, that is I can't locate the explanation.

Intuitively what I think this means is: that when you use the previous indicators data, you are using the the actual values of the indicator as calculated (ie. averaged of the calculated values for iForce or iMomentum etc) where as using the 1st indicators data would mean averaging the price or volume used to calculate the 1st indicator

Is this correct.....I still find it confusing

Thanks as always

 
pullend:

Thanks RaptorUK, I actually had a good look through forum before posting question, but have done again.

Am now satisfied how to treat the double array[]......(I think....I will post the results)

But an explanation of "applying the moving average to the 1st indicators data or previous" still escapes me, that is I can't locate the explanation.

Intuitively what I think this means is: that when you use the previous indicators data, you are using the the actual values of the indicator as calculated (ie. averaged of the calculated values for iForce or iMomentum etc) where as using the 1st indicators data would mean averaging the price or volume used to calculate the 1st indicator

Is this correct.....I still find it confusing

No, 1st Indicators data = the array you will be using in the iMAOnArray() call . . . so in your example the 1st indicator would be the iForce or iMomentum
 
RaptorUK:
No, 1st Indicators data = the array you will be using in the iMAOnArray() call . . . so in your example the 1st indicator would be the iForce or iMomentum


OK I am pretty close here, but couple of small issues I am hoping for some help to overcome


In this example, I am trying to create an indicator that compares the current OnBalanceVolume to the Average of the last (what ever Period Moving Average I set) OnBalanceVolumes and simply draws a histogram of height "one". With histogram bars green if current OBV is above average, red histogram bars if below and yesllow histogram bars if the current OBV and AveOBV are within a specified minimum distance.

I have got the indicator to work with 2 exceptions:

I am not sure how to keep the non drawing buffers from influencing the height of the Separate Chart Window.

My histogram looks very very small because of the window axis range instead of occupying the whole Separate Chart Window

(there was a similar question to this in the code base asking how to fix this, but it went unanswered)

Secondly the histogam seems to be out by a bar and I am not sure why or where to adjust the shift?

I have included the chart and code below and hope someone might assist.

I tried to really carefully follow examples in the forum to get this far

Thanks !!

//------------------------------------------------------------------------------------------------------------------------------|
// OBVPower.mq4 
// The code should be used for illustrating whether current OBV is above, below or equal to an averaged OBV (Period_MA);
//------------------------------------------------------------------------------------------------------------------------------|
#property indicator_separate_window                                  // Drawing in a separate window
#property indicator_buffers  6                                       // Number of buffers                               
//----------------------------------note to self - learn how non drawing buffers are distinguished from drawing   ?????????????
#property indicator_color1 CLR_NONE                                  // Color of the 1st indicator
#property indicator_color2 CLR_NONE                                  // Color of the 2nd indicator
#property indicator_color3 CLR_NONE                                  // Color of the 3rd indicator
#property indicator_color4 Green                                     // Color of the 4th indicator
#property indicator_color5 Red                                       // Color of the 5th indicator
#property indicator_color6 Yellow                                    // Color of the 6th indicator

extern int    Period_MA      =34;                                    // Averaging Period for Moving Average
extern string MA_Method_Help ="0-SMA, 1-EMA, 2-SMMA, 3-LWMA";
extern int    MA_Method      =1;                                     // Moving Average Emuneration Method
extern double GapMin         =0.5;                                   // Minimum Allowable Gap between OBV and AveOBV 

double OBV[];                                                        // OnBalanceVolume array
double AveOBV[];                                                     // Average of OnBalanceVolume array
double Gap[];                                                        // Difference between OBV and AveOBV array values                    
double UpBuffer[];                                                   // Declaring an indicator array 
double DownBuffer[];                                                 // Declaring an indicator array
double ZeroBuffer[];                                                 // Declaring an indicator array
//------------------------------------------------------------------------------------------------------------------------------|  
   int init()                                    // Special function init()
  {
   SetIndexBuffer(0,OBV);                                            // Assigning an array to a buffer
   SetIndexBuffer(1,AveOBV);                                         // Assigning an array to a buffer
   SetIndexBuffer(2,Gap);                                            // Assigning an array to a buffer
   SetIndexBuffer(3,UpBuffer);                                       // Assigning an array to a buffer
   SetIndexBuffer(4,DownBuffer);                                     // Assigning an array to a buffer
   SetIndexBuffer(5,ZeroBuffer);                                     // Assigning an array to a buffer
//------------------------------------------------------------------------------------------------------------------------------|
   SetIndexStyle (0,DRAW_NONE,STYLE_SOLID,3);                        // line style
   SetIndexStyle (1,DRAW_NONE,STYLE_SOLID,3);                        // line style
   SetIndexStyle (2,DRAW_NONE,STYLE_SOLID,3);                        // line style
   SetIndexStyle (3,DRAW_HISTOGRAM,STYLE_SOLID,3);                   // line style
   SetIndexStyle (4,DRAW_HISTOGRAM,STYLE_SOLID,3);                   // line style
   SetIndexStyle (5,DRAW_HISTOGRAM,STYLE_SOLID,3);                   // line style
//------------------------------------------------------------------------------------------------------------------------------|
   SetIndexLabel (0,"OBV");               
   SetIndexLabel (1,"AveOBV");               
   SetIndexLabel (2,"Gap");             
   SetIndexLabel (3,"UpBuffer");             
   SetIndexLabel (4,"DownBuffer");
   SetIndexLabel (5,"ZeroBuffer");
   return;                                       // Exit the special funct. init()
  }
//------------------------------------------------------------------------------------------------------------------------------|
int start()                                      // Special function start()
  {
//------------------------------------------------------------------------------------------------------------------------------|
   int  i;
   int  Counted_bars = IndicatorCounted();                                         
        i=Bars-Counted_bars-1;                                       // Index of the first uncounted
   if  (Counted_bars > 0) 
        Counted_bars--;
   double Diff; 
//---- the main loop       
   
//-------------calculate the array values for On Balance Volume                                         
   i=Bars-Counted_bars-1;           // Index of the first uncounted
   while(i>=0)                                                       // Loop for uncounted bars
     {
     OBV[i]=iOBV(NULL,0,PRICE_CLOSE,i);                              // Value of 0 buffer on i bar
     i--;                                                            // Calculating index of the next bar
     }
//-------------calculate the array values for the Average of 
//------------------On Balance Volume over specified Period_MA 
//-------double iMAOnArray( double array[], int total, int period, int ma_shift, int ma_method, int shift)   
   i=Bars-Counted_bars-1;                                            // Index of the first uncounted
   while(i>=0)                                                       // Loop for uncounted bars
     {
     AveOBV[i]=iMAOnArray(OBV,0,Period_MA,0,MA_Method,i);            // Value of 0 buffer on i bar
     i--;                                                            // Calculating index of the next bar
     }
//-------------calculate the array values for difference between On Balance Volume
//---------------and Average of On Balance Volume which equates to the difference
//-----------------the 2 indicator lines if they were drawn on the chart       
  i=Bars-Counted_bars-1;                                            // Index of the first uncounted
  while(i>=0)                                                       // Loop for uncounted bars
     {
     Gap[i]= (OBV[i]-AveOBV[i]);                                    // Value of 0 buffer on i bar
     i--;                                                           // Calculating index of the next bar
     }
  i=Bars-Counted_bars-1;                                            // Index of the first uncounted
  while(i>=0)
  {
     Diff= Gap[i];                                                  // Value of 0 buffer on i bar
     i--;                                                           // Calculating index of the next bar
       
  if      (Diff>GapMin)
                 {
                 UpBuffer[i]  =1;
                 }
  else if (Diff<-GapMin)
                 {
                 DownBuffer[i]=1;
                 }
  else           {
                 ZeroBuffer[i]=1;
                 }
      }
     
//--------------------------------------------------------------------
   return;                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------

 
pullend:

OK I am pretty close here, but couple of small issues I am hoping for some help to overcome


In this example, I am trying to create an indicator that compares the current OnBalanceVolume to the Average of the last (what ever Period Moving Average I set) OnBalanceVolumes and simply draws a histogram of height "one". With histogram bars green if current OBV is above average, red histogram bars if below and yesllow histogram bars if the current OBV and AveOBV are within a specified minimum distance.

I have got the indicator to work with 2 exceptions:

I am not sure how to keep the non drawing buffers from influencing the height of the Separate Chart Window.

My histogram looks very very small because of the window axis range instead of occupying the whole Separate Chart Window

(there was a similar question to this in the code base asking how to fix this, but it went unanswered)

These 2 lines will fix the scaling of the separate window . . .

#property indicator_maximum  2                                      // Max separate window scale
#property indicator_minimum -1                                      // Min separate window scale
 
RaptorUK:

These 2 lines will fix the scaling of the separate window . . .



Thanks RaptorUK again!!

The scaling is sorted superbly by that code.

But I am still stumped by why my Histogram is drawing 1 bar late.

I went through the values being calculated in the data window, and all match, all are correct.

All the values correspond to the MT4 terminal indicator values

But the drawn UpBuffer,DownBuffer,ZeroBuffers correspond to the previous bar.

Would you mind pointing out where in my code I have caused this to occur.

I am still unable to find the reason

thanks in advance

 
//---- note to self - learn how non drawing buffers are distinguished from drawing ?????????????
//---- also quit over commenting code, commenting the obvious is counter productive.

#property indicator_buffers  3      // the amount of drawing buffers
//----
#property indicator_color1 Green    // assign colors
#property indicator_color2 Red      // to drawing
#property indicator_color3 Yellow   // buffers only

double OBV[];
double AveOBV[];
double Gap[];
double UpBuffer[];
double DownBuffer[];
double ZeroBuffer[];
//-----------------------------------------------------------------------------------------------
int init()
{
 IndicatorBuffers(6);               // total amount of buffers including non drawing buffers
//----
 SetIndexBuffer(0,UpBuffer);        // set drawing buffers
 SetIndexBuffer(1,DownBuffer);      // to the lower indexes
 SetIndexBuffer(2,ZeroBuffer);
 SetIndexBuffer(3,OBV);             // set non drawing buffers
 SetIndexBuffer(4,AveOBV);          // to higher indexes
 SetIndexBuffer(5,Gap);
 
SDC:

thanks SDC, point taken about the commenting, I have been using the extra commenting as a tool to remind myself what each line does as I learn

but agree it is annoying and counterproductive when I ask you guy on the forum for help

Cheers

 
pullend:

thanks SDC, point taken about the commenting, I have been using the extra commenting as a tool to remind myself what each line does as I learn

but agree it is annoying and counterproductive when I ask you guy on the forum for help

I don't agree . . too many comments ( unless we are talking irrelevant padding and "eye candy" ) are much better than none . . .
 
RaptorUK:
I don't agree . . too many comments ( unless we are talking irrelevant padding and "eye candy" ) are much better than none . . .


Thanks RaptorUK, everything seems to work now except,

I still can't work out why the buffers calculate the correct values

(values matching the actual MT4 indicators in the next chart window)

but the code for my indicator draws the histogram one bar late.

Any help finding the error would be greatly appreciated !!!!!