Hardware components | ||||||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
![]() |
| × | 1 | |||
Software apps and online services | ||||||
![]() |
| |||||
![]() |
| |||||
![]() |
|
This project is use to alert the users when the limit price is reached. It is made user-friendly such that when ever you run it, it will ask for inputs.
1. Enter company stock symbol: (Enter the symbolic representation of that organization as listed in US share market like Microsoft: MSFT )
2. Enter the limit Price: (Set the lower limit price when you want to be alerted)
Every morning you will receive an telegram alert with yesterday's closing price.
There is no break point set so it will keep on running until stopped.
Share Market Alert via BOLT IOT and Telegram
Pythonuse terminal to run the program
>> python3 'filename'
>> python3 'filename'
import json
import requests
import time
from boltiot import Bolt
from decimal import *
PREV_DATE="" #create a gloabal variable to hold the yesterday's date
bolt_api_key = '' # This is your Bolt Cloud API Key
device_id = '' # This is the device ID and will be similar to BOLTXXXX where XXXX is some numbers
telegram_chat_id = '@' # This is the channel ID of the created Telegram channel. Paste after @ symbol.
telegram_bot_id = '' #This is the BOT id created in Telegram
alphavantage_api_key='' #Api key for alphavantage which provides the share price rates
bolt = Bolt(bolt_api_key, device_id)
def price_check(stocksymbol, limitprice):
url = "https://www.alphavantage.co/query?"
querystring = {"function":"TIME_SERIES_DAILY","symbol":stocksymbol,"apikey":alphavantage_api_key}
Limit_Price = Decimal(limitprice)
response = requests.request("GET", url, params=querystring) #Query string to get the price related to stock symbol
response = json.loads(response.text)
#print(response)
current_price = response['Time Series (Daily)']
result = json.dumps(current_price)
datestr= result[2:12] #splice the response to get only the date part
print()
close_price=current_price[datestr]['4. close'] #looking for the latest date closing price
print ("Company Stock Symbol: ",stocksymbol,"\nDate: ", datestr, "\nShare closing price: ", close_price)
#To change the global variable
global PREV_DATE
#Notify user only when there is change in date
if datestr != PREV_DATE:
PREV_DATE=datestr
#telegram alert with price & date every morning
message ="Date: " + str(PREV_DATE) + "\nShare closing price is" + str(close_price)
telegram_status = send_telegram_message(message)
print("This is the Telegram status:", telegram_status)
#compare limit and current price
compare_share_price(stocksymbol, close_price, Limit_Price)
return close_price
def send_telegram_message(message):
"""Sends message via Telegram"""
url = "https://api.telegram.org/" + telegram_bot_id + "/sendMessage"
data = {
"chat_id": telegram_chat_id,
"text": message
}
try:
response = requests.request(
"POST",
url,
params=data
)
#print("This is the Telegram URL")
#print(url)
#print("This is the Telegram response")
#print(response.text)
telegram_data = json.loads(response.text)
return telegram_data["ok"]
except Exception as e:
print("An error occurred in sending the alert message via Telegram")
print(e)
return False
def compare_share_price(stock_symbol, close_price, Limit_Price):
closing_price = Decimal(close_price)
if closing_price <= Limit_Price:
print ('Share price is less than or equal to limit price')
#Bolt Device Alert
bolt.digitalWrite("0", "HIGH") #will turn on the buzzer
time.sleep(60)
bolt.digitalWrite("0", "LOW") #will turn off the buzzer
#telegram alert
message = "Alert!! \nCompany Stock Symbol: "+ str(stock_symbol) + "\nShare closing price: " + str(closing_price) +"\nLimit price: " + str(Limit_Price)
telegram_status = send_telegram_message(message)
print("This is the Telegram status:", telegram_status)
company_symbol= input("Enter company stock symbol:")
limit_price = input("Enter the limit Price:")
while True:
market_close = price_check(company_symbol,limit_price )
print ('Market price is', market_close)
time.sleep(3600)
Comments