-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path030_ratios_eps
executable file
·91 lines (78 loc) · 3.79 KB
/
030_ratios_eps
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
#!/usr/bin/env python2
import fnmatch, re
import os
import pandas as pd
import datetime
import modules.config_details as conf_info
# ----------------
# HELPER FUNCTIONS
# ----------------
def build_outstanding_shares_dict(files_dir):
outstanding_shares_pattern = re.compile(conf_info.out_shares_filename_regex)
outstanding_shares_dict = {}
for file_name in os.listdir(files_dir):
if fnmatch.fnmatch(file_name, conf_info.out_shares_filename_glob):
current_ticker = outstanding_shares_pattern.search(file_name).group(1)
print 'Processing file:', file_name
print 'Processing ticker:', current_ticker
outstanding_shares_dict[current_ticker] = pd.read_csv(files_dir + file_name)
return outstanding_shares_dict
def build_fundamentals_data_dict(files_dir, tickers):
fundamentals_dict = {}
for ticker in tickers:
fundamentals_ticker_filename_and_path = files_dir + conf_info.get_fundamentals_filename(ticker)
# print fundamentals_ticker_filename_and_path
fundamentals_dict[ticker] = pd.read_csv(fundamentals_ticker_filename_and_path)
return fundamentals_dict
def build_eps_dataframe(outstanding_shares_df, fundamentals_df):
# fundamentals column names
FUDAMENTALS_END_DATE = 'end_date'
FUDAMENTALS_NET_INCOME = 'net_income'
FUDAMENTALS_DIVIDEND = 'dividend'
# outstanding shares dataframe details
DATE_FIELD = 'Date'
DATE_FORMAT = '%Y-%m-%d'
VALUE_FIELD = 'Value'
current_os_df = pd.DataFrame()
current_os_df = pd.to_datetime(outstanding_shares_df[DATE_FIELD])
# EPS dataframe
EPS_FIELD = 'EPS'
eps_df = pd.DataFrame(columns=[DATE_FIELD,EPS_FIELD])
for index_f_df,rows in fundamentals_df.iterrows():
# fundamentals details
EPS_CURRENT_DATE = rows[FUDAMENTALS_END_DATE]
EPS_NET_INCOME = rows[FUDAMENTALS_NET_INCOME]
EPS_DIVIDEND = rows[FUDAMENTALS_DIVIDEND]
# outstanding shares details
most_updated_os_date = current_os_df.loc[current_os_df <= rows[FUDAMENTALS_END_DATE]].max()
most_updated_outstanding_shares_row = outstanding_shares_df[outstanding_shares_df[DATE_FIELD] == most_updated_os_date.strftime(DATE_FORMAT)]
EPS_OUTSTANDING_SHARES = most_updated_outstanding_shares_row[VALUE_FIELD].item()
# EPS calculation
EPS = (EPS_NET_INCOME - EPS_DIVIDEND) / EPS_OUTSTANDING_SHARES
eps_df.loc[index_f_df] = [EPS_CURRENT_DATE,EPS]
return eps_df
def build_all_eps_dataframes(files_dir, ratios_storage_dir):
# making sure the folder exists
if not os.path.exists(ratios_storage_dir):
os.makedirs(ratios_storage_dir)
# collecting the data
outstanding_shares_dict = build_outstanding_shares_dict(conf_info.fundamentals_dir)
available_tickers = outstanding_shares_dict.keys()
fundamentals_tickers_dict = build_fundamentals_data_dict(conf_info.fundamentals_dir, available_tickers)
# looping on the tickers
for ticker in available_tickers:
print str(datetime.datetime.now()) + ' Processing ticker:',ticker
current_eps_df = build_eps_dataframe(outstanding_shares_dict[ticker], fundamentals_tickers_dict[ticker])
# saving the EPS df as CSV file
current_filename_with_path = ratios_storage_dir + conf_info.get_eps_filename(ticker)
current_eps_df.to_csv(current_filename_with_path, index=False)
# ----------
# PROCESSING
# ----------
# outstanding_shares_dict = build_outstanding_shares_dict(fundamentals_dir)
# print 'These tickers have "outstanding shares" historical data:', outstanding_shares_dict.keys()
# fundamentals_tickers_dict = build_fundamentals_data_dict(fundamentals_dir, outstanding_shares_dict.keys())
# print 'These ticker have fundamentals information:', fundamentals_tickers_dict.keys()
# tsla_eps_df = build_eps_dataframe(outstanding_shares_dict['TSLA'], fundamentals_tickers_dict['TSLA'])
# print 'TSLA EPS:', tsla_eps_df
build_all_eps_dataframes(conf_info.fundamentals_dir, conf_info.ratios_dir)