// 基于标准"MACD Sample.mq5"文件的EA
// 在Tester事件处理程序中显示TesterStatistics()的结果
#define MACD_MAGIC 1234502
//---
#include <Trade\Trade.mqh>
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\AccountInfo.mqh>
//---
input double InpLots =0.1; // 手数
input int InpTakeProfit =50; // 盈利(点数)
input int InpTrailingStop =30; // 追踪止损(点数)
input int InpMACDOpenLevel =3; // MACD开盘价(点数)
input int InpMACDCloseLevel=2; // MACD收盘价(点数)
input int InpMATrendPeriod =26; // MA趋势线周期
//+------------------------------------------------------------------+
//| EA交易初始化函数 |
//+------------------------------------------------------------------+
int OnInit(void)
{
//--- 创建所有必要的对象
if(!ExtExpert.Init())
return(INIT_FAILED);
//--- 成功初始化
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert new tick handling function |
//+------------------------------------------------------------------+
void OnTick(void)
{
static datetime limit_time=0; // 考虑'timeout'的最后一次调用时间
//--- 如果时间超过指定的limit_time值
if(TimeCurrent()>=limit_time)
{
//--- 检查数据
if(Bars(Symbol(),Period())>2*InpMATrendPeriod)
{
//--- 如果成功,将limit_time增加'timeout'秒
if(ExtExpert.Processing())
limit_time=TimeCurrent()+ExtTimeOut;
}
}
}
//+------------------------------------------------------------------+
//| Expert tester handling function |
//+------------------------------------------------------------------+
double OnTester(void)
{
double ret=TesterStatistics(STAT_PROFIT_FACTOR);
double profit=TesterStatistics(STAT_PROFIT);
int trades_total=(int)TesterStatistics(STAT_TRADES);
int profit_total=(int)TesterStatistics(STAT_PROFIT_TRADES);
int loss_total=(int)TesterStatistics(STAT_LOSS_TRADES);
PrintFormat("%s: Profit = %.2f, trades total: %lu, profit trades total: %lu, loss trades total: %lu, profit factor: %.2f",__FUNCTION__,profit,trades_total,profit_total,loss_total,ret);
return(ret);
/*
结果:
OnTester: Profit = 209.84, trades total: 13, profit trades total: 11, loss trades total: 2, profit factor: 3.02
final balance 10209.84 USD
OnTester result 3.020606644198363
*/
}
|