kieran-mackle / AutoTrader

A Python-based development platform for automated trading systems - from backtesting to optimisation to livetrading.
https://kieran-mackle.github.io/AutoTrader/
GNU General Public License v3.0
945 stars 217 forks source link

Telegram support for alerts to buy and signal #12

Closed rahulmr closed 1 year ago

rahulmr commented 2 years ago

Is your feature request related to a problem? Please describe. Email notifications are bit complicated to manage and Telegram is used worldwide

Describe the solution you'd like Along with Email, is it possible to send signals to Telegram?

Describe alternatives you've considered Need to implement forwarding of email signals to Telegram channel or coding for the same.

Additional context It will be great is Telegram Channel support is implemented. Adv:

  1. One can send Buy Sell signals along with SL and TARGET and even Quantity based on predefined risk.
  2. Not supported brokers can be handled using Telegram. Code is already there to read Telegram messages from channel suing python and trades can be taken using python.
Polypod commented 2 years ago

@rahulmr You could add that yourself quite easily, e.g. by

import telebot # or similar for linux ... bot = telebot.TeleBot(token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') # botname, chatId chatId = 12345678

.... in code example ... bot.send_message(text='{} LONG ENTRY SIGNAL: price={}'.format( instrument.symbol, price), chat_id=chatId)

rahulmr commented 2 years ago

@rahulmr You could add that yourself quite easily, e.g. by

import telebot # or similar for linux ... bot = telebot.TeleBot(token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx') # botname, chatId chatId = 12345678

.... in code example ... bot.send_message(text='{} LONG ENTRY SIGNAL: price={}'.format( instrument.symbol, price), chat_id=chatId)

Yes, I am aware of telebot and even a simple get request can be used to post messages to telegram, just wanted to inform the owner that this can be a good feature. @kieran-mackle can implement it in a better way. That was my thought behind opening this issue. If possible, can you suggest where this telebot code must be added?

kieran-mackle commented 2 years ago

Thanks for bringing this to my attention, it is definitely a good feature to have. If I was to add this (which I may get around to soon), I would create a telegram module under autotrader.comms, mimicking similar functionality as the email module. The main functionality I would include is a template to send signals, orders and trades from autobot.py, as well as a method for users to send custom messages from their strategy, by importing the telegram comms module. Admittedly I haven't used telegram, but the snippet above from @Polypod looks like it would be a relatively straightforward and worthwhile addition.

rahulmr commented 2 years ago

@kieran-mackle it is even simpler without using any extra python package, check below code.


import requests
from urllib.parse import urlencode, quote_plus

def telegram_bot_sendtext(message):
    bot_token = config.bot_token
    bot_chatID = '-100'+config.chatID_or_channel_id
    data = {
        'text': message,
        'chat_id': bot_chatID,
        'parse_mode': 'MARKDOWN'
    }
    encodedData = urlencode(data, quote_via=quote_plus)

    url = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?' + encodedData
    response = requests.get(url)
    return response.json()

Might be helpful.