文章 "如何采用 MQL5 创建用于 Telegram 的 bots" - 页 7

 

你好,安德烈、

为什么机器人总是出错?HTTP 请求失败,tq

 

你好,安德烈、

为什么机器人总是出错?HTTP 请求失败,tq


 

也许他们更改了协议。

检查过 Telegram API 吗?

 
Haijie Qi:

你好,安德烈、

为什么机器人总是出错?HTTP 请求失败,tq

我使用 TeamViewer 在您的电脑上调试了 "Telegram_Bot_EA",发现 WebRequest() 返回-1。_LastError = 5203。

但我不知道为什么。也许您的防火墙阻止了 TCP 数据包。

 
Andrey Voytenko:

我使用 TeamViewer 在您的电脑上调试了 "Telegram_Bot_EA",发现 WebRequest() 返回-1。_LastError = 5203。

但我不知道为什么。也许您的防火墙阻止了 TCP 数据包。


谢谢你,安德烈、

关闭防火墙后仍然出错、

我会再深入研究一下:)

 

嘿,各位,基本上我已经获取了源代码,并基本上编辑了 telegram_signal_EA 的代码,以便根据我的策略发送交易。因此,我编译检查 了错误,一切正常,甚至当我将 EA 应用到图表时,专家选项卡也显示它识别了我的电报机器人。现在我的问题在于发送警报本身,当我的交易规则得到满足时,我没有收到发送到电报的警报。你们能帮我解决这个问题吗?下面是代码

//+------------------------------------------------------------------+
//|Telegram_Signal_EA.mq5
//| 2014 年 MetaQuotes 软件公司版权所有。|
//|http://www.mql5.com | |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict

#include <Telegram.mqh>

//--- 输入参数
input string InpChannelName="ForexSignalChannel";//Channel Name
input string InpToken="";//代币

//--- 全局变量
CCustomBot bot;
int ema_handle;
int ema2_handle;
int rsi_handle;
datetime time_signal=0;
bool checked;
//+------------------------------------------------------------------+
//| 专家初始化函数|
//+------------------------------------------------------------------+
int OnInit()
  {
   time_signal=0;

   bot.Token(InpToken);

#ifdef __MQL5__   
   ema_handle=iMA(NULL,0,200,0,MODE_EMA,PRICE_CLOSE,0);
   if(ema_handle==INVALID_HANDLE)
      return(INIT_FAILED);
      
   ema2_handle=iMA(NULL,0,400,0,MODE_EMA,PRICE_CLOSE,0);
   if(ema2_handle==INVALID_HANDLE)
      return(INIT_FAILED);
      
   rsi_handle=iRSI(NULL,0, 14,PRICE_CLOSE,0);
   if(ema2_handle==INVALID_HANDLE)
      return(INIT_FAILED);
//--- 将指标添加到图表中
   int total=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
   ChartIndicatorAdd(0,total,macd_handle);
#endif   

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 专家去初始化函数|
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   if(reason==REASON_PARAMETERS ||
      reason==REASON_RECOMPILE ||
      reason==REASON_ACCOUNT)
     {
      checked=false;
     }

//--- 删除指标
#ifdef __MQL5__ 
   int total=(int)ChartGetInteger(0,CHART_WINDOWS_TOTAL);
   for(int subwin=total-1; subwin>=0; subwin--)
     {
      int amount=ChartIndicatorsTotal(0,subwin);
      for(int i=amount-1; i>=0; i--)
        {
         string name=ChartIndicatorName(0,subwin,i);
         if(StringFind(name,"MACD",0)==0)
            ChartIndicatorDelete(0,subwin,name);
        }
     }
#endif
  }
//+------------------------------------------------------------------+
//||
//+------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   if(id==CHARTEVENT_KEYDOWN && 
      lparam=='Q')
     {
         
         bot.SendMessage(InpChannelName,"ee\nAt:100\nDDDD");
     }
  }
//+------------------------------------------------------------------+
//| 专家勾选功能|
//+------------------------------------------------------------------+
void OnTick()
  {

   if(!checked)
     {
      if(StringLen(InpChannelName)==0)
        {
         Print("Error: Channel name is empty");
         Sleep(10000);
         return;
        }

      int result=bot.GetMe();
      if(result==0)
        {
         Print("Bot name: ",bot.Name());
         checked=true;
        }
      else
        {
         Print("Error: ",GetErrorDescription(result));
         Sleep(10000);
         return;
        }
     }

//--- 获取时间
   datetime time[1];
   if(CopyTime(NULL,0,0,1,time)!=1)
      return;

//--- 检查每个条形图上的信号
   if(time_signal!=time[0])
     {
      //--- 第一次计算
      if(time_signal==0)
        {
         time_signal=time[0];
         return;
        }

     double ema = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, 0);
      double ema2 = iMA(NULL, 0, 400, 0, MODE_EMA, PRICE_CLOSE, 0); 
      double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);


      time_signal=time[0];

      //--- 发送信号 BUY
      if ((Bid > ema)&&(Bid < ema2)&&(rsi < 30))
      
        {
         string msg=StringFormat("Name: BossPips Signal\xF4E3\nSymbol: %s\nTimeframe: %s\nType: Buy\nPrice: %s\nTime: %s",
                                 _Symbol,
                                 StringSubstr(EnumToString((ENUM_TIMEFRAMES)_Period),7),
                                 DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits),
                                 TimeToString(time[0]));
         int res=bot.SendMessage(InpChannelName,msg);
         if(res!=0)
            Print("Error: ",GetErrorDescription(res));
        }

      //--- 发送卖出信号
      
       if ((Bid < ema)&&(Bid > ema2)&&(rsi > 70))
       
        {
         string msg=StringFormat("Name: BossPips Signal\xF4E3\nSymbol: %s\nTimeframe: %s\nType: Sell\nPrice: %s\nTime: %s",
                                 _Symbol,
                                 StringSubstr(EnumToString((ENUM_TIMEFRAMES)_Period),7),
                                 DoubleToString(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits),
                                 TimeToString(time[0]));
         int res=bot.SendMessage(InpChannelName,msg);
         if(res!=0)
            Print("Error: ",GetErrorDescription(res));
        }
     }
  }
//+------------------------------------------------------------------+
 
有 mq4 的版本吗?
 
Mohd Fahmi Azlan Mohamad Zuki:
有适用于 mq4 的版本吗?

本文所附代码适用于 MetaTrader 4 和 MetaTrader 5 这两个平台的交易。

只需将 EA 文件扩展名改为 mq4 即可。

 

我将信号 ea 上的 for(int i=0;i<=1;i++) 改为 for(int i=0;i>=1;i--)、

现在我没有收到任何电报信号,请帮助/指导我;(

 
Andrey Voytenko:

本文所附代码适用于在 MetaTrader 4 和 MetaTrader 5 这两个平台上进行交易。

只需将 EA 文件扩展名改为 mq4 即可。

我试图更改。但当我下单时,我没有收到任何通知。我需要检查 什么吗?