//--- 定义
#define BALANCE_PROFIT_WITHDRAWAL 5 // 结余盈利值,在该值时资金从测试器的账户中提取
//--- 输入参数
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_op_sum; // 结余操作的总额
uint balance_op_total; // 结余操作次数
//+------------------------------------------------------------------+
//| EA交易初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
...
//--- 保存初始结余值
balance_prev=account.Balance();
balance_op_sum=0;
balance_op_total=0;
//--- 成功初始化
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- 更新当前报价
if(!symb.RefreshRates())
return;
...
//--- 如果结余盈利超过在BALANCE_PROFIT_WITHDRAWAL宏替换中指定的当前结余值,
//--- 则需要从账户中提取这些资金。调用TesterWithdrawal()函数。
//--- 检查结余盈利是否超过BALANCE_PROFIT_WITHDRAWAL
if(balance_prev!=account.Balance())
{
if(account.Balance()>balance_prev+BALANCE_PROFIT_WITHDRAWAL)
{
double profit=account.Balance()-balance_prev;
PrintFormat("The account balance has been increased by %.2f %s. Need to withdraw these funds from the account.",profit,account.Currency());
if(TesterWithdrawal(profit))
{
balance_op_total++;
balance_op_summ+=profit;
balance_prev=account.Balance();
PrintFormat("Funds have been withdrawn from the account. Account balance: %.2f %s.",account.Balance(),account.Currency());
PrintFormat("Total withdrawals: %lu. Amount of withdrawals: %.2f %s.",balance_op_total,balance_op_summ,account.Currency());
}
/*
结果:
The account balance has been increased by 21.00 USD. Need to withdraw these funds from the account.
deal #13 balance -21.00 [withdrawal] done
Funds have been withdrawn from the account. Account balance: 10000.00 USD.
Total withdrawals: 1. Amount of withdrawals: 21.00 USD.
*/
}
}
}
//+------------------------------------------------------------------+
//| 测试函数 |
//+------------------------------------------------------------------+
double OnTester()
{
//--- 将以货币形式表示的最大结余回撤设置为输出处理程序值
double ret=TesterStatistics(STAT_BALANCE_DD);
//--- 在日志中显示有关回撤、提款次数及其总金额的消息
PrintFormat("%s: Maximum balance drawdown in money: %.2f %s. Total withdrawals: %lu. Amount of withdrawals: %.2f %s.",__FUNCTION__,ret,account.Currency(),balance_op_total,balance_op_summ,account.Currency());
//--- 返回结果
return(ret);
/*
结果:
OnTester: Maximum balance drawdown in money: 5188.50 USD. Total withdrawals: 2. Amount of withdrawals: 36.00 USD.
final balance 4867.50 USD
OnTester result 5188.5
*/
}
|