龙听期货论坛's Archiver

龙听 发表于 2025-1-12 15:54

鼎元C++量化程式码指标与函数模块系列之【市场强度函数(marketstrength)计算方法及调用方法】

第一部分:头文件声明变量[code]
        double marketstrength(string period,string inst, int num);//marketstrength函数调用与计算
[/code][code]
        vector<double> pc; //通用收盘价容器变量声明
[/code]第二部分:源文件函数核心计算程式码[code]
//marketstrength函数调用与计算
double test::marketstrength(string period, string inst, int num)
{
        double val1 = 0;
        double val2 = 0;
        double val3 = 0;
        int key = 0;
        double marketstrength = 0;
        map<string, TKVALUE>::reverse_iterator it; //建立逆向迭代
        for (it = mapK[sPeriod][sInst].rbegin(); it != mapK[sPeriod][sInst].rend(); it++) //遍历
        {
                pc.push_back(it->second.dClose); //将收盘价装入pc容器,注意是反向的,越靠前数据越新,比方说pc[0]是最新价格
                if (key > length)break; //截取前0-11个收盘价,一共12个数据
                key++;
        }
        for (int i = 0; i <= num; i++)
        {
                val1 += iff(pc[i] > pc[i + 1], pc[i], -pc[i]);
                val2 += iff(pc[i] > pc[i + 1], pc[i], 0);
                val3 += iff(pc[i] > pc[i + 1], 0, pc[i]);
        }

        if (val1 >= 0)
        {
                marketstrength = 100 * val1 / val2;
        }
        else
        {
                marketstrength = 100 * val1 / val3;
        }
        return marketstrength;
}
[/code]第三部分:调用方法[code]
        RsqBar(sPeriod, sInst); //申请数据
        marketstrength(sPeriod, sInst, length);

        InsertLog("数据量一共有: " + to_string(length+2) + "marketstrength: " + to_string(marketstrength(sPeriod, sInst, length)));
[/code]

龙听 发表于 2025-1-12 15:54

运行效果与演示:[url]http://www.qhlt.cn/thread-161582-1-1.html[/url];

页: [1]