-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStocksAPI
More file actions
115 lines (83 loc) · 4.54 KB
/
StocksAPI
File metadata and controls
115 lines (83 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#Learning Objective
Put into practice using Python to interact and call a public API successfully
Look at the data within JSON and DataFrames
Write your data to a local file (JSON)
Use basic visualization to display information
Interact with users
Challenge:
You have seen some examples of how to interact with JSON, CSV and make API Calls. Take some time to explore the YAHOO Finance Guide which shows you endpoints for calling information. Your job is to code a Python Program that does the following things:
Takes user input for a stock (using the Ticker Symbol): IE with the input() command
You will display back the user: Name Ticker, Full Name of the Company, Current Price (or last price if after hours), Target Mean Price, Cash on Hand, Profit Margins
Store the Results Locally in JSON Format with just those items and include a date of when that data was pulled
Handle Errors (IE, the stock doesn’t exist and/or the API is not returning information)
https://financeapi.net/Links to an external site.
You will have to read the documentation and figure out which modules to call to get the data that you need. You will need to make more than one call to this API. Use the examples in our Zoom Session to guide you, but the documentation will be enough. Post your code to GitHub.
Bonus –
Use MatPlotLib to chart the historical price of a stock price’s highest value over the past 5 days.
import mysql.connector
from mysql.connector import Error
import pandas as pd
import requests
import os
import json
import pprint
import requests.exceptions
def get_api_response(url, headers, params):
try:
response = requests.get(url, headers=headers, params=params)
response.raise_for_status()
except requests.exceptions.HTTPError as errh:
return "An Http Error occurred: " + repr(errh)
except requests.exceptions.ConnectionError as errc:
return "An Error Connecting to the API occurred: " + repr(errc)
except requests.exceptions.Timeout as errt:
return "A Timeout Error occurred: " + repr(errt)
except requests.exceptions.InvalidHeader as erri:
return "A Header Error occurred: " + repr(erri)
except requests.exceptions.RequestException as err:
return "An Unknown Error occurred: " + repr(err)
return response.json()
stock = str(input())
apikey="rrwuwqQ9A24rOR5zRRUI61jSpPSzyJNb1jGG263E"
headers = {
'x-api-key': apikey
}
querystring = {"symbols":stock}
url = "https://yfapi.net/v6/finance/quote"
url_2 = "https://yfapi.net/v6/finance/quote?region=US&lang=en&symbols="+stock
url_3 = "https://yfapi.net/v11/finance/quoteSummary/" + stock + "?lang=en®ion=US&modules=defaultKeyStatistics%2CassetProfile"
url_4 = "https://yfapi.net/ws/insights/v1/finance/insights?symbol="+stock
response1 = requests.request("GET", url, headers=headers, params=querystring)
response2 = requests.request("GET", url_2, headers=headers, params=querystring)
response3 = requests.request("GET", url_3, headers=headers, params=querystring)
response4 = requests.request("GET", url_4, headers=headers, params=querystring)
data1 = response1.json()
data2 = response2.json()
data3 = response3.json()
data4 = response4.json()
print(data1)
def get_stock_info(stock):
stock = str(input())
apikey="rrwuwqQ9A24rOR5zRRUI61jSpPSzyJNb1jGG263E"
headers = {
'x-api-key': apikey
}
querystring = {"symbols":stock}
url = "https://yfapi.net/v6/finance/quote"
url_2 = "https://yfapi.net/v6/finance/quote?region=US&lang=en&symbols="+stock
url_3 = "https://yfapi.net/v11/finance/quoteSummary/" + stock + "?lang=en®ion=US&modules=defaultKeyStatistics%2CassetProfile"
url_4 = "https://yfapi.net/ws/insights/v1/finance/insights?symbol="+stock
response1 = requests.request("GET", url, headers=headers, params=querystring)
response2 = requests.request("GET", url_2, headers=headers, params=querystring)
response3 = requests.request("GET", url_3, headers=headers, params=querystring)
response4 = requests.request("GET", url_4, headers=headers, params=querystring)
data1 = response1.json()
data2 = response2.json()
data3 = response3.json()
data4 = response4.json()
print(data1)
stock_json = response.json()
print(stock_json['quoteResponse']['result'][0]["displayName"] + " Price:$" + str(stock_json['quoteResponse']['result'][0]["regularMarketPrice"]))
def exc_handle(url, response_type):
json_string = get_api_response(url,response_type)
return print(json_string)