Sending MT4 trading notifications to Telegram with bot

 

Hello,

I have configured a Telegram Bot in order to send my trades notifications from MT4 to my Telegram channel through an Expert advisor but I am having issue which is my bot in Telegram is sending the same message multiple times until I cut the EA.

I tried to modify the variable msg.updater in another file Telegram.mqh but still having the problem. So I have no idea where can I fix it.

I attached the Telegram.mqh file and below is my core file .mql4 for EA :

#include <Telegram.mqh>


input string InpChannelName="@xxx";//Channel Name
input string InpToken="1049045330:AxxxxxxbFxxx0-IxwKXyxxxxxxxx";//Bot Token


CCustomBot bot;

datetime time_signal=0;
//int SendMessage(const string channel_name,
                //const string text);




//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   time_signal=0;

//--- set token
   bot.Token(InpToken);


//--- done
   return(INIT_SUCCEEDED);
  }
 
  datetime _opened_last_time = TimeCurrent() ;
  datetime _closed_last_time = TimeCurrent()  ;
  
 

//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)

{

}
     
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  

   string message = "";
   int total=OrdersTotal();
  
        
   for(int pos=0;pos<total;pos++){  // Current orders -----------------------
     if(OrderSelect(pos,SELECT_BY_POS)==false) continue;
     if(OrderOpenTime() <= _opened_last_time) continue;
     
     message += StringFormat("Order opened!\r\nType: %s\r\nSymbol: %s\r\nPrice: %s\r\nSL: %s\r\nTP: %s\r\nTime: %s\r\nTicket:.%s ",
     order_type(),
     OrderSymbol(),
     DoubleToStr(OrderOpenPrice(),MarketInfo(OrderSymbol(),MODE_DIGITS)),
     DoubleToStr(OrderStopLoss(),MarketInfo(OrderSymbol(),MODE_DIGITS)),
     DoubleToStr(OrderTakeProfit(),MarketInfo(OrderSymbol(),MODE_DIGITS)),
     TimeToStr(OrderOpenTime(),TIME_MINUTES),
     IntegerToString(OrderTicket())
          
      );
      
     int res=bot.SendMessage(InpChannelName,message);
     if(res!=0)
         Print("Error: ",GetErrorDescription(res));
     
     }
     
      bool is_closed = false;

  
  total = OrdersHistoryTotal();
      
   for(int pos=0;pos<total;pos++){  // History orders-----------------------
      if(OrderSelect(pos,SELECT_BY_POS,MODE_HISTORY)==false) continue;
      if(OrderCloseTime() <= _closed_last_time) continue;
     printf(OrderCloseTime());
     is_closed = true;
     
     message += StringFormat("Order closed!\r\nTicket: %s\r\nSymbol: %s\r\nClosing Price: %s\r\nTime: %s",
     IntegerToString(OrderTicket()),
     OrderSymbol(),
     DoubleToStr(OrderClosePrice(),MarketInfo(OrderSymbol(),MODE_DIGITS)),
     TimeToStr(OrderCloseTime(),TIME_MINUTES),
     DoubleToStr(order_pips(),1)
     
     
     );
      
      int res=bot.SendMessage(InpChannelName,message);
     if(res!=0)
         Print("Error: ",GetErrorDescription(res));
      
     }
 
   }
   
   
double order_pips() {
   double pips;
   
   if(OrderType() == OP_BUY) {
      pips =  (OrderClosePrice()-OrderOpenPrice())/MarketInfo(OrderSymbol(),MODE_POINT);
   } else {
      pips =  (OrderOpenPrice()-OrderClosePrice())/MarketInfo(OrderSymbol(),MODE_POINT);
   }
   return pips/10;
}

string order_type_to_str(int type)
{
   return StringSubstr(EnumToString((ENUM_ORDER_TYPE)type), 11);
}
string order_type () {
   return order_type_to_str(OrderType());
   
   if(OrderType() == OP_BUY)        return "BUY";
   if(OrderType() == OP_SELL)       return "SELL";
   if(OrderType() == OP_BUYLIMIT)   return "BUY LIMIT";
   if(OrderType() == OP_SELLLIMIT)  return "SELL LIMIT";
   if(OrderType() == OP_BUYSTOP)    return "BUYSTOP";
   if(OrderType() == OP_SELLSTOP)   return "SELLSTOP";
   
   return "{err}";
}
   

   
//---


Thank you in advance for your help.

Files:
Telegram.mqh  64 kb
 

Hi, how can I put Telegram.mqh and the .mql4 files both together in order to have the bot functioning

Thank you in advance

Reason: