Calculates a sequence of moving averages for a sequence of templates that can be realized by moving averages

Source: Internet
Author: User

#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 code: http://download.csdn.net/detail/corivsky/8916855


The advantage of this code is that, as long as it is not clear, the moving average sequence is not repeated, and when the incoming sequence increases, he calculates the new value of the incoming sequence on the original basis.


How to use: 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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Calculates a sequence of moving averages for a sequence of templates that can be realized by moving averages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.