总体方法
用相同策略,分别在Tb旗舰版和TBQuant上运行,对比完成时间长短。
一、测试目的:如何改善TBQuant的运行效率。
二、两个策略WriteMacd、ReadMacd。
WriteMacd指标计算策略,负责计算MACD并写入本地数据库。
ReadMacd交易策略,负责读取本地数据库的MACD,并进行开平仓。
三、环境
Tb旗舰版V6.0.3.9
TBQuant V1.3.5.3
计算机:windows10操作系统,内存16G,处理器4核3GHz
四、对比测试内容
加载RB000, 15M,时间20160701至20220701
测试以下内容:
WriteMacd策略在Tb旗舰版图表运行、TBQuant图表运行、TBQuant无图表运行。
ReadMacd策略在Tb旗舰版图表运行、TBQuant图表运行、TBQuant无图表运行。
旗舰版默认是异步写入的,quant的下个把也默认改成异步。
收到。我们做个对比分析再给您回复。谢谢!
已收到,我们研究一下
不过这也许跟旧的函数移植有关
统计结果
策略 |
Tb旗舰版 |
TBQuant图表 |
TBQuant无图表 |
WriteMacd |
3秒 |
119秒 |
116秒 |
ReadMacd |
3秒 |
13秒 |
13秒 |
1、写数据库的操作,相差巨大的,TBQuant花费时间增加为40倍。
2、读数据,相差略小, TBQuant花费时间增加为4倍,还可以接受。
3、TBQuant的图表和非图表,在这个策略上,并没有显著差异。
展望和分析
在真正交易中,会运行很多策略,很多品种,配置环境会复杂很多,系统的资源消耗也会增加很多。目前的时间花费,是无法实战的。
解决的途径可能如下,请技术人员指导:
1、 用其它方法,替换本地数据库的使用。比如用数据中心的自定义合约、用基础数据等高效率的替换。
2、 改善SetTBProfileString、GetTBProfileString的执行效率
3、 其它
我们对平台的理解有限,请需要技术人员帮助想办法。
开始测试
4.ReadMacd策略在Tb旗舰版图表运行
7.数据库保存的内容格式
策略详细代码如下
在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
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版本,做适当语法修改。由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) +"秒");
}
}
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")+"秒");
}
谢谢您的反馈!希望能解决您的问题。
感谢这么快反馈,感谢指导。
我去试一试,不懂再请教你们。