Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

How to Send Alerts from Amibroker to Telegram Channel using Telgram API

2 min read

In this tutorial we will be discussing practical examples of how to send alerts from Amibroker to a Telegram channel using Telegram Bot API

In order to be able to do so, you will have first to:

  • Create a Telegram public channel
  • Create a Telegram BOT via BotFather
  • Set the bot as administrator in your channel

Getting the BOT API key from BotFather in Telgram APP

Provided that you did the above, now you can send a message to your channel by issuing an HTTP GET request to the Telegram BOT API at the following URL:

 https://api.telegram.org/bot[BOT_API_KEY]/sendMessage?chat_id=[MY_CHANNEL_NAME]&text=[MY_MESSAGE_TEXT] 

where:

  • BOT_API_KEY is the API Key generated by BotFather when you created your bot
  • MY_CHANNEL_NAME is the handle of your channel (e.g. @my_channel_name)
  • MY_MESSAGE_TEXT is the message you want to send (URL-encoded)

One can send Telgram Alerts from Amibroker in two different ways

1)Modern Way to access HTTP API using InternetOpenURL functions (Supports Amibroker 6.17 or higher)
2)Using AFL scripting (VBScript) inside the AFL Program (legacy method compatible with most of the versions of Amibroker)

Legacy Method 
Using AFL scripting (VBScript)

If you understand Amibroker from a coding perspective it supports AFL scripting host. AFL scripting host is an interface between AFL engine and JScript/VBScript engines (aka. Active Scripting technologies) available as a part of Internet Tools & Technologies platform provided by Microsoft.

//Coded by Rajandran R 
//Founder - Marketcalls - https://www.marketcalls.in
//Coded on 12th Jun 2019
/*

Note : Before Using this Code make sure you had followed the below mentioned steps

1)Create a Telegram public channel
2)Create a Telegram BOT via BotFather
3)Set the bot as administrator in your channel 

*/

_SECTION_BEGIN("Telegram Alerts - Legacy Method");


TelegramAlerts = ParamTrigger("Telegram Alert","Send Alert");
TelegramAPI_ID = ParamStr("Telegram Bot API Key","854431837:AAEeSAtL_rgxxxxxxxxxx");  //Get the Bot API key via BotFather in Telgram
TelgramCHAT_ID = ParamStr("Telegram Channel ID","@marketcalls_in"); //Channel ID example : @marketcalls_in

//User Defined Function -> Created using VBscript
EnableScript("VBScript"); 

<% 

Public Sub Telegram(Message_Text)


sAPI_ID = AFL.Var("TelegramAPI_ID")
sChat_ID = AFL.Var("TelgramCHAT_ID")
sMSG = Message_Text

'URL to open....
sUrl = "https://api.telegram.org/bot" & sAPI_ID & "/sendMessage"
'POST Request to send.
sRequest = "text=" & sMSG & "&chat_id=" & sChat_ID


set oHTTP = CreateObject("Microsoft.XMLHTTP")
oHTTP.open "POST", sUrl,false
oHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
oHTTP.setRequestHeader "Content-Length", Len(sRequest)
oHTTP.send sRequest
HTTPPost = oHTTP.responseText


'Store response 
'msgbox(objXmlHttpMain.responseText)
'response.Write (objXmlHttpMain.responseText) 

End Sub 

%> 

tg = GetScriptObject(); 

if (TelegramAlerts)
{
//code for Telegram alerts to channel

tg.Telegram("This is Telegram Alert from Amibroker");

}//end Telegram alert

_SECTION_END();

Modern Method
using InternetOpenURL functions

Code requires Amibroker 6.17 as some of the new AFL functions like InternetClose( handle ) , InternetOpenURL( “url” ) , InternetReadString( handle ) – which is essecial to read from internet resource/http api is available from v6.17 or higher version.

//Coded by Rajandran R 
//Founder - Marketcalls - https://www.marketcalls.in
//Coded on 12th June 2019
/*

Note : Before Using this Code make sure you had followed the below mentioned steps

1)Create a Telegram public channel
2)Create a Telegram BOT via BotFather
3)Set the bot as administrator in your channel 

*/


_SECTION_BEGIN("Telegram Alerts - Modern Method");

Version(6.17);  //Code is Compatible with 6.17 and Higher

TelegramAlerts = ParamTrigger("Telegram Alert","Send Alert");
TelegramAPI_ID = ParamStr("Telegram Bot API Key","854431837:AAEeSAtL_rgRZAMfxgxxxxxxxxxx");  //Get the Bot API key via BotFather in Telgram
TelgramCHAT_ID = ParamStr("Telegram Channel ID","@marketcalls_in");  //Channel ID example : @marketcalls_in

Message = "This is a Test Message from Amibroker";

if (TelegramAlerts)
{
ih = InternetOpenURL("https://api.telegram.org/bot"+TelegramAPI_ID+"/sendMessage?chat_id="+TelgramCHAT_ID+"&text="+Message ); 
InternetClose(ih);
}

_SECTION_END();

Amibroker Controls

1)Apply the AFL to the chart and over the chart -> right click and select Properties

2)Enter the Telegram Bot API Key and Channel ID as per your requirement

3) Press the Send Alert (Trigger Button)

4)Bingo you are done. You should be able to see the Telegram Alert from Amibroker in your Telegram Channel

Hope you enjoyed this article!

Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

[Live Coding Webinar] Build Your First Trading Bridge for…

In this course, you will be learning to build your own trading bridge using Python. This 60-minute session is perfect for traders, Python enthusiasts,...
Rajandran R
1 min read

[Webinar] Understanding and Mitigating Curve Fitting in System Trading

"Understanding and Mitigating Curve Fitting in System Trading"! This dynamic session aims to equip novice to intermediate traders, quantitative analysts, and financial engineers with...
Rajandran R
1 min read

P-Signal Strategy Long Only Strategy – Amibroker AFL Code

This tutorial provides an overview of the P-Signal reversal strategy, a quantitative trading strategy that utilizes statistical parameters and error functions to generate probabilistic...
Rajandran R
2 min read

36 Replies to “How to Send Alerts from Amibroker to Telegram Channel…”

  1. excellent code..im already follow this instruction but alert not send to telegram… what i missed? should i change my own code? thanks

  2. Hi Rajandran, thank for share.
    but i can’t run after use ur code like this:

    _SECTION_BEGIN(“Telegram Alerts – Legacy Method”);

    TelegramAlerts = ParamTrigger(“Telegram Alert”,”Send Alert”);
    TelegramAPI_ID = ParamStr(“Telegram Bot API Key”,”727373270:AAFTaMLexxxxxxxxxxI”); //Get the Bot API key via BotFather in Telgram
    TelgramCHAT_ID = ParamStr(“Telegram Channel ID”,”-1001389xxxxxx”); //Channel ID example : @marketcalls_in

    //User Defined Function -> Created using VBscript
    EnableScript(“VBScript”);
    // “normal” AFL statements
    Buy=Cover=Cross(j,nw);
    Sell=Short=Cross(nw,j);

    // “normal” AFL statements
    Buy = ExRem( Buy, Sell );

    tg = GetScriptObject();

    if (TelegramAlerts)
    {
    //code for Telegram alerts to channel

    tg.Telegram(“This is Telegram Alert from Amibroker”);

    }//end Telegram alert

    _SECTION_END();

  3. _SECTION_BEGIN(“Telegram Alerts – Legacy Method”);

    TelegramAlerts = ParamTrigger(“Telegram Alert”,”Send Alert”);
    TelegramAPI_ID = ParamStr(“Telegram Bot API Key”,”727373270:AAFTaMLeQYsehpVxxxxxxxxxxxxxxI”); //Get the Bot API key via BotFather in Telgram
    TelgramCHAT_ID = ParamStr(“Telegram Channel ID”,”-100xxxxxxx”); //Channel ID example : @marketcalls_in

    //User Defined Function -> Created using VBscript
    EnableScript(“VBScript”);
    // “normal” AFL statements
    Buy=Cover=Cross(j,nw);
    Sell=Short=Cross(nw,j);

    // “normal” AFL statements
    Buy = ExRem( Buy, Sell );

    tg = GetScriptObject();

    if (TelegramAlerts)
    {
    //code for Telegram alerts to channel

    tg.Telegram(“This is Telegram Alert from Amibroker”);

    }//end Telegram alert

    _SECTION_END();

  4. Code requires Amibroker 6.17 :
    please code a sample for this:

    InternetOpenURL(“https://api.telegram.org/bot”+TelegramAPI_ID+”/sendMessage?chat_id=”+TelgramCHAT_ID+”&text=”+Message );

    i can’t run arlert this step

      1. how to send amibroker exploration signal to telegram channel.

        only ‘this is a test message from amibroker’ is sent

  5. Thank you Rajandran. I have learnt many things from this site. I could replicate the technique and able to include it along with the exploration.

    For the benefit of readers, if your script has to send M&M.NSE then you need to send M%26M.NSE

    url encoding online:
    https://www.urlencoder.io/

  6. Sir, I cud able to send test msg after writing your given code in the afl, but when some Buy Sell Signal coming on chart, that signals is not going to the telegram. pls help

  7. Hello Sir,

    It is showing below error even i added correct telegram id and api key.

    A connection with the server could not be established

  8. hi … your coding is work for me, then I try to send photo (chart from AB and local file) but it doesn’t work. Try to make via multipart data from and still doesn’t work (maybe its wrong coding). Can you give me an example how to sendphoto with caption?

    thank you
    Hayanto Hadiwijoto
    Indonesian Trader

  9. Sir, how to limit the message to be sent only one time after an indicator signal triggered? This code works but non-stop to send messages and cause amibroker nearly .

  10. Dear sir,
    When i add buy/sell trigger , the messages repeat too much, how can i solve this problem???

    Thnks you

  11. I also suffer from the same problem. Messages come non-stop. Please help solve the problem

  12. HI, Good Evening Shri Rajendran.

    Thanks for the Code. It is working fine on my system. However it is orking only on one chaRT WHICH IS OPEN AND ACTIVE AT THE TIME. hOW TO GET ALLERTS FROM DIFFERENT charts at a time.

  13. I am facing kind different error. Which is error 65 internet handle passed to internet close( ) function is invalid…

    Same code is running well on other Amibroker user tried several things but error remain same
    Could anyone replay on This..

    Thanks

  14. Hi Rajendran tks for the code No error at all but message not sent to telegram..can you help

  15. Thanks Sir for this excellent article!! This very helpful.

    Can you guide how to generate trade signals using , alerts when doing rotational trading in Amibroker ?

    Means I am using “enablerotationalTrading()” and PositionScore in my formula. How can use AlertIf function to automate my rotational strategy ? Because AlertIf only accepts Boolean value.
    Thanks a lot .

  16. Hi as per above I have add code but could you please add a sample buy or sell strategy _SECTION_BEGIN(“Slack Alert”);

    trigger = ParamTrigger(“Trigger”,”Send Alert”);
    webhookurl = ParamStr(“Slack Webhook URL”,”https://hooks.slack.com/services/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″);
    alerttext = “This is a test from Amibroker”;
    EnableScript(“vbscript”);

    if(trigger)
    {
    //transmit alert to slack
    message = GetScriptObject();
    message.Slack(alerttext);
    }
    _SECTION_END();

    _SECTION_BEGIN(“Price”);
    SetChartOptions(0,chartShowArrows|chartShowDates);
    _N(Title = StrFormat(“{{NAME}} – {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}”, O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
    Plot( C, “Close”, ParamColor(“Color”, colorDefault ), styleNoTitle | ParamStyle(“Style”) | GetPriceStyle() );
    _SECTION_END();

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More