This tutorial is similar to RapidAPI example found in the official blog, with one exception, this example is users request method instead of unirest package which is not available on RapidAPI blog.
RapidAPI
RapidAPI is a website which provides useful APIs (Application Programing Interface) like Yahoo Finance, Email Verification, Geolocation which can be used to add features to your websites and apps using variety of programming languages like Python, Java, .Net etc.
Yahoo Finance example
For creating application to show stock chart using YahooFinanceAPI and Python, we are go through the following
- Create a Rapid API Account
- Subscribe to Yahoo Finance to get the endpoint (Which is a URL for getting information)
- Create methods to connect API and Process data
- Draw Plot
Install requirements
Install required packages using pip. I suggest using Python virtual environment.
pip install seaborn pandas
Complete Source code
import json from datetime import datetime import matplotlib.pyplot as plt import pandas as pd import requests import seaborn as sns from matplotlib import rcParams from past.builtins import raw_input RAPIDAPI_KEY = "<YOUR_RAPIDAPI_KEY>" RAPIDAPI_HOST = "<YOUR_RAPIDAPI_ENDPOINT>" symbol_string = "" inputdata = {} def fetchStockData(symbol): url = "https://apidojo-yahoo-finance-v1.p.rapidapi.com/market/get-charts" querystring = {"comparisons": "%5EGDAXI%2C%5EFCHI", "region": "US", "lang": "en", "symbol": symbol, "interval": "5m", "range": "1d"} headers = { 'x-rapidapi-host': "apidojo-yahoo-finance-v1.p.rapidapi.com", 'x-rapidapi-key': "275d968795msheb0c2d90f2d7a32p1141eajsnef8c254e756a" } response = requests.request("GET", url, headers=headers, params=querystring) if (response.status_code == 200): return json.loads(response.text) else: return None def parseTimestamp(inputdata): timestamplist = [] timestamplist.extend(inputdata["chart"]["result"][0]["timestamp"]) timestamplist.extend(inputdata["chart"]["result"][0]["timestamp"]) calendertime = [] for ts in timestamplist: dt = datetime.fromtimestamp(ts) calendertime.append(dt.strftime("%m/%d/%Y")) return calendertime def parseValues(inputdata): valueList = [] valueList.extend(inputdata["chart"]["result"][0]["indicators"]["quote"][0]["open"]) valueList.extend(inputdata["chart"]["result"][0]["indicators"]["quote"][0]["close"]) return valueList def attachEvents(inputdata): eventlist = [] for i in range(0, len(inputdata["chart"]["result"][0]["timestamp"])): eventlist.append("open") for i in range(0, len(inputdata["chart"]["result"][0]["timestamp"])): eventlist.append("close") return eventlist if __name__ == "__main__": try: while len(symbol_string) <= 2: symbol_string = raw_input("Enter the stock symbol: ") retdata = fetchStockData(symbol_string) if (None != inputdata): inputdata["Timestamp"] = parseTimestamp(retdata) inputdata["Values"] = parseValues(retdata) inputdata["Events"] = attachEvents(retdata) df = pd.DataFrame(inputdata) sns.set(style="darkgrid") rcParams['figure.figsize'] = 13, 5 rcParams['figure.subplot.bottom'] = 0.2 ax = sns.lineplot(x="Timestamp", y="Values", hue="Events", dashes=False, markers=True, data=df, sort=False) ax.set_title('Symbol: ' + symbol_string) plt.xticks( rotation=45, horizontalalignment='right', fontweight='light', fontsize='xx-small' ) plt.show() except Exception as e: print("Error") print(e)
Output
