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!
excellent code..im already follow this instruction but alert not send to telegram… what i missed? should i change my own code? thanks
you have to set your channel id in the code.
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();
_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();
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
Hi I tried it
it doesn’t work
Whats the error you are getting.
Is it possible to send Amibroker exploration signals via Telegram API?
Yes Possible
how to send amibroker exploration signal to telegram channel.
only ‘this is a test message from amibroker’ is sent
What is Bootfather?
botfather is a bot based messaging API service from Telegram
sir how to write buy sell conditions message in modern afl to get alert with buy sell values.
Explained detailed in this course https://courses.marketcalls.in/p/practical-approach-to-amibroker-afl-coding
hello, sir, can u help me to connect amibroker to telegram channel ????
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/
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
The VBscript code is working good for me. Thank you 🙂
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
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
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 .
Dear sir,
When i add buy/sell trigger , the messages repeat too much, how can i solve this problem???
Thnks you
I also suffer from the same problem. Messages come non-stop. Please help solve the problem
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.
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
Hi Rajendran tks for the code No error at all but message not sent to telegram..can you help
Thanks!! worked like a charm…..heartfelt thanks for this website too
works great. any idea how to add a new line within the message itself?
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 .
Can i generate the alerts when i try Bar replay feature in Amibroker
Yes possible with authentic vendors but not with 500 rs vendors
error 47: – error occurred in the secure channel support.
how to solve this
which Amibroker version you are using?
Is it possible to send Emojis? from AFL?
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();
Try here for sample telegram signal
https://www.marketcalls.in/amibroker/allinonealerts-amibroker-alerts-module-for-algomojo-users.html