Skip to content

Commit

Permalink
Using template.Must for HTMLReport.
Browse files Browse the repository at this point in the history
  • Loading branch information
cinar committed Sep 14, 2024
1 parent f5a88d0 commit f26a7ad
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
41 changes: 40 additions & 1 deletion backtest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
Expand All @@ -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
Expand All @@ -61,6 +64,24 @@ const (
)
```

<a name="HTMLReportBuilderName"></a>

```go
const (
// HTMLReportBuilderName is the name for the HTML report builder.
HTMLReportBuilderName = "html"
)
```

<a name="RegisterReportBuilder"></a>
## func [RegisterReportBuilder](<https://github.com/cinar/indicator/blob/master/backtest/report_factory.go#L25>)

```go
func RegisterReportBuilder(name string, builder ReportBuilderFunc)
```

RegisterReportBuilder registers the given builder.

<a name="Backtest"></a>
## type [Backtest](<https://github.com/cinar/indicator/blob/master/backtest/backtest.go#L43-L61>)

Expand Down Expand Up @@ -105,7 +126,7 @@ Run executes a comprehensive performance evaluation of the designated strategies
<a name="HTMLReport"></a>
## type [HTMLReport](<https://github.com/cinar/indicator/blob/master/backtest/html_report.go#L36-L53>)

HTMLReport is the backtest HTML report interface.
HTMLReport is the backtest HTML report.

```go
type HTMLReport struct {
Expand Down Expand Up @@ -198,4 +219,22 @@ type Report interface {
}
```

<a name="NewReport"></a>
### func [NewReport](<https://github.com/cinar/indicator/blob/master/backtest/report_factory.go#L30>)

```go
func NewReport(name, config string) (Report, error)
```

NewReport builds a new report by the given name type and the configuration.

<a name="ReportBuilderFunc"></a>
## type [ReportBuilderFunc](<https://github.com/cinar/indicator/blob/master/backtest/report_factory.go#L17>)

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](<https://github.com/princjef/gomarkdoc>)
10 changes: 2 additions & 8 deletions backtest/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>)
Expand Down Expand Up @@ -348,6 +349,15 @@ fmt.Println(<- s) // 3
fmt.Println(<- s) // 4
```

<a name="DaysBetween"></a>
## func [DaysBetween](<https://github.com/cinar/indicator/blob/master/helper/days_between.go#L13>)

```go
func DaysBetween(from, to time.Time) int
```

DaysBetween calculates the days between the given two times.

<a name="DecrementBy"></a>
## func [DecrementBy](<https://github.com/cinar/indicator/blob/master/helper/decrement_by.go#L16>)

Expand Down

0 comments on commit f26a7ad

Please sign in to comment.