Mayank Gupta
Published

Share Market Alert using BOLT IOT & Telegram

Will alert on telegram and BOLT IOT buzzer when yesterday's closing price is lower than the limit price.

IntermediateFull instructions provided20 hours459
Share Market Alert using BOLT IOT & Telegram

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
Bolt wifi module and Buzzer to alert the user when limit price is reached
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Buzzer
Buzzer
×1

Software apps and online services

Bolt IoT Android App
Bolt IoT Android App
Bolt Cloud
Bolt IoT Bolt Cloud
Snappy Ubuntu Core
Snappy Ubuntu Core

Story

Read more

Schematics

Bolt IOT wifi module and Buzzer circuit diagram

About Setup

Demo Alert status

Code

Share Market Alert via BOLT IOT and Telegram

Python
use terminal to run the program
>> 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)

Credits

Mayank Gupta
1 project • 0 followers

Comments