from dash import Dash, dcc, html, Input, Output import plotly.graph_objects as go 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__) # pull stock data from json files # timestamps_file = open('timestamps.json', 'r') # timestamps_file_data = timestamps_file.read() # timestamps_raw = json.loads(timestamps_file_data) # timestamps = [datetime.datetime.fromtimestamp(t) for t in timestamps_raw] # prices_file = open('close_prices.json', 'r') # prices = json.loads(prices_file.read()) # intersection_indices = find_intersections(ema_5, ema_13, offset=13) # offset so don't calculate the SMA days # interpolated_intersections = [interpolate_intersection(indices, timestamps, ema_5, ema_13) for indices in intersection_indices] # intersected_x = [] # intersected_y = [] # for x,y in interpolated_intersections: # intersected_x.append(x) # intersected_y.append(y) 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:"), html.P(id="percent_gain") ]) @app.callback( Output("results_table", "figure"), Output("graph", "figure"), Output("error_message", "children"), Input("file_id", "value"), ) 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') fig = go.Figure( data = [ go.Scatter(name='Price', x=timestamps, y=prices, line=dict(color='rgb(0, 0, 0)'), mode='lines'), comp_scatter ] # + algo_graphs + buy_sell_scatters , layout = go.Layout( title=go.layout.Title(text='Chart for ' + chart_data['name']), xaxis=go.layout.XAxis(title='Date (dt=' + url_params['interval'] + ', range=' + url_params['period'] + ')'), yaxis=go.layout.YAxis(title='Price ($)') ) ) return fig, percent_gain, error_style, error_message app.run(debug=True)