def plot_benchmark_returns(benchmark_data, etf_data, title): config = helper.generate_config() index_trace = go.Scatter( name="Index", x=benchmark_data.index, y=benchmark_data, line={"color": helper.color_scheme["index"]}, ) etf_trace = go.Scatter( name="ETF", x=etf_data.index, y=etf_data, line={"color": helper.color_scheme["etf"]}, ) layout = go.Layout( title=title, xaxis={"title": "Date"}, yaxis={ "title": "Cumulative Returns", "range": [0, 3] }, ) fig = go.Figure(data=[index_trace, etf_trace], layout=layout) offline_py.iplot(fig, config=config)
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_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_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.tools.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_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 print_dataframe(df, n_rows=10, n_columns=3): missing_val_str = '...' config = helper.generate_config() formatted_df = df.iloc[:n_rows, :n_columns] formatted_df = formatted_df.applymap('{:.3f}'.format) if len(df.columns) > n_columns: formatted_df[missing_val_str] = [missing_val_str]*len(formatted_df.index) if len(df.index) > n_rows: formatted_df.loc[missing_val_str] = [missing_val_str]*len(formatted_df.columns) trace = go.Table( type='table', columnwidth=[1, 3], header={ 'values': [''] + list(formatted_df.columns.values), 'line': {'color': helper.color_scheme['df_line']}, 'fill': {'color': helper.color_scheme['df_header']}, 'font': {'size': 13}}, cells={ 'values': formatted_df.reset_index().values.T, 'line': {'color': helper.color_scheme['df_line']}, 'fill': {'color': [helper.color_scheme['df_header'], helper.color_scheme['df_value']]}, 'font': {'size': 13}}) offline_py.iplot([trace], 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.tools.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)
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_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_returns(returns, title): config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([('Returns', returns, helper.color_scheme['major_line'])]) offline_py.iplot({'data': traces, 'layout': layout}, config=config)
def plot_returns(returns, title): config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([("Returns", returns, helper.color_scheme["major_line"])]) offline_py.iplot({"data": traces, "layout": layout}, config=config)
def plot_returns(returns, title): config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([ ('Returns', returns, helper.color_scheme['major_line'])]) offline_py.iplot({'data': traces, 'layout': layout}, config=config)
def plot_resampled_prices(df_resampled, df, title): config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([ ('Monthly Close', df_resampled, helper.color_scheme['major_line']), ('Close', df, helper.color_scheme['minor_line']) ])
def plot_shifted_returns(df_shited, df, title): config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([ ('Shifted Returns', df_shited, helper.color_scheme['major_line']), ('Returns', df, helper.color_scheme['minor_line']) ])
def plot_stock(prices, title): configure_plotly_browser_state() 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_shifted_returns(df_shited, df, title): config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([ ('Shifted Returns', df_shited, helper.color_scheme['major_line']), ('Returns', df, helper.color_scheme['minor_line'])]) offline_py.iplot({'data': traces, 'layout': layout}, config=config)
def plot_resampled_prices(df_resampled, df, title): config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([ ('Monthly Close', df_resampled, helper.color_scheme['major_line']), ('Close', df, helper.color_scheme['minor_line'])]) offline_py.iplot({'data': traces, 'layout': layout}, config=config)
def plot_xty(xty, title): config = helper.generate_config() trace = go.Bar(x=xty.index, y=xty.values) layout = go.Layout(title=title, xaxis={'title': 'Tickers'}, yaxis={'title': 'Covariance'}) fig = go.Figure(data=[trace], layout=layout) offline_py.iplot(fig, config=config)
def plot_shifted_returns(df_shited, df, title): config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([ ("Shifted Returns", df_shited, helper.color_scheme["major_line"]), ("Returns", df, helper.color_scheme["minor_line"]), ]) offline_py.iplot({"data": traces, "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_resampled_prices(df_resampled, df, title): config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([ ("Monthly Close", df_resampled, helper.color_scheme["major_line"]), ("Close", df, helper.color_scheme["minor_line"]), ]) offline_py.iplot({"data": traces, "layout": layout}, config=config)
def plot_resampled_prices(df_resampled, df, title): configure_plotly_browser_state() config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([ ('Monthly Close', df_resampled, helper.color_scheme['major_line']), ('Close', df, helper.color_scheme['minor_line'])]) offline_py.iplot({'data': traces, 'layout': layout}, config=config)
def plot_stock(prices, title): """ A utility function to plot the stock data. """ 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_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_returns(returns, title): """ A utility function to plot the stock returns. """ config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([('Returns', returns, helper.color_scheme['major_line'])]) offline_py.iplot({'data': traces, 'layout': layout}, config=config)
def plot_tracking_error(tracking_error, title): config = helper.generate_config() trace = go.Scatter(x=tracking_error.index, y=tracking_error, line={'color': helper.color_scheme['tracking_error']}) layout = go.Layout(title=title, xaxis={'title': 'Date'}, yaxis={'title': 'Error'}) fig = go.Figure(data=[trace], layout=layout) offline_py.iplot(fig, config=config)
def plot_returns(returns, title): config = helper.generate_config() graph_path = 'graphs/{}.html'.format(_sanatize_string(title)) trace = _generate_heatmap_trace(returns.sort_index(axis=1, ascending=False), 'Date', 'Ticker', 'Weight', -0.3, 0.3) layout = go.Layout( title=title, xaxis={'title': 'Dates'}, yaxis={'title': 'Tickers'}) fig = go.Figure(data=[trace], layout=layout) offline_py.plot(fig, config=config, filename=graph_path, auto_open=False) display(HTML('The graph for {} is too large. You can view it <a href="{}" target="_blank">here</a>.' .format(title, graph_path)))
def plot_resampled_prices(df_resampled, df, title): """ A utility function to plot the prices after resample. """ config = helper.generate_config() layout = go.Layout(title=title) traces = _generate_traces([ ('Monthly Close', df_resampled, helper.color_scheme['major_line']), ('Close', df, helper.color_scheme['minor_line']) ]) offline_py.iplot({'data': traces, '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 print_dataframe(df, n_rows=10, n_columns=3): missing_val_str = "..." config = helper.generate_config() formatted_df = df.iloc[:n_rows, :n_columns] formatted_df = formatted_df.applymap("{:.3f}".format) if len(df.columns) > n_columns: formatted_df[missing_val_str] = [missing_val_str] * len( formatted_df.index) if len(df.index) > n_rows: formatted_df.loc[missing_val_str] = [missing_val_str] * len( formatted_df.columns) trace = go.Table( type="table", columnwidth=[1, 3], header={ "values": [""] + list(formatted_df.columns.values), "line": { "color": helper.color_scheme["df_line"] }, "fill": { "color": helper.color_scheme["df_header"] }, "font": { "size": 13 }, }, cells={ "values": formatted_df.reset_index().values.T, "line": { "color": helper.color_scheme["df_line"] }, "fill": { "color": [ helper.color_scheme["df_header"], helper.color_scheme["df_value"], ] }, "font": { "size": 13 }, }, ) offline_py.iplot([trace], 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_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_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.tools.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)
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_benchmark_returns(benchmark_data, etf_data, title): config = helper.generate_config() index_trace = go.Scatter(name='Index', x=benchmark_data.index, y=benchmark_data, line={'color': helper.color_scheme['index']}) etf_trace = go.Scatter(name='ETF', x=etf_data.index, y=etf_data, line={'color': helper.color_scheme['etf']}) layout = go.Layout(title=title, xaxis={'title': 'Date'}, yaxis={ 'title': 'Cumulative Returns', 'range': [0, 3] }) fig = go.Figure(data=[index_trace, etf_trace], layout=layout) offline_py.iplot(fig, config=config)
def plot_signal_returns(prices, signal_return_list, titles): configure_plotly_browser_state() 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.tools.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_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)