//--- 定义
#define BALANCE_LOSS_DEPOSIT 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; // 交易账户类实例
...
double balance_dep_summ; //结余充值的总金额
uint balance_dep_total; // 结余充值次数
//+------------------------------------------------------------------+
//| EA交易初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
...
//--- 保存初始结余值
balance_prev=account.Balance();
balance_dep_summ=0;
balance_dep_total=0;
//--- 成功初始化
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- 更新当前报价
if(!symb.RefreshRates())
return;
...
//--- 如果结余下降幅度超过BALANCE_LOSS_DEPOSIT宏替换中指定的值,
//--- 则需要充值账户并调用TesterDeposit()函数
//--- 检查结余损失是否超过BALANCE_LOSS_DEPOSIT
if(balance_prev!=account.Balance())
{
if(account.Balance()<balance_prev-BALANCE_LOSS_DEPOSIT)
{
double loss=balance_prev-account.Balance();
PrintFormat("The initial balance of %.2f %s decreased by %.2f %s. It is necessary to make a deposit to the account for %.2f %s.",balance_prev,account.Currency(),loss,account.Currency(),loss,account.Currency());
if(TesterDeposit(loss))
{
balance_dep_total++;
balance_dep_summ+=loss;
balance_prev=account.Balance();
PrintFormat("Funds have been deposited into the account. Account balance: %.2f %s.",account.Balance(),account.Currency());
PrintFormat("Total deposits: %lu. Amount of deposits: %.2f %s.",balance_dep_total,balance_dep_summ,account.Currency());
}
/*
结果:
The initial balance of 10000.00 USD decreased by 116.00 USD. It is necessary to make a deposit to the account for 116.00 USD.
deal #45 balance 116.00 [deposit] done
Funds have been deposited into the account. Account balance: 10000.00 USD.
Total deposits: 1. Amount of deposits: 116.00 USD.
*/
}
}
}
//+------------------------------------------------------------------+
//| 测试函数 |
//+------------------------------------------------------------------+
double OnTester()
{
//--- 将以货币形式表示的最大结余回撤设置为输出处理程序值
double ret=TesterStatistics(STAT_BALANCE_DD);
//--- 在日志中显示有关回撤、存款次数及其总金额的消息
PrintFormat("%s: Maximum balance drawdown in money: %.2f %s. Total deposits: %lu. Amount of deposits: %.2f %s.",__FUNCTION__,ret,account.Currency(),balance_dep_total,balance_dep_summ,account.Currency());
//--- 返回结果
return(ret);
/*
结果:
OnTester: Maximum balance drawdown in money: 5188.50 USD. Total deposits: 46. Amount of deposits: 5128.50 USD.
final balance 4867.50 USD
OnTester result 5188.5
*/
}
|