diff --git a/backtester/statistics/charts.py b/backtester/statistics/charts.py index f0333a3..2bdea84 100644 --- a/backtester/statistics/charts.py +++ b/backtester/statistics/charts.py @@ -5,59 +5,59 @@ import altair as alt def returns_chart(report): # Time interval selector - time_interval = alt.selection(type="interval", encodings=["x"]) + time_interval = alt.selection(type='interval', encodings=['x']) # Area plot - areas = alt.Chart().mark_area(opacity=0.6).encode(x='index:T', - y=alt.Y("accumulated return:Q", axis=alt.Axis(format="%"))) + areas = alt.Chart().mark_area(opacity=0.7).encode(x='index:T', + y=alt.Y('accumulated return:Q', axis=alt.Axis(format='%'))) # Nearest point selector - nearest = alt.selection(type="single", nearest=True, on="mouseover", fields=["index"], empty="none") + nearest = alt.selection(type='single', nearest=True, on='mouseover', fields=['index'], empty='none') points = areas.mark_point().encode(opacity=alt.condition(nearest, alt.value(1), alt.value(0))) # Transparent date selector selectors = alt.Chart().mark_point().encode( - x="index:T", + x='index:T', opacity=alt.value(0), ).add_selection(nearest) - text = areas.mark_text(align="left", dx=5, - dy=-5).encode(text=alt.condition(nearest, "accumulated return:Q", alt.value(" "))) + text = areas.mark_text( + align='left', dx=5, + dy=-5).encode(text=alt.condition(nearest, 'accumulated return:Q', alt.value(' '))).transform_calculate( + label='datum.y + " %"') - layered = alt.layer(areas.encode( - alt.X('index:T', axis=alt.Axis(title="date"), scale=alt.Scale(domain=time_interval))), - selectors, + layered = alt.layer(selectors, points, text, + areas.encode( + alt.X('index:T', axis=alt.Axis(title='date'), scale=alt.Scale(domain=time_interval))), width=700, height=350, - title="Returns over time") + title='Returns over time') lower = areas.properties(width=700, height=70).add_selection(time_interval) - chart = alt.vconcat(layered, lower, data=report.reset_index()) - - return chart + return alt.vconcat(layered, lower, data=report.reset_index()) def returns_histogram(report): - bar = alt.Chart(report).mark_bar().encode(x=alt.X("% change:Q", + bar = alt.Chart(report).mark_bar().encode(x=alt.X('% change:Q', bin=alt.BinParams(maxbins=100), axis=alt.Axis(format='%')), - y="count():Q") + y='count():Q') return bar def monthly_returns_heatmap(report): - resample = report.resample("M")["capital"].last() + resample = report.resample('M')['capital'].last() monthly_returns = resample.pct_change().reset_index() monthly_returns['capital'].iat[0] = resample.iloc[0] / report.iloc[0]['capital'] - 1 - monthly_returns.columns = ["date", "capital"] + monthly_returns.columns = ['date', 'capital'] chart = alt.Chart(monthly_returns).mark_rect().encode( - alt.X("year(date):O", title="Year"), alt.Y("month(date):O", title="Month"), - alt.Color("mean(capital)", title="Return", scale=alt.Scale(scheme="redyellowgreen")), - alt.Tooltip("mean(capital)", format=".2f")).properties(title="Monthly Returns") + alt.X('year(date):O', title='Year'), alt.Y('month(date):O', title='Month'), + alt.Color('mean(capital)', title='Return', scale=alt.Scale(scheme='redyellowgreen')), + alt.Tooltip('mean(capital)', format='.2f')).properties(title='Monthly Returns') return chart