-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFinancialData.py
More file actions
162 lines (130 loc) · 4.95 KB
/
FinancialData.py
File metadata and controls
162 lines (130 loc) · 4.95 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import pandas as pd
from urllib.request import urlopen, Request
import json, feedparser
from pandas.io.json import json_normalize
class financial_data:
def __init__(self):
self.end_point_prefix_ = r'https://api.iextrading.com/1.0/'
def get_rss_news_(self):
feed_sources = ['http://feeds.reuters.com/reuters/companyNews?format=xml', 'http://feeds.reuters.com/news/wealth?format=xml', 'http://feeds.reuters.com/reuters/businessNews?format=xml']
rss_result = {}
for url in feed_sources:
feed = feedparser.parse(url)
for item in feed['entries']:
partial = []
partial.append(date_parser(item['published']).date().__str__())
part_1 = item['title']
html = bs(item['summary'], features="html.parser")
part_2 = html.text
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', part_2)
for ur in urls:
part_2 = part_2.replace(ur, '')
partial.append('%s %s'%(part_1, part_2))
rss_result[len(rss_result)] = partial
for k in range(len(rss_result)):
words = nltk.tokenize.word_tokenize(rss_result[k][1].lower().replace("'",""))
for j in ref_str:
for i in range(words.count(j)):
words.remove(j)
for j in string.punctuation:
for i in range(words.count(j)):
words.remove(j)
for j in stopwords.words('english'):
for i in range(words.count(j)):
words.remove(j)
rss_result[k][1] = words
return rss_result
def all_ticker(self):
suffix = r'ref-data/symbols'
valid_ticker = self.endpoint_connector(self.end_point_prefix_+suffix)['symbol']
return valid_ticker
def verify_ticker(self, ticker):
return [x for x in ticker if x in set(self.all_ticker())]
def endpoint_connector(self, url, nest=None):
request = Request(url)
response = urlopen(request)
elevations = response.read()
data = json.loads(elevations.decode("utf8"))
if nest:
data = json_normalize(data[nest])
else:
data = json_normalize(data)
return pd.DataFrame(data)
def get_quote_and_trade(self, ticker):
ticker = self.verify_ticker(ticker)
if ticker:
suffix = r'tops?symbols='
symbols = ','.join(ticker)
df = self.endpoint_connector(self.end_point_prefix_ + suffix + symbols)
df['lastSaleTime'] = pd.to_datetime(df['lastSaleTime'], unit='ms')
df['lastUpdated'] = pd.to_datetime(df['lastUpdated'], unit='ms')
df.set_index(['symbol'], inplace=True)
return df
else:
print('[-] One(or more) of your requested tickers doesn\'t exist')
def get_latest_trade(self, ticker):
ticker = self.verify_ticker(ticker)
if ticker:
suffix = r'tops/last?symbols='
symbols = ','.join(ticker)
df = self.endpoint_connector(self.end_point_prefix_ + suffix + symbols)
df['time'] = pd.to_datetime(df['time'], unit='ms')
df.set_index(['symbol'], inplace=True)
return df
else:
print('[-] One(or more) of your requested tickers doesn\'t exist')
def get_news(self, ticker, count=1):
ticker = self.verify_ticker(ticker)
final_df = pd.DataFrame({})
if ticker:
for symbol in ticker:
suffix = r'stock/{symbol}/news/last/{count}'.format(symbol=symbol, count=count)
df = self.endpoint_connector(self.end_point_prefix_ + suffix)
df['time'] = pd.to_datetime(df['datetime'])
del df['datetime']
df['symbol'] = symbol
df = df[['symbol', 'time', 'headline', 'summary', 'source', 'url', 'related']]
final_df = final_df.append(df, ignore_index=True)
return final_df
else:
print('[-] One(or more) of your requested tickers doesn\'t exist')
def get_financials(self, ticker):
ticker = self.verify_ticker(ticker)
final_df = pd.DataFrame({})
if ticker:
for symbol in ticker:
suffix = r'stock/{symbol}/financials'.format(symbol=symbol)
df = self.endpoint_connector(self.end_point_prefix_ + suffix, 'financials')
df['symbol'] = symbol
final_df = final_df.append(df, ignore_index=True)
return final_df
else:
print('[-] One(or more) of your requested tickers doesn\'t exist')
def get_earnings(self, ticker):
ticker = self.verify_ticker(ticker)
final_df = pd.DataFrame({})
if ticker:
for symbol in ticker:
suffix = r'stock/{symbol}/earnings'.format(symbol=symbol)
df = self.endpoint_connector(self.end_point_prefix_ + suffix, 'earnings')
df['symbol'] = symbol
final_df = final_df.append(df, ignore_index=True)
return final_df
else:
print('[-] One(or more) of your requested tickers doesn\'t exist')
def get_trade_bars(self, ticker, bucket='1m'):
ticker = self.verify_ticker(ticker)
final_df = pd.DataFrame({})
if ticker:
for symbol in ticker:
suffix = r'stock/{symbol}/chart/{bucket}'.format(symbol=symbol, bucket=bucket)
df = self.endpoint_connector(self.end_point_prefix_ + suffix)
df['symbol'] = symbol
final_df = final_df.append(df, ignore_index=True)
return final_df
else:
print('[-] One(or more) of your requested tickers doesn\'t exist')
if __name__ == "__main__":
iex = financial_data()
print(iex.get_news(['AAPL']))
print(iex.get_trade_bars(['AAPL', 'IBM']))