Skip to content

Commit

Permalink
added parameters section and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesir committed Mar 31, 2024
1 parent 25ddc5b commit 14b4693
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Icon
.vscode
Icon
QuantStats_Lumi.egg-info/*
test_tearsheet.html
test_tearsheet_parameters.html
3 changes: 3 additions & 0 deletions quantstats_lumi/report.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ <h3>Key Performance Metrics</h3>
<h3>Worst 10 Drawdowns</h3>
{{dd_info}}
</div>

<!--- Add parameters if available -->
{{parameters_section}}
</div>

</div>
Expand Down
89 changes: 77 additions & 12 deletions quantstats_lumi/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,56 @@ def _match_dates(returns, benchmark):

def html(
returns,
benchmark=None,
rf=0.0,
grayscale=False,
title="Strategy Tearsheet",
output=None,
compounded=True,
periods_per_year=365,
download_filename="quantstats-tearsheet.html",
figfmt="svg",
template_path=None,
match_dates=True,
benchmark: _pd.Series = None,
rf: float = 0.0,
grayscale: bool = False,
title: str = "Strategy Tearsheet",
output: str = None,
compounded: bool = True,
periods_per_year: int = 365,
download_filename: str = "tearsheet.html",
figfmt: str = "svg",
template_path: str = None,
match_dates: bool = True,
parameters: dict = None,
**kwargs,
):
"""generates full HTML tear sheet report"""
"""
Generates a full HTML tearsheet with performance metrics and plots
Parameters
----------
returns : pd.Series, pd.DataFrame
Strategy returns
benchmark : pd.Series, optional
Benchmark returns
rf : float, optional
Risk-free rate, default is 0
grayscale : bool, optional
Plot in grayscale, default is False
title : str, optional
Title of the HTML report, default is "Strategy Tearsheet"
output : str, optional
Output file path
compounded : bool, optional
Whether to use compounded returns, default is True
periods_per_year : int, optional
Trading periods per year, default is 365
download_filename : str, optional
Download filename, default is "tearsheet.html"
figfmt : str, optional
Figure format, default is "svg"
template_path : str, optional
Custom template path
match_dates : bool, optional
Match dates of returns and benchmark, default is True
parameters : dict, optional
Strategy parameters
Returns
-------
None
"""

if output is None and not _utils._in_notebook():
raise ValueError("`output` must be specified")
Expand Down Expand Up @@ -167,6 +203,9 @@ def html(
"<tr><td></td><td></td></tr>", '<tr><td colspan="2"><hr></td></tr>'
)

if parameters is not None:
tpl = tpl.replace("{{parameters_section}}", parameters_section(parameters))

if benchmark is not None:
yoy = _stats.compare(
returns, benchmark, "YE", compounded=compounded, prepare_returns=False
Expand Down Expand Up @@ -739,6 +778,32 @@ def basic(
active=active,
)

def parameters_section(parameters):
"""returns a formatted section for strategy parameters"""
if parameters is None:
return ""

tpl = """
<div id="params">
<h3>Parameters Used</h3>
<table style="width:100%; font-size: 12px">
"""

# Add titles to the table
tpl += "<thead><tr><th>Parameter</th><th>Value</th></tr></thead>"

for key, value in parameters.items():
# Make sure that the value is something that can be displayed
if not isinstance(value, (int, float, str)):
value = str(value)

tpl += f"<tr><td>{key}</td><td>{value}</td></tr>"
tpl += """
</table>
</div>
"""

return tpl

def metrics(
returns,
Expand Down
2 changes: 1 addition & 1 deletion quantstats_lumi/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "0.1.13"
version = "0.2.0"

0 comments on commit 14b4693

Please sign in to comment.