Topics
Replies
maciel.rafael1
27 Oct 2020, 17:50
( Updated at: 21 Dec 2023, 09:22 )
RE:
bart1 said:
Here is a simple instruction to add Telegram notifications into your cBot or indicator.
First you need to create your own bot by chatting with BotFather in Telegram
Send /newbot and it will ask desired display name and user name for your new Telegram bot.
From the final message you need Token. Also you need to get chat ID for your new bot to send messages to you.
Go to your bot by clicking link in the final message and send some text.
To get chat ID you need to request latest updates from your bot by entering following URL in your browser: https://api.telegram.org/bot<TOKEN>/getUpdates
replace <TOKEN> placeholder with your bot tokenIn response you will get text where you can find chat id
Example: ..."chat":{"id":123,...Now you have 2 required parameters to send Telegram notifications and you can create cBot or indicator to test it. Telegram messages can be send using simple HTTP request. Here is a helper class that will do it for you.
public class TelegramBot { public string Token { get; private set; } public int ChatId { get; private set; } public TelegramBot(string token, int chatId) { UseTls12(); Token = token; ChatId = chatId; } public bool Send(string message) { var url = string.Format("https://api.telegram.org/bot{0}/sendMessage?chat_id={1}&text={2}", Token, ChatId, message); var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url); var response = (System.Net.HttpWebResponse)request.GetResponse(); return response.StatusCode == System.Net.HttpStatusCode.OK; } public bool Send(string message, params object[] args) { var formattedMessage = string.Format(message, args); return Send(formattedMessage); } private void UseTls12() { // Required for SSL/TLS connection with Telegram // Will work only on .Net 4.5 or higher // Using number to avoid compilation error System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072; } }
After adding this class to your cBot or indicator you can send notifications like this:
replace <TOKEN> and <CHAT_ID> with yoursprivate TelegramBot TelegramBot; protected override void OnStart() { TelegramBot = new TelegramBot("<TOKEN>", <CHAT_ID>); TelegramBot.Send("Started {0} {1}", SymbolName, TimeFrame); }
NOTE: cBot and indicators need extended permission. You need to use Internet or FullAccess permissions:
[Robot(AccessRights = AccessRights.Internet)] public class NewcBot : Robot
I tried this in my bot and I get the following errors : TelegramBot is a type but is used like a variable and the second error is an object reference is required for the non-static field, method, or property. Both points to the lines OnStart
Any suggestions how to fix?
@maciel.rafael1
maciel.rafael1
24 Sep 2020, 17:25
RE:
Where can I send it in private to have it looked at?
PanagiotisCharalampous said:
Hi maciel.rafael1,
You need to be more specific. Please provide the cBot code and the exception you get.
Best Regards,
Panagiotis
@maciel.rafael1
maciel.rafael1
24 Sep 2020, 17:06
RE:
Hi,
I verified and the log says that it is because both bots are accessing at the same time. Do you have an idea how can I fix it that one can write and the other read at the same time?
Thank you
PanagiotisCharalampous said:
Hi maciel.rafael1,
cTrader does not stop cBots. cBots are either stopped manually/automatically by the trader or due to some exception. Did you check your logs? Are there any messages there indicating why the cBot has stopped?
Best Regards,
Panagiotis
@maciel.rafael1
maciel.rafael1
24 Sep 2020, 16:48
RE:
Hi Pani,
When I try this bot on back testing and I verify the information on trading view the value for the RSI is not the same
Could it be that the bot is selecting the wrong price ?
PanagiotisCharalampous said:
Hi Sam,
Here you go
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } RelativeStrengthIndex _rsiDaily; RelativeStrengthIndex _rsiHourly; protected override void OnStart() { var dailyBars = MarketData.GetBars(TimeFrame.Daily); var hourlyBars = MarketData.GetBars(TimeFrame.Hour); _rsiDaily = Indicators.RelativeStrengthIndex(dailyBars.ClosePrices, 14); _rsiHourly = Indicators.RelativeStrengthIndex(hourlyBars.ClosePrices, 14); } protected override void OnBar() { if (_rsiDaily.Result.Last(1) > 60 && _rsiHourly.Result.Last(1) > 60) { ExecuteMarketOrder(TradeType.Buy, Symbol.Name, 1000); } } protected override void OnStop() { // Put your deinitialization logic here } } }
Best Regards,
Panagiotis
@maciel.rafael1
maciel.rafael1
29 May 2021, 17:15
RE:
amusleh said:
So the idea of the bot I`m trying to make would be that I`ll give it 10 horizontal Lines and whenever it touches a line from the bottom I get a down alert and if it touches from the top a top alert
@maciel.rafael1