-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinancebackend.py
executable file
·70 lines (58 loc) · 2 KB
/
financebackend.py
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
'''
File name: financebackend.py
Author: lhvphan
Email: lhvphan@gmail.com
Date created: April 3, 2022
Python Version: 3.8
Description: call yahoo finance to get ticker information, returns json object
'''
import yfinance as yf
import json
class FinanceBackend:
def getone(self, input):
"""
function getone: get information for one stock
:param self: self object
:param input: expect stock ticker type string
:return returnval: json object
"""
# get info for stock symbol
print("stock symbol input: %s" % input)
stock = yf.Ticker(input)
# create temp object (dict)
x = {
"symbol": stock.info["symbol"],
"price": stock.info["currentPrice"],
"high": stock.info["fiftyTwoWeekHigh"],
"low": stock.info["fiftyTwoWeekLow"]
}
# convert object to json object and return
returnval = json.dumps(x)
print("stock is (json): %s" % returnval)
return returnval
def getmany(self, input):
"""
function getmany: get information for more than one stock
:param self: self object
:param input: expect stock tickers type json object
:return returnval: json object
"""
# create empty object (dict) array
returnval = []
# loop though each key pair
for key in input:
#print(key, ":", input[key])
stock = yf.Ticker(input[key])
# create temp object (dict)
x = {
"symbol": stock.info["symbol"],
"price": stock.info["currentPrice"],
"high": stock.info["fiftyTwoWeekHigh"],
"low": stock.info["fiftyTwoWeekLow"]
}
print("%s : %s" % (key,x))
returnval.append(x)
# convert dict array to json and return
returnval = json.dumps(returnval)
print("Stocks are (json): %s" % returnval)
return returnval