diff options
author | sotech117 <michael_foiani@brown.edu> | 2025-09-03 13:54:19 -0400 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2025-09-03 13:54:19 -0400 |
commit | ea05658d9868b775fef74fba79a69bd21a43f93d (patch) | |
tree | 6c73e814224b9ea9fa756fb93b5c32c7501b62c1 /app.py | |
parent | 5bf22fc7e3c392c8bd44315ca2d06d7dca7d084e (diff) |
new fft algo with laplacian range of convergencemain
Diffstat (limited to 'app.py')
-rw-r--r-- | app.py | 133 |
1 files changed, 77 insertions, 56 deletions
@@ -1,80 +1,101 @@ -from dash import Dash, dcc, html, Input, Output +from dash import Dash, dcc, html, Input, Output, dash_table 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 +from analysis import summarize_results import pytz +import pandas as pd 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] +# pull the data from the files +table_data, algos_to_results = summarize_results('test-1-ema') -# 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.1.0]'), + dcc.Dropdown( + id='batch_name', + options=[ + {'label': 'Test 1 - EMA', 'value': 'test-1-ema'} + ], + value='test-1-ema', + clearable=False + ), + html.Hr(), + dash_table.DataTable( + table_data.to_dict('records'), + [{"name": i, "id": i} for i in table_data.columns], + style_table={'overflowX': 'auto'}, + style_cell={'textAlign': 'center'}, + style_header={ + 'backgroundColor': 'rgb(230, 230, 230)', + 'fontWeight': 'bold' + }, + style_data={ + 'whiteSpace': 'normal', + 'height': 'auto' + }, + sort_action='native', + ), -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"), + # dcc.Graph(id="graph"), html.P("If bought and sold on these signals, the percent gain/loss would be:"), - html.P(id="percent_gain") + html.P(id="percent_gain"), + + # for each algo, print the individual results given by the map + html.Div(id='algo_results', children=[ + html.Div([ + html.Div([ + + ]) + ]) for algo_name, result in algos_to_results.items() + ]) ]) @app.callback( - Output("results_table", "figure"), - Output("graph", "figure"), - Output("error_message", "children"), - Input("file_id", "value"), - ) -def display_color(file_id): + Input("batch_name", "value"), +) +def display_color(batch_name): # compute the results to show in the table - path = 'test-1-ema' - result_summary = compute_results(path, file_id) + path = batch_name + result_summary = summarize_results(path) if not result_summary: - return go.Figure(), go.Figure(), "No results found for the given file ID." + return "No results found for the given file ID." + + # update the table data + global table_data + table_data = result_summary + + # table = go.Figure( + # data = [go.Table( + # header=dict(values=["algo name", "algo params", "avg percent gain", "best stock for gain"]), + # 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 ($)') + # ) + # ) + - 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 + return |