forked from asimarahim/Financial-Trading-using-RRL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_scraper.py
45 lines (35 loc) · 1.49 KB
/
data_scraper.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
import yfinance as yf
import pandas as pd
import datetime
# Reference yfinance tutorial here: https://towardsdatascience.com/downloading-historical-stock-prices-in-python-93f85f059c1f
# and here: https://blog.quantinsti.com/download-futures-data-yahoo-finance-library-python/
def retrieve_data(asset, start, end):
# print stock infor to confirm I pulled the intended asset
security_info = yf.Ticker(asset).info
# stock_info.keys() for other properties you can explore
# create empty dataframe
security_final = pd.DataFrame()
try:
# download the stock price
security = []
security = yf.download(asset, start=start, end=end, progress=False)
# append the individual stock prices
if len(security) == 0:
None
else:
security['Name']=asset
security.index = pd.to_datetime(security.index)
security_final = security_final.append(security,sort=False)
except Exception:
None
return security_final
if __name__ == "__main__":
start = datetime.datetime(2000,12,31)
end = datetime.datetime(2021,12,4)
folder="Security Data/"
exxon_data = retrieve_data("XOM", start, end)
exxon_data.to_csv(folder+'Exxon_20yr_OHLC.csv')
corn_data = retrieve_data("ZC=F", start, end)
corn_data.to_csv(folder+'Corn_20yr_OHLC.csv')
nyse_data = retrieve_data("^NYA", start, end)
nyse_data.to_csv(folder+'NYSE_20yr_OHLC.csv')