Search
Long SPY

Buy and hold the SPY etf.

asset: SPY etf
start date: January 2, 2019
trade enter time: 9:31am
starting investment size: 100,000

import pandas as pd
import datetime as dt
import urllib3
import json
import plotly.graph_objects as go
from IPython.core.display import display, HTML
from plotly.offline import init_notebook_mode, plot

# get the results data
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
http = urllib3.PoolManager()
response = http.request('GET', "https://www.backtestzoo.com/equitysingleasset/long_spy_data.json")
backtest_results_json_string = response.data.decode('utf-8')
backtest_results_json = json.loads(backtest_results_json_string)
strategy_equity_json = backtest_results_json['Charts']['Strategy Equity']['Series']['Equity']['Values']
strategy_equity_df = pd.DataFrame(strategy_equity_json)
timestamp = strategy_equity_df['x']
equity = strategy_equity_df['y']
dates=[dt.datetime.fromtimestamp(ts) for ts in timestamp]

final_value = backtest_results_json['RuntimeStatistics']['Equity']
pct_return = backtest_results_json['TotalPerformance']['PortfolioStatistics']['TotalNetProfit']
sharpe_ratio = backtest_results_json['TotalPerformance']['PortfolioStatistics']['SharpeRatio']
max_drawdown = backtest_results_json['TotalPerformance']['PortfolioStatistics']['Drawdown']
start_date = dates[0]
end_date = dates[-1]
date_range = start_date.strftime("%b %d, %Y") + " to\r\n" + end_date.strftime("%b %d, %Y")

layout2 = go.Layout(
    height = 50,
    margin=go.layout.Margin(
        l=0,
        r=0,
        b=0,
        t=0,
        pad=0
    )
)

fig2 = go.Figure(data=[go.Table(header=dict(values=['Date Range','Ending Value', '% Return', 'Sharpe Ratio', 'Max Drawdown']),
                 cells=dict(values=[[date_range],[final_value.split('.',1)[0]], ["{:.1%}".format(pct_return)], [round(sharpe_ratio,1)], ["{:.1%}".format(max_drawdown)]]))
                     ], layout=layout2)
plot(fig2, filename = 'long_spy_performance_table.html')
display(HTML('long_spy_performance_table.html'))
init_notebook_mode(connected=True)
data = [go.Scatter(x=dates, y=equity)]
layout = go.Layout(
    title=go.layout.Title(
        text='Equity Value of Account',
        xref='paper',
        x=0
    ),
    xaxis=go.layout.XAxis(
        title=go.layout.xaxis.Title(
            text='Date',
            font=dict(
                family='Courier New, monospace',
                size=18,
                color='#7f7f7f'
            )
        )
    ),
    yaxis=go.layout.YAxis(
        title=go.layout.yaxis.Title(
            text='Dollar Value',
            font=dict(
                family='Courier New, monospace',
                size=18,
                color='#7f7f7f'
            )
        )
    ),
    margin=go.layout.Margin(
        l=0,
        r=0,
        b=0,
        t=25,
        pad=0
    )
)
fig = go.Figure(data=data, layout=layout)

plot(fig, filename = 'long_spy_equity_chart.html')
display(HTML('long_spy_equity_chart.html'))