鼎元C++期货量化/程序化教程【highest、lowest的计算方法及调用方法】
第一部分,在头文件test.h中声明highest、lowest变量:[code]double highest(string period, string inst, int num);
double lowerest(string period, string inst, int num);
[/code]第二部分,在源文件test.cpp中设计计算公式:[code]
double test::highest(string period, string inst, int num)
{
double d = 0;
int n = 0;
map<string, TKVALUE>::reverse_iterator it;
for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
{
if (d < it->second.dHigh)d = it->second.dHigh;
n++;
if (n >= num)break;
}
return d;
}
[/code][code]double test::lowerest(string period, string inst, int num)
{
double d = 0;
int n = 0;
map<string, TKVALUE>::reverse_iterator it;
for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
{
if (d == 0)d = it->second.dLow;
if (d > it->second.dLow)d = it->second.dLow;
n++;
if (n >= num)break;
}
return d;
}[/code] 优化与升级:之前的highest与lowest都是仅对于最高价和最低价的比较与引用,今天升级与优化一下,可以回溯与调用(open,high,low,close四个价格中的任一个来做最高和最低的调用)
lowest优化版:
一、头文件中声明[code]double lowest2(string period, string inst, int bartypt, int num);[/code]二、源文件中的公式计算部分:[code]//最低价lowest计算公式开始,bartype(1:open ,2high,3low,4close)
double test::lowest2(string period, string inst, int bartype, int num)
{
double d = 0;
int n = 0;
int bart =3;
map<string, TKVALUE>::reverse_iterator it;
for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
{
if (bartype == 1) bart = it->second.dOpen;
if (bartype == 2) bart = it->second.dHigh;
if (bartype == 3) bart = it->second.dLow;
if (bartype == 4) bart = it->second.dClose;
if (d == 0)d = bart;
if (d > bart)d = bart;
n++;
if (n >= num)break;
}
return d;
}
//最低价lowest计算公式结束,bartype(1:open ,2high,3low,4close)[/code]三、说明:[code]lowest2(string period, string inst, int bartype, int num)[/code]1、period:在交易界面设计的运行合约的周期;
2、inst:交易界面设计的运行的品种合约;
3、bartype:调用的bar的要素,1是open价,2是最高价,3是最低价,4是收盘价。
4、num:想回溯的周期。
范例:[code]RsqBar(sPeriod, sInst);
lowest2(sPeriod, sInst, 1,2); [/code]调用前2bar的开盘价中最低的那个值。类似MC中的[code]lowest(open,2)[/code][code]RsqBar(sPeriod, sInst);
lowest2(sPeriod, sInst, 3,5); [/code]调用前5个bar中最低价中最低的那个价格。类似MC中的[code] lowest(low,5)[/code] highest优化版:
一、头文件声明:[code]double highest2(string period, string inst, int bartypt, int num); //最高价highest函数变量[/code]二、源文件计算公式:[code]//最高价highest计算公式开始,bartype(1:open ,2high,3low,4close)
double test::highest2(string period, string inst, int bartype, int num)
{
double d = 0;
int n = 0;
int bart = 2;
map<string, TKVALUE>::reverse_iterator it;
for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
{
if (bartype == 1) bart = it->second.dOpen;
if (bartype == 2) bart = it->second.dHigh;
if (bartype == 3) bart = it->second.dLow;
if (bartype == 4) bart = it->second.dClose;
if (d == 0)d = bart;
if (d < bart)d = bart;
n++;
if (n >= num)break;
}
return d;
}
//最高价highest计算公式结束,bartype(1:open ,2high,3low,4close)[/code]三、调用范例:[code]RsqBar(sPeriod, sInst);
highest2(sPeriod, sInst, 1,2);[/code]调用前2bar的开盘价中最高的那个值。类似MC中的[code]highest(open,2)[/code] 今天在做策略时比方说做突破策略时上面的就会出现一直实现不了,因为获取最高价和最低价时比较的价格永久不会大于最高或最低。所以我们需要做一下处理,就是当前bar的数据跟上一根bar的高低价,类似在MC中做突破时要用highest(high,N)[1]或lowest(low,N)[1] 这样才能成功实现我们的策略思路。
下面我将更新如何获取上一根bar的最高或最低以用来做策略。
[b]上一bar最高:[/b]
[b]一、头文件声明变量:[/b][code]
double highest1(string period, string inst, int num, int ref);//回溯前N个bar的最高价
[/code][b]二、源文件计算公式:[/b][code]
//回溯上N个bar的最高价开始
double test::highest1(string period, string inst, int num, int ref)
{
double d = 0;
int n = 0;
int r = 0;
map<string, TKVALUE>::reverse_iterator it;
for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
{
r++;
if (r <= ref)continue;
if (d < it->second.dHigh)d = it->second.dHigh;
n++;
if (n >= num)break;
}
return d;
}
//回溯上N个bar的最高价结束
[/code][b]上一bar最低:[/b]
[b]一、头文件声明变量:[/b][code]
double lowest1(string period, string inst, int num,int ref);回溯前N个bar的最低价
[/code][b]二、源文件计算公式函数:[/b][code]
//回溯上N个bar的最低价开始
double test::lowest1(string period, string inst, int num, int ref)
{
double d = 0;
int n = 0;
int r = 0;
map<string, TKVALUE>::reverse_iterator it;
for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
{
r++;
if (r <= ref)continue;
if (d == 0)d = it->second.dLow;
if (d > it->second.dLow)d = it->second.dLow;
n++;
if (n >= num)break;
}
return d;
}
//回溯上N个bar的最低价结束[/code][b]三、调用方法[/b][code]
rangeh = highest1(sPeriod, sInst, jxzq,1); //N周期区间最高价
rangel = lowest1(sPeriod, sInst, jxzq,1); //N周期区间最低价
[/code]
页:
[1]