-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_helper.py
250 lines (198 loc) · 9.77 KB
/
project_helper.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import helper
import scipy.stats
from colour import Color
import numpy as np
import pandas as pd
import plotly as py
import plotly.graph_objs as go
import plotly.offline as offline_py
offline_py.init_notebook_mode(connected=True)
def _generate_stock_trace(prices):
return go.Scatter(
name='Index',
x=prices.index,
y=prices,
line={'color': helper.color_scheme['main_line']})
def _generate_buy_annotations(prices, signal):
return [{
'x': index, 'y': price, 'text': 'Long', 'bgcolor': helper.color_scheme['background_label'],
'ayref': 'y', 'ax': 0, 'ay': 20}
for index, price in prices[signal == 1].iteritems()]
def _generate_sell_annotations(prices, signal):
return [{
'x': index, 'y': price, 'text': 'Short', 'bgcolor': helper.color_scheme['background_label'],
'ayref': 'y', 'ax': 0, 'ay': 160}
for index, price in prices[signal == -1].iteritems()]
def _generate_second_tetration_stock(stock_symbol, dates):
"""
Generate stock that follows the second tetration curve
:param stock_symbol: Stock Symbol
:param dates: Dates for ticker
:return: Stock data
"""
n_stock_columns = 5
linear_line = np.linspace(1, 5, len(dates))
all_noise = ((np.random.rand(n_stock_columns, len(dates)) - 0.5) * 0.01)
sector_stock = pd.DataFrame({
'ticker': stock_symbol,
'date': dates,
'base_line': np.power(linear_line, linear_line)})
sector_stock['base_line'] = sector_stock['base_line'] + all_noise[0]*sector_stock['base_line']
sector_stock['adj_open'] = sector_stock['base_line'] + all_noise[1]*sector_stock['base_line']
sector_stock['adj_close'] = sector_stock['base_line'] + all_noise[2]*sector_stock['base_line']
sector_stock['adj_high'] = sector_stock['base_line'] + all_noise[3]*sector_stock['base_line']
sector_stock['adj_low'] = sector_stock['base_line'] + all_noise[4]*sector_stock['base_line']
sector_stock['adj_high'] = sector_stock[['adj_high', 'adj_open', 'adj_close']].max(axis=1)
sector_stock['adj_low'] = sector_stock[['adj_low', 'adj_open', 'adj_close']].min(axis=1)
return sector_stock.drop(columns='base_line')
def generate_tb_sector(dates):
"""
Generate TB sector of stocks
:param dates: Dates that stocks should have market data on
:return: TB sector stocks
"""
symbol_length = 6
stock_names = [
'kaufmanniana', 'clusiana', 'greigii', 'sylvestris', 'turkestanica', 'linifolia', 'gesneriana',
'humilis', 'tarda', 'saxatilis', 'dasystemon', 'orphanidea', 'kolpakowskiana', 'praestans',
'sprengeri', 'bakeri', 'pulchella', 'biflora', 'schrenkii', 'armena', 'vvedenskyi', 'agenensis',
'altaica', 'urumiensis']
return [
_generate_second_tetration_stock(stock_name[:symbol_length].upper(), dates)
for stock_name in stock_names]
def plot_stock(prices, title):
config = helper.generate_config()
layout = go.Layout(title=title)
stock_trace = _generate_stock_trace(prices)
offline_py.iplot({'data': [stock_trace], 'layout': layout}, config=config)
def plot_high_low(prices, lookback_high, lookback_low, title):
config = helper.generate_config()
layout = go.Layout(title=title)
stock_trace = _generate_stock_trace(prices)
high_trace = go.Scatter(
x=lookback_high.index,
y=lookback_high,
name='Column lookback_high',
line={'color': helper.color_scheme['major_line']})
low_trace = go.Scatter(
x=lookback_low.index,
y=lookback_low,
name='Column lookback_low',
line={'color': helper.color_scheme['minor_line']})
offline_py.iplot({'data': [stock_trace, high_trace, low_trace], 'layout': layout}, config=config)
def plot_signal(price, signal, title):
config = helper.generate_config()
buy_annotations = _generate_buy_annotations(price, signal)
sell_annotations = _generate_sell_annotations(price, signal)
layout = go.Layout(
title=title,
annotations=buy_annotations + sell_annotations)
stock_trace = _generate_stock_trace(price)
offline_py.iplot({'data': [stock_trace], 'layout': layout}, config=config)
def plot_lookahead_prices(prices, lookahead_price_list, title):
config = helper.generate_config()
layout = go.Layout(title=title)
colors = Color(helper.color_scheme['low_value'])\
.range_to(Color(helper.color_scheme['high_value']), len(lookahead_price_list))
traces = [_generate_stock_trace(prices)]
for (lookahead_prices, lookahead_days), color in zip(lookahead_price_list, colors):
traces.append(
go.Scatter(
x=lookahead_prices.index,
y=lookahead_prices,
name='{} Day Lookahead'.format(lookahead_days),
line={'color': str(color)}))
offline_py.iplot({'data': traces, 'layout': layout}, config=config)
def plot_price_returns(prices, lookahead_returns_list, title):
config = helper.generate_config()
layout = go.Layout(
title=title,
yaxis2={
'title': 'Returns',
'titlefont': {'color': helper.color_scheme['y_axis_2_text_color']},
'tickfont': {'color': helper.color_scheme['y_axis_2_text_color']},
'overlaying': 'y',
'side': 'right'})
colors = Color(helper.color_scheme['low_value'])\
.range_to(Color(helper.color_scheme['high_value']), len(lookahead_returns_list))
traces = [_generate_stock_trace(prices)]
for (lookahead_returns, lookahead_days), color in zip(lookahead_returns_list, colors):
traces.append(
go.Scatter(
x=lookahead_returns.index,
y=lookahead_returns,
name='{} Day Lookahead'.format(lookahead_days),
line={'color': str(color)},
yaxis='y2'))
offline_py.iplot({'data': traces, 'layout': layout}, config=config)
def plot_signal_returns(prices, signal_return_list, titles):
config = helper.generate_config()
layout = go.Layout(
yaxis2={
'title': 'Signal Returns',
'titlefont': {'color': helper.color_scheme['y_axis_2_text_color']},
'tickfont': {'color': helper.color_scheme['y_axis_2_text_color']},
'overlaying': 'y',
'side': 'right'})
colors = Color(helper.color_scheme['low_value'])\
.range_to(Color(helper.color_scheme['high_value']), len(signal_return_list))
stock_trace = _generate_stock_trace(prices)
for (signal_return, signal, lookahead_days), color, title in zip(signal_return_list, colors, titles):
non_zero_signals = signal_return[signal_return != 0]
signal_return_trace = go.Scatter(
x=non_zero_signals.index,
y=non_zero_signals,
name='{} Day Lookahead'.format(lookahead_days),
line={'color': str(color)},
yaxis='y2')
buy_annotations = _generate_buy_annotations(prices, signal)
sell_annotations = _generate_sell_annotations(prices, signal)
layout['title'] = title
layout['annotations'] = buy_annotations + sell_annotations
offline_py.iplot({'data': [stock_trace, signal_return_trace], 'layout': layout}, config=config)
def plot_signal_histograms(signal_list, title, subplot_titles):
assert len(signal_list) == len(subplot_titles)
signal_series_list = [signal.stack() for signal in signal_list]
all_values = pd.concat(signal_series_list)
x_range = [all_values.min(), all_values.max()]
y_range = [0, 1500]
config = helper.generate_config()
colors = Color(helper.color_scheme['low_value']).range_to(Color(helper.color_scheme['high_value']), len(signal_series_list))
fig = py.subplots.make_subplots(rows=1, cols=len(signal_series_list), subplot_titles=subplot_titles, print_grid=False)
fig['layout'].update(title=title, showlegend=False)
for series_i, (signal_series, color) in enumerate(zip(signal_series_list, colors), 1):
filtered_series = signal_series[signal_series != 0].dropna()
trace = go.Histogram(x=filtered_series, marker={'color': str(color)})
fig.append_trace(trace, 1, series_i)
fig['layout']['xaxis{}'.format(series_i)].update(range=x_range)
fig['layout']['yaxis{}'.format(series_i)].update(range=y_range)
offline_py.iplot(fig, config=config)
def plot_signal_to_normal_histograms(signal_list, title, subplot_titles):
assert len(signal_list) == len(subplot_titles)
signal_series_list = [signal.stack() for signal in signal_list]
all_values = pd.concat(signal_series_list)
x_range = [all_values.min(), all_values.max()]
y_range = [0, 1500]
config = helper.generate_config()
fig = py.subplots.make_subplots(rows=1, cols=len(signal_series_list), subplot_titles=subplot_titles, print_grid=False)
fig['layout'].update(title=title)
for series_i, signal_series in enumerate(signal_series_list, 1):
filtered_series = signal_series[signal_series != 0].dropna()
filtered_series_trace = go.Histogram(
x=filtered_series,
marker={'color': helper.color_scheme['low_value']},
name='Signal Return Distribution',
showlegend=False)
normal_trace = go.Histogram(
x=np.random.normal(np.mean(filtered_series), np.std(filtered_series), len(filtered_series)),
marker={'color': helper.color_scheme['shadow']},
name='Normal Distribution',
showlegend=False)
fig.append_trace(filtered_series_trace, 1, series_i)
fig.append_trace(normal_trace, 1, series_i)
fig['layout']['xaxis{}'.format(series_i)].update(range=x_range)
fig['layout']['yaxis{}'.format(series_i)].update(range=y_range)
# Show legened
fig['data'][0]['showlegend'] = True
fig['data'][1]['showlegend'] = True
offline_py.iplot(fig, config=config)