You need to enable JavaScript to run this app. MQL4 EA:已声明字符串变量为何未包含在SendMail邮件正文中
优惠活动
大模型
产品
解决方案
定价
更多

MQL4 EA:已声明字符串变量为何未包含在SendMail邮件正文中

问题现象

开发的MQL4 EA计划在初始化阶段发送携带运行状态的邮件,已在代码中声明全部用到的字符串变量,但邮件正文中所有对应变量的位置都是空值,无实际填充内容。

问题原始代码

int      hourOfDay = Hour();
int      startingHour = 12;
bool     BarTime;
string   eaName = WindowExpertName();
bool     InitOrders;
bool     InitTime;
string   symbol = OrderSymbol();

int OnInit()
{
   /*lastStamp = 0;
   GetMarketInfo_PerSymbol(Symbol(), true);
   fEvents(MODE_INIT);*/ /*Unrelated Code*/
   
   InitOrders = OrdersTotal();
   
   if(InitOrders == 0){
      InitOrders = true;
      Print("EA has been initialised to true");
   }else{
      InitOrders = false;
      Print("EA has been initialised to false");
   }
         
   InitTime = (hourOfDay >= startingHour && hourOfDay < 20);
   
   if(InitTime = true){
      Print("Trades will be placed");
      SendMail("Initialisation confirmation: "+(eaName),"Initialised by: User\n\nEA Name: "+(eaName)+"\n\nAsset: Forex CFD "+(symbol)+"\n\nDate of Initialisation "+TimeToString(TimeCurrent(),TIME_DATE | TIME_MINUTES)+" (This may differ from GMT)\n\nInitTime Status: Trades will be placed\n\nInitOrders: "+(InitOrders == True? "You have no open positions" : "You have an open position"));   
   
   }else{
      InitTime = false;
      Print("Trades will not be placed.");
      MessageBox("Trades will not be placed until "+IntegerToString(startingHour - 2,0)+"AM GMT\n\nIf you feel this is incorrect, please email us at xxx","| Important Notification",MB_OK|MB_ICONINFORMATION|MB_TOPMOST);
      SendMail("Initialisation confirmation: "+(eaName),"Initialised by: User\n\nEA Name: "+(eaName)+"\n\nAsset: Forex CFD "+(symbol)+"\n\nDate of Initialisation "+TimeToString(TimeCurrent(),TIME_DATE | TIME_MINUTES)+" (This may differ from GMT)\n\nInitTime Status: Trades will not be placed\n\nInitOrders: "+(InitOrders == True? "You have no open positions" : "You have an open position"));
   }
   
   BarTime=Time[0];
   
   return(INIT_SUCCEEDED);
}

邮件测试结果显示,所有变量占位位置无实际内容,完全符合空值表现。

问题根源与修正方案

  • 全局变量初始化时机错误导致变量值为空
    代码中string symbol = OrderSymbol()是在全局作用域执行的,此时EA刚完成加载,系统还未关联任何持仓订单,OrderSymbol()会返回空字符串,该变量从始至终都没有有效赋值。修正方式:将该变量移到OnInit函数内部赋值,直接用Symbol()获取EA当前挂载的交易品种即可,不需要依赖订单接口取品种。
  • 全局变量取值时机存在偏差
    代码中int hourOfDay = Hour()、string eaName = WindowExpertName()同样是在全局作用域加载时取值,在回测或实盘加载场景下,该取值可能和EA真正初始化的时刻存在偏差,甚至可能因为EA未完成挂载直接返回空值,需要统一移到OnInit函数内部赋值,保证取到的是初始化执行时刻的有效内容。
  • 逻辑判断误用赋值运算符
    代码中if(InitTime = true)使用了单个等号,属于赋值操作而非相等判断:无论之前计算的InitTime结果是什么,这行代码都会强制把InitTime设为true,导致else分支永远不会执行,逻辑完全偏离预期。修正方式:将判断语句改为if(InitTime == true),更简洁的写法是直接写if(InitTime),符合MQL4的bool类型判断规范。
  • 变量类型混用导致潜在异常
    代码中bool类型的InitOrders变量,先被用来存储OrdersTotal()返回的整型持仓数,后续又被用来存储无持仓的判断布尔值,虽然MQL4的弱类型机制会做隐式转换,但容易触发不可预期的类型冲突。修正方式:拆分变量,单独用int orderCount = OrdersTotal()存持仓数,再用独立bool变量存储判断结果,避免类型混淆。

修正后可正常运行的代码示例

int      startingHour = 12;
bool     BarTime;
string   eaName;
bool     InitOrders;
bool     InitTime;
string   symbol;
int      hourOfDay;

int OnInit()
{
   // 初始化阶段统一赋值,保证取值时机正确
   hourOfDay = Hour();
   eaName = WindowExpertName();
   symbol = Symbol(); // 替代原有的OrderSymbol(),避免全局加载时取空值
   
   int orderCount = OrdersTotal(); // 单独存持仓数,避免类型混用
   
   if(orderCount == 0){
      InitOrders = true;
      Print("EA has been initialised to true");
   }else{
      InitOrders = false;
      Print("EA has been initialised to false");
   }
         
   InitTime = (hourOfDay >= startingHour && hourOfDay < 20);
   
   // 修正赋值为相等判断
   if(InitTime){
      Print("Trades will be placed");
      SendMail("Initialisation confirmation: "+eaName,
               "Initialised by: User\n\nEA Name: "+eaName+
               "\n\nAsset: Forex CFD "+symbol+
               "\n\nDate of Initialisation "+TimeToString(TimeCurrent(),TIME_DATE | TIME_MINUTES)+" (This may differ from GMT)"+
               "\n\nInitTime Status: Trades will be placed"+
               "\n\nInitOrders: "+(InitOrders ? "You have no open positions" : "You have an open position"));   
   
   }else{
      InitTime = false;
      Print("Trades will not be placed.");
      MessageBox("Trades will not be placed until "+IntegerToString(startingHour - 2,0)+"AM GMT\n\nIf you feel this is incorrect, please email us at xxx","| Important Notification",MB_OK|MB_ICONINFORMATION|MB_TOPMOST);
      SendMail("Initialisation confirmation: "+eaName,
               "Initialised by: User\n\nEA Name: "+eaName+
               "\n\nAsset: Forex CFD "+symbol+
               "\n\nDate of Initialisation "+TimeToString(TimeCurrent(),TIME_DATE | TIME_MINUTES)+" (This may differ from GMT)"+
               "\n\nInitTime Status: Trades will not be placed"+
               "\n\nInitOrders: "+(InitOrders ? "You have no open positions" : "You have an open position"));
   }
   
   BarTime = Time[0];
   
   return(INIT_SUCCEEDED);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.28 12:06:04