Simple oscillator request...

 
Hi,
How do you make an oscillator that simply plots the difference between a moving average of a certain period and the price?
The code should be easy to write but.. this is not it.
What am I doing wrong here???



//+------------------------------------------------------------------+
//|                                             MApricediff.mq4               |
//|               Copyright © 2008, MetaQuotes Software Corp. |
//|                                          http://www.metaquotes.net |
//+------------------------------------------------------------------+

// indicator based MAdistance.mq4 by Tim Hyder aka Hiachiever  
// https://www.forex-tsd.com/blogs/newdigital/12-ma-distance-between-indicator-price-itself.html

#property indicator_buffers 1
#property indicator_color1 Black
extern int MA.Period = 10; 
extern int MA.Method = MODE_SMA;
extern int MA.Price = PRICE_CLOSE;
extern bool UseTickdata = True;
int DiFF;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  IndicatorBuffers(1);
  SetIndexStyle(0,DRAW_LINE);
  SetIndexBuffer(0,DiFF);
  return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int MA.Shift = 1;
   if(UseTickdata) MA.Shift = 0;
   
   double MA = iMA(Symbol(),Period(),MA.Period, 0, MA.Method, MA.Price, MA.Shift);
   int DiFF = (Close[MA.Shift] - MA)/Point; 
   return(0);
}
 
You forgot to update the indicator buffer with the calculated values.
 
bstone:
You forgot to update the indicator buffer with the calculated values.

So how do I do that?

 

You create an array of doubles and pass it to SetIndexBuffer() instead of passing 'int DiFF'. Then you update the contents of that array instead of the local variable 'int DiFF'. Something like this:


double diff[];


int init()
{
IndicatorBuffers(1);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,diff);
return(0);
}


int start()
{
int MA.Shift = 1;

if(UseTickdata) MA.Shift = 0;


for( int i = Bars - IndicatorCounted() - 1; i >= MA.Shift; i-- )

{

double MA = iMA(Symbol(),Period(),MA.Period, 0, MA.Method, MA.Price, i);

diff[ i ] = (Close[i] - MA)/Point;

}

return(0);

}


 
Thank you bstone for the explanation
Reason: