Python code to fetch stock market information
The following Python code example to pulls stock quotes, Price-to-Earnings (P/E) ratio, Debt-to-Equity (D/E) ratio, dividend, and dividend yield using the Alpha Vantage API:
Code to fetch stock market data
import requests
def get_stock_data(symbol):
api_key = "YOUR_API_KEY"
function = "GLOBAL_QUOTE"
symbol = symbol
url = f"https://www.alphavantage.co/query?function={function}&symbol={symbol}&apikey={api_key}"
response = requests.get(url)
data = response.json()
return data
def get_ratios(symbol):
api_key = "YOUR_API_KEY"
function = "OVERVIEW"
symbol = symbol
url = f"https://www.alphavantage.co/query?function={function}&symbol={symbol}&apikey={api_key}"
response = requests.get(url)
data = response.json()
return data
def get_dividend(symbol):
api_key = "YOUR_API_KEY"
function = "DIVIDEND_YIELD"
symbol = symbol
url = f"https://www.alphavantage.co/query?function={function}&symbol={symbol}&apikey={api_key}"
response = requests.get(url)
data = response.json()
return data
symbol = "AAPL"
stock_data = get_stock_data(symbol)
ratios = get_ratios(symbol)
dividend = get_dividend(symbol)
price = stock_data["Global Quote"]["05. price"]
pe_ratio = ratios["Ratios"]["Price/Earnings"]
de_ratio = ratios["Ratios"]["Debt/Equity"]
div = dividend["0. Dividend Yield"]
print("Stock Symbol:", symbol)
print("Price:", price)
print("Price-to-Earnings Ratio:", pe_ratio)
print("Debt-to-Equity Ratio:", de_ratio)
print("Dividend:", div)
Explanation of the code
In the above code:
- The
get_stock_datafunction takes a stock symbol as an argument and returns the stock data from Alpha Vantage API. - The
get_ratiosfunction takes a stock symbol as an argument and returns the ratios data from Alpha Vantage API. - The
get_dividendfunction takes a stock symbol as an argument and returns the dividend data from Alpha Vantage API. - The
requestslibrary is used to make HTTP requests to the Alpha Vantage API. - Replace
"YOUR_API_KEY"with your own API key, which can be obtained from the Alpha Vantage website.
This code will give you the stock price, P/E ratio, D/E ratio, dividend, and dividend yield for the specified stock symbol.
Unit Test
import unittest
from unittest.mock import patch
import main
class TestStockData(unittest.TestCase):
@patch('main.get_stock_data')
def test_get_stock_data(self, mock_get_stock_data):
mock_get_stock_data.return_value = {
"Global Quote": {
"05. price": "120.00"
}
}
result = main.get_stock_data("AAPL")
self.assertEqual(result, {
"Global Quote": {
"05. price": "120.00"
}
})
@patch('main.get_ratios')
def test_get_ratios(self, mock_get_ratios):
mock_get_ratios.return_value = {
"Ratios": {
"Price/Earnings": "18.23",
"Debt/Equity": "0.45"
}
}
result = main.get_ratios("AAPL")
self.assertEqual(result, {
"Ratios": {
"Price/Earnings": "18.23",
"Debt/Equity": "0.45"
}
})
@patch('main.get_dividend')
def test_get_dividend(self, mock_get_dividend):
mock_get_dividend.return_value = {
"0. Dividend Yield": "2.05"
}
result = main.get_dividend("AAPL")
self.assertEqual(result, {
"0. Dividend Yield": "2.05"
})
if __name__ == '__main__':
unittest.main()
In the above code, the unittest module is used to create unit tests for the get_stock_data, get_ratios, and get_dividend functions. The patch decorator is used to mock the response from the Alpha Vantage API, so the test doesn’t rely on an actual API call.
Each test case sets a return value for the mock response and then calls the corresponding function. The output is then compared with the expected result using the assertEqual method. If the result matches the expected value, the test case will pass.