Skip to content

Commit

Permalink
fix(pdf): make montly-returns graph bigger
Browse files Browse the repository at this point in the history
  • Loading branch information
Caceresenzo committed Sep 26, 2023
1 parent 5188c01 commit 60e83ea
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions backtest/export/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def initialize(self) -> None:
def finalize(self) -> None:
df_returns = self.quantstats_exporter.returns if self.quantstats_exporter else None
df_benchmark = self.quantstats_exporter.benchmark if self.quantstats_exporter else None

df_dump = None
if self.dump_exporter and self.dump_exporter.dataframe is not None:
df_dump = self.dump_exporter.dataframe.reset_index().sort_values(by='date')
Expand All @@ -65,15 +65,15 @@ def finalize(self) -> None:
df_metrics = quantstats.reports.metrics(df_returns, benchmark=df_benchmark, display=False, mode="full")
df_metrics.index = df_metrics.index.map(slugify.slugify)
df_metrics.columns = df_metrics.columns.map(slugify.slugify)

df_drowdowns = quantstats.stats.to_drawdown_series(df_returns)
if not df_drowdowns.empty:
details = quantstats.stats.drawdown_details(df_drowdowns)
df_drowdowns = details.sort_values(
by=details.columns[4],
ascending = True
ascending=True
)[:5]

if df_drowdowns.empty:
df_drowdowns = None
else:
Expand All @@ -84,9 +84,11 @@ def finalize(self) -> None:
"$date": datetime.date.today().isoformat(),
})

figsize = (8, 5)

if df_returns is not None:
self.template.apply({
"$qs.montly-returns": lambda _: quantstats.plots.monthly_returns(df_returns, show=False, cbar=False),
"$qs.montly-returns": lambda _: quantstats.plots.monthly_returns(df_returns, show=False, cbar=False, figsize=(figsize[0], figsize[0]*.5)),
"$qs.cumulative-returns": lambda _: quantstats.plots.returns(df_returns, df_benchmark, show=False, subtitle=False),
"$qs.cumulative-returns-volatility": lambda _: quantstats.plots.returns(df_returns, df_benchmark, match_volatility=df_benchmark is not None, show=False),
"$qs.eoy-returns": lambda _: quantstats.plots.yearly_returns(df_returns, df_benchmark, show=False),
Expand Down Expand Up @@ -122,7 +124,7 @@ def get_drawdown(n: int, key: typing.Union[typing.Literal["dates"], typing.Liter
row = df_drowdowns.iloc[index]
else:
row = None

if key == "dates":
if row is None:
return "----/--/-- - ----/--/--"
Expand All @@ -146,12 +148,11 @@ def get_drawdown(n: int, key: typing.Union[typing.Literal["dates"], typing.Liter
self.template.apply_re({
r"\$qs\.metric\.strategy\.(.+)": lambda _, metric: get_metric("strategy", metric),
})

self.template.apply_re({
r"\$qs\.worst-drawdowns\.(\d+).(dates|value)": lambda _, n, key: get_drawdown(int(n), key),
})


self.template.apply(self.variables)

for user_script in self.user_scripts:
Expand Down

0 comments on commit 60e83ea

Please sign in to comment.