- UID
- 2
- 积分
- 2892617
- 威望
- 1396340 布
- 龙e币
- 1496277 刀
- 在线时间
- 13313 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2024-12-25
|
存档的一个版本,以后有空再继续研究,不要加到鼎元里面,会死掉的。
头文件声明:- void calculateHighLow(vector<double>highs, vector<double>lows, int period, vector<double>highest, vector<double>lowest);
- void calculateKDJ(vector<double>pc, vector<double>highs, vector<double>lows, int period, double smoothingFactor);
复制代码- //KDJ指标参数
- vector<double> closes,highest2, lowest2,highs,lows,rsv, kValues, dValues, jValues;
- double smoothingFactor;
- int period1;
复制代码 源文件指标模块:- #include <algorithm> // For std::min_element and std::max_element
复制代码 头部增加一个这样的- // 计算最低价和最高价范围
- void test::calculateHighLow(vector<double>highs, vector<double>lows, int period1, vector<double>highest2, vector<double>lowest2)
- {
- for (int i = 0; i < highs.size(); ++i)
- {
- if (i < period1 - 1) {
- highest2.push_back(0);
- lowest2.push_back(0);
- }
- else {
- double maxHigh = *max_element(highs.begin() + i - period1 + 1, highs.begin() + i + 1);
- double minLow = *min_element(lows.begin() + i - period1 + 1, lows.begin() + i + 1);
- highest2.push_back(maxHigh);
- lowest2.push_back(minLow);
- }
- }
- }
- // 计算 KDJ 指标
- void test::calculateKDJ(vector<double>closes, vector<double>highs,vector<double>lows, int period1, double smoothingFactor)
- {
- vector<double> highest2, lowest2, rsv, kValues, dValues, jValues;
- // 计算最高价和最低价
- calculateHighLow(highs, lows, period1, highest2, lowest2);
- // 计算 RSV
- for (int i = 0; i < closes.size(); ++i)
- {
- if (i < period1-1)
- {
- rsv.push_back(0);
- }
- else
- {
- double range = highest2[i] - lowest2[i];
- rsv.push_back(iff(range == 0,0,closes[i] - lowest2[i])/range * 100);
- }
- }
- // 初始化 K 和 D 值
- kValues.push_back(50); // 通常初始值设置为 50
- dValues.push_back(50);
- // 迭代计算 K 和 D 值
- for (int i = 1; i < rsv.size(); ++i)
- {
- double k = smoothingFactor * rsv[i] + (1 - smoothingFactor) * kValues.back();
- double d = smoothingFactor * k + (1 - smoothingFactor) * dValues.back();
- kValues.push_back(k);
- dValues.push_back(d);
- }
- // 计算 J 值
- for (size_t i = 0; i < kValues.size(); ++i)
- {
- double j = 3 * kValues[i] - 2 * dValues[i];
- jValues.push_back(j);
- }
- }
复制代码 源文件策略test.cpp里面- //计算KDJ
- map<string, TKVALUE>::iterator it;
- for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); it++)
- {
- closes.push_back(it->second.dClose);
- highs.push_back(it->second.dHigh);
- lows.push_back(it->second.dLow);
- }
- // 参数
- period1 = 9; // 计算周期
- smoothingFactor = 1.0/3.0; // 平滑系数 (通常为 1/3)
- calculateHighLow(highs, lows, period1, highest2, lowest2);
- // 计算 KDJ
- calculateKDJ(closes, highs, lows, period1, smoothingFactor);//获得DVALUE,KVALUE,JVALUE的 VECTOR形式数值
- double value1 = kValues[kValues.size() - 1];
- double value2 = dValues[dValues.size() - 1];
- double value3 = jValues[jValues.size() - 1];
- InsertLog("Kvalue: " + to_string(value1) + "Dvalue: " + to_string(value2) + "Jvalue: " + to_string(value3));
复制代码 我测试的会崩溃,大家不要再试了,以后我优化好了在这里贴出来。 |
|