This article contains step-by-step instructions for creating bots for Telegram in MQL5. This information may prove useful for users who wish to synchronize their trading robot with a mobile device. There are samples of bots in the article that provide trading signals, search for information on websites, send information about the account balance, quotes and screenshots of charts to you smart phone.
安德烈,您需要替换文章中的附加代码吗?在这种情况下,请写信给版主进行替换,好吗?
是的,拉希德,你需要用代码更新存档。几天前,我曾就此写信给版主。
你好,安德烈-沃伊滕科!感谢您的文章,但我无法以下面的方式接收和输出 Telegram 的警报信息(每次都返回空警报,即检测到新信息,但返回的是空变量):
{
for(int i=0; i<m_chats.Total(); i++)
{
CCustomChat *chat=m_chats.GetNodeAtIndex(i);
//--- 如果信息未被处理
if(!chat.m_new_one.done)
{
chat.m_new_one.done=true;
string text=chat.m_new_one.message_text;
Alert(text);
//--- 开始
if(text=="/start")
SendMessage(chat.m_id,"Hello, world! I am bot. \xF680");
//--- 帮助
if(text=="/help")
SendMessage(chat.m_id,"My commands list: \n/start-start chatting with me \n/help-get help");
}
}
}
我试着给自己做了一个机器人,当出现交易时向电报发送一张截图。
不知何故,每隔一段时间就会发送一次,你能看看问题出在哪里吗?
只有这个文件,它在附件中。
罗曼,如果你只需要机器人发送截图,你可以这样做:
1.向 @MyTelegramID_bot 询问您的聊天号码。
2.2. 编写一个简单的机器人,检查是否有新项目,并向指定 ID 的聊天室发送图片。例如
//+------------------------------------------------------------------+
//| 输入参数|
//+------------------------------------------------------------------+
input ENUM_LANGUAGES InpLanguage=LANGUAGE_EN;//语言
input string InpToken="";//代币
input long ChatId=0; //聊天 ID
//---
CCustomBot bot;
int pos_count;
//+------------------------------------------------------------------+
int OnInit()
{
bot.Token(InpToken);
int res=bot.GetMe();
if(res!=0)
{
Print(GetErrorDescription(res));
}
pos_count=PositionCount(_Symbol);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
void OnTick()
{
int pos_count_new=PositionCount(_Symbol);
if(pos_count_new>pos_count)
{
pos_count=pos_count_new;
int result=SendScreenShot(ChatId,_Symbol,0,NULL);
if(result!=0)
Print(GetErrorDescription(result,InpLanguage));
}
}
//+------------------------------------------------------------------+
int PositionCount(const string _symbol)
{
int count=0;
int orders_total=OrdersTotal();
for(int i=0; i<orders_total; i++)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
PrintError(ERR_ORDER_SELECT);
return(-1);
}
//---
if(_symbol==NULL || OrderSymbol()==_symbol)
count++;
}
//---
return(count);
}
//+------------------------------------------------------------------+
int SendScreenShot(const long _chat_id,
const string _symbol,
const ENUM_TIMEFRAMES _period,
const string _template=NULL)
{
int result=0;
long chart_id=ChartOpen(_symbol,_period);
if(chart_id==0)
return(ERR_CHART_NOT_FOUND);
ChartSetInteger(ChartID(),CHART_BRING_TO_TOP,true);
//--- 更新图表
int wait=60;
while(--wait>0)
{
if(SeriesInfoInteger(_symbol,_period,SERIES_SYNCHRONIZED))
break;
Sleep(500);
}
if(_template!=NULL)
if(!ChartApplyTemplate(chart_id,_template))
PrintError(_LastError,InpLanguage);
ChartRedraw(chart_id);
Sleep(500);
ChartSetInteger(chart_id,CHART_SHOW_GRID,false);
ChartSetInteger(chart_id,CHART_SHOW_PERIOD_SEP,false);
string filename=StringFormat("%s%d.gif",_symbol,_period);
if(FileIsExist(filename))
FileDelete(filename);
ChartRedraw(chart_id);
Sleep(100);
// if(ChartScreenShot(chart_id,filename,800,600,ALIGN_RIGHT))
if(ChartScreenShot(chart_id,filename,1024,768,ALIGN_RIGHT))
{
Sleep(100);
bot.SendChatAction(_chat_id,ACTION_UPLOAD_PHOTO);
//--- 等待 30 秒以保存屏幕截图
wait=60;
while(!FileIsExist(filename) && --wait>0)
Sleep(500);
//---
string screen_id;
result=bot.SendPhoto(_chat_id,filename,screen_id,_symbol+"_"+StringSubstr(EnumToString(_period),7));
}
ChartClose(chart_id);
return(result);
}
//+------------------------------------------------------------------+
感谢您提供此教程、
我想为 mt4 制作一个电报机器人,请提供相关教程。
谢谢
https:// www.mql5.com/en/articles/2355
里面有逐步说明...
您可以试一试,如果还不行,就在这里发帖,我们可以一起寻找解决方案。
你好、
首先感谢您的详细描述,非常感谢。
请原谅这听起来很傻,但我想试试 Telegram_signal_EA,希望能通过向 Telegram 频道发送指标来触发警报,但您在第三个示例中给出的代码是为 MT4 还是 MT5 编写的?

当我将代码复制并粘贴到 MetaEditor 中的一个新文件并编译时,我得到了一大堆错误,我完全不知道这意味着什么。当然,我应该花时间弄清楚这一切,但使用您给出的代码,实现我想要的目标应该不会太难,对吗?
如果有人愿意帮助我或为我指明正确的方向,我将非常感激。
致以最崇高的敬意,
Patrick。