From fd14fc90fd864f69f7da4a02f0f4681a7b2265d5 Mon Sep 17 00:00:00 2001 From: Onur Cinar Date: Sat, 20 Jul 2024 14:08:36 -0700 Subject: [PATCH] Added a timestamp to backtest reports. (#191) # Describe Request Added a timestamp to backtest reports. Fixed #181 # Change Type New feature. --- helper/README.md | 23 ++++++++++++----------- helper/report.go | 14 ++++++++------ helper/report.tmpl | 3 +++ strategy/backtest.go | 16 ++++++++++------ strategy/backtest_asset_report.tmpl | 3 +++ strategy/backtest_report.tmpl | 3 +++ 6 files changed, 39 insertions(+), 23 deletions(-) diff --git a/helper/README.md b/helper/README.md index 354d812..8720aa4 100644 --- a/helper/README.md +++ b/helper/README.md @@ -1050,7 +1050,7 @@ type Number interface { ``` -## type [Report]() +## type [Report]() Report generates an HTML file containing an interactive chart that visually represents the provided data and annotations. @@ -1058,16 +1058,17 @@ The generated HTML file can be opened in a web browser to explore the data visua ```go type Report struct { - Title string - Date <-chan time.Time - Columns []ReportColumn - Views [][]int - DateFormat string + Title string + Date <-chan time.Time + Columns []ReportColumn + Views [][]int + DateFormat string + GeneratedOn string } ``` -### func [NewReport]() +### func [NewReport]() ```go func NewReport(title string, date <-chan time.Time) *Report @@ -1076,7 +1077,7 @@ func NewReport(title string, date <-chan time.Time) *Report NewReport takes a channel of time as the time axis and returns a new instance of the Report struct. This instance can later be used to add data and annotations and subsequently generate a report. -### func \(\*Report\) [AddChart]() +### func \(\*Report\) [AddChart]() ```go func (r *Report) AddChart() int @@ -1085,7 +1086,7 @@ func (r *Report) AddChart() int AddChart adds a new chart to the report and returns its unique identifier. This identifier can be used later to refer to the chart and add columns to it. -### func \(\*Report\) [AddColumn]() +### func \(\*Report\) [AddColumn]() ```go func (r *Report) AddColumn(column ReportColumn, charts ...int) @@ -1094,7 +1095,7 @@ func (r *Report) AddColumn(column ReportColumn, charts ...int) AddColumn adds a new data column to the specified charts. If no chart is specified, it will be added to the main chart. -### func \(\*Report\) [WriteToFile]() +### func \(\*Report\) [WriteToFile]() ```go func (r *Report) WriteToFile(fileName string) error @@ -1103,7 +1104,7 @@ func (r *Report) WriteToFile(fileName string) error WriteToFile writes the generated report content to a file with the specified name. This allows users to conveniently save the report for later viewing or analysis. -### func \(\*Report\) [WriteToWriter]() +### func \(\*Report\) [WriteToWriter]() ```go func (r *Report) WriteToWriter(writer io.Writer) error diff --git a/helper/report.go b/helper/report.go index aca307b..cd411e4 100644 --- a/helper/report.go +++ b/helper/report.go @@ -46,11 +46,12 @@ type ReportColumn interface { // the data visually, interact with the chart elements, and view // the associated annotations. type Report struct { - Title string - Date <-chan time.Time - Columns []ReportColumn - Views [][]int - DateFormat string + Title string + Date <-chan time.Time + Columns []ReportColumn + Views [][]int + DateFormat string + GeneratedOn string } // NewReport takes a channel of time as the time axis and returns a new @@ -64,7 +65,8 @@ func NewReport(title string, date <-chan time.Time) *Report { Views: [][]int{ {}, }, - DateFormat: DefaultReportDateFormat, + DateFormat: DefaultReportDateFormat, + GeneratedOn: time.Now().String(), } } diff --git a/helper/report.tmpl b/helper/report.tmpl index 17ba99e..0e6e1f0 100644 --- a/helper/report.tmpl +++ b/helper/report.tmpl @@ -33,6 +33,9 @@

Indicator Copyright (c) 2021-2023 Onur Cinar. The source code is provided under GNU AGPLv3 License.

+

+ {{ .GeneratedOn }} +

diff --git a/strategy/backtest.go b/strategy/backtest.go index 16a5b91..277661d 100644 --- a/strategy/backtest.go +++ b/strategy/backtest.go @@ -247,13 +247,15 @@ func (b *Backtest) worker(names <-chan string, bestResults chan<- *backtestResul // writeAssetReport generates a detailed report for the asset, summarizing the backtest results. func (b *Backtest) writeAssetReport(name string, results []*backtestResult) error { type Model struct { - AssetName string - Results []*backtestResult + AssetName string + Results []*backtestResult + GeneratedOn string } model := Model{ - AssetName: name, - Results: results, + AssetName: name, + Results: results, + GeneratedOn: time.Now().String(), } file, err := os.Create(filepath.Join(b.outputDir, fmt.Sprintf("%s.html", name))) @@ -277,11 +279,13 @@ func (b *Backtest) writeAssetReport(name string, results []*backtestResult) erro // writeReport generates a detailed report for the backtest results. func (b *Backtest) writeReport(results []*backtestResult) error { type Model struct { - Results []*backtestResult + Results []*backtestResult + GeneratedOn string } model := Model{ - Results: results, + Results: results, + GeneratedOn: time.Now().String(), } file, err := os.Create(filepath.Join(b.outputDir, "index.html")) diff --git a/strategy/backtest_asset_report.tmpl b/strategy/backtest_asset_report.tmpl index 53892d4..0921865 100644 --- a/strategy/backtest_asset_report.tmpl +++ b/strategy/backtest_asset_report.tmpl @@ -69,6 +69,9 @@

Indicator Copyright (c) 2021-2023 Onur Cinar. The source code is provided under GNU AGPLv3 License.

+

+ {{ .GeneratedOn }} +

diff --git a/strategy/backtest_report.tmpl b/strategy/backtest_report.tmpl index 51475f1..b9b4749 100644 --- a/strategy/backtest_report.tmpl +++ b/strategy/backtest_report.tmpl @@ -67,6 +67,9 @@

Indicator Copyright (c) 2021-2023 Onur Cinar. The source code is provided under GNU AGPLv3 License.

+

+ {{ .GeneratedOn }} +