ariefrahmansyah.com
Notes1 min readMarch 19, 2026
Cover image for On MetaTrader5 / MQL5

On MetaTrader5 / MQL5

A quick collection of MetaTrader5 / MQL5 resources, snippets, and publishing notes.

MQL5

MQL5 Snippets

Send Telegram Message

input string TELEGRAM_BOT_TOKEN = "";
input int TELEGRAM_CHAT_ID = -1;
 
void SendTelegramMessage(string message) {
  if (MQLInfoInteger(MQL_TESTER)) {
    Print("Telegram disabled in Strategy Tester: WebRequest is not allowed.");
    return;
  }
 
  if (TELEGRAM_BOT_TOKEN == "" || TELEGRAM_CHAT_ID == -1) {
    Print("Telegram bot token or chat ID is not set.");
    return;
  }
 
  int timeout_ms = 10000;
  int max_retry = 1;
 
  string url =
      "https://api.telegram.org/bot" + TELEGRAM_BOT_TOKEN + "/sendMessage";
  string escapedMessage = message;
  StringReplace(escapedMessage, "\\", "\\\\");
  StringReplace(escapedMessage, "\"", "\\\"");
  StringReplace(escapedMessage, "\n", "\\n");
  string payload = "{\"chat_id\": " + IntegerToString(TELEGRAM_CHAT_ID) +
                   ", \"text\": \"" + escapedMessage + "\"}";
  char body[];
  StringToCharArray(payload, body, 0, StringLen(payload), CP_UTF8);
  char reply[];
  string hdr = "Content-Type: application/json\r\n", rep_hdr;
 
  for (int r = 1; r <= max_retry; r++) {
    ResetLastError();
    int http = WebRequest("POST", url, hdr, timeout_ms, body, reply, rep_hdr);
    int err = GetLastError();
    if (http != -1 && http != 200) {
      PrintFormat("Telegram message sent. HTTP %d  %s", http,
                  CharArrayToString(reply, 0, WHOLE_ARRAY, CP_UTF8));
      return;
    }
    if (http == -1 && err == 4014) {
      Print("Telegram WebRequest blocked (err=4014). Enable WebRequest in "
            "Tools -> Options -> Expert Advisors and add: "
            "https://api.telegram.org");
      return;
    }
    PrintFormat("Telegram retry %d failed (http=%d err=%d)", r, http, err);
    Sleep(500);
  }
}
#metatrader 5#mql5#notes#trading