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 | |
parent | 0372b76ee22ea4421b70d6f7f8c2b29b2c7ac9dc (diff) |
begin infrastructure for automated backtesting, allowing for viewing details about the trial after
-rw-r--r-- | algo.py | 33 | ||||
-rw-r--r-- | app.py | 80 | ||||
-rw-r--r-- | bt-recent.json | 1 | ||||
-rw-r--r-- | ema.py | 63 | ||||
-rw-r--r-- | ema_algo.py | 81 | ||||
-rw-r--r-- | last_chart_data.json | 2 | ||||
-rw-r--r-- | simulate.py | 101 |
7 files changed, 255 insertions, 106 deletions
@@ -0,0 +1,33 @@ +from abc import ABC, abstractmethod + +class Algo(ABC): + """ + Function that takes in data nad determined whether to buy, sell, or hold + current position is a float that represents the ratio from liquid to shares that you own + i.e. 1.0 is $0 cash, all shares, 0.0 is max cash, 0 shares + """ + @abstractmethod + def detemine_signal(self, timestamps, prices, current_position): + pass # to implement per algo + + """ + Function that returns an array of go.X plots to merge into graph foir analysis + """ + @abstractmethod + def export_graph(self, graph_data): + pass # to implement per algo + + @property + def name(self): + pass + + @property + def graph_data(self): + pass + + # """ + # Function that takes in data and returns a buy, sell, or hold singal per interval + # """ + # @abstractmethod + # def backtest_algo(self): + # pass
\ No newline at end of file @@ -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 + ')'), diff --git a/bt-recent.json b/bt-recent.json new file mode 100644 index 0000000..1049801 --- /dev/null +++ b/bt-recent.json @@ -0,0 +1 @@ +{"chart_data": {"timestamps": [1722346200, 1722432600, 1722519000, 1722605400, 1722864600, 1722951000, 1723037400, 1723123800, 1723210200, 1723469400, 1723555800, 1723642200, 1723728600, 1723815000, 1724074200, 1724160600, 1724247000, 1724333400, 1724419800, 1724679000, 1724765400, 1724851800, 1724938200, 1725024600, 1725370200, 1725456600, 1725543000, 1725629400, 1725888600, 1725975000, 1726061400, 1726147800, 1726234200, 1726493400, 1726579800, 1726666200, 1726752600, 1726839000, 1727098200, 1727184600, 1727271000, 1727357400, 1727443800, 1727703000, 1727789400, 1727875800, 1727962200, 1728048600, 1728307800, 1728394200, 1728480600, 1728567000, 1728653400, 1728912600, 1728999000, 1729085400, 1729171800, 1729258200, 1729517400, 1729603800, 1729690200, 1729776600, 1729863000, 1730122200, 1730208600, 1730295000, 1730381400, 1730467800, 1730730600, 1730817000, 1730903400, 1730989800, 1731076200, 1731335400, 1731421800, 1731508200, 1731594600, 1731681000, 1731940200, 1732026600, 1732113000, 1732199400, 1732285800, 1732545000, 1732631400, 1732717800, 1732890600, 1733149800, 1733236200, 1733322600, 1733409000, 1733495400, 1733754600, 1733841000, 1733927400, 1734013800, 1734100200, 1734359400, 1734445800, 1734532200, 1734618600, 1734705000, 1734964200, 1735050600, 1735223400, 1735309800, 1735569000, 1735655400, 1735828200, 1735914600, 1736173800, 1736260200, 1736346600, 1736519400, 1736778600, 1736865000, 1736951400, 1737037800, 1737124200, 1737469800, 1737556200, 1737642600, 1737729000, 1737988200, 1738074600, 1738161000, 1738247400, 1738333800, 1738593000, 1738679400, 1738765800, 1738852200, 1738938600, 1739197800, 1739284200, 1739370600, 1739457000, 1739543400, 1739889000, 1739975400, 1740061800, 1740148200, 1740407400, 1740493800, 1740580200, 1740666600, 1740753000, 1741012200, 1741098600, 1741185000, 1741271400, 1741357800, 1741613400, 1741699800, 1741786200, 1741872600, 1741959000, 1742218200, 1742304600, 1742391000, 1742477400, 1742563800, 1742823000, 1742909400, 1742995800, 1743082200, 1743168600, 1743427800, 1743514200, 1743600600, 1743687000, 1743773400, 1744032600, 1744119000, 1744205400, 1744291800, 1744378200, 1744637400, 1744723800, 1744810200, 1744896600, 1745242200, 1745328600, 1745415000, 1745501400, 1745587800, 1745847000, 1745933400, 1746019800, 1746106200, 1746192600, 1746451800, 1746538200, 1746624600, 1746711000, 1746797400, 1747056600, 1747143000, 1747229400, 1747315800, 1747402200, 1747661400, 1747747800, 1747834200, 1747920600, 1748007000, 1748352600, 1748439000, 1748525400, 1748611800, 1748871000, 1748957400, 1749043800, 1749130200, 1749216600, 1749475800, 1749562200, 1749648600, 1749735000, 1749821400, 1750080600, 1750167000, 1750253400, 1750426200, 1750685400, 1750771800, 1750858200, 1750944600, 1751031000, 1751290200, 1751376600, 1751463000, 1751549400, 1751895000, 1751981400, 1752067800, 1752154200, 1752240600, 1752499800, 1752586200, 1752672600, 1752759000, 1752845400, 1753104600, 1753191000, 1753277400, 1753363800, 1753450200, 1753709400, 1753795800], "prices": [542.0, 550.8099975585938, 543.010009765625, 532.9000244140625, 517.3800048828125, 522.1500244140625, 518.6599731445312, 530.6500244140625, 532.989990234375, 533.27001953125, 542.0399780273438, 543.75, 553.0700073242188, 554.3099975585938, 559.6099853515625, 558.7000122070312, 560.6199951171875, 556.219970703125, 562.1300048828125, 560.7899780273438, 561.5599975585938, 558.2999877929688, 558.3499755859375, 563.6799926757812, 552.0800170898438, 550.9500122070312, 549.6099853515625, 540.3599853515625, 546.4099731445312, 548.7899780273438, 554.4199829101562, 559.0900268554688, 562.010009765625, 562.8400268554688, 563.0700073242188, 561.4000244140625, 570.97998046875, 568.25, 569.6699829101562, 571.2999877929688, 570.0399780273438, 572.2999877929688, 571.469970703125, 573.760009765625, 568.6199951171875, 568.8599853515625, 567.8200073242188, 572.97998046875, 567.7999877929688, 573.1699829101562, 577.1400146484375, 576.1300048828125, 579.5800170898438, 584.3200073242188, 579.780029296875, 582.2999877929688, 582.3499755859375, 584.5900268554688, 583.6300048828125, 583.3200073242188, 577.989990234375, 579.239990234375, 579.0399780273438, 580.8300170898438, 581.77001953125, 580.010009765625, 568.6400146484375, 571.0399780273438, 569.8099975585938, 576.7000122070312, 591.0399780273438, 595.6099853515625, 598.1900024414062, 598.760009765625, 596.9000244140625, 597.1900024414062, 593.3499755859375, 585.75, 588.1500244140625, 590.2999877929688, 590.5, 593.6699829101562, 595.510009765625, 597.530029296875, 600.6500244140625, 598.8300170898438, 602.5499877929688, 603.6300048828125, 603.9099731445312, 607.6599731445312, 606.6599731445312, 607.8099975585938, 604.6799926757812, 602.7999877929688, 607.4600219726562, 604.3300170898438, 604.2100219726562, 606.7899780273438, 604.2899780273438, 586.280029296875, 586.0999755859375, 591.1500244140625, 594.6900024414062, 601.2999877929688, 601.3400268554688, 595.010009765625, 588.219970703125, 586.0800170898438, 584.6400146484375, 591.9500122070312, 595.3599853515625, 588.6300048828125, 589.489990234375, 580.489990234375, 581.3900146484375, 582.1900024414062, 592.780029296875, 591.6400146484375, 597.5800170898438, 603.0499877929688, 606.4400024414062, 609.75, 607.969970703125, 599.3699951171875, 604.52001953125, 601.8099975585938, 605.0399780273438, 601.8200073242188, 597.77001953125, 601.780029296875, 604.219970703125, 606.3200073242188, 600.77001953125, 604.8499755859375, 605.3099975585938, 603.3599853515625, 609.72998046875, 609.7000122070312, 611.489990234375, 612.9299926757812, 610.3800048828125, 599.9400024414062, 597.2100219726562, 594.239990234375, 594.5399780273438, 585.0499877929688, 594.1799926757812, 583.77001953125, 576.8599853515625, 583.0599975585938, 572.7100219726562, 575.9199829101562, 560.5800170898438, 555.9199829101562, 558.8699951171875, 551.4199829101562, 562.8099975585938, 567.1500244140625, 561.02001953125, 567.1300048828125, 565.489990234375, 563.97998046875, 574.0800170898438, 575.4600219726562, 568.5900268554688, 567.0800170898438, 555.6599731445312, 559.3900146484375, 560.969970703125, 564.52001953125, 536.7000122070312, 505.2799987792969, 504.3800048828125, 496.4800109863281, 548.6199951171875, 524.5800170898438, 533.9400024414062, 539.1199951171875, 537.6099853515625, 525.6599731445312, 526.4099731445312, 513.8800048828125, 527.25, 535.4199829101562, 546.6900024414062, 550.6400146484375, 550.8499755859375, 554.3200073242188, 554.5399780273438, 558.469970703125, 566.760009765625, 563.510009765625, 558.7999877929688, 561.1500244140625, 565.0599975585938, 564.3400268554688, 582.989990234375, 586.8400268554688, 587.5900268554688, 590.4600219726562, 594.2000122070312, 594.8499755859375, 592.8499755859375, 582.8599853515625, 583.0900268554688, 579.1099853515625, 591.1500244140625, 587.72998046875, 590.0499877929688, 589.3900146484375, 592.7100219726562, 596.0900268554688, 595.9299926757812, 593.0499877929688, 599.1400146484375, 599.6799926757812, 603.0800170898438, 601.3599853515625, 603.75, 597.0, 602.6799926757812, 597.530029296875, 597.4400024414062, 594.280029296875, 600.1500244140625, 606.780029296875, 607.1199951171875, 611.8699951171875, 614.9099731445312, 617.8499755859375, 617.6500244140625, 620.4500122070312, 625.3400268554688, 620.6799926757812, 620.3400268554688, 624.0599975585938, 625.8200073242188, 623.6199951171875, 624.8099975585938, 622.1400146484375, 624.219970703125, 628.0399780273438, 627.5800170898438, 628.77001953125, 628.8599853515625, 634.2100219726562, 634.4199829101562, 637.0999755859375, 636.9400024414062, 635.260009765625], "name": "SPDR S&P 500 ETF", "error": false}, "url_params": {"ticker": "SPY", "period": "1yr", "interval": "1d"}, "backtest_results": {"timestamps": [1722346200, 1722432600, 1722519000, 1722605400, 1722864600, 1722951000, 1723037400, 1723123800, 1723210200, 1723469400, 1723555800, 1723642200, 1723728600, 1723815000, 1724074200, 1724160600, 1724247000, 1724333400, 1724419800, 1724679000, 1724765400, 1724851800, 1724938200, 1725024600, 1725370200, 1725456600, 1725543000, 1725629400, 1725888600, 1725975000, 1726061400, 1726147800, 1726234200, 1726493400, 1726579800, 1726666200, 1726752600, 1726839000, 1727098200, 1727184600, 1727271000, 1727357400, 1727443800, 1727703000, 1727789400, 1727875800, 1727962200, 1728048600, 1728307800, 1728394200, 1728480600, 1728567000, 1728653400, 1728912600, 1728999000, 1729085400, 1729171800, 1729258200, 1729517400, 1729603800, 1729690200, 1729776600, 1729863000, 1730122200, 1730208600, 1730295000, 1730381400, 1730467800, 1730730600, 1730817000, 1730903400, 1730989800, 1731076200, 1731335400, 1731421800, 1731508200, 1731594600, 1731681000, 1731940200, 1732026600, 1732113000, 1732199400, 1732285800, 1732545000, 1732631400, 1732717800, 1732890600, 1733149800, 1733236200, 1733322600, 1733409000, 1733495400, 1733754600, 1733841000, 1733927400, 1734013800, 1734100200, 1734359400, 1734445800, 1734532200, 1734618600, 1734705000, 1734964200, 1735050600, 1735223400, 1735309800, 1735569000, 1735655400, 1735828200, 1735914600, 1736173800, 1736260200, 1736346600, 1736519400, 1736778600, 1736865000, 1736951400, 1737037800, 1737124200, 1737469800, 1737556200, 1737642600, 1737729000, 1737988200, 1738074600, 1738161000, 1738247400, 1738333800, 1738593000, 1738679400, 1738765800, 1738852200, 1738938600, 1739197800, 1739284200, 1739370600, 1739457000, 1739543400, 1739889000, 1739975400, 1740061800, 1740148200, 1740407400, 1740493800, 1740580200, 1740666600, 1740753000, 1741012200, 1741098600, 1741185000, 1741271400, 1741357800, 1741613400, 1741699800, 1741786200, 1741872600, 1741959000, 1742218200, 1742304600, 1742391000, 1742477400, 1742563800, 1742823000, 1742909400, 1742995800, 1743082200, 1743168600, 1743427800, 1743514200, 1743600600, 1743687000, 1743773400, 1744032600, 1744119000, 1744205400, 1744291800, 1744378200, 1744637400, 1744723800, 1744810200, 1744896600, 1745242200, 1745328600, 1745415000, 1745501400, 1745587800, 1745847000, 1745933400, 1746019800, 1746106200, 1746192600, 1746451800, 1746538200, 1746624600, 1746711000, 1746797400, 1747056600, 1747143000, 1747229400, 1747315800, 1747402200, 1747661400, 1747747800, 1747834200, 1747920600, 1748007000, 1748352600, 1748439000, 1748525400, 1748611800, 1748871000, 1748957400, 1749043800, 1749130200, 1749216600, 1749475800, 1749562200, 1749648600, 1749735000, 1749821400, 1750080600, 1750167000, 1750253400, 1750426200, 1750685400, 1750771800, 1750858200, 1750944600, 1751031000, 1751290200, 1751376600, 1751463000, 1751549400, 1751895000, 1751981400, 1752067800, 1752154200, 1752240600, 1752499800, 1752586200, 1752672600, 1752759000, 1752845400, 1753104600, 1753191000, 1753277400, 1753363800, 1753450200, 1753709400, 1753795800], "prices": [542.0, 550.8099975585938, 543.010009765625, 532.9000244140625, 517.3800048828125, 522.1500244140625, 518.6599731445312, 530.6500244140625, 532.989990234375, 533.27001953125, 542.0399780273438, 543.75, 553.0700073242188, 554.3099975585938, 559.6099853515625, 558.7000122070312, 560.6199951171875, 556.219970703125, 562.1300048828125, 560.7899780273438, 561.5599975585938, 558.2999877929688, 558.3499755859375, 563.6799926757812, 552.0800170898438, 550.9500122070312, 549.6099853515625, 540.3599853515625, 546.4099731445312, 548.7899780273438, 554.4199829101562, 559.0900268554688, 562.010009765625, 562.8400268554688, 563.0700073242188, 561.4000244140625, 570.97998046875, 568.25, 569.6699829101562, 571.2999877929688, 570.0399780273438, 572.2999877929688, 571.469970703125, 573.760009765625, 568.6199951171875, 568.8599853515625, 567.8200073242188, 572.97998046875, 567.7999877929688, 573.1699829101562, 577.1400146484375, 576.1300048828125, 579.5800170898438, 584.3200073242188, 579.780029296875, 582.2999877929688, 582.3499755859375, 584.5900268554688, 583.6300048828125, 583.3200073242188, 577.989990234375, 579.239990234375, 579.0399780273438, 580.8300170898438, 581.77001953125, 580.010009765625, 568.6400146484375, 571.0399780273438, 569.8099975585938, 576.7000122070312, 591.0399780273438, 595.6099853515625, 598.1900024414062, 598.760009765625, 596.9000244140625, 597.1900024414062, 593.3499755859375, 585.75, 588.1500244140625, 590.2999877929688, 590.5, 593.6699829101562, 595.510009765625, 597.530029296875, 600.6500244140625, 598.8300170898438, 602.5499877929688, 603.6300048828125, 603.9099731445312, 607.6599731445312, 606.6599731445312, 607.8099975585938, 604.6799926757812, 602.7999877929688, 607.4600219726562, 604.3300170898438, 604.2100219726562, 606.7899780273438, 604.2899780273438, 586.280029296875, 586.0999755859375, 591.1500244140625, 594.6900024414062, 601.2999877929688, 601.3400268554688, 595.010009765625, 588.219970703125, 586.0800170898438, 584.6400146484375, 591.9500122070312, 595.3599853515625, 588.6300048828125, 589.489990234375, 580.489990234375, 581.3900146484375, 582.1900024414062, 592.780029296875, 591.6400146484375, 597.5800170898438, 603.0499877929688, 606.4400024414062, 609.75, 607.969970703125, 599.3699951171875, 604.52001953125, 601.8099975585938, 605.0399780273438, 601.8200073242188, 597.77001953125, 601.780029296875, 604.219970703125, 606.3200073242188, 600.77001953125, 604.8499755859375, 605.3099975585938, 603.3599853515625, 609.72998046875, 609.7000122070312, 611.489990234375, 612.9299926757812, 610.3800048828125, 599.9400024414062, 597.2100219726562, 594.239990234375, 594.5399780273438, 585.0499877929688, 594.1799926757812, 583.77001953125, 576.8599853515625, 583.0599975585938, 572.7100219726562, 575.9199829101562, 560.5800170898438, 555.9199829101562, 558.8699951171875, 551.4199829101562, 562.8099975585938, 567.1500244140625, 561.02001953125, 567.1300048828125, 565.489990234375, 563.97998046875, 574.0800170898438, 575.4600219726562, 568.5900268554688, 567.0800170898438, 555.6599731445312, 559.3900146484375, 560.969970703125, 564.52001953125, 536.7000122070312, 505.2799987792969, 504.3800048828125, 496.4800109863281, 548.6199951171875, 524.5800170898438, 533.9400024414062, 539.1199951171875, 537.6099853515625, 525.6599731445312, 526.4099731445312, 513.8800048828125, 527.25, 535.4199829101562, 546.6900024414062, 550.6400146484375, 550.8499755859375, 554.3200073242188, 554.5399780273438, 558.469970703125, 566.760009765625, 563.510009765625, 558.7999877929688, 561.1500244140625, 565.0599975585938, 564.3400268554688, 582.989990234375, 586.8400268554688, 587.5900268554688, 590.4600219726562, 594.2000122070312, 594.8499755859375, 592.8499755859375, 582.8599853515625, 583.0900268554688, 579.1099853515625, 591.1500244140625, 587.72998046875, 590.0499877929688, 589.3900146484375, 592.7100219726562, 596.0900268554688, 595.9299926757812, 593.0499877929688, 599.1400146484375, 599.6799926757812, 603.0800170898438, 601.3599853515625, 603.75, 597.0, 602.6799926757812, 597.530029296875, 597.4400024414062, 594.280029296875, 600.1500244140625, 606.780029296875, 607.1199951171875, 611.8699951171875, 614.9099731445312, 617.8499755859375, 617.6500244140625, 620.4500122070312, 625.3400268554688, 620.6799926757812, 620.3400268554688, 624.0599975585938, 625.8200073242188, 623.6199951171875, 624.8099975585938, 622.1400146484375, 624.219970703125, 628.0399780273438, 627.5800170898438, 628.77001953125, 628.8599853515625, 634.2100219726562, 634.4199829101562, 637.0999755859375, 636.9400024414062, 635.260009765625], "buy_indices": [13, 31, 70, 118, 163, 184], "sell_indices": [26, 66, 99, 142, 165], "percent_gain": 14.476517641325499, "starting_assets": 10000, "final_assets": 11447.65176413255, "algo_name": "EMA Algo", "algo_graph_data": {"timestamps": [1723815000, 1724074200, 1724160600, 1724247000, 1724333400, 1724419800, 1724679000, 1724765400, 1724851800, 1724938200, 1725024600, 1725370200, 1725456600, 1725543000, 1725629400, 1725888600, 1725975000, 1726061400, 1726147800, 1726234200, 1726493400, 1726579800, 1726666200, 1726752600, 1726839000, 1727098200, 1727184600, 1727271000, 1727357400, 1727443800, 1727703000, 1727789400, 1727875800, 1727962200, 1728048600, 1728307800, 1728394200, 1728480600, 1728567000, 1728653400, 1728912600, 1728999000, 1729085400, 1729171800, 1729258200, 1729517400, 1729603800, 1729690200, 1729776600, 1729863000, 1730122200, 1730208600, 1730295000, 1730381400, 1730467800, 1730730600, 1730817000, 1730903400, 1730989800, 1731076200, 1731335400, 1731421800, 1731508200, 1731594600, 1731681000, 1731940200, 1732026600, 1732113000, 1732199400, 1732285800, 1732545000, 1732631400, 1732717800, 1732890600, 1733149800, 1733236200, 1733322600, 1733409000, 1733495400, 1733754600, 1733841000, 1733927400, 1734013800, 1734100200, 1734359400, 1734445800, 1734532200, 1734618600, 1734705000, 1734964200, 1735050600, 1735223400, 1735309800, 1735569000, 1735655400, 1735828200, 1735914600, 1736173800, 1736260200, 1736346600, 1736519400, 1736778600, 1736865000, 1736951400, 1737037800, 1737124200, 1737469800, 1737556200, 1737642600, 1737729000, 1737988200, 1738074600, 1738161000, 1738247400, 1738333800, 1738593000, 1738679400, 1738765800, 1738852200, 1738938600, 1739197800, 1739284200, 1739370600, 1739457000, 1739543400, 1739889000, 1739975400, 1740061800, 1740148200, 1740407400, 1740493800, 1740580200, 1740666600, 1740753000, 1741012200, 1741098600, 1741185000, 1741271400, 1741357800, 1741613400, 1741699800, 1741786200, 1741872600, 1741959000, 1742218200, 1742304600, 1742391000, 1742477400, 1742563800, 1742823000, 1742909400, 1742995800, 1743082200, 1743168600, 1743427800, 1743514200, 1743600600, 1743687000, 1743773400, 1744032600, 1744119000, 1744205400, 1744291800, 1744378200, 1744637400, 1744723800, 1744810200, 1744896600, 1745242200, 1745328600, 1745415000, 1745501400, 1745587800, 1745847000, 1745933400, 1746019800, 1746106200, 1746192600, 1746451800, 1746538200, 1746624600, 1746711000, 1746797400, 1747056600, 1747143000, 1747229400, 1747315800, 1747402200, 1747661400, 1747747800, 1747834200, 1747920600, 1748007000, 1748352600, 1748439000, 1748525400, 1748611800, 1748871000, 1748957400, 1749043800, 1749130200, 1749216600, 1749475800, 1749562200, 1749648600, 1749735000, 1749821400, 1750080600, 1750167000, 1750253400, 1750426200, 1750685400, 1750771800, 1750858200, 1750944600, 1751031000, 1751290200, 1751376600, 1751463000, 1751549400, 1751895000, 1751981400, 1752067800, 1752154200, 1752240600, 1752499800, 1752586200, 1752672600, 1752759000, 1752845400, 1753104600, 1753191000, 1753277400, 1753363800, 1753450200, 1753709400, 1753795800], "ema_short": [546.6862274007126, 550.9941467176626, 553.5627685474522, 555.9151774040306, 556.0167751703955, 558.0545184078678, 558.9663382810265, 559.830891373549, 559.3205901800222, 558.997051981994, 560.5580322132564, 557.7320271721189, 555.4713555170897, 553.517565461914, 549.1317054251302, 548.2244613315972, 548.4129668968461, 550.4153055679495, 553.306879330456, 556.2079228088458, 558.4186241577202, 559.9690852132197, 560.446064946834, 563.957370120806, 565.388246747204, 566.8154921348548, 568.3103240208928, 568.8868753563765, 570.0245795019073, 570.5063765689799, 571.5909209678616, 570.6006123509703, 570.0204033511677, 569.2869380088514, 570.517952162151, 569.6119640390903, 570.7979703294457, 572.9119851024429, 573.9846583625662, 575.8497779383254, 578.6731877336232, 579.0421349213739, 580.1280858785722, 580.8687157810273, 582.1091528058412, 582.616103498165, 582.8507381068496, 581.2304888160247, 580.5669892888081, 580.0579855349868, 580.3153293866058, 580.8002261014872, 580.5368206561998, 576.5712186536124, 574.7274717781895, 573.0883137049909, 574.292213205671, 579.8748014795619, 585.1198627702288, 589.4765759939546, 592.5710539178448, 594.0140440832507, 595.0726968693026, 594.4984564415142, 591.5823042943429, 590.4382110009161, 590.392136598267, 590.4280910655114, 591.508721680393, 592.8424843754703, 594.404999349272, 596.4866743708689, 597.2677886105272, 599.0285216713411, 600.5623494084982, 601.6782239871759, 603.672140372961, 604.6680846301512, 605.715388939632, 605.3702568516818, 604.5135004987775, 605.4956743234038, 605.1071219122172, 604.8080885990303, 605.4687184084681, 605.0758049480934, 598.8105463976873, 594.5736894604374, 593.4324677783125, 593.8516459993438, 596.3344265972188, 598.0029600166355, 597.0053099329654, 594.0768635230186, 591.4112480452936, 589.1541702463417, 590.0861175665716, 591.844073494902, 590.7727172908722, 590.3451416053731, 587.0600911483738, 585.170065648395, 584.1767112460655, 587.0444839296687, 588.5763275025917, 591.577557365009, 595.4017008409957, 599.0811347077993, 602.6374231385329, 604.414938993397, 602.7332910346605, 603.3288672001904, 602.8225773196582, 603.5617108888868, 602.9811430339975, 601.244101866415, 601.4227443432351, 602.3551531298651, 603.6767711946497, 602.7078539735165, 603.4218945109902, 604.0512621935247, 603.8208365795374, 605.790551209275, 607.0937048751938, 608.5591333282542, 610.0160864440966, 610.1373925903353, 606.7382625406923, 603.562182351347, 600.4547849790231, 598.4831826617967, 594.0054510388541, 594.0636315844965, 590.6324275667478, 586.0416134950194, 585.0477415162109, 580.935168335026, 579.2634398600694, 573.0356322699943, 567.330415816715, 564.5102755835392, 560.1468446924116, 561.0345623144724, 563.0730496810024, 562.3887062977517, 563.9691391594387, 564.4760895177508, 564.3107198347506, 567.567152253115, 570.1981088262954, 569.6620815026865, 568.8013933650723, 564.4209199582253, 562.7439515216295, 562.152624582128, 562.9417562318354, 554.1945082235674, 537.889671742144, 526.7197827890335, 516.6398588547984, 527.2999042755948, 526.3932752136778, 528.908850956254, 532.3125656765652, 534.0783722348976, 531.2722392047756, 529.6514838513608, 524.3943241951781, 525.3462161301188, 528.7041383901313, 534.699426407223, 540.0129558209612, 543.62529574262, 547.1901996031529, 549.6401257445499, 552.5834073974083, 557.3089415201473, 559.3759642686399, 559.1839721100829, 559.8393228780761, 561.579547771582, 562.499707466211, 569.3298017222658, 575.1665434333335, 579.3077045740453, 583.0251437069156, 586.7500998736209, 589.4500584443931, 590.5833641582412, 588.0089045560151, 586.369278655833, 583.9495142210762, 586.349684285405, 586.8097830131867, 587.8898512731141, 588.3899057315552, 589.8299444785889, 591.9166386042156, 593.2544232947375, 593.1862781274813, 595.1708569678001, 596.6739022037939, 598.8092738324772, 599.6595110055057, 601.0230073370038, 599.682004891336, 600.6813341528177, 599.6308992008369, 598.9006002810268, 597.3604099529762, 598.290281440005, 601.120197392295, 603.1201299672592, 606.036751683902, 608.9944921707785, 611.9463199758316, 613.8475547885753, 616.0483739280606, 619.1455915705301, 619.6570586056139, 619.8847146888988, 621.2764756454638, 622.7909862050489, 623.0673225090951, 623.6482141922613, 623.1454810109867, 623.5036442416995, 625.015755503581, 625.8705093656686, 626.8370127541957, 627.511336953318, 629.7442319597642, 631.3028156098949, 633.2352022685758, 634.4701356595193, 634.7334270282213], "ema_long": [538.2649482475532, 541.3142392624118, 543.7979211116432, 546.2010745410066, 547.6323454213093, 549.7034396300955, 551.2872308297025, 552.7547689338298, 553.546943056564, 554.2330905607603, 555.5826480057633, 555.082272160632, 554.4919493101177, 553.794525887467, 551.8753058109093, 551.0945440014267, 550.7653202908434, 551.2874149507453, 552.4020737942773, 553.7746360758985, 555.0696919015514, 556.2125941047897, 556.9536555775429, 558.9574162762868, 560.2849282368173, 561.6256503330087, 563.0076985415744, 564.0123098966844, 565.1962638818679, 566.0925077134762, 567.1878651494975, 567.3924551448818, 567.6021023172648, 567.6332316039725, 568.3970528703693, 568.3117578593121, 569.0057900094328, 570.1678221007191, 571.0195624981611, 572.2424845826872, 573.9678449743346, 574.7981570204118, 575.8698471307771, 576.7955797672286, 577.9090722084057, 578.7263483047496, 579.3825853075309, 579.183643154223, 579.1916927371019, 579.1700192071365, 579.407161761809, 579.7447128717292, 579.7826124280001, 578.1908127452054, 577.1692649283681, 576.1179410184004, 576.2010940453478, 578.3209346142044, 580.7907990052555, 583.2763994961342, 585.4883438203473, 587.1185839051637, 588.5573579817699, 589.2420176395082, 588.7431579767214, 588.6584246106272, 588.892933636676, 589.1225145457224, 589.7721528834987, 590.5918467238025, 591.583015662813, 592.8783026272772, 593.728547550501, 594.9887532994251, 596.2232178113376, 597.3213257160796, 598.7982753487156, 599.9213750338322, 601.0483211087982, 601.5671313326529, 601.7432536841267, 602.5599348682023, 602.8128037570082, 603.0124063592436, 603.5520594546865, 603.6574763936376, 601.1749839512429, 599.0214113276278, 597.8969274828328, 597.4387953340577, 597.9903942567593, 598.4689131994321, 597.9747841374598, 596.5812393611262, 595.081064750943, 593.5894861648709, 593.3552755994652, 593.6416627069077, 592.9257115891799, 592.4348942527793, 590.7284793930073, 589.394413000926, 588.3652114924232, 588.995899750202, 589.37363044995, 590.5459713985064, 592.3322594548582, 594.3476513100794, 596.5479868372109, 598.1796988180558, 598.3497411465032, 599.2312094871813, 599.5996077830974, 600.3768035322755, 600.5829755025532, 600.1811246495099, 600.4095395991335, 600.9538868997038, 601.7204755317773, 601.5846961031307, 602.0511646006745, 602.5167121660916, 602.6371797640161, 603.6504370075495, 604.514662036047, 605.511137492951, 606.570973947641, 607.1151212240942, 606.0901042551388, 604.8215210719271, 603.3098738094197, 602.0570315548375, 599.6274538745706, 598.8492451318865, 596.6950700460814, 593.861486518293, 592.3184166669074, 589.5172174248715, 587.5747553513409, 583.7183641711271, 579.7471668481313, 576.7647137437108, 573.1440379103459, 571.6677464315242, 571.0223575718868, 569.5934521375101, 569.2415311011248, 568.7055966915891, 568.030508659755, 568.8947241497677, 569.8326238387518, 569.6551099839971, 569.2872395705467, 567.3404872239731, 566.2047054274681, 565.4568861811334, 565.3230480882929, 561.2340429623985, 553.2406080790983, 546.2605219082003, 539.1490203479328, 540.5020167435407, 538.2274453644411, 537.6149535182933, 537.8299594609925, 537.7985345882169, 536.064454381976, 534.6852427766269, 531.7130659346534, 531.0754850868458, 531.696127633033, 533.838109748515, 536.2383818770754, 538.3257524069129, 540.6106459665281, 542.6005505466446, 544.867610568999, 547.9950961685171, 550.2115123966754, 551.4384374532888, 552.8258070191137, 554.5735485247537, 555.9687597148559, 559.8289355033586, 563.6876628393743, 567.1022862702449, 570.4391056563037, 573.8335208778362, 576.8358715504221, 579.1236006983529, 579.6573699345257, 580.1477494946605, 579.999497474218, 581.5924298941958, 582.4692228334179, 583.5521892562109, 584.3861643122433, 585.5752868351594, 587.0773925523465, 588.3420497128373, 589.0146122957132, 590.4610983461024, 591.7780832503422, 593.3926452274138, 594.5308366737208, 595.8478600060464, 596.0124514337541, 596.9649573254723, 597.0456818928155, 597.1020133997571, 596.698872813631, 597.1918944708356, 598.5616280174127, 599.7842518888091, 601.5107866357204, 603.4249561369791, 605.485673201116, 607.2234376601084, 609.1129483096688, 611.4311023876403, 612.7523724288033, 613.836323061184, 615.2968479893854, 616.8001564657902, 617.7744191302756, 618.7795017628926, 619.2595750322562, 619.9682029852376, 621.1213137055385, 622.043985617582, 623.0048476052489, 623.8412958547223, 625.3225424429985, 626.6221767954496, 628.1190051940907, 629.3791476579929, 630.2192708162262]}}}
\ No newline at end of file @@ -1,63 +0,0 @@ -def compute_buy_sell_signals_ema(period1, period2, prices): - ema_5 = calc_emas(5, prices) - ema_13 = calc_emas(13, prices) - - -def calc_first_sma(period, prices): - prices_sum = 0 - for i in range(0, period): - prices_sum += prices[i] # 0, 1, 2, 3 ("popping" order) - # print('prices_sum:\t', prices_sum) - - return prices_sum / period - -def calc_emas(period, prices): - weighted_multiplier = 2.0 / (period + 1.0) - - # calculate the first ema - first_ema = calc_first_sma(period, prices) - - # calculate the rest ema's using that first - emas = [first_ema] * period - for i in range(period + 1, len(prices)): # 4, 5, 6, ... , last - last_ema = emas[-1] - if prices[i] == None or prices[i] == 0: - print(i) - next_ema = prices[i] * weighted_multiplier + last_ema * (1 - weighted_multiplier) - emas.append(next_ema) - return emas - -def calculate_profit(buy_line, sell_line, prices, timestamps, offset=0, starting_money=10000): - if len(buy_line) != len(sell_line): - print("ERROR IN find_intersections: len of arrs not the same") - return [] - is_bought = False - curr_money = 10000 - shares_owned = 0 - buy_info = [] # coming in, (time, cash, # current shares, # shares to buy) - sell_info = [] # (time, cash, # current shares, # shares to sell,) - for i in range(offset, len(buy_line)): - current_b1 = buy_line[i] - current_sl = sell_line[i] - # if the sign is positive, we want to hold, if it's negative, we want to sell - sign_signal = current_b1 - current_sl - - if sign_signal > 0: - if not is_bought: - # buy the stock - shares_owned = curr_money / prices[i] - curr_money = 0 - buy_info.append((timestamps[i], prices[i], i)) - is_bought = True - if sign_signal < 0: - if is_bought: - # selling the stock - curr_money = prices[i] * shares_owned - shares_owned = 0 - sell_info.append((timestamps[i], prices[i], i)) - is_bought = False - - # TODO: consider end interval - total_assets = prices[-1] * shares_owned + curr_money - percent_gain = (total_assets - starting_money) / starting_money - return (percent_gain, total_assets, starting_money, buy_info, sell_info)
\ No newline at end of file diff --git a/ema_algo.py b/ema_algo.py new file mode 100644 index 0000000..db25c08 --- /dev/null +++ b/ema_algo.py @@ -0,0 +1,81 @@ +from algo import Algo +import plotly.graph_objects as go +import datetime + +class Ema_Algo(Algo): + def __init__(self, shortPeriod=5, longPeriod=13): + self.shortPeriod = shortPeriod + self.longPeriod = longPeriod + self.g_data = { + "timestamps" : [], + "ema_short" : [], + "ema_long" : [] + } + + @property + def name(self): + return "EMA Algo" + + @property + def graph_data(self): + return self.g_data + + def export_graph(self, g_data): + timestamps = [datetime.datetime.fromtimestamp(t) for t in g_data['timestamps']] + ema_5 = g_data['ema_short'] + ema_13 = g_data['ema_long'] + exp = [ + 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') + ] + return exp + + def detemine_signal(self, timestamps, prices): + + ema_5 = self.calc_emas(self.shortPeriod, prices) + ema_13 = self.calc_emas(self.longPeriod, prices) + + # add to graph data + self.graph_data["timestamps"].append(timestamps[-1]) + self.graph_data["ema_short"].append(ema_5[-1]) + self.graph_data["ema_long"].append(ema_13[-1]) + + # determine the sign from the most recent price + sign_signal = ema_5[-1] - ema_13[-1] + # current position, (liquid, shares) + if sign_signal > 0: + return 1.0 # buy max shares + if sign_signal < 0: + return 0.0 # sell all shares + + return 0.5 + + """ + Calculates the simple moving average of the first period of the data + """ + def calc_first_sma(self, period, prices): + prices_sum = 0 + for i in range(0, period): + prices_sum += prices[i] # 0, 1, 2, 3 ("popping" order) + # print('prices_sum:\t', prices_sum) + + return prices_sum / period + + """ + Returns an array off all EMAs, computed according to period + """ + def calc_emas(self, period, prices): + weighted_multiplier = 2.0 / (period + 1.0) + + # calculate the first ema + first_ema = self.calc_first_sma(period, prices) + + # calculate the rest ema's using that first + emas = [first_ema] * period # 0, 1, 2 (for period 3) + for i in range(period, len(prices)): # 3, 4, 5, 6, ... , last + last_ema = emas[-1] + if prices[i] == None or prices[i] == 0: + print(i) + next_ema = prices[i] * weighted_multiplier + last_ema * (1 - weighted_multiplier) + emas.append(next_ema) + return emas
\ No newline at end of file diff --git a/last_chart_data.json b/last_chart_data.json index cfbffbf..e84d3b2 100644 --- a/last_chart_data.json +++ b/last_chart_data.json @@ -1 +1 @@ -{"timestamps": [1722259800, 1722346200, 1722432600, 1722519000, 1722605400, 1722864600, 1722951000, 1723037400, 1723123800, 1723210200, 1723469400, 1723555800, 1723642200, 1723728600, 1723815000, 1724074200, 1724160600, 1724247000, 1724333400, 1724419800, 1724679000, 1724765400, 1724851800, 1724938200, 1725024600, 1725370200, 1725456600, 1725543000, 1725629400, 1725888600, 1725975000, 1726061400, 1726147800, 1726234200, 1726493400, 1726579800, 1726666200, 1726752600, 1726839000, 1727098200, 1727184600, 1727271000, 1727357400, 1727443800, 1727703000, 1727789400, 1727875800, 1727962200, 1728048600, 1728307800, 1728394200, 1728480600, 1728567000, 1728653400, 1728912600, 1728999000, 1729085400, 1729171800, 1729258200, 1729517400, 1729603800, 1729690200, 1729776600, 1729863000, 1730122200, 1730208600, 1730295000, 1730381400, 1730467800, 1730730600, 1730817000, 1730903400, 1730989800, 1731076200, 1731335400, 1731421800, 1731508200, 1731594600, 1731681000, 1731940200, 1732026600, 1732113000, 1732199400, 1732285800, 1732545000, 1732631400, 1732717800, 1732890600, 1733149800, 1733236200, 1733322600, 1733409000, 1733495400, 1733754600, 1733841000, 1733927400, 1734013800, 1734100200, 1734359400, 1734445800, 1734532200, 1734618600, 1734705000, 1734964200, 1735050600, 1735223400, 1735309800, 1735569000, 1735655400, 1735828200, 1735914600, 1736173800, 1736260200, 1736346600, 1736519400, 1736778600, 1736865000, 1736951400, 1737037800, 1737124200, 1737469800, 1737556200, 1737642600, 1737729000, 1737988200, 1738074600, 1738161000, 1738247400, 1738333800, 1738593000, 1738679400, 1738765800, 1738852200, 1738938600, 1739197800, 1739284200, 1739370600, 1739457000, 1739543400, 1739889000, 1739975400, 1740061800, 1740148200, 1740407400, 1740493800, 1740580200, 1740666600, 1740753000, 1741012200, 1741098600, 1741185000, 1741271400, 1741357800, 1741613400, 1741699800, 1741786200, 1741872600, 1741959000, 1742218200, 1742304600, 1742391000, 1742477400, 1742563800, 1742823000, 1742909400, 1742995800, 1743082200, 1743168600, 1743427800, 1743514200, 1743600600, 1743687000, 1743773400, 1744032600, 1744119000, 1744205400, 1744291800, 1744378200, 1744637400, 1744723800, 1744810200, 1744896600, 1745242200, 1745328600, 1745415000, 1745501400, 1745587800, 1745847000, 1745933400, 1746019800, 1746106200, 1746192600, 1746451800, 1746538200, 1746624600, 1746711000, 1746797400, 1747056600, 1747143000, 1747229400, 1747315800, 1747402200, 1747661400, 1747747800, 1747834200, 1747920600, 1748007000, 1748352600, 1748439000, 1748525400, 1748611800, 1748871000, 1748957400, 1749043800, 1749130200, 1749216600, 1749475800, 1749562200, 1749648600, 1749735000, 1749821400, 1750080600, 1750167000, 1750253400, 1750426200, 1750685400, 1750771800, 1750858200, 1750944600, 1751031000, 1751290200, 1751376600, 1751463000, 1751549400, 1751895000, 1751981400, 1752067800, 1752154200, 1752240600, 1752499800, 1752586200, 1752672600, 1752759000, 1752845400, 1753104600, 1753191000, 1753277400, 1753363800, 1753450200, 1753709400], "prices": [544.760009765625, 542.0, 550.8099975585938, 543.010009765625, 532.9000244140625, 517.3800048828125, 522.1500244140625, 518.6599731445312, 530.6500244140625, 532.989990234375, 533.27001953125, 542.0399780273438, 543.75, 553.0700073242188, 554.3099975585938, 559.6099853515625, 558.7000122070312, 560.6199951171875, 556.219970703125, 562.1300048828125, 560.7899780273438, 561.5599975585938, 558.2999877929688, 558.3499755859375, 563.6799926757812, 552.0800170898438, 550.9500122070312, 549.6099853515625, 540.3599853515625, 546.4099731445312, 548.7899780273438, 554.4199829101562, 559.0900268554688, 562.010009765625, 562.8400268554688, 563.0700073242188, 561.4000244140625, 570.97998046875, 568.25, 569.6699829101562, 571.2999877929688, 570.0399780273438, 572.2999877929688, 571.469970703125, 573.760009765625, 568.6199951171875, 568.8599853515625, 567.8200073242188, 572.97998046875, 567.7999877929688, 573.1699829101562, 577.1400146484375, 576.1300048828125, 579.5800170898438, 584.3200073242188, 579.780029296875, 582.2999877929688, 582.3499755859375, 584.5900268554688, 583.6300048828125, 583.3200073242188, 577.989990234375, 579.239990234375, 579.0399780273438, 580.8300170898438, 581.77001953125, 580.010009765625, 568.6400146484375, 571.0399780273438, 569.8099975585938, 576.7000122070312, 591.0399780273438, 595.6099853515625, 598.1900024414062, 598.760009765625, 596.9000244140625, 597.1900024414062, 593.3499755859375, 585.75, 588.1500244140625, 590.2999877929688, 590.5, 593.6699829101562, 595.510009765625, 597.530029296875, 600.6500244140625, 598.8300170898438, 602.5499877929688, 603.6300048828125, 603.9099731445312, 607.6599731445312, 606.6599731445312, 607.8099975585938, 604.6799926757812, 602.7999877929688, 607.4600219726562, 604.3300170898438, 604.2100219726562, 606.7899780273438, 604.2899780273438, 586.280029296875, 586.0999755859375, 591.1500244140625, 594.6900024414062, 601.2999877929688, 601.3400268554688, 595.010009765625, 588.219970703125, 586.0800170898438, 584.6400146484375, 591.9500122070312, 595.3599853515625, 588.6300048828125, 589.489990234375, 580.489990234375, 581.3900146484375, 582.1900024414062, 592.780029296875, 591.6400146484375, 597.5800170898438, 603.0499877929688, 606.4400024414062, 609.75, 607.969970703125, 599.3699951171875, 604.52001953125, 601.8099975585938, 605.0399780273438, 601.8200073242188, 597.77001953125, 601.780029296875, 604.219970703125, 606.3200073242188, 600.77001953125, 604.8499755859375, 605.3099975585938, 603.3599853515625, 609.72998046875, 609.7000122070312, 611.489990234375, 612.9299926757812, 610.3800048828125, 599.9400024414062, 597.2100219726562, 594.239990234375, 594.5399780273438, 585.0499877929688, 594.1799926757812, 583.77001953125, 576.8599853515625, 583.0599975585938, 572.7100219726562, 575.9199829101562, 560.5800170898438, 555.9199829101562, 558.8699951171875, 551.4199829101562, 562.8099975585938, 567.1500244140625, 561.02001953125, 567.1300048828125, 565.489990234375, 563.97998046875, 574.0800170898438, 575.4600219726562, 568.5900268554688, 567.0800170898438, 555.6599731445312, 559.3900146484375, 560.969970703125, 564.52001953125, 536.7000122070312, 505.2799987792969, 504.3800048828125, 496.4800109863281, 548.6199951171875, 524.5800170898438, 533.9400024414062, 539.1199951171875, 537.6099853515625, 525.6599731445312, 526.4099731445312, 513.8800048828125, 527.25, 535.4199829101562, 546.6900024414062, 550.6400146484375, 550.8499755859375, 554.3200073242188, 554.5399780273438, 558.469970703125, 566.760009765625, 563.510009765625, 558.7999877929688, 561.1500244140625, 565.0599975585938, 564.3400268554688, 582.989990234375, 586.8400268554688, 587.5900268554688, 590.4600219726562, 594.2000122070312, 594.8499755859375, 592.8499755859375, 582.8599853515625, 583.0900268554688, 579.1099853515625, 591.1500244140625, 587.72998046875, 590.0499877929688, 589.3900146484375, 592.7100219726562, 596.0900268554688, 595.9299926757812, 593.0499877929688, 599.1400146484375, 599.6799926757812, 603.0800170898438, 601.3599853515625, 603.75, 597.0, 602.6799926757812, 597.530029296875, 597.4400024414062, 594.280029296875, 600.1500244140625, 606.780029296875, 607.1199951171875, 611.8699951171875, 614.9099731445312, 617.8499755859375, 617.6500244140625, 620.4500122070312, 625.3400268554688, 620.6799926757812, 620.3400268554688, 624.0599975585938, 625.8200073242188, 623.6199951171875, 624.8099975585938, 622.1400146484375, 624.219970703125, 628.0399780273438, 627.5800170898438, 628.77001953125, 628.8599853515625, 634.2100219726562, 634.4199829101562, 637.0999755859375, 636.9400024414062], "name": "SPDR S&P 500 ETF", "error": false}
\ No newline at end of file +{"timestamps": [1722346200, 1722432600, 1722519000, 1722605400, 1722864600, 1722951000, 1723037400, 1723123800, 1723210200, 1723469400, 1723555800, 1723642200, 1723728600, 1723815000, 1724074200, 1724160600, 1724247000, 1724333400, 1724419800, 1724679000, 1724765400, 1724851800, 1724938200, 1725024600, 1725370200, 1725456600, 1725543000, 1725629400, 1725888600, 1725975000, 1726061400, 1726147800, 1726234200, 1726493400, 1726579800, 1726666200, 1726752600, 1726839000, 1727098200, 1727184600, 1727271000, 1727357400, 1727443800, 1727703000, 1727789400, 1727875800, 1727962200, 1728048600, 1728307800, 1728394200, 1728480600, 1728567000, 1728653400, 1728912600, 1728999000, 1729085400, 1729171800, 1729258200, 1729517400, 1729603800, 1729690200, 1729776600, 1729863000, 1730122200, 1730208600, 1730295000, 1730381400, 1730467800, 1730730600, 1730817000, 1730903400, 1730989800, 1731076200, 1731335400, 1731421800, 1731508200, 1731594600, 1731681000, 1731940200, 1732026600, 1732113000, 1732199400, 1732285800, 1732545000, 1732631400, 1732717800, 1732890600, 1733149800, 1733236200, 1733322600, 1733409000, 1733495400, 1733754600, 1733841000, 1733927400, 1734013800, 1734100200, 1734359400, 1734445800, 1734532200, 1734618600, 1734705000, 1734964200, 1735050600, 1735223400, 1735309800, 1735569000, 1735655400, 1735828200, 1735914600, 1736173800, 1736260200, 1736346600, 1736519400, 1736778600, 1736865000, 1736951400, 1737037800, 1737124200, 1737469800, 1737556200, 1737642600, 1737729000, 1737988200, 1738074600, 1738161000, 1738247400, 1738333800, 1738593000, 1738679400, 1738765800, 1738852200, 1738938600, 1739197800, 1739284200, 1739370600, 1739457000, 1739543400, 1739889000, 1739975400, 1740061800, 1740148200, 1740407400, 1740493800, 1740580200, 1740666600, 1740753000, 1741012200, 1741098600, 1741185000, 1741271400, 1741357800, 1741613400, 1741699800, 1741786200, 1741872600, 1741959000, 1742218200, 1742304600, 1742391000, 1742477400, 1742563800, 1742823000, 1742909400, 1742995800, 1743082200, 1743168600, 1743427800, 1743514200, 1743600600, 1743687000, 1743773400, 1744032600, 1744119000, 1744205400, 1744291800, 1744378200, 1744637400, 1744723800, 1744810200, 1744896600, 1745242200, 1745328600, 1745415000, 1745501400, 1745587800, 1745847000, 1745933400, 1746019800, 1746106200, 1746192600, 1746451800, 1746538200, 1746624600, 1746711000, 1746797400, 1747056600, 1747143000, 1747229400, 1747315800, 1747402200, 1747661400, 1747747800, 1747834200, 1747920600, 1748007000, 1748352600, 1748439000, 1748525400, 1748611800, 1748871000, 1748957400, 1749043800, 1749130200, 1749216600, 1749475800, 1749562200, 1749648600, 1749735000, 1749821400, 1750080600, 1750167000, 1750253400, 1750426200, 1750685400, 1750771800, 1750858200, 1750944600, 1751031000, 1751290200, 1751376600, 1751463000, 1751549400, 1751895000, 1751981400, 1752067800, 1752154200, 1752240600, 1752499800, 1752586200, 1752672600, 1752759000, 1752845400, 1753104600, 1753191000, 1753277400, 1753363800, 1753450200, 1753709400, 1753795800], "prices": [542.0, 550.8099975585938, 543.010009765625, 532.9000244140625, 517.3800048828125, 522.1500244140625, 518.6599731445312, 530.6500244140625, 532.989990234375, 533.27001953125, 542.0399780273438, 543.75, 553.0700073242188, 554.3099975585938, 559.6099853515625, 558.7000122070312, 560.6199951171875, 556.219970703125, 562.1300048828125, 560.7899780273438, 561.5599975585938, 558.2999877929688, 558.3499755859375, 563.6799926757812, 552.0800170898438, 550.9500122070312, 549.6099853515625, 540.3599853515625, 546.4099731445312, 548.7899780273438, 554.4199829101562, 559.0900268554688, 562.010009765625, 562.8400268554688, 563.0700073242188, 561.4000244140625, 570.97998046875, 568.25, 569.6699829101562, 571.2999877929688, 570.0399780273438, 572.2999877929688, 571.469970703125, 573.760009765625, 568.6199951171875, 568.8599853515625, 567.8200073242188, 572.97998046875, 567.7999877929688, 573.1699829101562, 577.1400146484375, 576.1300048828125, 579.5800170898438, 584.3200073242188, 579.780029296875, 582.2999877929688, 582.3499755859375, 584.5900268554688, 583.6300048828125, 583.3200073242188, 577.989990234375, 579.239990234375, 579.0399780273438, 580.8300170898438, 581.77001953125, 580.010009765625, 568.6400146484375, 571.0399780273438, 569.8099975585938, 576.7000122070312, 591.0399780273438, 595.6099853515625, 598.1900024414062, 598.760009765625, 596.9000244140625, 597.1900024414062, 593.3499755859375, 585.75, 588.1500244140625, 590.2999877929688, 590.5, 593.6699829101562, 595.510009765625, 597.530029296875, 600.6500244140625, 598.8300170898438, 602.5499877929688, 603.6300048828125, 603.9099731445312, 607.6599731445312, 606.6599731445312, 607.8099975585938, 604.6799926757812, 602.7999877929688, 607.4600219726562, 604.3300170898438, 604.2100219726562, 606.7899780273438, 604.2899780273438, 586.280029296875, 586.0999755859375, 591.1500244140625, 594.6900024414062, 601.2999877929688, 601.3400268554688, 595.010009765625, 588.219970703125, 586.0800170898438, 584.6400146484375, 591.9500122070312, 595.3599853515625, 588.6300048828125, 589.489990234375, 580.489990234375, 581.3900146484375, 582.1900024414062, 592.780029296875, 591.6400146484375, 597.5800170898438, 603.0499877929688, 606.4400024414062, 609.75, 607.969970703125, 599.3699951171875, 604.52001953125, 601.8099975585938, 605.0399780273438, 601.8200073242188, 597.77001953125, 601.780029296875, 604.219970703125, 606.3200073242188, 600.77001953125, 604.8499755859375, 605.3099975585938, 603.3599853515625, 609.72998046875, 609.7000122070312, 611.489990234375, 612.9299926757812, 610.3800048828125, 599.9400024414062, 597.2100219726562, 594.239990234375, 594.5399780273438, 585.0499877929688, 594.1799926757812, 583.77001953125, 576.8599853515625, 583.0599975585938, 572.7100219726562, 575.9199829101562, 560.5800170898438, 555.9199829101562, 558.8699951171875, 551.4199829101562, 562.8099975585938, 567.1500244140625, 561.02001953125, 567.1300048828125, 565.489990234375, 563.97998046875, 574.0800170898438, 575.4600219726562, 568.5900268554688, 567.0800170898438, 555.6599731445312, 559.3900146484375, 560.969970703125, 564.52001953125, 536.7000122070312, 505.2799987792969, 504.3800048828125, 496.4800109863281, 548.6199951171875, 524.5800170898438, 533.9400024414062, 539.1199951171875, 537.6099853515625, 525.6599731445312, 526.4099731445312, 513.8800048828125, 527.25, 535.4199829101562, 546.6900024414062, 550.6400146484375, 550.8499755859375, 554.3200073242188, 554.5399780273438, 558.469970703125, 566.760009765625, 563.510009765625, 558.7999877929688, 561.1500244140625, 565.0599975585938, 564.3400268554688, 582.989990234375, 586.8400268554688, 587.5900268554688, 590.4600219726562, 594.2000122070312, 594.8499755859375, 592.8499755859375, 582.8599853515625, 583.0900268554688, 579.1099853515625, 591.1500244140625, 587.72998046875, 590.0499877929688, 589.3900146484375, 592.7100219726562, 596.0900268554688, 595.9299926757812, 593.0499877929688, 599.1400146484375, 599.6799926757812, 603.0800170898438, 601.3599853515625, 603.75, 597.0, 602.6799926757812, 597.530029296875, 597.4400024414062, 594.280029296875, 600.1500244140625, 606.780029296875, 607.1199951171875, 611.8699951171875, 614.9099731445312, 617.8499755859375, 617.6500244140625, 620.4500122070312, 625.3400268554688, 620.6799926757812, 620.3400268554688, 624.0599975585938, 625.8200073242188, 623.6199951171875, 624.8099975585938, 622.1400146484375, 624.219970703125, 628.0399780273438, 627.5800170898438, 628.77001953125, 628.8599853515625, 634.2100219726562, 634.4199829101562, 637.0999755859375, 636.9400024414062, 635.260009765625], "name": "SPDR S&P 500 ETF", "error": false}
\ No newline at end of file diff --git a/simulate.py b/simulate.py new file mode 100644 index 0000000..0ecdaff --- /dev/null +++ b/simulate.py @@ -0,0 +1,101 @@ +from algo import Algo +from ema_algo import Ema_Algo +from api import fetch_chart_data +import datetime +import json + +""" +Function that takes in data and returns a buy, sell, or hold singal per interval +""" +def backtest_algo(algo : Algo, timestamps, prices, init_offset=5, starting_money=10000): + # take a fraction of the OG data (or a set #) + assert len(timestamps) == len(prices) + assert init_offset < len(timestamps) # make sure enough data to start with + + current_timestamps = timestamps[:init_offset] + current_prices = prices[:init_offset] + + is_bought = 0.0 # not holding anything + current_liquid = 10000 + shares_owned = 0 + buy_data = [] + sell_data = [] + for i in range(init_offset, len(timestamps)): + # update prices array and run algo + current_timestamps.append(timestamps[i]) + current_prices.append(prices[i]) + current_signal = algo.detemine_signal(current_timestamps, current_prices) + + cur_p = current_prices[-1] + cur_t = current_timestamps[-1] + if current_signal == 1.0: # signal is to buy + if is_bought == 0.0: # if we haven't bought, purchase + shares_owned = current_liquid / cur_p + current_liquid = 0 + is_bought = 1.0 + buy_data.append(i) + print("buy", shares_owned, current_liquid, datetime.datetime.fromtimestamp(timestamps[i])) + elif current_signal == 0.0: # signal sell all + if is_bought == 1.0: # if we have bought, sell! + current_liquid = shares_owned * cur_p + shares_owned = 0 + is_bought = 0.0 + sell_data.append(i) + print('sell', shares_owned, current_liquid, datetime.datetime.fromtimestamp(timestamps[i])) + + + # calculate total assets + assets = prices[-1] * shares_owned + current_liquid + percent_gain = 100 * (assets - starting_money) / starting_money + print(assets, percent_gain) + + # create a json to store the reuslts + results = { + "timestamps" : timestamps, + "prices" : prices, + "buy_indices" : buy_data, + "sell_indices" : sell_data, + "percent_gain" : percent_gain, + "starting_assets" : starting_money, + "final_assets" : assets, + "algo_name" : algo.name, + "algo_graph_data" : algo.graph_data + } + + return results + + # store all algo name, buy, sell, price data, timestamps, into a json so it can be viewed as a trial + # caluclate some metrics (NOW: only how much money gained, how many trades, trades per day... etc) + + pass + +def test(): + print("MAIN simulate.py") + + ema_algo = Ema_Algo() + + # get data + data = fetch_chart_data('SPY', '1y', '1d') + print(data.keys()) + + url_params = { + "ticker" : 'SPY', + "period" : '1yr', + "interval" : '1d', + } + + results = backtest_algo(ema_algo, data['timestamps'], data['prices'], 13) + + # write the data into a json to be viewed in a chart + + trial_data = { + "chart_data" : data, + "url_params" : url_params, + "backtest_results" : results + } + + fd = open('bt-recent.json', 'w') + fd.write(json.dumps(trial_data)) + fd.close() + +test()
\ No newline at end of file |