-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_label.py
executable file
·57 lines (49 loc) · 2.29 KB
/
create_label.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
#!/usr/bin/python
import util
import json
import datetime
from math import log
# input file name: ./input/stockPrices_raw.json
# output file name: ./input/stockReturns.json
# json structure: crawl daily price data from yahoo finance
# term (short/mid/long)
# / | \
# ticker A ticker B ticker C
# / \ / \ / \
# date1 date2 date1 date2 date1 date2
#
# Note: short: 1 day return, mid: 7 day return, long 28 day return
# calc long/mid term influence
def calc_mid_long_return(ticker, date, delta, priceSet):
baseDate = datetime.datetime.strptime(date, "%Y-%m-%d")
prevDate = (baseDate - datetime.timedelta(days=1)).strftime("%Y-%m-%d")
nextDate = (baseDate + datetime.timedelta(days=delta)).strftime("%Y-%m-%d")
try:
return_self = log(priceSet[ticker]['adjClose'][nextDate]) - log(priceSet[ticker]['adjClose'][prevDate])
return_sp500 = log(priceSet['^GSPC']['adjClose'][nextDate]) - log(priceSet['^GSPC']['adjClose'][prevDate])
return True, round(return_self - return_sp500, 4) # relative return
except:
return False, 0
def main():
raw_price_file = 'input/stockPrices_raw.json'
with open(raw_price_file) as file:
print("Loading price info ...")
priceSet = json.load(file)
dateSet = priceSet['^GSPC']['adjClose'].keys()
returns = {'short': {}, 'mid': {}, 'long': {}} # 1-depth dictionary
for ticker in priceSet:
print(ticker)
for term in ['short', 'mid', 'long']:
returns[term][ticker] = {} # 2-depth dictionary
for day in dateSet:
date = datetime.datetime.strptime(day, "%Y-%m-%d").strftime("%Y%m%d") # change date 2014-01-01 to 20140101
tag_short, return_short = calc_mid_long_return(ticker, day, 0, priceSet)
tag_mid, return_mid = calc_mid_long_return(ticker, day, 6, priceSet)
tag_long, return_long = calc_mid_long_return(ticker, day, 27, priceSet)
if tag_short: returns['short'][ticker][date] = return_short
if tag_mid: returns['mid'][ticker][date] = return_mid
if tag_long: returns['long'][ticker][date] = return_long
with open('./input/stockReturns.json', 'w') as outfile:
json.dump(returns, outfile, indent=4)
if __name__ == "__main__":
main()