策略详细代码如下
Tb旗舰版WriteMacd
在Tb旗舰版编写WriteMacd
Params
Numeric FastLength(12);
Numeric SlowLength(26);
Numeric MACDLength(9);
Vars
// 计算MACD参数
NumericSeries MACDValue;
Numeric AvgMACD;
Numeric MACDDiff;
// 保存MACD参数
String strSection;
string strkey;
string strValue;
// 统计运行时间参数
Numeric beginTime;
Numeric EndTime;
Numeric costTime;
Begin
// 计算MACD
MACDValue = XAverage( Close, FastLength ) - XAverage( Close, SlowLength ) ;
AvgMACD = XAverage(MACDValue,MACDLength);
MACDDiff = MACDValue - AvgMACD;
PlotNumeric("MACD",MACDValue);
PlotNumeric("MACDAvg",AvgMACD);
If (MACDDiff >= 0)
PlotNumeric("MACDDiff",MACDDiff,0,Red);
Else
PlotNumeric("MACDDiff",MACDDiff,0,Green);
PlotNumeric("零线",0);
// 保存MACD
strKey = DateToString(Date) + " " + TimeToString(Time);
strSection = "MACD_" + Symbol();
strValue = Text(MACDDiff);
SetTBProfileString(strSection,strKey,strValue); //指标保存数据库
commentary( "******已输出******");
// 统计运行时间
If( BarStatus==0) //第一根K线,开始时间
{
SetGlobalVar2("beginTime",CurrentTime);
}
If( BarStatus==2) //最后一根K线,结束时间
{
beginTime = GetGlobalVar2("beginTime");
EndTime = CurrentTime;
costTime = TimeDiff(beginTime,EndTime);
commentary("beginTime="+ Text(beginTime) + " EndTime="+ Text(EndTime) +" costTime="+ Text(costTime) +"秒");
}
End
Tb旗舰版ReadMacd
在Tb旗舰版编写ReadMacd
Params
Vars
// 读取MACD参数
String strSection;
string strkey;
string strValue;
NumericSeries MACDDiff;
// 交易参数
Bool Condition1;
Bool Condition2;
// 统计运行时间参数
Numeric beginTime;
Numeric EndTime;
Numeric costTime;
Begin
//读取MACD_
strKey = DateToString(Date) + " " + TimeToString(Time);
strSection = "MACD_" + Symbol();
strValue = GetTBProfileString(strSection,strKey);
commentary("strKey:"+ strKey + " strSection:"+ strSection + " strValue:"+ strValue);
MACDDiff = Value(strValue);
//根据MACD开平仓
Condition1 = MACDDiff[1] > 10;
Condition2 = MACDDiff[1] <-10;
if (Condition1)
{
Buy(1,Open);
}
if (Condition2)
{
SellShort(1,Open);
}
// 统计运行时间
If( BarStatus==0) //第一根K线,开始时间
{
SetGlobalVar2("beginTime",CurrentTime);
}
If( BarStatus==2) //最后一根K线,结束时间
{
beginTime = GetGlobalVar2("beginTime");
EndTime = CurrentTime;
costTime = TimeDiff(beginTime,EndTime);
commentary("beginTime="+ Text(beginTime) + " EndTime="+ Text(EndTime) +" costTime="+ Text(costTime) +"秒");
}
End
TBQuant版WriteMacd
导入老版策略至TbQuant版本,做适当语法修改。由begin-end,改为Event,onBar()函数体。
Params
Numeric FastLength(12);
Numeric SlowLength(26);
Numeric MACDLength(9);
Vars
// 计算MACD参数
Series<Numeric> MACDValue;
Numeric AvgMACD;
Numeric MACDDiff;
// 保存MACD参数
String strSection;
string strkey;
string strValue;
// 统计运行时间参数
Numeric beginTime;
Numeric EndTime;
Numeric costTime;
Events
//此处实现事件函数
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer> indexs)
{
// 计算MACD
MACDValue = XAverage( Close, FastLength ) - XAverage( Close, SlowLength ) ;
AvgMACD = XAverage(MACDValue,MACDLength);
MACDDiff = MACDValue - AvgMACD;
PlotNumeric("MACD",MACDValue);
PlotNumeric("MACDAvg",AvgMACD);
If (MACDDiff >= 0)
PlotNumeric("MACDDiff",MACDDiff,0,Red);
Else
PlotNumeric("MACDDiff",MACDDiff,0,Green);
PlotNumeric("零线",0);
// 保存MACD
strKey = DateToString(Date) + " " + TimeToString(Time);
strSection = "MACD_" + Symbol();
strValue = Text(MACDDiff);
SetTBProfileString(strSection,strKey,strValue); //指标保存数据库
commentary( "******已输出******");
// 统计运行时间
If( BarStatus==0) //第一根K线,开始时间
{
SetGlobalVar2("beginTime",CurrentTime);
}
If( BarStatus==2) //最后一根K线,结束时间
{
beginTime = GetGlobalVar2("beginTime");
EndTime = CurrentTime;
costTime = TimeDiff(beginTime,EndTime);
commentary("beginTime="+ Text(beginTime) + " EndTime="+ Text(EndTime) +" costTime="+ Text(costTime) +"秒");
}
}
TBQuant版ReadMacd
Params
Vars
// 读取MACD参数
String strSection;
string strkey;
string strValue;
Series<Numeric> MACDDiff;
// 交易参数
Bool Condition1;
Bool Condition2;
// 统计运行时间参数
Numeric beginTime;
Numeric EndTime;
Numeric costTime;
Events
//此处实现事件函数
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer> indexs)
{
//读取MACD_
strKey = DateToString(Date) + " " + TimeToString(Time);
strSection = "MACD_" + Symbol();
strValue = GetTBProfileString(strSection,strKey);
commentary("strKey:"+ strKey + " strSection:"+ strSection + " strValue:"+ strValue);
MACDDiff = Value(strValue);
//根据MACD开平仓
Condition1 = MACDDiff[1] > 10;
Condition2 = MACDDiff[1] <-10;
if (Condition1)
{
Buy(1,Open);
}
if (Condition2)
{
SellShort(1,Open);
}
// 统计运行时间
If( BarStatus==0) //第一根K线,开始时间
{
SetGlobalVar2("beginTime",CurrentTime);
}
If( BarStatus==2) //最后一根K线,结束时间
{
beginTime = GetGlobalVar2("beginTime");
EndTime = CurrentTime;
costTime = TimeDiff(beginTime,EndTime);
commentary("beginTime="+ Text(beginTime) + " EndTime="+ Text(EndTime) +" costTime="+ Text(costTime) +"秒");
}
}
回复:您好!根据您提供的代码和测试方法,我们做了对比分析,测试结果和您的结果有一些出入,但不影响大致结论。
我们测试下来,旗舰版读写都是3秒左右。TBQ写数据库较慢,大概要57-59秒,读数据库则很快,大概也是2-3秒。为什么写入比较慢这个问题,我们会继续跟研发反馈,去查找原因。
先讲如何解决这个问题。TBQ中其实只是兼容了旗舰版的读写数据库的功能,但实际功能更强大的是通过基础数据来记录和读取,不但方便,速度也很快。我实际测试下来,读写都小于3秒。
附上TBQ中的代码,读写基础数据部分,我都注释了。您的部分代码,我做了些许修改,是为了更好地观察测试结果。
//------------------------------------------------------------------------
// 简称: q8673_1
// 名称: 社区问题8673-写MACD
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
Numeric FastLength(12);
Numeric SlowLength(26);
Numeric MACDLength(9);
Vars
// 计算MACD参数
Series<Numeric> MACDValue;
Numeric AvgMACD;
Numeric MACDDiff;
// 保存MACD参数
String strSection;
// 读写数据库方法
////////////////////////////////
//string strkey;
//string strValue;
///////////////////////////////
// 保存MACD,读写基础数据方法
Dic<Numeric> dMacd("MACD-M15");
// 统计运行时间参数
Numeric beginTime;
Numeric nEndTime;
Numeric costTime;
Events
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer> indexs)
{
// 统计运行时间
If( BarStatus==0) //第一根K线,开始时间
{
SetGlobalVar2("beginTime",CurrentTime);
SetGlobalVar2("flag", 0);
}
// 计算MACD
MACDValue = XAverage( Close, FastLength ) - XAverage( Close, SlowLength ) ;
AvgMACD = XAverage(MACDValue,MACDLength);
MACDDiff = MACDValue - AvgMACD;
PlotNumeric("MACD",MACDValue);
PlotNumeric("MACDAvg",AvgMACD);
If (MACDDiff >= 0)
PlotNumeric("MACDDiff",MACDDiff,0,Red);
Else
PlotNumeric("MACDDiff",MACDDiff,0,Green);
PlotNumeric("零线",0);
// 保存MACD
strSection = "MACD_" + Symbol();
// 读写数据库方法
///////////////////////////////////////////////////////////////////////
//strKey = DateToString(Date) + " " + TimeToString(Time);
//strValue = Text(MACDDiff);
//SetTBProfileString(strSection,strKey,strValue); //指标保存数据库
///////////////////////////////////////////////////////////////////////
// 保存MACD,读写基础数据方法
dMACD[0] = MACDDiff;
commentary( "******已输出******");
If( BarStatus==2 And GetGlobalVar2("flag")==0 ) //最后一根K线,结束时间
{
beginTime = GetGlobalVar2("beginTime");
nEndTime = CurrentTime;
SetGlobalVar2("nEndTime",CurrentTime);
costTime = TimeDiff(beginTime, nEndTime);
SetTBProfileString(strSection,"WriteCostTime",Text(costTime)); //用时保存到数据库
SetGlobalVar2("flag", 1);
}
commentary("beginTime="+ Text(GetGlobalVar2("beginTime")) + " EndTime="+ Text(GetGlobalVar2("nEndTime"))
+" costTime="+ GetTBProfileString(strSection,"WriteCostTime")+"秒");
}
//------------------------------------------------------------------------
// 简称: q8673_2
// 名称: 社区问题8673-读MACD
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
Vars
// 读取MACD参数
String strSection;
Series<Numeric> MACDDiff;
// 读写数据库方法
////////////////////////////////
//string strkey;
//string strValue;
///////////////////////////////
// 读写基础数据方法
Dic<Numeric> dMacd("MACD-M15");
// 交易参数
Bool Condition1;
Bool Condition2;
// 统计运行时间参数
Numeric beginTime;
Numeric nEndTime;
Numeric costTime;
Events
//此处实现事件函数
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer> indexs)
{
// 统计运行时间
If( BarStatus==0) //第一根K线,开始时间
{
SetGlobalVar2("beginTime",CurrentTime);
SetGlobalVar2("flag", 0);
}
//读取MACD
strSection = "MACD_" + Symbol();
// 读写数据库方法
///////////////////////////////////////////////////////////////////////
//strKey = DateToString(Date) + " " + TimeToString(Time);
//strValue = GetTBProfileString(strSection,strKey);
//commentary("strKey:"+ strKey + " strSection:"+ strSection + " strValue:"+ strValue);
//MACDDiff = Value(strValue);
///////////////////////////////////////////////////////////////////////
//读取MACD,读写基础数据方法
MACDDiff = dMacd[0];
//根据MACD开平仓
Condition1 = MACDDiff[1] > 10;
Condition2 = MACDDiff[1] <-10;
if (Condition1)
{
Buy(1,Open);
}
if (Condition2)
{
SellShort(1,Open);
}
If( BarStatus==2 And GetGlobalVar2("flag")==0 ) //最后一根K线,统计用时
{
beginTime = GetGlobalVar2("beginTime");
nEndTime = CurrentTime;
SetGlobalVar2("nEndTime", CurrentTime);
costTime = TimeDiff(beginTime, nEndTime);
SetTBProfileString(strSection,"ReadCostTime",Text(costTime)); //用时保存到数据库
SetGlobalVar2("flag", 1);
}
commentary("beginTime="+ Text(GetGlobalVar2("beginTime")) + " EndTime="+ Text(GetGlobalVar2("nEndTime"))
+" costTime="+ GetTBProfileString(strSection,"ReadCostTime")+"秒");
}
谢谢您的反馈!希望能解决您的问题。
回复:感谢这么快反馈,感谢指导。
我去试一试,不懂再请教你们。