//+------------------------------------------------------------------+
//| MAadaptive (r2).mq4 |
//| |
//| based on MA adaptive R2 by Perry Kaufman |
//+------------------------------------------------------------------+
#property copyright "copyleft mladen"
#property link "mladenfx@gmail.com"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//
//
//
//
//
extern int MA.period = 20;
extern int MA.price = PRICE_CLOSE;
//
//
//
//
//
double buffer[];
double prices[];
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int init()
{
IndicatorBuffers(2);
SetIndexBuffer(0,buffer);
SetIndexBuffer(1,prices);
return(0);
}
int deinit() { return(0); }
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
int start()
{
int countedBars=IndicatorCounted();
int i,limit;
if (countedBars<0) return(-1);
if (countedBars>0) countedBars--;
limit = Bars-countedBars;
//
//
//
//
//
for(i = limit; i >=0; i--)
{
prices[i] = iMA(NULL,0,1,0,MODE_SMA,MA.price,i);
if (i>Bars-MA.period)
{
buffer[i]=prices[i]; continue;
}
//
//
//
//
//
double r2 = MathPow(iCorrelation(prices,MA.period,i),2);
buffer[i] = buffer[i+1] + r2*(prices[i]-buffer[i+1]);
}
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
double iCorrelation(double& array[],int period,int i)
{
int matches = 0;
for (int k=0; k<period; k++)
{
if (array[i+k] <= array[i+k+1]) continue;
matches++;
}
return (2.0*matches/period-1);
}
Comments