-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmscript.py
More file actions
50 lines (38 loc) · 1.48 KB
/
Copy pathmscript.py
File metadata and controls
50 lines (38 loc) · 1.48 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
## Main Download and Format Data Script
import datetime
import pandas as pd
from pandas import DataFrame
from pandas.io.data import DataReader
# Build up a potential list of stock names
# We got the NASDAQ, NYSE, and AMEX lists from:
# http://www.nasdaq.com/screening/company-list.aspx
# list of s and p 500 companies and tickers
# http://data.okfn.org/data/core/s-and-p-500-companies/r/constituents.csv
tickerdf = pd.read_csv('SandP500.csv')
tickers = list(symboldf['Symbol'].values)
symbols = []
for ticker in tickers:
try:
r = DataReader(ticker, "yahoo", start=datetime.datetime(2010, 01, 01))
r['Symbol'] = ticker
symbols.append(r)
print "Obtained data for ticker %s" % ticker
except:
print "No data for ticker %s" % ticker
df = pd.concat(symbols)
cell= df[['Symbol','Adj Close']]
cell.reset_index().sort(['Symbol', 'Date'], ascending=[1,0]).set_index('Symbol')
cell.to_pickle('close_price.pkl')
####### Begin contents of corr1.py
#df = pd.read_csv('all_stocks.csv')
#df['Date'] = pd.to_datetime(df['Date'],format='%d/%m/%Y') # convert to datetime
# Make a single dataframe of open prices
#tkrs = sorted(list(set(df['Symbol'])))
#fdf = pd.DataFrame()
#for j in xrange(2310,len(tkrs)):
# tmpdate = df[df.Symbol==tkrs[j]]['Date']
# tmpopen=df[df.Symbol==tkrs[j]]['Open']
# tmpdf=pd.DataFrame(tmpopen.values, index=tmpdate.values, columns=[tkrs[j]])
# fdf = pd.concat([fdf,tmpdf], axis=1)
#
#fdf.to_pickle('open_price.pkl')