//--- 定义
#define BALANCE_LOSS_STOP 100.0 // 结余回撤值,在该值停止测试
#define EQUITY_LOSS_STOP 100.0 // 净值回撤值,在该值停止测试
//--- 输入参数
input double InpLots = 0.1; // 手数
input uint InpStopLoss = 50; // 止损点数
input uint InpTakeProfit = 150; // 止盈点数
sinput ulong InpMagic = 123; // 幻数
sinput ulong InpDeviation = 5; // 偏差
//--- 全局变量
CTrade trade; // 交易类实例
CSymbolInfo symb; // 交易品种类实例
CAccountInfo account; // 交易账户类实例
...
//+------------------------------------------------------------------+
//| EA交易初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
...
//--- 成功初始化
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- 更新当前报价
if(!symb.RefreshRates())
return;
...
//--- 如果余额或净值下降幅度超过BALANCE_LOSS_STOP和EQUITY_LOSS_STOP宏替换中指定的值,
//--- 则测试被认为不成功,并且调用TesterStop()函数
//--- 检查结余损失是否超过BALANCE_LOSS_STOP
if(balance_prev!=account.Balance())
{
if(account.Balance()<balance_prev-BALANCE_LOSS_STOP)
{
PrintFormat("The initial balance of %.2f %s decreased by %.2f %s, and now has a value of %.2f %s. Stop testing.",balance_prev,account.Currency(),balance_prev-account.Balance(),account.Currency(),account.Balance(),account.Currency());
TesterStop();
/*
结果:
The initial balance of 10000.00 USD decreased by 100.10 USD, and now has a value of 9899.90 USD. Stop testing.
TesterStop() called on 9% of testing interval
*/
}
}
//--- 检查净值损失是否超过EQUITY_LOSS_STOP
if(equity_prev!=account.Equity())
{
if(account.Equity()<equity_prev-EQUITY_LOSS_STOP)
{
PrintFormat("The initial equity of %.2f %s decreased by %.2f %s, and now has a value of %.2f %s. Stop testing.",equity_prev,account.Currency(),equity_prev-account.Equity(),account.Currency(),account.Equity(),account.Currency());
TesterStop();
/*
结果:
The initial equity of 10000.00 USD decreased by 100.10 USD, and now has a value of 9899.90 USD. Stop testing.
TesterStop() called on 9% of testing interval
*/
}
}
}
|