diff options
Diffstat (limited to 'app.py')
-rw-r--r-- | app.py | 90 |
1 files changed, 22 insertions, 68 deletions
@@ -4,6 +4,7 @@ import json from datetime import datetime, timedelta from ema_algo import Ema_Algo from api import fetch_chart_data_yahoo +from analysis import compute_results import pytz app = Dash(__name__) @@ -25,29 +26,10 @@ app = Dash(__name__) # intersected_x.append(x) # intersected_y.append(y) -app.layout = html.Div([ - html.H4('Backtesting using the EMA method (5 vs 13) [ALPHA VERSION 0.0.2]'), - html.Div( - [ - html.Label("Ticker ", htmlFor="ticker"), - dcc.Input(id="ticker", value="SPY", type="text"), - html.Br(), - html.Label("Period ", htmlFor="period_dropdown"), - dcc.Dropdown( - id="period_dropdown", - options=["1d","5d","1mo","3mo","6mo","1y","2y","5y","10y","ytd","max"], - value = "1y"), - html.Br(), - html.Label("Interval ", htmlFor="interval_dropdown"), - dcc.Dropdown( - id="interval_dropdown", - options=["1m", "2m", "5m", "15m", "30m", "60m", "90m", "1h", "4h", "1d", "5d", "1wk", "1mo", "3mo"], - value = "1d", - ), - html.P(id='error_message'), - ], - id='input_params' - ), +app.layout = html.Div( + html.H4('Backtesting using EMA algos [ALPHA VERSION 0.0.5]'), + dcc.table(id="results_table") + dcc.Input(id="file_id", value="SPY", type="text"), html.Hr(), dcc.Graph(id="graph"), html.P("If bought and sold on these signals, the percent gain/loss would be:"), @@ -56,54 +38,26 @@ app.layout = html.Div([ ]) @app.callback( + Output("results_table", "figure"), Output("graph", "figure"), - Output("percent_gain", "children"), - Output("input_params", "style"), Output("error_message", "children"), - Input("ticker", "value"), - Input("period_dropdown", "value"), - Input("interval_dropdown", "value") + Input("file_id", "value"), ) -def display_color(ticker, period, interval): - fd = open('bt-recent.json', 'r') - raw_data = fd.read() - trial_data = json.loads(raw_data) - fd.close() - - chart_data = trial_data['chart_data'] - backtest_results = trial_data['backtest_results'] - url_params = trial_data['url_params'] - - percent_gain = backtest_results['percent_gain'] - error_style = {"color" : "red"} - error_message = "False error" - - # Code to execute no matter what (optional) - - raw_timestamps = chart_data['timestamps'] - timestamps = [datetime.fromtimestamp(t).astimezone(pytz.timezone('US/Eastern')) for t in raw_timestamps] - prices = chart_data['prices'] - - # test to see if graphc works, TODO make it abstracted - algoEMA = Ema_Algo() - algo_graph_data = backtest_results['algo_graph_data'] - algo_graphs = algoEMA.export_graph(algo_graph_data) - - buy_indices = backtest_results['buy_indices'] - sell_indices = backtest_results['sell_indices'] - - buy_prices, buy_times = [], [] - for i in buy_indices: - buy_prices.append(prices[i]) - buy_times.append(timestamps[i]) - sell_prices, sell_times = [], [] - for i in sell_indices: - sell_prices.append(prices[i]) - sell_times.append(timestamps[i]) - buy_sell_scatters = [ - go.Scatter(name='Buys', x=buy_times, y=buy_prices, line=dict(color='rgb(0, 0, 255)'), mode='markers', marker_size=10), - go.Scatter(name='Sells', x=sell_times, y=sell_prices, line=dict(color='rgb(255, 255, 0)'), mode='markers', marker_size=10) - ] +def display_color(file_id): + # compute the results to show in the table + path = 'test-1-ema' + result_summary = compute_results(path, file_id) + if not result_summary: + return go.Figure(), go.Figure(), "No results found for the given file ID." + + print(result_summary) + + table = go.Figure( + data = [go.Table( + header=dict(values=["algo name", "algo params", "avg percent gain", "best stock order"]), + cells=dict(values=result_summary) + )] + ) data = fetch_chart_data_yahoo('XRP-USD', '1d', None, timedelta(weeks=52)) times = [datetime.fromtimestamp(t).astimezone(pytz.timezone('US/Eastern')) for t in data['timestamps']] comp_scatter = go.Scatter(name='Price (yahoo)', x=times, y=data['prices'], line=dict(color='rgb(255, 0, 0)'), mode='lines') |