-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_information.py
63 lines (39 loc) · 1.63 KB
/
stock_information.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
from iexfinance.stocks import Stock
from forex_python.converter import CurrencyRates
# GENERAL DATA
# private auth key to access stock data
# IEX_TOKEN =
# caching variables that are given values after one access to the data
usd_to_eur = None
stocks = None
stocks_have_been_updated = False
def get_price(symbol):
"""Returns the price of a stock given the symbol."""
stock_string = get_stock_string(symbol)
price_in_dollar = float(extract_price_string(stock_string))
price_in_euro = dollars_to_euros(price_in_dollar)
return float("%.2f" % price_in_euro)
def get_stock_string(symbol):
"""Returns a string representation of the stock given the symbol."""
stock = Stock(symbol, token=IEX_TOKEN)
stock_string = stock.get_quote().__str__()
return stock_string
def extract_price_string(stock_data):
"""Support method for get_price. Extracts the price string from the stock (quote) data. Hardcoded values."""
start = "'latestPrice': "
end = ","
stock_data = stock_data[stock_data.find(start) + len(start):]
stock_data = stock_data[:stock_data.find(end)]
return stock_data
def dollars_to_euros(dollars):
global usd_to_eur
# because the API to get the rates is really slow, on initial startup the conversion rate is fetched
# and then gets cached so the next conversion will be seamless
if usd_to_eur is None:
set_usd_to_eur_rate()
return dollars * usd_to_eur
def set_usd_to_eur_rate():
"""Support method. Caches the conversion rate in a global variable."""
converter = CurrencyRates()
global usd_to_eur
usd_to_eur = converter.convert("USD", "EUR", 1)