-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_data.py
174 lines (135 loc) · 6.27 KB
/
get_data.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python
# Author: Ava Lee
import argparse
import datetime
import requests
import io
import pandas as pd
import constants # API keys
from urllib.request import urlopen, Request
from bs4 import BeautifulSoup
import pickle
from unicodedata import normalize
import os
parser = argparse.ArgumentParser(description='Get stock headlines or prices from webscraping or API')
parser.add_argument('-t', "--ticker", dest='ticker', default='', help='Stock ticker to obtain data for')
parser.add_argument('-w', "--website", dest='website', default='finnhub',
help='Website to scrape or get API from: finnhub (default), finviz, alphavantage')
parser.add_argument('-o', "--outdir", dest='outdir', default='parsed_data/', help='Output file directory')
parser.add_argument("-s", "--slice", dest='slice', default='', help='30 day window to get data for; year1month1 being most recent, see AlphaVantage docs')
args = parser.parse_args()
def scraper(ticker, outdir, website='finviz'):
""" Based on https://blog.thecodex.me/sentiment-analysis-tool-for-stock-trading/"""
if website == 'finviz':
url = 'https://finviz.com/quote.ashx?t='
url += ticker
req = Request(url=url, headers={'user-agent': 'my-app'})
response = urlopen(req)
html = BeautifulSoup(response, features='html.parser')
news_table = html.find(id='news-table')
# Parse the HTML to obtain data
parsed_data = []
for row in news_table.findAll('tr'):
title = row.a.text # Get headline
source = row.span.text # Get source of news
# Get date and time
date_data = row.td.text.split(' ')
if len(date_data) == 1:
time = normalize('NFKD', date_data[0]).rstrip()
else:
date = date_data[0]
time = normalize('NFKD', date_data[1]).rstrip()
timestamp = datetime.datetime.strptime(date + ' ' + time, '%b-%d-%y %I:%M%p')
parsed_data.append([timestamp, ticker, title, source])
# Save the parsed data
file = open(outdir + ticker + '_' + str(datetime.date.today()) + '_' + website + '.pkl', 'wb')
pickle.dump(parsed_data, file)
file.close()
def scraper_all(outdir, website='finviz'):
if website == 'finviz':
url = 'https://finviz.com/news.ashx'
req = Request(url=url, headers={'user-agent': 'my-app'})
response = urlopen(req)
html = BeautifulSoup(response, features='html.parser')
news_table = html.find(id='news').select("table")[3].findAll('tr', {'class': 'nn'})
parsed_data = []
for news in news_table:
content = news.text.strip().split('\n')
time = datetime.datetime.strptime(content[0], '%I:%M%p')
timestamp = datetime.datetime.combine(datetime.date.today(), time.time())
title = content[1]
parsed_data.append([timestamp, title])
file = open(outdir + 'all_' + str(datetime.date.today()) + '_' + website + '.pkl', 'wb')
pickle.dump(parsed_data, file)
file.close()
def get_historical_news(ticker, date_from, date_to, outdir, website='finnhub'):
data = {"symbol": ticker,
"from": date_from,
"to": date_to,
"token": constants.FINNHUB_KEY}
if website == 'finnhub':
response = requests.get('https://finnhub.io/api/v1/company-news', data)
df = pd.DataFrame.from_dict(response.json()).drop(['category', 'id', 'image', 'url'], axis=1)
df['datetime'] = pd.to_datetime(df['datetime'],unit='s')
df = df.set_index('datetime')
file = open(f'{outdir}/{ticker}_{date_from}_{date_to}_{website}.pkl', 'wb')
pickle.dump(df, file)
file.close()
def intraday_extended_price_data(ticker, window, interval, website='alphavantage'):
data = {"function": "TIME_SERIES_INTRADAY_EXTENDED",
"symbol": ticker,
"interval": interval,
"slice": window,
"apikey": constants.ALPHAVANTAGE_KEY
}
if website == 'alphavantage':
response = requests.get('https://www.alphavantage.co/query', data).content
df = pd.read_csv(io.StringIO(response.decode('utf-8')))
df = df.set_index('time', drop=True)
file = open(f'{outdir}/{ticker}_{str(datetime.date.today())}_{window}_{interval}_{website}.pkl', 'wb')
pickle.dump(df, file)
file.close()
def intraday_price_data(ticker, interval, website='alphavantage'):
data = { "function": "TIME_SERIES_INTRADAY", # Returns most recent 1-2 months data
"symbol": ticker,
"interval": interval,
"outputsize" : "full", # compact is default (latest 100 data points)
"apikey": constants.ALPHAVANTAGE_KEY
}
if website == 'alphavantage':
response = requests.get('https://www.alphavantage.co/query', data).json()
df = pd.DataFrame.from_dict(response['Time Series (5min)'],
orient='index').sort_index(axis=1)
df = df.rename(columns={ '1. open': 'Open', '2. high': 'High', '3. low': 'Low',
'4. close': 'Close', '5. volume': 'Volume'})
df.index = pd.to_datetime(df.index)
file = open(f'{outdir}/{ticker}_{str(datetime.date.today())}_{interval}_{website}.pkl', 'wb')
pickle.dump(df, file)
file.close()
if __name__ == "__main__":
# Check output directory format
if args.outdir[-1] != '/':
outdir = args.outdir + '/'
else:
outdir = args.outdir
if not (os.path.isdir(outdir)): os.makedirs(outdir)
# Set dates to get historical data for
date_from = '2020-01-01'
date_to = '2021-03-05'
interval = '5min'
# Web scrape or API
if args.website == 'finviz':
if args.ticker != '':
scraper(args.ticker, outdir, args.website)
else:
scraper_all(outdir, args.website)
if args.website == 'finnhub':
if args.ticker == '':
print ("Need to supply ticker in argument -t")
else:
get_historical_news(args.ticker, date_from, date_to, outdir)
if args.website == 'alphavantage':
if args.slice != '':
intraday_extended_price_data(args.ticker, args.slice, interval, website='alphavantage')
else:
intraday_price_data(args.ticker, interval, website='alphavantage')