: | : | :期货量化学习 | :期货量化 |
返回列表 发帖

鼎元C++量化程式码技术指标源码C++版模板【C++期货技术指标模板,建议在test.h和test.cpp中同步至最新,方便统一调用和使用,一次编辑多次使用!】

鼎元C++量化程式码技术指标源码C++版模板【C++期货技术指标模板,建议在test.h和test.cpp中同步至最新,方便统一调用和使用,一次编辑多次使用!】

第一部分:使用、调用办法
  1. RsqBar(sPeriod, sInst);//申请bar数据
  2. atr = avgtruerange(sPeriod, sInst, length);//指标调用
复制代码
第二部分:使用程式码模块

1、开始、结束模块;
2、tick模块、bar模块;
.
第三部分:疑问咨询解决办法

1、疑问、咨询、报错解决专用版:http://www.qhlt.cn/forum-244-1.html

2、管理员微信、QQ、群

C++微信群: ; QQ群: ;管理员微信:;管理员QQ:

论坛官方微信、群(期货热点、量化探讨、开户与绑定实盘)
 
期货论坛 - 版权/免责声明   1.本站发布源码(包括函数、指标、策略等)均属开放源码,用意在于让使用者学习程序化语法撰写,使用者可以任意修改语法內容并调整参数。仅限用于个人学习使用,请勿转载、滥用,严禁私自连接实盘账户交易
  2.本站发布资讯(包括文章、视频、历史记录、教材、评论、资讯、交易方案等)均系转载自网络主流媒体,内容仅为作者当日个人观点,本网转载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。本网不对该类信息或数据做任何保证。不对您构成任何投资建议,不能依靠信息而取代自身独立判断,不对因使用本篇文章所诉信息或观点等导致的损失承担任何责任。
  3.本站发布资源(包括书籍、杂志、文档、软件等)均从互联网搜索而来,仅供个人免费交流学习,不可用作商业用途,本站不对显示的内容承担任何责任。请在下载后24小时内删除。如果喜欢,请购买正版,谢谢合作!
  4.龙听期货论坛原创文章属本网版权作品,转载须注明来源“龙听期货论坛”,违者本网将保留追究其相关法律责任的权力。本论坛除发布原创文章外,亦致力于优秀财经文章的交流分享,部分文章推送时若未能及时与原作者取得联系并涉及版权问题时,请及时联系删除。联系方式:http://www.qhlt.cn/thread-262-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

现有的C++技术指标公式、使用方式及源码汇总集锦:

1、简单移动均线:http://www.qhlt.cn/thread-159054-1-1.html

2、最高highest,最低lowest:http://www.qhlt.cn/thread-160007-1-1.html

3、平均真实波动幅度(averagetruerange,atr)指标公式:http://www.qhlt.cn/thread-160002-1-1.html

4、加权移动均线ema调用:http://www.qhlt.cn/thread-160008-1-1.html

5、布林带(bollingerbands,BOLL)计算方法及调用方法:http://www.qhlt.cn/thread-160288-1-1.html

6、平滑异同移动平均线(Moving Average Convergence Divergence,MACD),并形成模块化,可以在指标和策略中方便调用:http://www.qhlt.cn/thread-160291-1-1.html

7、方差(variance)、标准差(standard deviation,sdev)计算方法及调用方法:http://www.qhlt.cn/thread-160300-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

为了更新方方便,在下楼分别更新C++头文件和C++源文件技术指标程式码模块。可以不时更新一下,这样可以得到最新的技术指标模块。
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

  1. //指标公式变量声明******************************************************************test头文件指标公式变量名称模块开始(修改与编辑指标参数变量)
  2.         double iff(bool cond1, int num, int ref);//iff函数
  3.         double max2(double a, double b);//返回a,b中较大值
  4.         double max3(double a, double b, double c);//返回a,b,c中最大值
  5.         double min2(double a, double b); //返回a,b中较小值
  6.         double min3(double a, double b, double c);//返回a,b,c中最小值
  7.         double waverage(string period, string inst, int num);//计算Weighted Moving Average,加权移动平均线
  8.         double average(string period, string inst, int num); //average,ma函数变量,atr计算功能
  9.         double highest(string period, string inst, int num);//计算NUM周期最高价格
  10.         double highestN(string period, string inst, int num, int ref);//计算REF前个bar的num周期最高价格
  11.     double lowest(string period, string inst, int num); //计算NUM周期最低价格
  12.         double lowestN(string period, string inst, int num, int ref);//计算ref前个bar的num周期的最低价格
  13.         double closeN(string period, string inst, int ref);//计算ref前个bar的收盘价格
  14.         double avgtruerange(string period, string inst, int num); //计算真实波动幅度平均值(average true range)
  15.         double Variance(string period, string inst, int num);//计算方差公式变量
  16.         double StandardDev(string period, string inst, int num);//计算标准差公式变量
  17.         double Volatility(string period, string inst, int num);//计算历史波动率(CALCULATING HISTORICAL VOLATILITY)
  18.         double bollingerbands(string period, string inst, int num, int ref); //计算boll布林带变量
  19.         //指数移动平均返回具体数据时调用xaverage,返回数组时则用ema调用。数组形式多用于指标计算中间变量
  20.         double xaverage(string period, string inst, int num);//计算num周期指数移动平均(Exponential Moving Average)值
  21.         vector<double>ema(vector<double>pc, int num);//ema计算返回数组vector形式指数移动平均
  22.         //指数移动平均返回具体数据时调用xaverage,返回数组时则用ema调用。数组形式多用于指标计算中间变量
  23.         vector<double>EMAvectorCaL(vector<double>pc, int num);//EMA容器计算形式
  24.         vector<double>MACDcalculate(vector<double>pc, int shortPeriod, int longPeriod);//计算MACD
  25.         double mav(vector<double>pc, int num);//计算简单MA数组形式
  26.         double hhv(vector<double> pc, int num);
  27.         double llv(vector<double> pc, int num);
  28.         double AEMA(string period, string inst, int num, int pds);//自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
  29.         vector<double>aema, highs, lows; //vector变量容器
  30.         double a0, a1, a2, a3, a4, value2, value3, mltp1, mltp2, rate, aema1, aema2;//必要变量,自适应移动平均线

  31. //指标公式变量声明******************************************************************test头文件指标公式变量名称模块结束(修改与编辑指标参数变量)
复制代码
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

  1. //****************************************************************外部引用文件开始(谨慎修改)
  2. #include "pch.h"
  3. #include "test.h"
  4. #include <vector>
  5. #include <windows.h>
  6. #include <fstream>
  7. #include <stdio.h>
  8. #include <mmsystem.h>
  9. #include <thread>
  10. #pragma comment(lib,"winmm.lib")
  11. #include <algorithm> // For std::min_element and std::max_element
  12. #include <deque>
  13. using namespace std;
  14. //****************************************************************主程序指标公式function计算模块(公式编辑与设计区)
  15. // iff功能函数开始
  16. double test::iff(bool cond1, int num, int ref)
  17. {
  18.         int d = 0;
  19.         if (cond1)
  20.         {
  21.                 d = num;
  22.         }
  23.         else
  24.         {
  25.                 d = ref;
  26.         }

  27.         return d;
  28. }

  29. //max2(a,b)比较a,b两个数字
  30. double test::max2(double a, double b)
  31. {
  32.         double c = 0;
  33.         if (a >= b)
  34.         {
  35.                 c = a;
  36.         }
  37.         else
  38.         {
  39.                 c = b;
  40.         }
  41.         return c;
  42. }

  43. //max3(a,b)比较a,b,c三个数字
  44. double test::max3(double a, double b, double c)
  45. {
  46.         double d = 0;

  47.         if (a >= b)
  48.         {
  49.                 d = a;
  50.         }
  51.         else if (a < b)
  52.         {
  53.                 d = b;
  54.         }
  55.         if (d < c)
  56.         {
  57.                 d = c;
  58.         }
  59.         return d;
  60. }

  61. //min2(a,b)比较两个数字大小
  62. double test::min2(double a, double b)
  63. {
  64.         double c = 0;
  65.         if (a >= b)
  66.         {
  67.                 c = b;
  68.         }
  69.         else
  70.         {
  71.                 c = a;
  72.         }
  73.         return c;
  74. }

  75. //min3(a,b,c)比较三个数字大小
  76. double test::min3(double a, double b, double c)
  77. {
  78.         double d = 0;

  79.         if (a >= b)
  80.         {
  81.                 d = b;
  82.         }
  83.         else if (a < b)
  84.         {
  85.                 d = a;
  86.         }
  87.         if (d > c)
  88.         {
  89.                 d = c;
  90.         }
  91.         return d;
  92. }

  93. //均线average计算公式开始
  94. double test::average(string period, string inst, int num)
  95. {
  96.         int key = 0;
  97.         double sump = 0;
  98.         if (mapK[period][inst].size() < num) return 0;
  99.         map<string, TKVALUE>::reverse_iterator it;
  100.         for (it = mapK[sPeriod][sInst].rbegin(); it != mapK[sPeriod][sInst].rend(); it++)
  101.         {
  102.                 sump = sump + it->second.dClose;
  103.                 key++;
  104.                 if (key >= num)break;
  105.         }
  106.         return sump / num;
  107. }
  108. //计算num周期内bar最高价格
  109. double test::highest(string period, string inst, int num)
  110. {
  111.         double hiprice = 0;
  112.         int key = 0;
  113.         map<string, TKVALUE>::reverse_iterator it; //建立逆向迭代器从右往左
  114.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++) //FOR循环遍历
  115.         {
  116.                 if (hiprice < it->second.dHigh) hiprice = it->second.dHigh; //如果hiprice值小于遍历的bar的最高价,则将最高价赋值给hiprice
  117.                 key++;//计算循环次数
  118.                 if (key >= num)break; //达到num个bar后跳出循环
  119.         }
  120.         return hiprice;//将获取的最高价格值返回
  121. }
  122. //计算num周期内bar的最低价格
  123. double test::lowest(string period, string inst, int num)
  124. {
  125.         double lowprice = 0;
  126.         int key = 0;
  127.         map<string, TKVALUE>::reverse_iterator it;
  128.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  129.         {
  130.                 if (lowprice == 0)lowprice = it->second.dLow;
  131.                 if (lowprice > it->second.dLow)lowprice = it->second.dLow;
  132.                 key++;
  133.                 if (key >= num)break;
  134.         }
  135.         return lowprice;
  136. }

  137. //计算历史波动率(calculating historical volatility)
  138. double test::Volatility(string period, string inst, int num)
  139. {
  140.         double PreClose = 0;//获取每个bar前一个bar的收盘价
  141.         double tr = 0; //tr的值计算
  142.         int key = 0; //计数器
  143.         vector<double>vtr; //建立一个数组,用来存放每个bar对应的truerange值,它们的下标号与bar的是一致的
  144.         if (mapK[period][inst].size() < num) return 0; //如果数据数量不够返回0;
  145.         map<string, TKVALUE >::iterator it; //建立正迭代器,标号从0开始,从左向右开始
  146.         for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); ++it) //从第一开始的bar数据开始遍历
  147.         {
  148.                 if (PreClose != 0) //如果值不为0说明是在中间的bar,开始在bar数据要素中采集需要的变量数值
  149.                 {
  150.                         double dHL = it->second.dHigh - it->second.dLow; //当根bar的最高减最低
  151.                         double dHC = abs(it->second.dHigh - PreClose); //当根bar最高减昨日收盘
  152.                         double dLC = abs(it->second.dLow - PreClose); //当根bar最低减昨日收盘
  153.                         double e = max3(dHL, dHC, dLC); //获得每一个bar对应的truerange值(上面三者之最大者)
  154.                         vtr.push_back(e); //将上面e的值放到tr的vector(数组)里面供下面使用
  155.                 }
  156.                 PreClose = it->second.dClose; //将当前bar的收盘价数值赋给此变量供下一个bar使用
  157.         }
  158.         //计算tr的num周期指数移动平均值
  159.         if (key <= num)
  160.         {
  161.                 tr = 0;
  162.                 key++;
  163.         }
  164.         else
  165.         {
  166.                 tr = (vtr[vtr.size() - 1]) * 2 / (num + 1) + tr * (num - 1) / (num + 1); // 2/(num + 1)指数移动平均线的平滑因子
  167.                 //tr = (2 * vtr[vtr.size()-1] + (num - 1) * tr)/(num + 1); //第二种计算vtr指数移动平均方式,平滑因子不变
  168.         }
  169.         return tr;
  170. }

  171. //计算真实波动幅度平均值(average true range)
  172. double test::avgtruerange(string period, string inst, int num)
  173. {
  174.         double preclose = 0; //设计一个局部变量存储上一日收盘价
  175.         double sumatr = 0;  // 设计一个加总变量, 存储atr的值
  176.         vector<double>vatr;//设计一个vector变量,存储tr值
  177.         if (mapK[period][inst].size() < num) return 0; //如果数据不够用返回0
  178.         map<string, TKVALUE>::iterator it;//建立一个正向迭代器
  179.         for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); it++) //建立 for 循环采集需要的要素数据
  180.         {
  181.                 if (preclose != 0) //这次不是第一次循环
  182.                 {
  183.                         double dHL = it->second.dHigh - it->second.dLow; //当天最高价减当天最低价
  184.                         double dHC = abs(it->second.dHigh - preclose);  //当天最高价减昨天收盘价
  185.                         double dLC = abs(it->second.dLow - preclose);   //当天最低价减昨天收盘价
  186.                         vatr.push_back(max3(dHL, dHC, dLC)); //将上面三个值中最大值加入到vector里面
  187.                 }
  188.                 preclose = it->second.dClose; //每次遍历后将当天的收盘价赋值给preclose供下次遍历使用
  189.         }
  190.         for (int i = 1; i <= length; i++)
  191.         {
  192.                 sumatr = sumatr + vatr[vatr.size() - i];//将vatr中的数据从右至左依顺序加总num个
  193.         }
  194.         return sumatr / num;
  195. }
  196. //方差计算公式
  197. double test::Variance(string period, string inst, int num)
  198. {
  199.         double ma = average(sPeriod, sInst, num);//中间均线
  200.         int key = 0;
  201.         double sumdev = 0;
  202.         double madev = 0;
  203.         if (mapK[period][inst].size() < num) return 0;
  204.         map<string, TKVALUE>::reverse_iterator it;
  205.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  206.         {
  207.                 sumdev += pow(it->second.dClose - ma, 2); //计算偏离平方和
  208.                 key++;
  209.                 if (key >= num)break; //num次循环后跳出
  210.         }
  211.         return sumdev / num;
  212. }

  213. //标准差计算公式
  214. double test::StandardDev(string period, string inst, int num)
  215. {
  216.         double value1 = 0;
  217.         if (mapK[period][inst].size() < num) return 0;
  218.         value1 = Variance(sPeriod, sInst, num);

  219.         return sqrt(value1);
  220. }
  221. //boll布林带计算公式
  222. double test::bollingerbands(string period, string inst, int num, int ref)
  223. {
  224.         double ma = average(sPeriod, sInst, num); //获取num周期平均线ma
  225.         int key = 0;
  226.         double sumdev = 0;
  227.         double madev = 0;
  228.         if (mapK[period][inst].size() < num) return 0;
  229.         map<string, TKVALUE>::reverse_iterator it;
  230.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  231.         {
  232.                 sumdev += pow(it->second.dClose - ma, 2);
  233.                 key++;
  234.                 if (key >= num)break;
  235.         }
  236.         madev = sqrt(sumdev / num); //获得标准差

  237.         return ma + ref * madev;
  238. }

  239. //计算num周期指数移动平均(Exponential Moving Average)值
  240. double test::xaverage(string period, string inst, int num)
  241. {
  242.         double n = 0;
  243.         double ema = 0;
  244.         if (mapK[period][inst].size() < num) return 0;
  245.         map<string, TKVALUE>::iterator it;
  246.         for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); it++)
  247.         {
  248.                 if (n < num)
  249.                 {
  250.                         ema = (ema + it->second.dClose) / (n + 1);
  251.                         n++;
  252.                 }
  253.                 else
  254.                 {
  255.                         ema = (2 * it->second.dClose + (n - 1) * ema) / (n + 1); //指数移动平均计算核心公式,ema = a*close + (1-a)*ema[1],a为加权因子,通常=2/(N+1)
  256.                 }
  257.         }
  258.         return ema;
  259. }

  260. //计算Weighted Moving Average,加权移动平均
  261. double test::waverage(string period, string inst, int num)
  262. {
  263.         double sumwma = 0;
  264.         int i = 0;
  265.         if (mapK[period][inst].size() < num) return 0; //bar数量小于num个时返回0
  266.         map<string, TKVALUE>::reverse_iterator it;
  267.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  268.         {
  269.                 sumwma += (num-i) * it->second.dClose; //计算num*close + (num-1)*close[1] +...+ 1*close[num-1]的总和
  270.                 i++;
  271.                 if(i >= num) break;
  272.          }
  273.         double d = (num+1)*num * 0.5; //计算num从1到num的总和
  274.         return sumwma / d;
  275. }
  276. //ema计算返回数组vector形式指数移动平均
  277. vector<double>test::ema(vector<double>pc, int num)
  278. {
  279.         vector<double>emav;
  280.         double emavector = 0;
  281.         int i = 0;

  282.         for (int it = 0; it < pc.size(); it++)
  283.         {
  284.                 if (i < num)
  285.                 {
  286.                         emavector = (emavector + pc[it])/(i+1);
  287.                         i++;
  288.                         emav.push_back(emavector);
  289.                 }
  290.                 else
  291.                 {
  292. //                        emavector = (2*pc[it]+(num-1)*emavector)/(num+1);//ema均线计算公式
  293.                         emavector = emavector + 2 * (pc[it] - emavector) / (num + 1);
  294.                         emav.push_back(emavector);
  295.                 }
  296.         }
  297.         return emav;
  298. }
  299. //计算REF前个bar的num周期最高价格
  300. double test::highestN(string period, string inst, int num, int ref)
  301. {
  302.         double d = 0;
  303.         int n = 0;
  304.         int r = 0;
  305.         map<string, TKVALUE>::reverse_iterator it;
  306.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  307.         {
  308.                 r++;
  309.                 if (r <= ref)continue; //需要跳过的bar数
  310.                 if (d < it->second.dHigh)d = it->second.dHigh; //将最大值赋值给d
  311.                 n++;
  312.                 if (n >= num) break; //num 次循环后跳出(从0开始,所以从num-1个后就退出)
  313.         }
  314.         return d; //返回d
  315. }
  316. //计算ref前个bar的num周期的最低价格
  317. double test::lowestN(string period, string inst, int num, int ref)
  318. {
  319.         double d = 0;
  320.         int n = 0;
  321.         int r = 0;
  322.         map<string, TKVALUE>::reverse_iterator it;
  323.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  324.         {
  325.         r++;
  326.         if (r <= ref) continue; //需要跳过的bar数
  327.         if (d == 0)d = it->second.dLow; //判断是否是第一次循环
  328.         if (d > it->second.dLow)d = it->second.dLow; //将低最值赋给d
  329.         n++;
  330.         if (n >= num)break; //num个循环后退出
  331.         }
  332.         return d; //返回d值
  333. }
  334. //返回ref个bar前的收盘价
  335. double test::closeN(string period, string inst, int ref)
  336. {
  337.         double d = 0;
  338.         int r = 0;
  339.         int n = 0;
  340.         map<string, TKVALUE>::reverse_iterator it;
  341.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  342.         {
  343.                 r++;
  344.                 if (r <= ref)continue; //跳过ref个bar
  345.                 d = it->second.dClose; //将ref个bar前一个bar的收盘价赋值给d ;
  346.                 break;
  347.         }
  348.         return d;
  349. }
  350. ////////////////////////////////////////////////////////////////////////////////////////////////////////////MACD公式计算
  351. vector<double>test::EMAvectorCaL(vector<double>pc, int num)//EMA容器计算形式
  352. {
  353.         vector<double> ema(pc.size());
  354.         double multiplier = 2.0 / (num + 1);
  355.         ema[0] = pc[0]; // 初始化第一值为价格的第一个值
  356.         for (int i = 1; i < pc.size(); i++) {
  357.                 ema[i] = (pc[i] - ema[i - 1]) * multiplier + ema[i - 1];
  358.         }
  359.         return ema;
  360. }
  361. vector<double>test::MACDcalculate(vector<double>pc, int shortPeriod, int longPeriod)
  362. {
  363.         // 计算快线和慢线的 EMA
  364.         vector<double> shortEMA = EMAvectorCaL(pc, shortPeriod);
  365.         vector<double> longEMA = EMAvectorCaL(pc, longPeriod);

  366.         // 计算 DIFF
  367.         vector<double> diff(pc.size());
  368.         for (int i = 0; i < pc.size(); i++)
  369.         {
  370.                 diff[i] = shortEMA[i] - longEMA[i];
  371.         }
  372.         return diff;
  373. }
  374. ////////////////////////////////////////////////////////////////////////////////////////////////////////////MACD公式计算

  375. ////////////////////////////////////////////////////////////////////////////////////////////////////////////KDJ公式计算
  376. double test::hhv(vector<double> pc, int num)
  377. {
  378.         double d = 0;
  379.         for (size_t i = pc.size() - 1; i >= pc.size() - num; i--)
  380.         {
  381.                 d = max2(d, pc[i]);
  382.         }
  383.         return d;
  384. }

  385. double test::llv(vector<double> pc, int num)
  386. {
  387.         double d = 999999;
  388.         for (size_t i = pc.size() - 1; i >= pc.size() - num; i--)
  389.         {
  390.                 d = min2(d, pc[i]);
  391.         }
  392.         return d;
  393. }
  394. ////////////////////////////////////////////////////////////////////////////////////////////////////////////KDJ公式计算

  395. double test::mav(vector<double>pc, int num)//计算简单MA数组形式
  396. {
  397.         if (pc.size() <2) return 0;
  398.         double d = 0;
  399.         for (size_t i = pc.size() - 1; i >= pc.size() - num; i--)
  400.         {
  401.                 d = d + pc[i];
  402.         }
  403.         return d / num;
  404. }

  405. //自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
  406. double test::AEMA(string period, string inst, int num, int pds)//自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
  407. {
  408.         int key = 0;
  409.         mltp1 = 2/(pds+1);
  410.         mltp2 = 0;
  411.         rate = 0;
  412.         aema1 = 0;
  413.         aema2 = 0;
  414.         value2 = -99999;
  415.         value3 = 99999;
  416.         map<string, TKVALUE>::iterator it;
  417.         for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); it++)
  418.         {
  419.                 highs.push_back(it->second.dHigh);//将最高价装入vector中
  420.                 lows.push_back(it->second.dLow); //将最低价装入vector中
  421.                 a0 = it->second.dClose;
  422.                 if (key < num)
  423.                 {
  424.                         aema1 = aema1 + a0;//收盘价总和
  425.                         aema2 = aema1 / (key + 1);
  426.                         key++;
  427.                 }
  428.                 else
  429.                 {
  430.                         for (int i = 0; i < pds; i++) //将近10个周期的bar最高值装入vector
  431.                         {
  432.                                 value2 = max2(value2, highs[key - 1 - i]); //遍历最近10个最高价找到最大的那个值放入value2
  433.                                 value3 = min2(value3, lows[key - 1 - i]);
  434.                         }
  435.                         a1 = (a0 - value3);
  436.                         a2 = (value2 - a0);
  437.                         a3 = value2 - value3;
  438.                         a4 = abs(a1 - a2);
  439.                         if (a3 != 0)mltp2 = a4 / a3;
  440.                         rate = (1 + mltp2) * 2 / 11;
  441.                         aema2 = aema2 + rate * (it->second.dClose - aema2);
  442.                         key++;
  443.                 }
  444.         }
  445.         return aema2;
  446. }
复制代码
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

2024.11.13日增加c++语言下的布林带(bollingerbans,BOLL)指标的编写,并形成模块化,可以在指标和策略中方便调用:http://www.qhlt.cn/thread-160288-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

2024.11.13 新增C++编程语言下的MACD指标公式程式码,平滑异同移动平均线(Moving Average Convergence Divergence,MACD),并形成模块化,可以在指标和策略中方便调用:http://www.qhlt.cn/thread-160291-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

2024.11.13 新增C++编程语言下的方差(variance)、标准差(standard deviation,sdev)计算公式,方便设计像boll带这样的指标时直接调用。【方差(variance)、标准差(standard deviation,sdev)计算方法及调用方法】:http://www.qhlt.cn/thread-160300-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

2024.12.12新增:

鼎元C++量化程式码指标与函数模块系列之【指数移动平均(Exponential Moving Average,ema)计算方法及调用方法】:http://www.qhlt.cn/thread-160997-1-1.html

鼎元C++量化程式码指标与函数模块系列之【加权移动平均线(Weighted Moving Average,WMA)的计算方法及调用方法】:http://www.qhlt.cn/thread-161023-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

鼎元C++量化程式码指标与函数模块系列之【highest[N]、lowest[N]及close[N]的计算方法及调用方法】:http://www.qhlt.cn/thread-161180-1-1.html;
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

C++程序化/量化学习视频教程系列 第050节:鼎元C++量化指标封装之【市场强度函数(marketstrength)计算方法及调用方法】【C++量化指标公式开发系列】:http://www.qhlt.cn/thread-161582-1-1.html;
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

2025.01.16 新增【动量指标(Momentum Index,MTM)计算方法及调用方法】:http://www.qhlt.cn/thread-161650-1-1.html
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

2025.04.02  更新并优化了一下MACD指标的调用:http://www.qhlt.cn/thread-162976-1-1.html

并且加到了指标模块中。
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

2025年4.12日我重新整理的函数公式变量声明和模板

test.h头文件声明部分:
  1. //指标公式变量声明******************************************************************test头文件指标公式变量名称模块开始(修改与编辑指标参数变量)
  2.         double iff(bool cond1, int num, int ref);//iff函数
  3.         double max2(double a, double b);//返回a,b中较大值
  4.         double max3(double a, double b, double c);//返回a,b,c中最大值
  5.         double min2(double a, double b); //返回a,b中较小值
  6.         double min3(double a, double b, double c);//返回a,b,c中最小值
  7.         double waverage(string period, string inst, int num);//计算Weighted Moving Average,加权移动平均线
  8.         double average(string period, string inst, int num); //average,ma函数变量,atr计算功能
  9.         double highest(string period, string inst, int num);//计算NUM周期最高价格
  10.         double highestN(string period, string inst, int num, int ref);//计算REF前个bar的num周期最高价格
  11.     double lowest(string period, string inst, int num); //计算NUM周期最低价格
  12.         double lowestN(string period, string inst, int num, int ref);//计算ref前个bar的num周期的最低价格
  13.         double closeN(string period, string inst, int ref);//计算ref前个bar的收盘价格
  14.         double avgtruerange(string period, string inst, int num); //计算真实波动幅度平均值(average true range)
  15.         double Variance(string period, string inst, int num);//计算方差公式变量
  16.         double StandardDev(string period, string inst, int num);//计算标准差公式变量
  17.         double Volatility(string period, string inst, int num);//计算历史波动率(CALCULATING HISTORICAL VOLATILITY)
  18.         double bollingerbands(string period, string inst, int num, int ref); //计算boll布林带变量
  19.         //指数移动平均返回具体数据时调用xaverage,返回数组时则用ema调用。数组形式多用于指标计算中间变量
  20.         double xaverage(string period, string inst, int num);//计算num周期指数移动平均(Exponential Moving Average)值
  21.         vector<double>ema(vector<double>pc, int num);//ema计算返回数组vector形式指数移动平均
  22.         //指数移动平均返回具体数据时调用xaverage,返回数组时则用ema调用。数组形式多用于指标计算中间变量
  23.         vector<double>EMAvectorCaL(vector<double>pc, int num);//EMA容器计算形式
  24.         vector<double>MACDcalculate(vector<double>pc, int shortPeriod, int longPeriod);//计算MACD
  25.         double mav(vector<double>pc, int num);//计算简单MA数组形式
  26.         double hhv(vector<double> pc, int num);
  27.         double llv(vector<double> pc, int num);
  28.         double AEMA(string period, string inst, int num, int pds);//自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
  29.         vector<double>aema, highs, lows; //vector变量容器
  30.         double a0, a1, a2, a3, a4, value2, value3, mltp1, mltp2, rate, aema1, aema2;//必要变量,自适应移动平均线
  31.         double marketstrength(string period,string inst, int num);//marketstrength函数调用与计算
  32.         double Momentum(string period, string inst, int num); //动量指标(Momentum Index,MTM)计算与指标调用
  33. //指标公式变量声明******************************************************************test头文件指标公式变量名称模块结束(修改与编辑指标参数变量)
复制代码
函数模板模块:
  1. //****************************************************************外部引用文件开始(谨慎修改)
  2. #include "pch.h"
  3. #include "test.h"
  4. #include <vector>
  5. #include <windows.h>
  6. #include <fstream>
  7. #include <stdio.h>
  8. #include <mmsystem.h>
  9. #include <thread>
  10. #pragma comment(lib,"winmm.lib")
  11. #include <algorithm> // For std::min_element and std::max_element
  12. #include <deque>
  13. using namespace std;
  14. //****************************************************************主程序指标公式function计算模块(公式编辑与设计区)
  15. // iff功能函数开始
  16. double test::iff(bool cond1, int num, int ref)
  17. {
  18.         int d = 0;
  19.         if (cond1)
  20.         {
  21.                 d = num;
  22.         }
  23.         else
  24.         {
  25.                 d = ref;
  26.         }

  27.         return d;
  28. }

  29. //max2(a,b)比较a,b两个数字
  30. double test::max2(double a, double b)
  31. {
  32.         double c = 0;
  33.         if (a >= b)
  34.         {
  35.                 c = a;
  36.         }
  37.         else
  38.         {
  39.                 c = b;
  40.         }
  41.         return c;
  42. }

  43. //max3(a,b)比较a,b,c三个数字
  44. double test::max3(double a, double b, double c)
  45. {
  46.         double d = 0;

  47.         if (a >= b)
  48.         {
  49.                 d = a;
  50.         }
  51.         else if (a < b)
  52.         {
  53.                 d = b;
  54.         }
  55.         if (d < c)
  56.         {
  57.                 d = c;
  58.         }
  59.         return d;
  60. }

  61. //min2(a,b)比较两个数字大小
  62. double test::min2(double a, double b)
  63. {
  64.         double c = 0;
  65.         if (a >= b)
  66.         {
  67.                 c = b;
  68.         }
  69.         else
  70.         {
  71.                 c = a;
  72.         }
  73.         return c;
  74. }

  75. //min3(a,b,c)比较三个数字大小
  76. double test::min3(double a, double b, double c)
  77. {
  78.         double d = 0;

  79.         if (a >= b)
  80.         {
  81.                 d = b;
  82.         }
  83.         else if (a < b)
  84.         {
  85.                 d = a;
  86.         }
  87.         if (d > c)
  88.         {
  89.                 d = c;
  90.         }
  91.         return d;
  92. }

  93. //均线average计算公式开始
  94. double test::average(string period, string inst, int num)
  95. {
  96.         int key = 0;
  97.         double sump = 0;
  98.         if (mapK[period][inst].size() < num) return 0;
  99.         map<string, TKVALUE>::reverse_iterator it;
  100.         for (it = mapK[sPeriod][sInst].rbegin(); it != mapK[sPeriod][sInst].rend(); it++)
  101.         {
  102.                 sump = sump + it->second.dClose;
  103.                 key++;
  104.                 if (key >= num)break;
  105.         }
  106.         return sump / num;
  107. }
  108. //计算num周期内bar最高价格
  109. double test::highest(string period, string inst, int num)
  110. {
  111.         double hiprice = 0;
  112.         int key = 0;
  113.         map<string, TKVALUE>::reverse_iterator it; //建立逆向迭代器从右往左
  114.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++) //FOR循环遍历
  115.         {
  116.                 if (hiprice < it->second.dHigh) hiprice = it->second.dHigh; //如果hiprice值小于遍历的bar的最高价,则将最高价赋值给hiprice
  117.                 key++;//计算循环次数
  118.                 if (key >= num)break; //达到num个bar后跳出循环
  119.         }
  120.         return hiprice;//将获取的最高价格值返回
  121. }
  122. //计算num周期内bar的最低价格
  123. double test::lowest(string period, string inst, int num)
  124. {
  125.         double lowprice = 0;
  126.         int key = 0;
  127.         map<string, TKVALUE>::reverse_iterator it;
  128.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  129.         {
  130.                 if (lowprice == 0)lowprice = it->second.dLow;
  131.                 if (lowprice > it->second.dLow)lowprice = it->second.dLow;
  132.                 key++;
  133.                 if (key >= num)break;
  134.         }
  135.         return lowprice;
  136. }

  137. //计算历史波动率(calculating historical volatility)
  138. double test::Volatility(string period, string inst, int num)
  139. {
  140.         double PreClose = 0;//获取每个bar前一个bar的收盘价
  141.         double tr = 0; //tr的值计算
  142.         int key = 0; //计数器
  143.         vector<double>vtr; //建立一个数组,用来存放每个bar对应的truerange值,它们的下标号与bar的是一致的
  144.         if (mapK[period][inst].size() < num) return 0; //如果数据数量不够返回0;
  145.         map<string, TKVALUE >::iterator it; //建立正迭代器,标号从0开始,从左向右开始
  146.         for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); ++it) //从第一开始的bar数据开始遍历
  147.         {
  148.                 if (PreClose != 0) //如果值不为0说明是在中间的bar,开始在bar数据要素中采集需要的变量数值
  149.                 {
  150.                         double dHL = it->second.dHigh - it->second.dLow; //当根bar的最高减最低
  151.                         double dHC = abs(it->second.dHigh - PreClose); //当根bar最高减昨日收盘
  152.                         double dLC = abs(it->second.dLow - PreClose); //当根bar最低减昨日收盘
  153.                         double e = max3(dHL, dHC, dLC); //获得每一个bar对应的truerange值(上面三者之最大者)
  154.                         vtr.push_back(e); //将上面e的值放到tr的vector(数组)里面供下面使用
  155.                 }
  156.                 PreClose = it->second.dClose; //将当前bar的收盘价数值赋给此变量供下一个bar使用
  157.         }
  158.         //计算tr的num周期指数移动平均值
  159.         if (key <= num)
  160.         {
  161.                 tr = 0;
  162.                 key++;
  163.         }
  164.         else
  165.         {
  166.                 tr = (vtr[vtr.size() - 1]) * 2 / (num + 1) + tr * (num - 1) / (num + 1); // 2/(num + 1)指数移动平均线的平滑因子
  167.                 //tr = (2 * vtr[vtr.size()-1] + (num - 1) * tr)/(num + 1); //第二种计算vtr指数移动平均方式,平滑因子不变
  168.         }
  169.         return tr;
  170. }

  171. //计算真实波动幅度平均值(average true range)
  172. double test::avgtruerange(string period, string inst, int num)
  173. {
  174.         double preclose = 0; //设计一个局部变量存储上一日收盘价
  175.         double sumatr = 0;  // 设计一个加总变量, 存储atr的值
  176.         vector<double>vatr;//设计一个vector变量,存储tr值
  177.         if (mapK[period][inst].size() < num) return 0; //如果数据不够用返回0
  178.         map<string, TKVALUE>::iterator it;//建立一个正向迭代器
  179.         for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); it++) //建立 for 循环采集需要的要素数据
  180.         {
  181.                 if (preclose != 0) //这次不是第一次循环
  182.                 {
  183.                         double dHL = it->second.dHigh - it->second.dLow; //当天最高价减当天最低价
  184.                         double dHC = abs(it->second.dHigh - preclose);  //当天最高价减昨天收盘价
  185.                         double dLC = abs(it->second.dLow - preclose);   //当天最低价减昨天收盘价
  186.                         vatr.push_back(max3(dHL, dHC, dLC)); //将上面三个值中最大值加入到vector里面
  187.                 }
  188.                 preclose = it->second.dClose; //每次遍历后将当天的收盘价赋值给preclose供下次遍历使用
  189.         }
  190.         for (int i = 1; i <= length; i++)
  191.         {
  192.                 sumatr = sumatr + vatr[vatr.size() - i];//将vatr中的数据从右至左依顺序加总num个
  193.         }
  194.         return sumatr / num;
  195. }
  196. //方差计算公式
  197. double test::Variance(string period, string inst, int num)
  198. {
  199.         double ma = average(sPeriod, sInst, num);//中间均线
  200.         int key = 0;
  201.         double sumdev = 0;
  202.         double madev = 0;
  203.         if (mapK[period][inst].size() < num) return 0;
  204.         map<string, TKVALUE>::reverse_iterator it;
  205.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  206.         {
  207.                 sumdev += pow(it->second.dClose - ma, 2); //计算偏离平方和
  208.                 key++;
  209.                 if (key >= num)break; //num次循环后跳出
  210.         }
  211.         return sumdev / num;
  212. }

  213. //标准差计算公式
  214. double test::StandardDev(string period, string inst, int num)
  215. {
  216.         double value1 = 0;
  217.         if (mapK[period][inst].size() < num) return 0;
  218.         value1 = Variance(sPeriod, sInst, num);

  219.         return sqrt(value1);
  220. }
  221. //boll布林带计算公式
  222. double test::bollingerbands(string period, string inst, int num, int ref)
  223. {
  224.         double ma = average(sPeriod, sInst, num); //获取num周期平均线ma
  225.         int key = 0;
  226.         double sumdev = 0;
  227.         double madev = 0;
  228.         if (mapK[period][inst].size() < num) return 0;
  229.         map<string, TKVALUE>::reverse_iterator it;
  230.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  231.         {
  232.                 sumdev += pow(it->second.dClose - ma, 2);
  233.                 key++;
  234.                 if (key >= num)break;
  235.         }
  236.         madev = sqrt(sumdev / num); //获得标准差

  237.         return ma + ref * madev;
  238. }

  239. //计算num周期指数移动平均(Exponential Moving Average)值
  240. double test::xaverage(string period, string inst, int num)
  241. {
  242.         double n = 0;
  243.         double ema = 0;
  244.         if (mapK[period][inst].size() < num) return 0;
  245.         map<string, TKVALUE>::iterator it;
  246.         for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); it++)
  247.         {
  248.                 if (n < num)
  249.                 {
  250.                         ema = (ema + it->second.dClose) / (n + 1);
  251.                         n++;
  252.                 }
  253.                 else
  254.                 {
  255.                         ema = (2 * it->second.dClose + (n - 1) * ema) / (n + 1); //指数移动平均计算核心公式,ema = a*close + (1-a)*ema[1],a为加权因子,通常=2/(N+1)
  256.                 }
  257.         }
  258.         return ema;
  259. }

  260. //计算Weighted Moving Average,加权移动平均
  261. double test::waverage(string period, string inst, int num)
  262. {
  263.         double sumwma = 0;
  264.         int i = 0;
  265.         if (mapK[period][inst].size() < num) return 0; //bar数量小于num个时返回0
  266.         map<string, TKVALUE>::reverse_iterator it;
  267.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  268.         {
  269.                 sumwma += (num-i) * it->second.dClose; //计算num*close + (num-1)*close[1] +...+ 1*close[num-1]的总和
  270.                 i++;
  271.                 if(i >= num) break;
  272.         }
  273.         double d = (num+1)*num * 0.5; //计算num从1到num的总和
  274.         return sumwma / d;
  275. }
  276. //ema计算返回数组vector形式指数移动平均
  277. vector<double>test::ema(vector<double>pc, int num)
  278. {
  279.         vector<double>emav;
  280.         double emavector = 0;
  281.         int i = 0;

  282.         for (int it = 0; it < pc.size(); it++)
  283.         {
  284. //                if (i < num)
  285. //                {
  286. //                        emavector = (emavector + pc[it])/(i+1);
  287. //                        i++;
  288. //                        emav.push_back(emavector);
  289. //                }
  290. //                else
  291. //                {
  292. ////                        emavector = (2*pc[it]+(num-1)*emavector)/(num+1);//ema均线计算公式
  293. //                        emavector = emavector + 2 * (pc[it] - emavector) / (num + 1);
  294. //                        emav.push_back(emavector);
  295. //                }
  296.                 if (it == 0)
  297.                 {
  298.                         emavector = emavector + pc[it];
  299.                         emav.push_back(pc[0]);
  300.                 }else
  301.                 {
  302. //                        emavector = (2*pc[it]+(num-1)*emavector)/(num+1);//ema均线计算公式
  303.                         emavector = emavector + 2 * (pc[it] - emavector) / (num + 1);
  304.                         emav.push_back(emavector);
  305.                 }
  306.         }
  307.         return emav;
  308. }
  309. //计算REF前个bar的num周期最高价格
  310. double test::highestN(string period, string inst, int num, int ref)
  311. {
  312.         double d = 0;
  313.         int n = 0;
  314.         int r = 0;
  315.         map<string, TKVALUE>::reverse_iterator it;
  316.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  317.         {
  318.                 r++;
  319.                 if (r <= ref)continue; //需要跳过的bar数
  320.                 if (d < it->second.dHigh)d = it->second.dHigh; //将最大值赋值给d
  321.                 n++;
  322.                 if (n >= num) break; //num 次循环后跳出(从0开始,所以从num-1个后就退出)
  323.         }
  324.         return d; //返回d
  325. }
  326. //计算ref前个bar的num周期的最低价格
  327. double test::lowestN(string period, string inst, int num, int ref)
  328. {
  329.         double d = 0;
  330.         int n = 0;
  331.         int r = 0;
  332.         map<string, TKVALUE>::reverse_iterator it;
  333.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  334.         {
  335.         r++;
  336.         if (r <= ref) continue; //需要跳过的bar数
  337.         if (d == 0)d = it->second.dLow; //判断是否是第一次循环
  338.         if (d > it->second.dLow)d = it->second.dLow; //将低最值赋给d
  339.         n++;
  340.         if (n >= num)break; //num个循环后退出
  341.         }
  342.         return d; //返回d值
  343. }
  344. //返回ref个bar前的收盘价
  345. double test::closeN(string period, string inst, int ref)
  346. {
  347.         double d = 0;
  348.         int r = 0;
  349.         int n = 0;
  350.         map<string, TKVALUE>::reverse_iterator it;
  351.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  352.         {
  353.                 r++;
  354.                 if (r <= ref)continue; //跳过ref个bar
  355.                 d = it->second.dClose; //将ref个bar前一个bar的收盘价赋值给d ;
  356.                 break;
  357.         }
  358.         return d;
  359. }
  360. ////////////////////////////////////////////////////////////////////////////////////////////////////////////MACD公式计算
  361. vector<double>test::EMAvectorCaL(vector<double>pc, int num)//EMA容器计算形式
  362. {
  363.         vector<double> ema(pc.size());
  364.         double multiplier = 2.0 / (num + 1);
  365.         ema[0] = pc[0]; // 初始化第一值为价格的第一个值
  366.         for (int i = 1; i < pc.size(); i++) {
  367.                 ema[i] = (pc[i] - ema[i - 1]) * multiplier + ema[i - 1];
  368.         }
  369.         return ema;
  370. }
  371. vector<double>test::MACDcalculate(vector<double>pc, int shortPeriod, int longPeriod)
  372. {
  373.         // 计算快线和慢线的 EMA
  374.         vector<double> shortEMA = EMAvectorCaL(pc, shortPeriod);
  375.         vector<double> longEMA = EMAvectorCaL(pc, longPeriod);

  376.         // 计算 DIFF
  377.         vector<double> diff(pc.size());
  378.         for (int i = 0; i < pc.size(); i++)
  379.         {
  380.                 diff[i] = shortEMA[i] - longEMA[i];
  381.         }
  382.         return diff;
  383. }
  384. ////////////////////////////////////////////////////////////////////////////////////////////////////////////MACD公式计算

  385. ////////////////////////////////////////////////////////////////////////////////////////////////////////////KDJ公式计算
  386. double test::hhv(vector<double> pc, int num)
  387. {
  388.         double d = 0;
  389.         for (size_t i = pc.size() - 1; i >= pc.size() - num; i--)
  390.         {
  391.                 d = max2(d, pc[i]);
  392.         }
  393.         return d;
  394. }

  395. double test::llv(vector<double> pc, int num)
  396. {
  397.         double d = 999999;
  398.         for (size_t i = pc.size() - 1; i >= pc.size() - num; i--)
  399.         {
  400.                 d = min2(d, pc[i]);
  401.         }
  402.         return d;
  403. }
  404. ////////////////////////////////////////////////////////////////////////////////////////////////////////////KDJ公式计算

  405. double test::mav(vector<double>pc, int num)//计算简单MA数组形式
  406. {
  407.         if (pc.size() <2) return 0;
  408.         double d = 0;
  409.         for (size_t i = pc.size() - 1; i >= pc.size() - num; i--)
  410.         {
  411.                 d = d + pc[i];
  412.         }
  413.         return d / num;
  414. }

  415. //自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
  416. double test::AEMA(string period, string inst, int num, int pds)//自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
  417. {
  418.         int key = 0;
  419.         mltp1 = 2/(pds+1);
  420.         mltp2 = 0;
  421.         rate = 0;
  422.         aema1 = 0;
  423.         aema2 = 0;
  424.         value2 = -99999;
  425.         value3 = 99999;
  426.         map<string, TKVALUE>::iterator it;
  427.         for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); it++)
  428.         {
  429.                 highs.push_back(it->second.dHigh);//将最高价装入vector中
  430.                 lows.push_back(it->second.dLow); //将最低价装入vector中
  431.                 a0 = it->second.dClose;
  432.                 if (key < num)
  433.                 {
  434.                         aema1 = aema1 + a0;//收盘价总和
  435.                         aema2 = aema1 / (key + 1);
  436.                         key++;
  437.                 }
  438.                 else
  439.                 {
  440.                         for (int i = 0; i < pds; i++) //将近10个周期的bar最高值装入vector
  441.                         {
  442.                                 value2 = max2(value2, highs[key - 1 - i]); //遍历最近10个最高价找到最大的那个值放入value2
  443.                                 value3 = min2(value3, lows[key - 1 - i]);
  444.                         }
  445.                         a1 = (a0 - value3);
  446.                         a2 = (value2 - a0);
  447.                         a3 = value2 - value3;
  448.                         a4 = abs(a1 - a2);
  449.                         if (a3 != 0)mltp2 = a4 / a3;
  450.                         rate = (1 + mltp2) * 2 / 11;
  451.                         aema2 = aema2 + rate * (it->second.dClose - aema2);
  452.                         key++;
  453.                 }
  454.         }
  455.         return aema2;
  456. }
  457. //marketstrength函数调用与计算
  458. double test::marketstrength(string period, string inst, int num)
  459. {
  460.         double val1 = 0;
  461.         double val2 = 0;
  462.         double val3 = 0;
  463.         int key = 0;
  464.         double marketstrength = 0;
  465.         map<string, TKVALUE>::reverse_iterator it; //建立逆向迭代
  466.         for (it = mapK[sPeriod][sInst].rbegin(); it != mapK[sPeriod][sInst].rend(); it++) //遍历
  467.         {
  468.                 pc.push_back(it->second.dClose); //将收盘价装入pc容器,注意是反向的,越靠前数据越新,比方说pc[0]是最新价格
  469.                 if (key > length)break; //截取前0-11个收盘价,一共12个数据
  470.                 key++;
  471.         }
  472.         for (int i = 0; i <= num; i++)
  473.         {
  474.                 val1 += iff(pc[i] > pc[i + 1], pc[i], -pc[i]);
  475.                 val2 += iff(pc[i] > pc[i + 1], pc[i], 0);
  476.                 val3 += iff(pc[i] > pc[i + 1], 0, pc[i]);
  477.         }

  478.         if (val1 >= 0)
  479.         {
  480.                 marketstrength = 100 * val1 / val2;
  481.         }
  482.         else
  483.         {
  484.                 marketstrength = 100 * val1 / val3;
  485.         }
  486.         return marketstrength;
  487. }
  488. //动量指标(Momentum Index,MTM)计算与指标调用
  489. double test::Momentum(string period, string inst, int num)
  490. {
  491.         double d = 0;
  492.         int r = 0;
  493.         int n = 0;
  494.         map<string, TKVALUE>::reverse_iterator it;
  495.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); it++)
  496.         {
  497.                 if (r == 0) n = it->second.dClose;
  498.                 r++;
  499.                 if (r < num)continue; //跳过NUM个bar
  500.                 d = it->second.dClose; //将ref个bar前一个bar的收盘价赋值给d ;
  501.                 break;
  502.         }
  503.         return n - d;
  504. }
复制代码
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

2025.04.20日更新模板:

test.h声明变量:
  1.         //公式指标(function)变量设计(开始)******************************************
  2.         double iff(bool cond1, int num, int ref);//iff函数
  3.         double max2(double a, double b);//返回a,b中较大值
  4.         double max3(double a, double b, double c);//返回a,b,c中最大值
  5.         double min2(double a, double b); //返回a,b中较小值
  6.         double min3(double a, double b, double c);//返回a,b,c中最小值
  7.         double average(string period, string inst, int num); //均线函数变量
  8.         double averageref(string period, string inst, int num, int ref); //均线average[1]功能函数变量
  9.         double highest(string period, string inst, int num); //最高价highest函数变量
  10.         double highestref(string period, string inst, int num, int ref);//最高high[1]功能函数变量
  11.         double lowest(string period, string inst, int num); //最低价lowest函数变量
  12.         double lowestref(string period, string inst, int num, int ref);//最低lowest[1] 功能函数变量
  13.         double avgtruerange(string period, string inst, int num); //averagetruerange函数变量,atr计算功能
  14.         double bollingerbands(string period, string inst, int num,int ref);//BOLL计算公式;
  15.         double Variance(string period, string inst, int num);//方差计算公式变量
  16.         double StandardDev(string period, string inst, int num);//标准差计算公式变量
  17.         double xaverage(string period, string inst, int num);//xaverage计算公式;
  18.         double ema(string period, string inst, int num);//ema计算公式;
  19.         vector<double>emaseries(vector<double> pc, int num); //ema计算公式(返回数组);
  20.         vector<double>averagearray(vector<double> pc, int num); //ema计算公式(返回数组);
  21.         double closeref(string period, string inst, int ref);//获得close[ref]值
  22.         double macd(string period, string inst, int num, int num1, int num2);//macd计算公式变量
  23.         double aema(string period, string inst, int num, int pds);//自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
  24.         double highestseries(vector<double> pc, int num);//求数组容器形式的highest(high,length)
  25.         double lowestseries(vector<double> pc, int num); //求数组容器形式的lowest(low,length)
  26.         vector<double>smaseries(vector<double>pc, int num, int num2);//指数平滑移动平均函数(SMA,sma(x,N,M),即x变量的N周期平均,最新值权重为M,即SMAt = SMAt-1 * (N-M)/N + x*M/N )返回数组形式
  27.         //公式指标(function)变量设计(结束)******************************************
复制代码
function模块区或主程序区指标模块:
  1. //******************************************************************公式(function)模块(开始)*******************************************************************/
  2. // iff功能函数开始
  3. double test::iff(bool cond1, int num, int ref)
  4. {
  5.         int d = 0;
  6.         if (cond1)
  7.         {
  8.                 d = num;
  9.         }
  10.         else
  11.         {
  12.                 d = ref;
  13.         }
  14.         return d;
  15. }
  16. //max2(a,b)比较a,b两个数字
  17. double test::max2(double a, double b)
  18. {
  19.         double c = 0;
  20.         if (a >= b)
  21.         {
  22.                 c = a;
  23.         }
  24.         else
  25.         {
  26.                 c = b;
  27.         }
  28.         return c;
  29. }
  30. //max3(a,b)比较a,b,c三个数字
  31. double test::max3(double a, double b, double c)
  32. {
  33.         double d = 0;

  34.         if (a >= b)
  35.         {
  36.                 d = a;
  37.         }
  38.         else if (a < b)
  39.         {
  40.                 d = b;
  41.         }
  42.         if (d < c)
  43.         {
  44.                 d = c;
  45.         }
  46.         return d;
  47. }
  48. //min2(a,b)比较两个数字大小
  49. double test::min2(double a, double b)
  50. {
  51.         double c = 0;
  52.         if (a >= b)
  53.         {
  54.                 c = b;
  55.         }
  56.         else
  57.         {
  58.                 c = a;
  59.         }
  60.         return c;
  61. }
  62. //min3(a,b,c)比较三个数字大小
  63. double test::min3(double a, double b, double c)
  64. {
  65.         double d = 0;
  66.         if (a >= b)
  67.         {
  68.                 d = b;
  69.         }
  70.         else if (a < b)
  71.         {
  72.                 d = a;
  73.         }
  74.         if (d > c)
  75.         {
  76.                 d = c;
  77.         }
  78.         return d;
  79. }
  80. //均线average计算公式开始
  81. double test::average(string period, string inst, int num)
  82. {
  83.         double d = 0;
  84.         int n = 0;
  85.         map<string, TKVALUE>::reverse_iterator it;
  86.         //  InsertLog(to_string(mapK[period][inst].size()));
  87.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
  88.         {
  89.                 d += it->second.dClose;
  90.                 n++;
  91.                 if (n >= num)break;
  92.         }
  93.         return d / n;
  94. }
  95. //均线average[1]计算公式开始
  96. double test::averageref(string period, string inst, int num, int ref)
  97. {
  98.         double d = 0;
  99.         int n = 0;
  100.         int r = 0;
  101.         map<string, TKVALUE>::reverse_iterator it;
  102.         //  InsertLog(to_string(mapK[period][inst].size()));
  103.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
  104.         {
  105.                 r++;
  106.                 if (r <= ref)continue;
  107.                 d += it->second.dClose;
  108.                 n++;
  109.                 if (n >= num)break;
  110.         }
  111.         return d / n;
  112. }
  113. //最高价highest计算公式开始
  114. double test::highest(string period, string inst, int num)
  115. {
  116.         double d = 0;
  117.         int n = 0;
  118.         map<string, TKVALUE>::reverse_iterator it;
  119.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
  120.         {
  121.                 if (d < it->second.dHigh)d = it->second.dHigh;
  122.                 n++;
  123.                 if (n >= num)break;
  124.         }
  125.         return d;
  126. }
  127. //最高价highest[1]计算公式开始
  128. double test::highestref(string period, string inst, int num, int ref)
  129. {
  130.         double d = 0;
  131.         int n = 0;
  132.         int r = 0;
  133.         map<string, TKVALUE>::reverse_iterator it;
  134.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
  135.         {
  136.                 r++;
  137.                 if (r <= ref)continue;
  138.                 if (d < it->second.dHigh)d = it->second.dHigh;
  139.                 n++;
  140.                 if (n >= num)break;
  141.         }
  142.         return d;
  143. }
  144. //最低价lowest计算公式
  145. double test::lowest(string period, string inst, int num)
  146. {
  147.         double d = 0;
  148.         int n = 0;
  149.         map<string, TKVALUE>::reverse_iterator it;
  150.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
  151.         {
  152.                 if (d == 0)d = it->second.dLow;
  153.                 if (d > it->second.dLow)d = it->second.dLow;
  154.                 n++;
  155.                 if (n >= num)break;
  156.         }
  157.         return d;
  158. }
  159. //最低价lowest[1]计算公式开始
  160. double test::lowestref(string period, string inst, int num, int ref)
  161. {
  162.         double d = 0;
  163.         int n = 0;
  164.         int r = 0;
  165.         map<string, TKVALUE>::reverse_iterator it;
  166.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
  167.         {
  168.                 r++;
  169.                 if (r <= ref)continue;
  170.                 if (d == 0)d = it->second.dLow;
  171.                 if (d > it->second.dLow)d = it->second.dLow;
  172.                 n++;
  173.                 if (n >= num)break;
  174.         }
  175.         return d;
  176. }
  177. //ATR,averagetruerange计算公式开始
  178. double test::avgtruerange(string period, string inst, int num)
  179. {
  180.         double dPreClose = 0;
  181.         int n = 0;
  182.         double sumatr = 0;
  183.         vector<double>vAtr;
  184.         if (mapK[period][inst].size() < num) return 0;
  185.         map<string, TKVALUE >::iterator it;
  186.         for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); ++it)
  187.         {

  188.                 if (dPreClose != 0)
  189.                 {
  190.                         double d = it->second.dHigh - it->second.dLow;
  191.                         double dHC = abs(it->second.dHigh - dPreClose);
  192.                         double dLC = abs(it->second.dLow - dPreClose);
  193.                         if (d < dHC)d = dHC;
  194.                         if (d < dLC)d = dLC;
  195.                         vAtr.push_back(d);
  196.                 }
  197.                 dPreClose = it->second.dClose;
  198.         }
  199.         for (int i = 1; i <= num; i++)
  200.         {
  201.                 sumatr += vAtr[vAtr.size() - i];
  202.         }
  203.         return  sumatr / num;
  204. }

  205. //布林带(bollingerbands)开始
  206. double test::bollingerbands(string period, string inst, int num, int ref)
  207. {
  208.         double avg1 = average(sPeriod, sInst,num);//中间均线
  209.         int n = 0;
  210.         double sumdev = 0;
  211.         double madev = 0;
  212.         if(mapK[period][inst].size()<num) return 0;
  213.         map<string, TKVALUE>::reverse_iterator it;
  214.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
  215.         {
  216.                 sumdev += pow(it->second.dClose - avg1,2); //diff平方加总
  217.                 n++;
  218.                 if (n >= num)break;
  219.         }
  220.           madev = sqrt(sumdev/num); //得到方差后再取标准差
  221.    return avg1 + ref*madev;
  222. }

  223. //方差计算公式开始
  224. double test::Variance(string period, string inst, int num)
  225. {
  226.         double avg1 = average(sPeriod, sInst, num);//中间均线
  227.         int n = 0;
  228.         double sumdev = 0;
  229.         double madev = 0;
  230.         if (mapK[period][inst].size() < num) return 0;
  231.         map<string, TKVALUE>::reverse_iterator it;
  232.         for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
  233.         {
  234.                 sumdev += pow(it->second.dClose - avg1, 2); //偏离平方加总
  235.                 n++;
  236.                 if (n >= num)break;
  237.         }
  238.         return sumdev/n;
  239. }

  240. // 方差计算公式结束
  241. //标准差计算公式开始
  242. double test::StandardDev(string period, string inst, int num)
  243. {
  244.         double value1 = 0;
  245.         if (mapK[period][inst].size() < num) return 0;
  246.         value1 = Variance(sPeriod, sInst, num);
  247.         return sqrt(value1);
  248. }
  249. // 标准差计算公式结束
  250. //获得close[ref]值
  251. double test::closeref(string period, string inst, int ref)
  252. {
  253.         int d = 0;
  254.         int r = 0;
  255.         map<string, TKVALUE>::reverse_iterator it;//逆向迭代
  256.         for (it = mapK[sPeriod][sInst].rbegin(); it != mapK[sPeriod][sInst].rend(); it++) //逆向遍历所有K线
  257.         {
  258.                 r++;
  259.                 if (r <= ref)continue; //跳过ref个bar
  260.                 d = it->second.dClose; //将ref个bar前一个bar的收盘价赋值给d ;
  261.                 break;
  262.         }
  263.         return d;
  264. }
  265. //xaverage计算公式(跟下面ema函数为同一函数,方便大家偏好)
  266. double test::xaverage(string period, string inst, int num)
  267. {
  268.         double d = 0;
  269.         int r = 0;
  270.         map<string, TKVALUE >::iterator it;
  271.         for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); ++it)
  272.         {
  273.                 if (r < num)//当数据不够num个bar时用总和/(bar个数加1)
  274.                 {
  275.                         d = (d + it->second.dClose) / (r + 1);
  276.                         r++;
  277.                 }
  278.                 else //当bar的总个数大于num个周期时,ema = a*close + (1-a)*ema[1],a一般为2/(num+1)
  279.                 {
  280.                         d = (2 * it->second.dClose + (num - 1) * d) / (num + 1);
  281.                 }
  282.         }
  283.         return d;
  284. }
  285. //ema计算公式开始
  286. double test::ema(string period, string inst, int num)
  287. {
  288.         double d = 0;
  289.         int r = 0;
  290.         map<string, TKVALUE >::iterator it;
  291.         for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); ++it)
  292.         {
  293.                 if (r < num)//当数据不够num个bar时用总和/(bar个数加1)
  294.                 {
  295.                         d=(d+it->second.dClose)/(r+1);
  296.                         r++;
  297.                 }
  298.                 else //当bar的总个数大于num个周期时,ema = a*close + (1-a)*ema[1],a一般为2/(num+1)
  299.                 {
  300.                         d=(2*it->second.dClose+(num-1)*d)/(num+1);
  301.                 }
  302.         }
  303.         return d;
  304. }
  305. //ema计算公式开始(返回数组)
  306. vector<double> test::emaseries(vector<double> pc, int num)
  307. {
  308.         vector<double> r;
  309.         double d = 0;

  310.         for (int i = 0; i < pc.size(); i++)
  311.         {
  312.                 if (i == 0)
  313.                 {
  314.                         d = pc[0];
  315.                         r.push_back(d);
  316.                 }
  317.                 else
  318.                 {
  319.                         d = (2*pc[i]+(num-1)*d)/(num+1);
  320.                         r.push_back(d);
  321.                 }
  322.         }
  323.         return r;
  324. }
  325. //averagearray计算公式开始(返回数组)
  326. vector<double> test::averagearray(vector<double> pc, int num)
  327. {
  328.         vector<double> re;
  329.         int r = 0;
  330.         double d = 0;
  331.         double e = 0;
  332.         for (int i = 0; i < pc.size(); i++)
  333.         {
  334.                 if (r < num) //如果bar的数目不够基本的周期,则返回加总除以bar数目代替
  335.                 {
  336.                         d = (d + pc[i])/(r);
  337.                         r++;
  338.                         re.push_back(d);
  339.                 }
  340.                 else //如何bar数目大于基本周期num个数据,则通过for循环向前加总num个数组除num周期得均线值
  341.                 {
  342.                         for (size_t j = 0; j < num ; j++) //循环num次
  343.                         {
  344.                                 e = e + pc[i - j]; //从现有bar向左加总num个数据
  345.                         }
  346.                         d = e/num;
  347.                         re.push_back(d);
  348.                         e = 0;//将e重置成0方便下一个bar继续循环使用
  349.                 }
  350.         }
  351.         return re;
  352. }
  353. //macd计算公式开始
  354. double test::macd(string period, string inst, int num, int num1, int num2)
  355. {
  356.         double diff = ema(sPeriod, sInst, num) - ema(sPeriod, sInst, num1);//diff = ema(12)-ema(26);
  357.         double dea = ema(sPeriod, sInst, num2);//dea
  358.         return (diff - dea) * 2;
  359. }
  360. //自适应移动平均线(Adaptive Exponential Moving Average,AEMA)返回数值模式
  361. double test::aema(string period, string inst, int num, int pds)//自适应移动平均线(Adaptive Exponential Moving Average, AEMA)
  362. {
  363.         //批量声明计算参数
  364.         int key = 0;
  365.         double j = num + 1;
  366.         double mltp1 = 2 / j;//权重系数
  367.         double mltp2 = 0;
  368.         double rate = 0;
  369.         double aema1 = 0;
  370.         double aema2 = 0;
  371.         double value2 = -99999;
  372.         double value3 = 99999;
  373.         vector<double>aemavector, hs, ls; //声明vector变量容器
  374.         double a0, a1, a2, a3, a4;//声明计算变量
  375.         map<string, TKVALUE>::iterator it;
  376.         for (it = mapK[sPeriod][sInst].begin(); it != mapK[sPeriod][sInst].end(); it++)
  377.         {
  378.                 hs.push_back(it->second.dHigh);//将最高价装入vector中
  379.                 ls.push_back(it->second.dLow); //将最低价装入vector中
  380.                 a0 = it->second.dClose;
  381.                 if (key < num)
  382.                 {
  383.                         aema1 = aema1 + a0;//收盘价总和
  384.                         aema2 = aema1 / length;
  385.                         key++;
  386.                 }
  387.                 else
  388.                 {
  389.                         for (int i = 0; i < pds; i++) //将近10个周期的bar最高值装入vector
  390.                         {
  391.                                 value2 = max2(value2, hs[key - 1 - i]); //遍历最近10个最高价找到最大的那个值放入value2
  392.                                 value3 = min2(value3, ls[key - 1 - i]);
  393.                         }
  394.                         a1 = (a0 - value3);
  395.                         a2 = (value2 - a0);
  396.                         a3 = value2 - value3;
  397.                         a4 = abs(a1 - a2);
  398.                         if (a3 != 0)mltp2 = a4 / a3;
  399.                         rate = (1 + mltp2) * mltp1;
  400.                         aema2 = aema2 + rate * (it->second.dClose - aema2);
  401.                         key++;
  402.                 }
  403.         }
  404.         return aema2;
  405. }
  406. // 求数组容器形式的highest(high, length)
  407. double test::highestseries(vector<double> pc, int num)
  408. {
  409.         double d = 0;
  410.         for (int i = 0; i < pc.size(); i++)
  411.         {
  412.                 if (i >= num)break;
  413.                 d = max2(d, pc[pc.size() - 1 - i]);

  414.         }
  415.         return d;
  416. }
  417. //求数组容器形式的lowest(low,length)
  418. double test::lowestseries(vector<double> pc, int num)
  419. {
  420.         double d1 = 999999;
  421.         for (int i = 0; i < pc.size(); i++)
  422.         {
  423.                 if (i >= num)break;
  424.                 d1 = min2(d1, pc[pc.size() - 1 - i]);
  425.         }
  426.         return d1;
  427. }

  428. //指数平滑移动平均函数(SMA,sma(x,N,M),即x变量的N周期平均,最新值权重为M,即SMAt = SMAt-1 * (N-M)/N + x*M/N )返回数组形式
  429. vector<double> test::smaseries(vector<double> pc, int num,int num2)
  430. {
  431.         vector<double>smav; //声明一个设计变量容器
  432.         for (int i = 0; i < pc.size(); i++)
  433.         {
  434.                 if (i < num)
  435.                 {
  436.                         smav.push_back(pc[i]);
  437.                 }
  438.                 else
  439.                 {
  440.                         smav.push_back( smav[i-1]* (num - num2) /num  + pc[i]/num);
  441.                 }
  442.         }
  443.         return smav;
  444. }
  445. ////////////////////////////////////////////////////////////////////////////////////////////////////////////KDJ公式计算
  446. //******************************************************************公式(function)模块(结束)*******************************************************************/
复制代码
如何访问权限为100/255贴子:/thread-37840-1-1.html;注册后仍无法回复:/thread-23-1-1.html;微信/QQ群:/thread-262-1-1.html;网盘链接失效解决办法:/thread-93307-1-1.html

TOP

返回列表

站长推荐 关闭


鼎元C++程序化软件欢迎试用!

绿色免安装,策略免费下、点击就运行、多品种/策略/账户一键全搞定!小巧简洁、不占空间、不挑配置、38元租云服务器跑一年!


查看