-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathYahooForward.py
66 lines (52 loc) · 2.23 KB
/
YahooForward.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
import os
import pandas as pd
from glob import glob
from Parser import Parser
dataPath = '/Users/marcel/workspace/Equities/data/'
keyStatsPath = dataPath + 'Yahoo/forward/data/*'
fileGlob = '*html'
p = Parser(dataPath)
fullpaths = (e for e in glob(keyStatsPath))
output_df = pd.DataFrame()
for fullpath in fullpaths:
output = {}
keyStats = {}
regex = 'market_time.....,\s(...)\s(..),\s(....)'
ticker = p.getTickerFromFullPath(fullpath, 'forward/data/', '.html')
forwardDate, _ = p.getDateFromMarketTime(fullpath)
fwdUnixTime, date = p.getDateFromMarketTime(fullpath)
# pull key stats from Yahoo screen
for feature in p.features:
value = p.searchSourceForFeature(fullpath, feature)
value = p.cleanup(value)
keyStats[feature] = value
# Pull stock price and s&p adjusted close on date and forward date and clean missing values
price = p.getValueFromDf(p.stock_df, ticker.upper(), p.oneYearAgo(fwdUnixTime))
priceFwd = p.getValueFromDf(p.stock_df, ticker.upper(),forwardDate)
sp500 = p.getValueFromDf(p.sp500_df, 'Adjusted Close', p.oneYearAgo(fwdUnixTime))
sp500Fwd = p.getValueFromDf(p.sp500_df, 'Adjusted Close', forwardDate)
# clean up
p.setDefaultIfNone(price, fwdUnixTime)
p.setDefaultIfNone(priceFwd, fwdUnixTime)
p.setDefaultIfNone(sp500, fwdUnixTime)
p.setDefaultIfNone(sp500Fwd, fwdUnixTime)
# calculate returns and alphas
stockReturn = p.getReturn(price, priceFwd)
sp500Return = p.getReturn(sp500, sp500Fwd)
if all([stockReturn, sp500Return]):
difference = stockReturn - sp500Return
else:
difference = 0.0
# concatenate key stats from screens at forward date, stock price from Quandl at forward date
# and index value from Yahoo index at forward date
output['difference'] = difference
output['stock_p_change'] = stockReturn
output['sp500_p_change'] = sp500Return
output['stock_price'] = price
output['sp500_value'] = sp500
output['ticker'] = ticker
output['fullpath'] = fullpath
output = dict({k: keyStats[v] for (k, v) in p.featuresDict.items()}, **output)
output_df = output_df.append(output, ignore_index=True)
# write output dataframe to file
output_df.to_csv("forward_sample_ALL.csv")