Skip to content

Commit

Permalink
Added a timestamp to backtest reports. (#191)
Browse files Browse the repository at this point in the history
# Describe Request

Added a timestamp to backtest reports.

Fixed #181 

# Change Type

New feature.
  • Loading branch information
cinar authored Jul 20, 2024
1 parent c271eff commit fd14fc9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 23 deletions.
23 changes: 12 additions & 11 deletions helper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1050,24 +1050,25 @@ type Number interface {
```

<a name="Report"></a>
## type [Report](<https://github.com/cinar/indicator/blob/master/helper/report.go#L48-L54>)
## type [Report](<https://github.com/cinar/indicator/blob/master/helper/report.go#L48-L55>)

Report generates an HTML file containing an interactive chart that visually represents the provided data and annotations.

The generated HTML file can be opened in a web browser to explore the data visually, interact with the chart elements, and view the associated annotations.

```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
}
```

<a name="NewReport"></a>
### func [NewReport](<https://github.com/cinar/indicator/blob/master/helper/report.go#L59>)
### func [NewReport](<https://github.com/cinar/indicator/blob/master/helper/report.go#L60>)

```go
func NewReport(title string, date <-chan time.Time) *Report
Expand All @@ -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.

<a name="Report.AddChart"></a>
### func \(\*Report\) [AddChart](<https://github.com/cinar/indicator/blob/master/helper/report.go#L74>)
### func \(\*Report\) [AddChart](<https://github.com/cinar/indicator/blob/master/helper/report.go#L76>)

```go
func (r *Report) AddChart() int
Expand All @@ -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.

<a name="Report.AddColumn"></a>
### func \(\*Report\) [AddColumn](<https://github.com/cinar/indicator/blob/master/helper/report.go#L81>)
### func \(\*Report\) [AddColumn](<https://github.com/cinar/indicator/blob/master/helper/report.go#L83>)

```go
func (r *Report) AddColumn(column ReportColumn, charts ...int)
Expand All @@ -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.

<a name="Report.WriteToFile"></a>
### func \(\*Report\) [WriteToFile](<https://github.com/cinar/indicator/blob/master/helper/report.go#L109>)
### func \(\*Report\) [WriteToFile](<https://github.com/cinar/indicator/blob/master/helper/report.go#L111>)

```go
func (r *Report) WriteToFile(fileName string) error
Expand All @@ -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.

<a name="Report.WriteToWriter"></a>
### func \(\*Report\) [WriteToWriter](<https://github.com/cinar/indicator/blob/master/helper/report.go#L97>)
### func \(\*Report\) [WriteToWriter](<https://github.com/cinar/indicator/blob/master/helper/report.go#L99>)

```go
func (r *Report) WriteToWriter(writer io.Writer) error
Expand Down
14 changes: 8 additions & 6 deletions helper/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,7 +65,8 @@ func NewReport(title string, date <-chan time.Time) *Report {
Views: [][]int{
{},
},
DateFormat: DefaultReportDateFormat,
DateFormat: DefaultReportDateFormat,
GeneratedOn: time.Now().String(),
}
}

Expand Down
3 changes: 3 additions & 0 deletions helper/report.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
<p>
<strong><a href="https://github.com/cinar/indicator">Indicator</a></strong> Copyright (c) 2021-2023 Onur Cinar. The source code is provided under GNU AGPLv3 License.
</p>
<p>
{{ .GeneratedOn }}
</p>
</div>
</footer>

Expand Down
16 changes: 10 additions & 6 deletions strategy/backtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -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"))
Expand Down
3 changes: 3 additions & 0 deletions strategy/backtest_asset_report.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
<p>
<strong><a href="https://github.com/cinar/indicator">Indicator</a></strong> Copyright (c) 2021-2023 Onur Cinar. The source code is provided under GNU AGPLv3 License.
</p>
<p>
{{ .GeneratedOn }}
</p>
</div>
</footer>
</body>
Expand Down
3 changes: 3 additions & 0 deletions strategy/backtest_report.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
<p>
<strong><a href="https://github.com/cinar/indicator">Indicator</a></strong> Copyright (c) 2021-2023 Onur Cinar. The source code is provided under GNU AGPLv3 License.
</p>
<p>
{{ .GeneratedOn }}
</p>
</div>
</footer>
</body>
Expand Down

0 comments on commit fd14fc9

Please sign in to comment.