|
1 | 1 | import dash_core_components as dcc
|
2 | 2 | import dash_html_components as html
|
| 3 | +import dash_table |
3 | 4 | from dash import Dash
|
4 | 5 | from dash.dependencies import Input, Output
|
5 | 6 |
|
6 |
| -from dash_app_functions import get_search_window_sizes, visualize_graph, get_symbols |
| 7 | +import stock_pattern_analyzer as spa |
| 8 | +from dash_app_functions import get_search_window_sizes, get_symbols, search_most_recent |
7 | 9 |
|
8 | 10 | app = Dash(__name__, meta_tags=[{"name": "viewport", "content": "width=device-width"}])
|
| 11 | +app.title = "Stock Patterns" |
9 | 12 | server = app.server
|
10 | 13 |
|
11 | 14 | ##### Header #####
|
|
60 | 63 | ##### Stats & Graph #####
|
61 | 64 |
|
62 | 65 | graph_id = "id-graph"
|
63 |
| - |
64 | 66 | stats_and_graph_div = html.Div([html.Div(id="id-stats-container", className="row container-display"),
|
65 | 67 | html.Div([dcc.Graph(id=graph_id)], id="id-graph-div", className="pretty_container")],
|
66 | 68 | id="id-graph-container", className="nine columns")
|
67 | 69 |
|
| 70 | +##### Matched Stocks List ##### |
| 71 | + |
| 72 | +matched_table_id = "id-matched-list" |
| 73 | +table_columns = ["Index", "Match distance", "Symbol", "Start date", "End Date", "Start Close Value ($)", |
| 74 | + "End Close Value ($)"] |
| 75 | +table = dash_table.DataTable(id=matched_table_id, columns=[{"id": c, "name": c} for c in table_columns], page_size=5) |
| 76 | +matched_div = html.Div([html.Div([html.H6("Matched patterns"), table], |
| 77 | + className="pretty_container")], |
| 78 | + id="id-matched-list-container", |
| 79 | + className="eleven columns") |
| 80 | + |
| 81 | +##### Reference Links ##### |
| 82 | + |
| 83 | +css_link = dcc.Link("[1] Style of the page (css)", |
| 84 | + href="https://github.com/plotly/dash-sample-apps/tree/master/apps/dash-oil-and-gas") |
| 85 | +yahoo_data_link = dcc.Link("[2] Yahoo data", href="https://finance.yahoo.com") |
| 86 | +reference_links_div = html.Div([html.Div([html.H6("References"), |
| 87 | + css_link, |
| 88 | + html.Br(), |
| 89 | + yahoo_data_link], |
| 90 | + className="pretty_container")], |
| 91 | + className="four columns") |
| 92 | + |
68 | 93 | ##### Layout #####
|
69 | 94 |
|
70 | 95 | app.layout = html.Div([header_div,
|
71 | 96 | html.Div([settings_div,
|
72 | 97 | stats_and_graph_div],
|
73 |
| - className="row flex-display")], |
| 98 | + className="row flex-display"), |
| 99 | + html.Div([matched_div], className="row flex-display"), |
| 100 | + reference_links_div], |
74 | 101 | id="mainContainer",
|
75 | 102 | style={"display": "flex", "flex-direction": "column"})
|
76 | 103 |
|
77 | 104 |
|
78 | 105 | ##### Callbacks #####
|
79 | 106 |
|
80 |
| -@app.callback(Output(graph_id, "figure"), |
| 107 | +@app.callback([Output(graph_id, "figure"), |
| 108 | + Output(matched_table_id, "data")], |
81 | 109 | [Input(symbol_dropdown_id, "value"),
|
82 | 110 | Input(window_size_dropdown_id, "value"),
|
83 | 111 | Input(future_size_input_id, "value"),
|
84 | 112 | Input(top_k_input_id, "value")])
|
85 |
| -def update_plot(symbol_value, window_size_value, future_size_value, top_k_value): |
86 |
| - fig = visualize_graph(symbol=symbol_value, |
87 |
| - window_size=window_size_value, |
88 |
| - future_size=future_size_value, |
89 |
| - top_k=top_k_value) |
90 |
| - return fig |
| 113 | +def update_plot_and_table(symbol_value, window_size_value, future_size_value, top_k_value): |
| 114 | + # RetAPI search |
| 115 | + ret = search_most_recent(symbol=symbol_value, |
| 116 | + window_size=window_size_value, |
| 117 | + top_k=top_k_value, |
| 118 | + future_size=future_size_value) |
| 119 | + |
| 120 | + # Parse response and build the HTML table rows |
| 121 | + table_rows = [] |
| 122 | + values = [] |
| 123 | + symbols = [] |
| 124 | + start_end_dates = [] |
| 125 | + for i, match in enumerate(ret.matches): |
| 126 | + values.append(match.values) |
| 127 | + symbols.append(match.symbol) |
| 128 | + start_end_dates.append((match.start_date, match.end_date)) |
| 129 | + row_values = [i + 1, match.distance, match.symbol, match.start_date, match.end_date, match.values[0], |
| 130 | + match.values[-1]] |
| 131 | + row_dict = {c: v for c, v in zip(table_columns, row_values)} |
| 132 | + table_rows.append(row_dict) |
| 133 | + |
| 134 | + # Visualize the data on a graph |
| 135 | + fig = spa.visualize_graph(match_values_list=values, |
| 136 | + match_symbols=symbols, |
| 137 | + match_str_dates=start_end_dates, |
| 138 | + window_size=window_size_value, |
| 139 | + future_size=future_size_value, |
| 140 | + anchor_symbol=ret.anchor_symbol, |
| 141 | + anchor_values=ret.anchor_values, |
| 142 | + show_legend=False) |
| 143 | + |
| 144 | + return fig, table_rows |
91 | 145 |
|
92 | 146 |
|
93 | 147 | if __name__ == "__main__":
|
|
0 commit comments