Creating a Simple Stock Application with Python and APIs | by Nikhil Adithyan | CodeX | Dec, 2021

An easy yet interesting project which can be done within a day in Python

Nikhil Adithyan
Photo by Christin Hume on Unsplash

1. Importing Packages

pip install requests
pip install pandas
import requests
import pandas as pdapi_key = 'YOUR API KEY'

2. Creating the Search Function

def search_asset(asset, search_input, api_key):
url = f'{search_input}?api_token={api_key}&type={asset}'
results_json = requests.get(url).json()
results_df = pd.DataFrame(results_json).drop('ISIN', axis = 1).rename(columns = {'Code':'Symbol'})

return results_df{search_input}?api_token={api_key}&type={asset}

3. Extracting Inputs and Showing the Results

assets = pd.DataFrame(pd.Series(['stock', 'etf', 'fund', 'bonds', 'index', 'commodity', 'crypto'])).rename(columns = {0:'asset'})
asset = input('Type of asset [stock, etf, fund, bonds, index, commodity, crypto]: ')

if len(assets[assets.asset == asset]) == 0:
print(cl('Input Error: Asset not found', color = 'red'))
search_input = input(f'Search {asset}: ')
print(cl(f'nSearch Results for {search_input}', attrs = ['bold']), 'n')
search_results = search_asset(asset, search_input, api_key)

Video by Author