From f26a7adb817dc8d2b1f9b77075cebaf97553a7f5 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Sat, 14 Sep 2024 12:56:15 -0700 Subject: [PATCH] Using template.Must for HTMLReport. --- backtest/README.md | 41 ++++++++++++++++++++++++++++++++++++++++- backtest/html_report.go | 10 ++-------- helper/README.md | 10 ++++++++++ 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/backtest/README.md b/backtest/README.md index fffa95a..da79197 100644 --- a/backtest/README.md +++ b/backtest/README.md @@ -25,6 +25,7 @@ The information provided on this project is strictly for informational purposes ## Index - [Constants](<#constants>) +- [func RegisterReportBuilder\(name string, builder ReportBuilderFunc\)](<#RegisterReportBuilder>) - [type Backtest](<#Backtest>) - [func NewBacktest\(repository asset.Repository, report Report\) \*Backtest](<#NewBacktest>) - [func \(b \*Backtest\) Run\(\) error](<#Backtest.Run>) @@ -36,6 +37,8 @@ The information provided on this project is strictly for informational purposes - [func \(h \*HTMLReport\) End\(\) error](<#HTMLReport.End>) - [func \(h \*HTMLReport\) Write\(assetName string, currentStrategy strategy.Strategy, snapshots \<\-chan \*asset.Snapshot, actions \<\-chan strategy.Action, outcomes \<\-chan float64\) error](<#HTMLReport.Write>) - [type Report](<#Report>) + - [func NewReport\(name, config string\) \(Report, error\)](<#NewReport>) +- [type ReportBuilderFunc](<#ReportBuilderFunc>) ## Constants @@ -61,6 +64,24 @@ const ( ) ``` + + +```go +const ( + // HTMLReportBuilderName is the name for the HTML report builder. + HTMLReportBuilderName = "html" +) +``` + + +## func [RegisterReportBuilder]() + +```go +func RegisterReportBuilder(name string, builder ReportBuilderFunc) +``` + +RegisterReportBuilder registers the given builder. + ## type [Backtest]() @@ -105,7 +126,7 @@ Run executes a comprehensive performance evaluation of the designated strategies ## type [HTMLReport]() -HTMLReport is the backtest HTML report interface. +HTMLReport is the backtest HTML report. ```go type HTMLReport struct { @@ -198,4 +219,22 @@ type Report interface { } ``` + +### func [NewReport]() + +```go +func NewReport(name, config string) (Report, error) +``` + +NewReport builds a new report by the given name type and the configuration. + + +## type [ReportBuilderFunc]() + +ReportBuilderFunc defines a function to build a new report using the given configuration parameter. + +```go +type ReportBuilderFunc func(config string) (Report, error) +``` + Generated by [gomarkdoc]() diff --git a/backtest/html_report.go b/backtest/html_report.go index ba9b6b8..fa6aee4 100644 --- a/backtest/html_report.go +++ b/backtest/html_report.go @@ -216,10 +216,7 @@ func (h *HTMLReport) writeAssetReport(name string, results []*htmlReportResult) defer helper.CloseAndLogError(file, "unable to close asset report file") - tmpl, err := template.New("report").Parse(htmlAssetReportTmpl) - if err != nil { - return fmt.Errorf("unable to initialize asset report template: %w", err) - } + tmpl := template.Must(template.New("report").Parse(htmlAssetReportTmpl)) err = tmpl.Execute(file, model) if err != nil { @@ -248,10 +245,7 @@ func (h *HTMLReport) writeReport() error { defer helper.CloseAndLogError(file, "unable to close main report file") - tmpl, err := template.New("report").Parse(htmlReportTmpl) - if err != nil { - return fmt.Errorf("unable to execute main report template: %w", err) - } + tmpl := template.Must(template.New("report").Parse(htmlReportTmpl)) err = tmpl.Execute(file, model) if err != nil { diff --git a/helper/README.md b/helper/README.md index 0d691b3..fffc3bb 100644 --- a/helper/README.md +++ b/helper/README.md @@ -38,6 +38,7 @@ The information provided on this project is strictly for informational purposes - [func CheckEquals\[T comparable\]\(inputs ...\<\-chan T\) error](<#CheckEquals>) - [func CloseAndLogError\(closer io.Closer, message string\)](<#CloseAndLogError>) - [func Count\[T Number, O any\]\(from T, other \<\-chan O\) \<\-chan T](<#Count>) +- [func DaysBetween\(from, to time.Time\) int](<#DaysBetween>) - [func DecrementBy\[T Number\]\(c \<\-chan T, d T\) \<\-chan T](<#DecrementBy>) - [func Divide\[T Number\]\(ac, bc \<\-chan T\) \<\-chan T](<#Divide>) - [func DivideBy\[T Number\]\(c \<\-chan T, d T\) \<\-chan T](<#DivideBy>) @@ -348,6 +349,15 @@ fmt.Println(<- s) // 3 fmt.Println(<- s) // 4 ``` + +## func [DaysBetween]() + +```go +func DaysBetween(from, to time.Time) int +``` + +DaysBetween calculates the days between the given two times. + ## func [DecrementBy]()