- 显示:
- 2595
- 等级:
- 已发布:
- 2016.12.22 11:29
-
需要基于此代码的EA交易或指标吗?请在自由职业者服务中订购 进入自由职业者服务
此代码库设计用于发送邮件消息。
函数集可扩展传统的 SendMail。现在, 您可以采用文本和 HTML 格式发送邮件。消息可以发送到若干收件人。您可以将一个或多个文件附加到您的信件里。支持 SSL。
DLL 源代码由 Delphi XE4 编写, 使用了 Indy 代码库。
除了项目文件, 您还应当安装 OpenSSL 代码库。
库函数列表:
函数 | 描述 |
---|---|
MailConnect | 连接邮件服务器 |
MailSendText | 发送文本 |
MailSendHtml | 发送 HTML 消息 |
MailSendInlineTextFile | 发送插入文本文件内容的消息 |
MailSendInlineHtmlFile | 发送插入 HTML 文件内容的消息 |
MailErrorDescription | 返回错误描述 |
MailClose | 关闭邮件服务器连接 |
错误消息:
常量 | 数值 | 描述 |
---|---|---|
MAIL_NO_CONNECTION | -2 | 无连接 |
MAIL_NO_INITIALIZATION | -1 | 无 SMTP 服务器对象 |
MAIL_NO_ERROR | 0 | 无错误 |
MAIL_TIMEOUT | 1 | 因超时关闭连接 |
MAIL_WRONG_LOGIN | 2 | 错误的登录/密码 |
MAIL_WRONG_HOST_NAME | 3 | 错误的主机名 |
MAIL_HOST_NOT_FOUND | 60 | 主机未找到 |
脚本例程:
//+------------------------------------------------------------------+ //| MailSend.mq5 | //| avoitenko | //| https://www.mql5.com/en/users/avoitenko | //+------------------------------------------------------------------+ #property copyright "avoitenko" #property link "https://www.mql5.com/en/users/avoitenko" #property version "1.00" #property script_show_inputs #include <Trade\Trade.mqh> #include <Arrays\ArrayString.mqh> #include <SmtpMailLibrary.mqh> //+------------------------------------------------------------------+ //| ENUM_MESSAGE | //+------------------------------------------------------------------+ enum ENUM_MESSAGE { MESSAGE_TEXT, // 文本 MESSAGE_HTML, // html MESSAGE_INLINE_TEXT, // 内联文本 MESSAGE_INLINE_HTML // 内联 html }; //+------------------------------------------------------------------+ //| 输入参数 | //+------------------------------------------------------------------+ input string mail_options="=== Mail Options ==="; // 邮件选项 input string InpMailHost="smtp.gmail.com"; // 主机 input int InpMailPort=465; // 端口 input string InpMailUser="user@gmail.com"; // 用户 input string InpMailPassword="password"; // 密码 input string InpMailFrom=""; // 发自 (文本) input string InpMailSubject="Smtp Mail Library"; // 标题 (文本) input string InpMailTo="user@ukr.net"; // 发至 input uint InpMailConnectionTimeout=5000; // 连接超时, 毫秒 input string msg_options="=== Message Options ==="; // 消息选项 input ENUM_MESSAGE InpMessageType=MESSAGE_HTML; // 类型 input string InpMessageAttachmentFiles="d:\\Temp\\dollar.bmp;d:\\Temp\\euro.bmp";// 附件文件 input string InpMessageInlineFiles="d:\\Temp\\ReportTester-20066082.html";// 内联文件 //+------------------------------------------------------------------+ //| 脚本程序开始函数 | //+------------------------------------------------------------------+ void OnStart() { long smtp=0;//initialize //--- 连接 int err=MailConnect(smtp,InpMailHost,InpMailPort,InpMailUser,InpMailPassword,InpMailConnectionTimeout); if(err!=0) { Print("MailConnect 错误: ",MailErrorDescription(err)); return; } switch(InpMessageType) { //--- case MESSAGE_TEXT: { //--- 纯文本 string text=StringFormat("账户: %d\r\n余额: %.2f %s",AccountInfoInteger(ACCOUNT_LOGIN),AccountInfoDouble(ACCOUNT_BALANCE),AccountInfoString(ACCOUNT_CURRENCY)); //--- 将邮件发给自己 err=MailSendText(smtp,InpMailUser,InpMailFrom,InpMailSubject,text,InpMessageAttachmentFiles); if(err!=0) Print("MailSendText 错误: ",MailErrorDescription(err)); else PrintFormat("程序 '%s' 已发邮件至 '%s'",MQLInfoString(MQL_PROGRAM_NAME),InpMailUser); //--- 将邮件发送到 MailTo err=MailSendText(smtp,InpMailTo,InpMailFrom,InpMailSubject,text,InpMessageAttachmentFiles); if(err!=0) Print("MailSendText 错误: ",MailErrorDescription(err)); else PrintFormat("程序 '%s' 已发送邮件至 '%s'",MQLInfoString(MQL_PROGRAM_NAME),InpMailTo); } break; //--- case MESSAGE_HTML: { //--- 构建 html string html=BuildReport(); //--- 发送 html err=MailSendHtml(smtp,InpMailTo,InpMailFrom,InpMailSubject,html,""); if(err!=0) Print("MailSendText 错误: ",MailErrorDescription(err)); else PrintFormat("程序 '%s' 已发送邮件至 '%s'",MQLInfoString(MQL_PROGRAM_NAME),InpMailTo); } break; //--- case MESSAGE_INLINE_TEXT: { err=MailSendInlineTextFile(smtp,InpMailTo,InpMailFrom,InpMailSubject,InpMessageInlineFiles); if(err!=0) Print("MailSendText 错误: ",MailErrorDescription(err)); else PrintFormat("程序 '%s' 已发送邮件至 '%s'",MQLInfoString(MQL_PROGRAM_NAME),InpMailTo); } break; //--- case MESSAGE_INLINE_HTML: { err=MailSendInlineHtmlFile(smtp,InpMailTo,InpMailFrom,InpMailSubject,InpMessageInlineFiles); if(err!=0) Print("MailSendText 错误: ",MailErrorDescription(err)); else PrintFormat("程序 '%s' 已发送邮件至 '%s'",MQLInfoString(MQL_PROGRAM_NAME),InpMailTo); } break; } //--- 关闭连接 MailClose(smtp); } //+------------------------------------------------------------------+ //| BuildReport | //+------------------------------------------------------------------+ string BuildReport() { CArrayString html; //--- html.Add("<html><head><title>报告</title>"); html.Add("<meta name=\"format-detection\" content=\"telephone=no\">"); html.Add("<style type=\"text/css\" media=\"screen\">"); html.Add("<!--"); html.Add("td{font: 8pt Tahoma,Arial;}"); html.Add("//-->"); html.Add("</style>"); html.Add("<style type=\"text/css\" media=\"print\">"); html.Add("<!--"); html.Add("td{font: 7pt Tahoma,Arial;}"); html.Add("//-->"); html.Add("</style>"); html.Add("</head>"); html.Add("<body topmargin=1 marginheight=1> <font face=\"tahoma,arial\" size=1>"); html.Add("<div align=center>"); html.Add(StringFormat("<div style=\"font: 18pt Times New Roman\"><b>%s</b></div>",AccountInfoString(ACCOUNT_SERVER))); html.Add("<table cellspacing=1 cellpadding=3 border=0>"); //--- html.Add("<tr>"); html.Add(StringFormat("<td colspan=2>账号: <b>%d</b></td>",AccountInfoInteger(ACCOUNT_LOGIN))); html.Add(StringFormat("<td colspan=6>客户名: <b>%s</b></td>",AccountInfoString(ACCOUNT_NAME))); html.Add("<td colspan=2> </td>"); html.Add(StringFormat("<td colspan=2 align=right>%s</td>",TimeToString(TimeCurrent()))); html.Add("</tr>"); //--- html.Add("<tr>"); html.Add("<td colspan=13><b>已开交易:</b></td>"); html.Add("</tr>"); //--- html.Add("<tr align=center bgcolor=\"#C0C0C0\">"); html.Add("<td>单号</td>"); html.Add("<td nowrap>开单时间</td>"); html.Add("<td>类型</td>"); html.Add("<td>手数</td>"); html.Add("<td>品名</td>"); html.Add("<td>价格</td>"); html.Add("<td>止损</td>"); html.Add("<td>止盈</td>"); html.Add("<td>价格</td>"); html.Add("<td nowrap>佣金</td>"); html.Add("<td>掉期利率</td>"); html.Add("<td>盈利</td>"); html.Add("</tr>"); //--- double profit=0.0; int total=PositionsTotal(); if(total == 0) { html.Add("<tr align=right><td colspan=13 nowrap align=center>无业务</td></tr>"); } else { CPositionInfo m_position; for(int i=0; i<total; i++) { m_position.SelectByIndex(i); //--- 颜色 if((i&1)==0)html.Add("<tr align=right>"); else html.Add("<tr bgcolor=#E0E0E0 align=right>"); //--- 数据 html.Add(StringFormat("<td>%d</td>",m_position.Identifier())); html.Add(StringFormat("<td> %s</td>",TimeToString(m_position.Time()))); if(m_position.PositionType()==POSITION_TYPE_BUY) html.Add("<td>买入</td>"); else html.Add("<td>卖出</td>"); html.Add(StringFormat("<td>%.2f</td>",m_position.Volume())); html.Add(StringFormat("<td>%s</td>",m_position.Symbol())); html.Add(StringFormat("<td>%.5f</td>",m_position.PriceOpen())); html.Add(StringFormat("<td>%.5f</td>",m_position.StopLoss())); html.Add(StringFormat("<td>%.5f</td>",m_position.TakeProfit())); html.Add(StringFormat("<td>%.5f</td>",m_position.PriceCurrent())); html.Add(StringFormat("<td>%.2f</td>",m_position.Commission())); html.Add(StringFormat("<td>%.2f</td>",m_position.Swap())); html.Add(StringFormat("<td>%.2f</td>",m_position.Profit())); html.Add("</tr>"); //--- profit+=m_position.Profit(); } } //--- html.Add("<tr>"); html.Add(StringFormat("<td colspan=2><b>余额: %.2f</b></td>",AccountInfoDouble(ACCOUNT_BALANCE))); html.Add(StringFormat("<td colspan=5><b>净值: %.2f</b></td>",AccountInfoDouble(ACCOUNT_EQUITY))); html.Add("<td colspan=3><b>浮动盈亏:</b></td>"); html.Add(StringFormat("<td colspan=4 align=right><b>%.2f</b></td>",profit)); html.Add("</tr>"); //--- html.Add("</table></div></font></body></html>"); //--- 保存为单独字符串 string result=""; total=html.Total(); for(int i=0;i<total;i++) result+=html.At(i); //--- done return(result); } //+------------------------------------------------------------------+
结果:
由MetaQuotes Ltd译自俄语
原代码: https://www.mql5.com/ru/code/11466

以字符串为键实现行哈希数组的示例。

就像周期分隔符, 每个新交易日将在开始处获得垂线。有若干个如何显示的选项

此智能交易可以运行于所有品种和时间帧, 使用两条线性均线的交叉作为信号, 且用一条指数均线作为尾随停止。

基于增量之字折线的持仓跟踪模块, 适用于 MQL5 向导。