diff options
author | loit <michael.foiani@gmail.com> | 2025-07-29 20:32:36 -0400 |
---|---|---|
committer | loit <michael.foiani@gmail.com> | 2025-07-29 20:32:36 -0400 |
commit | 4722ce02ff70cd30ceb11b0ffa93f4e53ca6f80c (patch) | |
tree | 24ba1b51f503153b4f8731ef6dd6788e663f4c0b /app.py | |
parent | 0372b76ee22ea4421b70d6f7f8c2b29b2c7ac9dc (diff) |
begin infrastructure for automated backtesting, allowing for viewing details about the trial after
Diffstat (limited to 'app.py')
-rw-r--r-- | app.py | 80 |
1 files changed, 38 insertions, 42 deletions
@@ -1,11 +1,8 @@ from dash import Dash, dcc, html, Input, Output -from analysis import find_intersections, interpolate_intersection -from api import fetch_chart_data -from ema import calc_emas, calculate_profit import plotly.graph_objects as go import json -import datetime -import pandas as pd +from datetime import datetime +from ema_algo import Ema_Algo app = Dash(__name__) @@ -66,49 +63,48 @@ app.layout = html.Div([ Input("interval_dropdown", "value") ) def display_color(ticker, period, interval): - chart_data = fetch_chart_data(ticker, period, interval) - error_style = {"color" : "inherit"} - error_message = '' - if chart_data['error'] == True: - # implement a feeback mechanism for ERROR codes - error_style = {"color" : "red"} - error_message = 'Issue with parameter selection. Please try again.' - - timestamps_raw = chart_data['timestamps'] - timestamps = [datetime.datetime.fromtimestamp(t) for t in timestamps_raw] - prices = chart_data['prices'] + 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'] - ema_5 = calc_emas(5, prices) - ema_13 = calc_emas(13, prices) - profit = calculate_profit(ema_5, ema_13, prices, timestamps, 13) - buy_info = profit[-2] - print(buy_info) - buy_x = [] - buy_y = [] - for x,y,_ in buy_info: - buy_x.append(x) - buy_y.append(y) - - sell_info = profit[-1] - sell_x = [] - sell_y = [] - for x,y,_ in sell_info: - sell_x.append(x) - sell_y.append(y) - - print("Result Analysis:\n", "Percent gain/loss:\t", profit[0], profit[1], profit[2]) - percent_gain = profit[0] * 100 + 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) 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) + ] fig = go.Figure( data = [ go.Scatter(name='Price', x=timestamps, y=prices, line=dict(color='rgb(0, 0, 0)'), mode='lines'), - # go.Scatter(name='5 day EMA', x=timestamps, y=ema_5, line=dict(color='rgb(0, 255, 0)'), mode='lines'), - # go.Scatter(name='13 day EMA', x=timestamps, y=ema_13, line=dict(color='rgb(0, 0, 255)'), mode='lines'), - # go.Scatter(name='EMA Intersections', x=intersected_x, y=intersected_y, line=dict(color='rgb(255, 0, 0)'), mode='markers'), - go.Scatter(name='Buys', x=buy_x, y=buy_y, line=dict(color='rgb(0, 0, 255)'), mode='markers', marker_size=10), - go.Scatter(name='Sells', x=sell_x, y=sell_y, line=dict(color='rgb(255, 255, 0)'), mode='markers', marker_size=10), - ], + ] + 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=' + interval + ', range=' + period + ')'), |