#pragma once
Write by Chen Mursin 20150718
Function: Calculates the moving average of a sequence and returns the sequence
Template<class T>class FUNCMA
{
Public
FUNCMA () {lasttick = 0;};
~FUNCMA () {};
void Clear ()
{
T.clear ();
T.swap (vector<t> (T));
Lasttick = 0;
}
Vector<t> caculate (vector<t> p,int n,int direction)
{
int size = P.size ()-1;
if (size <= 0)
{
return t;
}
Vector<double> TSum;
if (direction = = 1)
{
for (; size > Lasttick; size--)
{
T sum = 0;
T ma = 0;
if (N > Size)
{
N = size + 1;
}
for (int i = size; i > size-n; i--)
{
Sum + = P[i];
}
Ma = sum/n;
Tsum.push_back (sum);
T.push_back (MA);
}
Lasttick = size + 1;
}
Else
{
for (int i = Lasttick; i<=size; i++)
{
T sum = 0;
T ma = 0;
int temp = N;
if (Temp > i)
temp = i + 1;
for (int j = i; j > i-temp; j--)
{
SUM+=P[J];
}
Ma = sum/temp;
T.push_back (MA);
}
Lasttick = size + 1;
}
return t;
}
Private
int Lasttick;
Vector<t> T;
};
Source: http://download.csdn.net/detail/corivsky/8916855
The advantage of this code is that. Without clear, the moving average sequence is not repeated, when the incoming sequence increases. He calculates the new value of the incoming sequence based on the original.
Usage: static funcma<double> ma60;static funcma<double> ma2;static funcma<double> ma22;static Vector <double> c;//closing price sequence vector<double> ma60temp = Ma60. Caculate (c,n*2,0);//The moving average of the closing price vector<double> ma2temp = Ma2. Caculate (ma60temp,m1*2,0);///The EMA of the EMA vector<double> ma22temp =ma22. Caculate (ma2temp,m2*2,0);//The moving average of the EMA
Calculates a sequence of moving averages for a sequence of templates that can be realized by moving averages