Need help with custom Expert Advisor that should send price alerts to telegram.

 
I have been using AI to create custom expert advisor for my needs, I have provided code below for the custom EA that should send alerts to my telegram channel. There is no error in compilation. 
This ea sends initial message that "Alert system initiated" to my telegram which means that the API and webmail setup to send message to telegram is configured properly. But once my condition is met in manual checkup, it won't send any of the further messages which are described in " Check and send alert for trading condition" section of the code. Can you please check the cause of the problem and if possible, please correct the code section which might me causing this issue.



input string TELEGRAM_BOT_TOKEN = "Your_Telegram_Bot_Token";
input string TELEGRAM_CHAT_ID = "Your_Telegram_Chat_ID";

//+------------------------------------------------------------------+
//| URL encode a string                                              |
//+------------------------------------------------------------------+
string UrlEncode(string str) {
    string result = "";
    for (int i = 0; i < StringLen(str); i++) {
        ushort ch = StringGetCharacter(str, i);
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') ||
            (ch >= '0' && ch <= '9') || ch == '-' || ch == '_' ||
            ch == '.' || ch == '~') {
            result += CharToString(ch);
        } else {
            result += "%" + StringFormat("%02X", int(ch)); // Explicitly cast ushort to int
        }
    }
    return result;
}

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit() {
    double initialPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    string testMessage = "Signal system initiated, current price is " + DoubleToString(initialPrice, _Digits);
    SendTelegramAlert(testMessage);
    return INIT_SUCCEEDED;
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick() {
    static double previousFastEMA = 0, previousMediumEMA = 0, previousSlowEMA = 0; 
    double fastEMA = iMA(_Symbol, PERIOD_M1, 8, 0, MODE_EMA, PRICE_CLOSE);
    double mediumEMA = iMA(_Symbol, PERIOD_M1, 16, 0, MODE_EMA, PRICE_CLOSE);
    double slowEMA = iMA(_Symbol, PERIOD_M1, 690, 0, MODE_EMA, PRICE_CLOSE);

    CheckTradeConditions(fastEMA, mediumEMA, slowEMA, previousFastEMA, previousMediumEMA, previousSlowEMA);

    previousFastEMA = fastEMA;
    previousMediumEMA = mediumEMA;
    previousSlowEMA = slowEMA;
}

//+------------------------------------------------------------------+
//| Check and send alert for trading conditions                      |
//+------------------------------------------------------------------+
void CheckTradeConditions(double fastEMA, double mediumEMA, double slowEMA, double previousFastEMA, double previousMediumEMA, double previousSlowEMA) {
    double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

    if (previousFastEMA < previousSlowEMA && fastEMA > slowEMA) {
        SendTelegramAlert("Alert for buy condition");
    }
    if (previousMediumEMA < previousSlowEMA && mediumEMA > slowEMA) {
        SendTelegramAlert("Buy condition met at " + DoubleToString(currentPrice, _Digits));
    }
    if (previousFastEMA > previousSlowEMA && fastEMA < slowEMA) {
        SendTelegramAlert("Alert for sell condition");
    }
    if (previousMediumEMA > previousSlowEMA && mediumEMA < slowEMA) {
        SendTelegramAlert("Sell condition met at " + DoubleToString(currentPrice, _Digits));
    }
}

//+------------------------------------------------------------------+
//| Function to send a message via Telegram                          |
//+------------------------------------------------------------------+
void SendTelegramAlert(string message) {
    string headers = "Content-Type: application/x-www-form-urlencoded";
    uchar data[];
    string messageText = "chat_id=" + TELEGRAM_CHAT_ID + "&text=" + UrlEncode(message);
    int dataSize = StringToCharArray(messageText, data);
    char result[];
    string url = "https://api.telegram.org/bot" + TELEGRAM_BOT_TOKEN + "/sendMessage";
    string contentLength = "Content-Length: " + IntegerToString(dataSize);
    headers = headers + "\r\n" + contentLength;
    int res = WebRequest("POST", url, headers, "", 3000, data, dataSize, result, headers);
    if (res != 200) {
        int error = GetLastError();
        Print("Error sending message to Telegram: ", error);
    }
}

 
Fasi RathoreI have been using AI to create custom expert advisor for my needs, I have provided code below for the custom EA that should send alerts to my telegram channel. There is no error in compilation. This ea sends initial message that "Alert system initiated" to my telegram which means that the API and webmail setup to send message to telegram is configured properly. But once my condition is met in manual checkup, it won't send any of the further messages which are described in " Check and send alert for trading condition" section of the code. Can you please check the cause of the problem and if possible, please correct the code section which might me causing this issue.

Implement the following Script to your EA:

Code Base

send a trade position to telegram channel

Rabi Nateghi, 2023.04.02 22:12

hi, this code need your telegram token and telegram chat id . you can make robot by BotFather and then search in google how you can find telegram token and chat id then attach them to this code . now every trade would be signals on your telegram channel.

 
Vinicius Pereira De Oliveira #:

Implement the following Script to your EA:


Thanks for your reply, I have used chat id and bot token for my expert. The configuration of this EA with Telegram is correct. I'm receiving the initial message to my telegram group that "signal system initiated" without any problem. But once our condition is met, it is not sending any messages in regard to our condition.