The stock price is the highest amount someone is willing to pay for the stock. In this article, we are going to write code for getting live share prices for each company and bind it with GUI Application.
Module Needed
Yahoo_fin: This module is used to scrape historical stock price data, as well as to provide current information on market caps, dividend yields, and which stocks comprise the major exchanges. To install this module type the below command in the terminal.
pip install yahoo_fin
Below is what the GUI looks like:-
Let’s write code to get stock data.
Method 1:
Import the yahoo_fin module.
Python3
from yahoo_fin import stock_info |
Use stock_info.get_live_price() method to get live stock price.
Python3
stock_info.get_live_price( "AMZN" ) |
Output:
3198.93994140625
Note: please visit this site to get company Symbol like Amazon is AMZN, Reliance is RELFF.
Method 2:
Prerequisites:
- yfinance:
We obtain and gather the financial data of the company (such as financial ratios, etc.) with the aid of the yfinance module using its functionalities, as well as the history of marketing data. You can explore a wide range of additional characteristics offered by the yfinance library. Zip, Sector, FullTimeEmployees, LongBusinessSummary, City, Phone, State, and Country are a few of these. The yfinance Python library is among the easiest to use for setting up, retrieving data, and performing data analytic activities.
Run the following command in the Terminal window to install the module:
pip install yfinance
Steps to find the Live Stock Price:
Step 1: Import the required module yfinance
Step 2: Input the Stock Name from the user
Step 3: The Ticker( ) function, allows you to access ticker data in a more Pythonic way and we are taking the information from it using the info
Step 4: Taking the Market Price from the information extracted
Step 5: Print the Stock price
Code:
Python3
# Import the Required modules import yfinance as yf # Inputting the name of the Stock and Storing it in a Variable STK = input ( "Enter share name : " ) # Extract the Share information using the Ticker() Function Share = yf.Ticker(STK).info # Extracting the MarketPrice from the data market_price = Share[ 'regularMarketPrice' ] # Printing the market price print (market_price) #This Code is Contributed by PL VISHNUPPRIYAN |
Output:
In Terminal Window:
Real-Time Price Output:
Stock price Application with Tkinter
This Script implements the above Implementation into a GUI.
Python3
from yahoo_fin import stock_info from tkinter import * def stock_price(): price = stock_info.get_live_price(e1.get()) Current_stock. set (price) master = Tk() Current_stock = StringVar() Label(master, text = "Company Symbol : " ).grid(row = 0 , sticky = W) Label(master, text = "Stock Result:" ).grid(row = 3 , sticky = W) result2 = Label(master, text = "", textvariable = Current_stock, ).grid(row = 3 , column = 1 , sticky = W) e1 = Entry(master) e1.grid(row = 0 , column = 1 ) b = Button(master, text = "Show" , command = stock_price) b.grid(row = 0 , column = 2 , columnspan = 2 , rowspan = 2 , padx = 5 , pady = 5 ) mainloop() |
Output: