current stock price Algorithm
The main purpose of these methods is to predict future market prices, or more generally, potential market prices, and thus to profit from price movement – stocks that are judged undervalued (with respect to their theoretical value) are buy, while stocks that are judged overvalued are sell, in the expectation that undervalued stocks will overall rise in value, while overvalued stocks will generally decrease in value. In fiscal markets, stock valuation is the method of calculate theoretical values of company and their stocks.
import requests
from bs4 import BeautifulSoup
def stock_price(symbol: str = "AAPL") -> str:
url = f"https://in.finance.yahoo.com/quote/{symbol}?s={symbol}"
soup = BeautifulSoup(requests.get(url).text, "html.parser")
class_ = "My(6px) Pos(r) smartphone_Mt(6px)"
return soup.find("div", class_=class_).find("span").text
if __name__ == "__main__":
for symbol in "AAPL AMZN IBM GOOG MSFT ORCL".split():
print(f"Current {symbol:<4} stock price is {stock_price(symbol):>8}")