龙听期货论坛's Archiver

龙听 发表于 2025-1-8 09:44

鼎元C++量化程式码指标与函数模块系列之【自适应指数移动平均线(Adaptive Exponential Moving Average,AEMA )的计算方法及调用方法】

主要有两个版本,一个是vector数组版本,二是直接调用数据版本

vector容器返回变量版本:

第一部分,头文件变量声明[code]
        vector<double>aema, highs, lows; //vector变量容器
        double a0, a1, a2, a3, a4, value2, value3, mltp1, mltp2, rate, aema1, aema2;//必要变量
[/code]第二部分:源文件核心策略程式码[code]
        RsqBar(sPeriod, sInst);
         int key = 0;
         mltp1 = 2/(10+1);
         mltp2 = 0;
         rate = 0;
         aema1 = 0;
         aema2 = 0;
         value2 =-99999;
         value3 = 99999;
        map<string, TKVALUE>::iterator it;
        for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); it++)
        {
                highs.push_back(it->second.dHigh);//将最高价装入vector中
                lows.push_back(it->second.dLow); //将最低价装入vector中
                a0 = it->second.dClose;
                if (key < 10)
                {
                        aema1 = aema1 + a0;//收盘价总和
                        aema2 = aema1/(key+1);
                        aema.push_back(aema2); // 将平均值装入vector中
                        key++;
                }
                else
                {
                        for (int i = 0; i < 10; i++) //将近10个周期的bar最高值装入vector
                        {
                                value2 = max2(value2, highs[key-1-i]); //遍历最近10个最高价找到最大的那个值放入value2
                                value3 = min2(value3, lows[key-1-i]);
                        }
                                a1 = (a0 - value3);
                                a2 = (value2 - a0);
                                a3 = value2 - value3;
                                a4 =abs(a1-a2);
                                if(a3 != 0)mltp2 = a4/a3;
                                rate =(1 + mltp2)* 2/11;
                                aema2 = aema2 + rate * (it->second.dClose - aema2);
                                aema.push_back(aema2);
                                key++;
                }
        }
[/code][code]aema[aema.size()-1][/code]这样的形式调用,因为这个AEMA是数组形式。

龙听 发表于 2025-1-8 09:48

返回数值形式:

第一部分:头文件声明变量[code]
        double AEMA(string period, string inst, int num, int pds);//自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
        vector<double>aema, highs, lows; //vector变量容器
        double a0, a1, a2, a3, a4, value2, value3, mltp1, mltp2, rate, aema1, aema2;//必要变量
[/code]第二部分:源文件公式区域核心策略[code]
//自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
double test::AEMA(string period, string inst, int num, int pds)//自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
{
        int key = 0;
        mltp1 = 2/(pds+1);
        mltp2 = 0;
        rate = 0;
        aema1 = 0;
        aema2 = 0;
        value2 = -99999;
        value3 = 99999;
        map<string, TKVALUE>::iterator it;
        for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); it++)
        {
                highs.push_back(it->second.dHigh);//将最高价装入vector中
                lows.push_back(it->second.dLow); //将最低价装入vector中
                a0 = it->second.dClose;
                if (key < num)
                {
                        aema1 = aema1 + a0;//收盘价总和
                        aema2 = aema1 / (key + 1);
                        key++;
                }
                else
                {
                        for (int i = 0; i < pds; i++) //将近10个周期的bar最高值装入vector
                        {
                                value2 = max2(value2, highs[key - 1 - i]); //遍历最近10个最高价找到最大的那个值放入value2
                                value3 = min2(value3, lows[key - 1 - i]);
                        }
                        a1 = (a0 - value3);
                        a2 = (value2 - a0);
                        a3 = value2 - value3;
                        a4 = abs(a1 - a2);
                        if (a3 != 0)mltp2 = a4 / a3;
                        rate = (1 + mltp2) * 2 / 11;
                        aema2 = aema2 + rate * (it->second.dClose - aema2);
                        key++;
                }
        }
        return aema2;
}
[/code]第三部分,调用办法[code]
AEMA(sPeriod,sInst,10,10)
[/code]

龙听 发表于 2025-1-8 09:48

自适应移动平均线(AEMA)指标研究【原文+程式码】

**** Hidden Message *****

页: [1]