Stock Market Price Notifier Bot For Telegram

Saurabh Kadam
codeburst
Published in
4 min readDec 15, 2019

I tried many app to stock price alert for notification.I tried few apps which I registered but it does not notify me on target price.So I decided to build simple bot to notify me current price.In this article I will show you how to build simple bot which run on Heroku (free).Reason I choose Telegram over Whatsapp.Too much documentation for whatsapp for simple thing like bot creation.

First thing you need is python.I am using python 3.7.3.Some packages you need to install like yfinance.I like yahoo finance cause you can fetch data from various stock exchange and crypto market.

Below is the running version of code for mybot.

# -*- coding: utf-8 -*-
"""
Created on Sun Dec 15 09:36:36 2019
@author: saura
"""
import requests
import time
import schedule
import datetime
from pandas_datareader import data as pdr
import yfinance as yf
def telegram_bot_sendtext(bot_message):
bot_token='Your_Token'
bot_chatID='your_chatid'
send_text='https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message
response=requests.get(send_text)
return response.json()
test=telegram_bot_sendtext("Testing bot")
print(test)
price=''def getStock():
bot_token='Your_Token'
bot_chatID='your_chatid'
response= "Let me fetch Latest quote ford you \n"
symbol='ITC.NS'
aapl=pdr.get_data_yahoo(symbol, start=datetime.datetime(2019, 12, 14),
end=datetime.datetime(2019, 12, 14))
price=(aapl["Close"][0]).round(2)
price=str(price)
price="Current price for "+symbol+ " is "+price
price=str(price.encode('utf-8','ignore'),errors='ignore')
send_text='https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + price
response=requests.get(send_text)
print(response)
def btcTracker():
bot_token='Your_Token'
bot_chatID='your_chatid'
response= "Let me fetch Latest quote for you \n"
symbol='ETH-USD'
aapl=pdr.get_data_yahoo(symbol, start=datetime.datetime(2019, 12, 15),
end=datetime.datetime(2019, 12, 15))
price=(aapl["Close"][0]).round(2)
price=str(price)
price="Current price for "+symbol+ " is "+price
price=str(price.encode('utf-8','ignore'),errors='ignore')
send_text='https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + price
response=requests.get(send_text)
print(response)
import threadingwhile True:
getStock()
btcTracker()
time.sleep(60)

Before all of this you need to register your bot on telegram first.Telegram made it easy for developers.You just need to login telegram first and search for botfather. You will see Marlon Brando icon then you reach at right location.just type /start to start process.

BotFather First screen

Give your botname and what it is going to be called in botfather.It will give you token which will be used in further programming.

BotFather second screen

When your bot goes live.make simple request in your web browser.with following URL

https://api.telegram.org/bot<your_token>/getUpdates

You will get response something like this.

Just take that id from from tag.That is your user id just use that.When more user login into this.then search from chat tag then first_name criteria.

So you got chat_id and html_token then add to above text.

Choosing Ticker

This is one of the important part in this code.Yahoo finance hold data from various stock exchange.How you choose your symbol.

It is simple.you just have to pay attention to URL.Whatever comes next to ?p is your symbol.Here it is YESBANK.NS.

Yes Bank stock quote

Same applicable to Crypto too.I added ETH-USD.For tracking ethereum prices in code.Above code is created to notify me for notifying me on telegram current price for specific quote.If you run it on your local machine.You will get something like this.

Program Running on local Machine

On right hand corner you can see that I received notification for the same.I am currently tracking price for infy and eth.I got that message on telegram.If you got 200 status code means everything is good.

Running code on online platform.

First made skeleton for your app.use that skeleton.Change script file with your code.Do not rename script.py.

Folder contains many file like requirement,runtime,etc.

requirement contains packages need to be installed on Heroku and runtime is python version on which program will be run.

I will suggest you should make your app in CLI.

Follow below steps.

  1. git init #To initiate git repo
  2. heroku create #make app
  3. Commit to master branch
git add .
git commit -m "initial commit"
git push heroku master

4. heroku ps:scale worker=1 #set 1 worker

5.heroku logs — tail #To check logs of build

Voila! Your app running on server.Bot is giving you continuous notificaton. No need to third party app to notify you. You can change this program furthur cause I changed it to notify me on 52-week lowest value and target price limit reached conditions.

Love to hear your ideas so we can make better chatbot to trade.

Feel free to contact me on linkedin or gmail on saurabhkdm721[at]gmail.com

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Saurabh Kadam

Devops Professional by Job.Machine Learning ,Deep learning and 3D modelling for Hobby.

Responses (2)

What are your thoughts?