//+------------------------------------------------------------------+ //| Demo_HistoryDealSelectByIndex.mq5 | //| Copyright 2010, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2010, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property version "1.00" input long my_magic=111; //+------------------------------------------------------------------+ //| 脚本程序起始函数 | //+------------------------------------------------------------------+ void OnStart() { // --- 需要交易历史的时间间隔 datetime end=TimeCurrent(); // 当前服务器时间 datetime start=end-PeriodSeconds(PERIOD_D1);// 减去一天 //--- 对交易历史的请求需要在MQL5程序的缓存中读取 HistorySelect(start,end); //--- 获得历史中的总交易数 int deals=HistoryDealsTotal(); int returns=0; double profit=0; double loss=0; //--- 处理历史中所有的交易 for(int i=0;i0) // 交易已经被选择,我们来处理它 { string symbol=HistoryDealGetString(deal_ticket,DEAL_SYMBOL); datetime time=HistoryDealGetInteger(deal_ticket,DEAL_TIME); ulong order=HistoryDealGetInteger(deal_ticket,DEAL_ORDER); long order_magic=HistoryDealGetInteger(deal_ticket,DEAL_MAGIC); long pos_ID=HistoryDealGetInteger(deal_ticket,DEAL_POSITION_ID); ENUM_DEAL_ENTRY entry_type=(ENUM_DEAL_ENTRY)HistoryDealGetInteger(deal_ticket,DEAL_ENTRY); //--- 使用指定的DEAL_MAGIC继续处理交易 if(order_magic==my_magic) { //... 使用 DEAL_MAGIC 进行处理 } //--- 让我们计算亏损和利润 if(entry_type==DEAL_ENTRY_OUT) { //--- 增加交易数目 returns++; //--- 交易结果 double result=HistoryDealGetDouble(deal_ticket,DEAL_PROFIT); //--- 把利润累加 if(result>0) profit+=result; //--- 把亏损累加 if(result<0) loss+=result; } } else // 选择交易出错 { PrintFormat("根据索引 %d 选择交易出错. 错误 %d", i,GetLastError()); } } //--- 打印结果 PrintFormat("共有 %d 个交易,财务结果为利润=%.2f , 亏损= %.2f", returns,profit,loss); } //+------------------------------------------------------------------+