//---- parameters
input int MA_Length = 40;
//---- buffers
double GreenBuffer[];
double RedBuffer[];
double BufferMoment[];
double BufferMomentLine[];
//----
int ExtCountedBars = 0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_HISTOGRAM, 0, 1, Green);
SetIndexBuffer(0, GreenBuffer);
SetIndexStyle(1, DRAW_HISTOGRAM, 0, 1, Red);
SetIndexBuffer(1, RedBuffer);
SetIndexStyle(2, DRAW_LINE, 0, 1, RoyalBlue);
SetIndexBuffer(2, BufferMoment);
SetIndexStyle(3, DRAW_LINE, 0, 1, Yellow);
SetIndexBuffer(3, BufferMomentLine);
//---- initialization done
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
if(Bars <= MA_Length)
return(0);
//---
ExtCountedBars = IndicatorCounted();
if(ExtCountedBars < 0)
return(-1);
if(ExtCountedBars > 0)
ExtCountedBars--;
//---
int pos = Bars - ExtCountedBars - 1;
if(ExtCountedBars == 0)
pos -= MA_Length;
//---
while(pos >= 0)
{
double BolUpper = iBands(NULL, 0, MA_Length, 2, 0, PRICE_CLOSE, MODE_UPPER, pos),
BolLower = iBands(NULL, 0, MA_Length, 2, 0, PRICE_CLOSE, MODE_LOWER, pos),
Moment = BolUpper - BolLower;
//---
BufferMoment[pos] = Moment;
//---
double MomentLine = 0;
for(int i = MA_Length; i >= 1; i--)
MomentLine += BufferMoment[pos + i];
//---
MomentLine = MomentLine / MA_Length;
//---
BufferMomentLine[pos] = MomentLine;
//---
if(Moment >= MomentLine)
GreenBuffer[pos] = Moment;
else
if(Moment <= MomentLine)
RedBuffer[pos] = Moment;
//---
pos--;
}
//----
return (rates_total);