You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MQL4中按货币对统计交易历史的盈利及交易次数?

MQL4 统计交易历史中各货币对的交易次数与盈利

实现思路

  1. 定义结构体存储每个货币对的交易次数和总盈利
  2. 遍历所有已平仓的历史订单,按货币对分组累加统计数据
  3. 将统计结果按指定格式输出到专家日志和图表界面

完整代码(脚本示例)

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
    string currencyPairs[];
    CurrencyStats stats[];
    int statsCount = 0;

    // 遍历所有历史订单
    for(int i = 0; i < HistoryOrdersTotal(); i++)
    {
        if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) continue;
        
        // 跳过未平仓的挂单
        if(OrderCloseTime() == 0) continue;
        
        string pair = OrderSymbol();
        double profit = OrderProfit(); // 包含手续费、隔夜利息的净利润

        // 查找当前货币对是否已在统计列表中
        int index = -1;
        for(int j = 0; j < statsCount; j++)
        {
            if(currencyPairs[j] == pair)
            {
                index = j;
                break;
            }
        }

        if(index == -1)
        {
            // 新增货币对统计项
            ArrayResize(currencyPairs, statsCount + 1);
            ArrayResize(stats, statsCount + 1);
            currencyPairs[statsCount] = pair;
            stats[statsCount].count = 1;
            stats[statsCount].totalProfit = profit;
            statsCount++;
        }
        else
        {
            // 更新已有货币对的统计数据
            stats[index].count++;
            stats[index].totalProfit += profit;
        }
    }

    // 输出到专家日志
    Print("=== 货币对交易统计结果 ===");
    // 输出到图表界面
    for(int i = 0; i < statsCount; i++)
    {
        string outputStr = StringFormat("%s : Number Position = %d , Profit = %.2f $", 
                                        currencyPairs[i], 
                                        stats[i].count, 
                                        stats[i].totalProfit);
        Print(outputStr);

        // 在图表左上角创建标签显示结果,每个标签间隔20像素
        string labelName = "Stats_Label_" + IntegerToString(i);
        ObjectCreate(0, labelName, OBJ_LABEL, 0, 0, 0);
        ObjectSetString(0, labelName, OBJPROP_TEXT, outputStr);
        ObjectSetInteger(0, labelName, OBJPROP_CORNER, CORNER_LEFT_UPPER);
        ObjectSetInteger(0, labelName, OBJPROP_YDISTANCE, i * 20);
        ObjectSetInteger(0, labelName, OBJPROP_FONTSIZE, 10);
    }
}

//+------------------------------------------------------------------+
//| 统计数据结构体定义                                               |
//+------------------------------------------------------------------+
struct CurrencyStats {
    int count;          // 交易次数
    double totalProfit; // 总盈利(美元)
};
//+------------------------------------------------------------------+

关键函数说明

  • HistoryOrdersTotal():获取账户历史订单的总数量
  • OrderSelect():选中指定位置的历史订单,MODE_HISTORY参数指定操作历史订单
  • OrderSymbol():获取当前选中订单对应的货币对代码
  • OrderProfit():返回订单的净利润(已包含手续费、隔夜利息等费用)
  • StringFormat():按指定格式拼接字符串,匹配需求的输出样式
  • ObjectCreate():在MT4图表上创建文本标签,用于界面展示统计结果

注意事项

  • 运行脚本前,确保MT4已开启"允许自动交易"权限(工具->选项->EA交易)
  • 脚本仅统计已平仓的订单,未成交的挂单会被自动跳过
  • 若历史订单数量较多,遍历过程可能需要几秒时间,建议在非交易时段运行

内容的提问来源于stack exchange,提问作者Mohammad Hasanpoor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 18:15:59