Skip to content

Commit 3881d2c

Browse files
committed
WIP refactoring by ticker
1 parent bd06132 commit 3881d2c

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

040_ticker_plots

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,82 +6,105 @@ import matplotlib.dates as mdates
66
import matplotlib.ticker as mticker
77
import pandas as pd
88

9-
plt.suptitle('AAPL ticker')
10-
plt.xlabel('Date')
11-
num_rows_num_cols_tuple = (6,1)
12-
139
dates_formatter = '%Y-%m-%d'
1410
dateparse = lambda x: pd.datetime.strptime(x, dates_formatter)
1511

12+
def load_df(path, ticker, date_field):
13+
if (False == isinstance(path, basestring)):
14+
dfs = []
15+
for curr_path in path:
16+
file_path = curr_path + ticker.upper() + '.csv'
17+
curr_df = pd.read_csv(file_path, parse_dates=[date_field], date_parser=dateparse)
18+
dfs.append(curr_df)
19+
merged_df = pd.concat(dfs)
20+
return merged_df
21+
else:
22+
file_path = path + ticker.upper() + '.csv'
23+
df = pd.read_csv(file_path, parse_dates=[date_field], date_parser=dateparse)
24+
return df;
1625

17-
def build_plot(cur_dict, shared_axis):
18-
eps_series = pd.read_csv(cur_dict['file_path'], parse_dates=[cur_dict['date_field_name']], date_parser=dateparse)
26+
def build_plot(cur_dict, ticker, shared_axis):
27+
current_df = load_df(cur_dict['path_prefixes'], ticker, cur_dict['date_field_name'])
1928
ax = plt.subplot2grid(num_rows_num_cols_tuple, (cur_dict['row_position'], 0), rowspan=cur_dict['rowspan'])
2029
if (shared_axis):
2130
ax = plt.subplot2grid(num_rows_num_cols_tuple, (cur_dict['row_position'], 0), rowspan=cur_dict['rowspan'], sharex=shared_axis)
2231
print 'Shared axis!'
23-
ax.plot(eps_series[cur_dict['date_field_name']],eps_series[cur_dict['value_field_name']])
32+
ax.plot(current_df[cur_dict['date_field_name']],current_df[cur_dict['value_field_name']])
2433
ax.grid(True)
2534
ax.yaxis.set_major_locator(mticker.MaxNLocator(nbins='5', prune='upper'))
2635
plt.ylabel(cur_dict['y_label'])
2736

2837
return ax
2938

39+
3040
eps_dict = {
31-
'file_path' : './data/calculated_ratios/eps_AAPL.csv',
41+
'path_prefixes' : './data/calculated_ratios/eps_',
3242
'date_field_name' : 'Date',
3343
'value_field_name' : 'EPS',
3444
'y_label' : 'EPS',
3545
'row_position' : 0,
3646
'rowspan' : 1
3747
}
3848
peg_dict = {
39-
'file_path' : './data/calculated_ratios/peg_AAPL.csv',
49+
'path_prefixes' : './data/calculated_ratios/peg_',
4050
'date_field_name' : 'Date',
4151
'value_field_name' : 'PEG',
4252
'y_label' : 'PEG',
4353
'row_position' : 1,
4454
'rowspan' : 1
4555
}
4656
pe_median_dict = {
47-
'file_path' : './data/calculated_ratios/pe_median_AAPL.csv',
57+
'path_prefixes' : './data/calculated_ratios/pe_median_',
4858
'date_field_name' : 'Date',
4959
'value_field_name' : 'PE_median',
5060
'y_label' : 'P/E median',
5161
'row_position' : 2,
5262
'rowspan' : 1
5363
}
54-
5564
outstanding_shares_dict = {
56-
'file_path' : './data/fundamentals/outstanding_shares_RAYMOND_AAPL.csv',
65+
'path_prefixes' : './data/fundamentals/outstanding_shares_RAYMOND_',
5766
'date_field_name' : 'Date',
5867
'value_field_name' : 'Value',
5968
'y_label' : 'Outstanding shares',
6069
'row_position' : 3,
6170
'rowspan' : 1
6271
}
6372
closing_price_dict = {
64-
'file_path' : './data/stock_prices/close_prices_pre_2016_02_AAPL.csv',
73+
'path_prefixes' : ['./data/stock_prices/close_prices_pre_2016_02_', './data/stock_prices/close_prices_post_2016_02_'],
6574
'date_field_name' : 'Date',
6675
'value_field_name' : 'Close',
6776
'y_label' : 'Stock closing price',
6877
'row_position' : 4,
69-
'rowspan' : 2
78+
'rowspan' : 3
79+
}
80+
81+
dicts = {
82+
'eps_data': eps_dict,
83+
'peg_data': peg_dict,
84+
'pe_median_data': pe_median_dict,
85+
'outstanding_shares_data': outstanding_shares_dict,
86+
'closing_price_data': closing_price_dict
7087
}
7188

72-
ax_eps = build_plot(eps_dict, None)
73-
ax_peg = build_plot(peg_dict, ax_eps)
74-
ax_pe_median = build_plot(pe_median_dict, ax_eps)
75-
ax_outstanding_shares = build_plot(outstanding_shares_dict, ax_eps)
76-
ax_closing_price = build_plot(closing_price_dict, ax_eps)
89+
num_rows_num_cols_tuple = (7,1)
7790

78-
plt.xticks(rotation=45)
79-
plt.setp(ax_eps.get_xticklabels(), visible=False)
80-
plt.setp(ax_pe_median.get_xticklabels(), visible=False)
81-
plt.setp(ax_peg.get_xticklabels(), visible=False)
82-
plt.setp(ax_outstanding_shares.get_xticklabels(), visible=False)
83-
plt.subplots_adjust(left=.1,right=.96,top=.94,bottom=.16,hspace=0)
91+
def get_plt(dicts, ticker):
92+
plt.suptitle(ticker + ' ticker')
93+
ax_eps = build_plot(dicts['eps_data'], ticker, None)
94+
ax_peg = build_plot(dicts['peg_data'], ticker, ax_eps)
95+
ax_pe_median = build_plot(dicts['pe_median_data'], ticker, ax_eps)
96+
ax_outstanding_shares = build_plot(dicts['outstanding_shares_data'], ticker, ax_eps)
97+
ax_closing_price = build_plot(dicts['closing_price_data'], ticker, ax_eps)
8498

99+
plt.xlabel('Date')
100+
plt.xticks(rotation=45)
101+
plt.setp(ax_eps.get_xticklabels(), visible=False)
102+
plt.setp(ax_pe_median.get_xticklabels(), visible=False)
103+
plt.setp(ax_peg.get_xticklabels(), visible=False)
104+
plt.setp(ax_outstanding_shares.get_xticklabels(), visible=False)
105+
plt.subplots_adjust(left=.1,right=.96,top=.94,bottom=.16,hspace=0)
85106

86-
plt.show()
107+
return plt
87108

109+
aapl_plt = get_plt(dicts, 'AAPL')
110+
aapl_plt.show()

0 commit comments

Comments
 (0)