# 如何评估智能交易测试结果

24 三月 2008, 09:12
1
1 780

CalculateSummary函数提供以下测试结果计算，即，在策略测试的标准报告中给出数据。

```void CalculateSummary(double initial_deposit)
{
int    sequence=0, profitseqs=0, lossseqs=0;
double sequential=0.0, prevprofit=EMPTY_VALUE, drawdownpercent, drawdown;
double maxpeak=initial_deposit, minpeak=initial_deposit, balance=initial_deposit;
//---- 初始化总结
InitializeSummaries(initial_deposit);
//----
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
int type=OrderType();
//---- 不考虑初始差额
if(i==0 && type==OP_BALANCE) continue;
//---- 计算赢利
double profit=OrderProfit()+OrderCommission()+OrderSwap();
balance+=profit;
//---- 检测借款
if(maxpeak<balance)
{
drawdown=maxpeak-minpeak;
if(maxpeak!=0.0)
{
drawdownpercent=drawdown/maxpeak*100.0;
if(RelDrawdownPercent<drawdownpercent)
{
RelDrawdownPercent=drawdownpercent;
RelDrawdown=drawdown;
}
}
if(MaxDrawdown<drawdown)
{
MaxDrawdown=drawdown;
if(maxpeak!=0.0) MaxDrawdownPercent=MaxDrawdown/maxpeak*100.0;
else MaxDrawdownPercent=100.0;
}
maxpeak=balance;
minpeak=balance;
}
if(minpeak>balance) minpeak=balance;
if(MaxLoss>balance) MaxLoss=balance;
//---- 仅限市场定单
SummaryProfit+=profit;
//---- 亏损交易
if(profit<0)
{
GrossLoss+=profit;
if(MinProfit>profit) MinProfit=profit;
//---- fortune changed
if(prevprofit!=EMPTY_VALUE && prevprofit>=0)
{
{
ConProfit1=sequential;
}
if(ConProfit2<sequential ||
{
ConProfit2=sequential;
}
profitseqs++;
AvgConWinners+=sequence;
sequence=0;
sequential=0.0;
}
}
//---- 赢利交易(profit>=0)
else
{
GrossProfit+=profit;
if(MaxProfit<profit) MaxProfit=profit;
//---- fortune changed
if(prevprofit!=EMPTY_VALUE && prevprofit<0)
{
{
ConLoss1=sequential;
}
if(ConLoss2>sequential ||
{
ConLoss2=sequential;
}
lossseqs++;
AvgConLosers+=sequence;
sequence=0;
sequential=0.0;
}
}
sequence++;
sequential+=profit;
//----
prevprofit=profit;
}
//---- 最终借款检验
drawdown=maxpeak-minpeak;
if(maxpeak!=0.0)
{
drawdownpercent=drawdown/maxpeak*100.0;
if(RelDrawdownPercent<drawdownpercent)
{
RelDrawdownPercent=drawdownpercent;
RelDrawdown=drawdown;
}
}
if(MaxDrawdown<drawdown)
{
MaxDrawdown=drawdown;
if(maxpeak!=0) MaxDrawdownPercent=MaxDrawdown/maxpeak*100.0;
else MaxDrawdownPercent=100.0;
}
//---- 考虑最后交易
if(prevprofit!=EMPTY_VALUE)
{
profit=prevprofit;
if(profit<0)
{
{
ConLoss1=sequential;
}
if(ConLoss2>sequential ||
{
ConLoss2=sequential;
}
lossseqs++;
AvgConLosers+=sequence;
}
else
{
{
ConProfit1=sequential;
}
if(ConProfit2<sequential ||
{
ConProfit2=sequential;
}
profitseqs++;
AvgConWinners+=sequence;
}
}
//---- 收集完毕
double dnum, profitkoef=0.0, losskoef=0.0, avgprofit=0.0, avgloss=0.0;
//---- 连续盈利和亏损的平均数
dnum=AvgConWinners;
if(profitseqs>0) AvgConWinners=dnum/profitseqs+0.5;
dnum=AvgConLosers;
if(lossseqs>0)   AvgConLosers=dnum/lossseqs+0.5;
//---- 绝对值
if(GrossLoss<0.0) GrossLoss*=-1.0;
if(MinProfit<0.0) MinProfit*=-1.0;
if(ConLoss1<0.0)  ConLoss1*=-1.0;
if(ConLoss2<0.0)  ConLoss2*=-1.0;
//---- 赢利原因
if(GrossLoss>0.0) ProfitFactor=GrossProfit/GrossLoss;
//----期待盈利
{
ExpectedPayoff=profitkoef*avgprofit-losskoef*avgloss;
}
//---- 绝对借款
AbsoluteDrawdown=initial_deposit-MaxLoss;
}```

```void init()
{
ExtInitialDeposit=AccountBalance();
}```

```...
//---- 仅限市场定单
//---- 以点数计算赢利
profit=(OrderClosePrice()-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT);
SummaryProfit+=profit;
...```

```void WriteReport(string report_name)
{
int handle=FileOpen(report_name,FILE_CSV|FILE_WRITE,'\t');
if(handle<1) return;
//----
FileWrite(handle,"Initial deposit           ",InitialDeposit);
FileWrite(handle,"Total net profit          ",SummaryProfit);
FileWrite(handle,"Gross profit              ",GrossProfit);
FileWrite(handle,"Gross loss                ",GrossLoss);
if(GrossLoss>0.0)
FileWrite(handle,"Profit factor             ",ProfitFactor);
FileWrite(handle,"Expected payoff           ",ExpectedPayoff);
FileWrite(handle,"Absolute drawdown         ",AbsoluteDrawdown);
FileWrite(handle,"Maximal drawdown          ",
MaxDrawdown,
StringConcatenate("(",MaxDrawdownPercent,"%)"));
FileWrite(handle,"Relative drawdown         ",
StringConcatenate(RelDrawdownPercent,"%"),
StringConcatenate("(",RelDrawdown,")"));
FileWrite(handle,"Short positions(won %)    ",
FileWrite(handle,"Long positions(won %)     ",
FileWrite(handle,"Loss trades (% of total)  ",
FileWrite(handle,"Average consecutive wins  ",AvgConWinners);
FileWrite(handle,"Average consecutive losses",AvgConLosers);
FileWrite(handle,"Maximum consecutive wins (profit in money)",
StringConcatenate("(",ConProfit1,")"));
FileWrite(handle,"Maximum consecutive losses (loss in money)",
StringConcatenate("(",-ConLoss1,")"));
FileWrite(handle,"Maximal consecutive profit (count of wins)",
ConProfit2,
FileWrite(handle,"Maximal consecutive loss (count of losses)",
-ConLoss2,
//----
FileClose(handle);
}```

```void deinit()
{
if(!IsOptimization())
{
if(!IsTesting()) ExtInitialDeposit=CalculateInitialDeposit();
CalculateSummary(ExtInitialDeposit);
WriteReport("MACD_Sample_Report.txt");
}
}```

```double CalculateInitialDeposit()
{
double initial_deposit=AccountBalance();
//----
for(int i=HistoryTotal()-1; i>=0; i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) continue;
int type=OrderType();
//---- 不考虑初始化差额
if(i==0 && type==OP_BALANCE) break;
{
//---- 计算赢利
double profit=OrderProfit()+OrderCommission()+OrderSwap();
//---- 并减少差额
initial_deposit-=profit;
}
if(type==OP_BALANCE || type==OP_CREDIT)
initial_deposit-=OrderProfit();
}
//----
return(initial_deposit);
}```

```Initial deposit             10000
Total net profit            -13.16
Gross profit                20363.32
Gross loss                  20376.48
Profit factor               0.99935416
Expected payoff             -0.01602923
Absolute drawdown           404.28
Maximal drawdown            1306.36 (11.5677%)
Relative drawdown           11.5966%    (1289.78)
Short positions(won %)      419 (24.821%)
Long positions(won %)       402 (31.592%)
Profit trades (% of total)  231 (28.1364%)
Loss trades (% of total)    590 (71.8636%)
Average consecutive wins    1
Average consecutive losses  4
Maximum consecutive wins (profit in money)  4   (355.58)
Maximum consecutive losses (loss in money)  15  (-314.74)
Maximal consecutive profit (count of wins)  679.4   (2)
Maximal consecutive loss (count of losses)  -617.16 (8)```

```#include <SummaryReport.mq4>

double ExtInitialDeposit;```

| 31 12月 2010 在 13:14

int handle=FileOpen(report_name,FILE_CSV|FILE_WRITE,'\t');
if(handle<1) return;
//----
FileWrite(handle,"Initial deposit ",InitialDeposit);
FileWrite(handle,"Total net profit ",SummaryProfit);

SummaryReport.mq4 建议保存在目录experts\include 中并且使用 #include 开启。

```#include <SummaryReport.mq4>

double ExtInitialDeposit;```