# README
backtest
import "github.com/cinar/indicator/v2/backtest"
Package backtest contains the backtest functions.
This package belongs to the Indicator project. Indicator is a Golang module that supplies a variety of technical indicators, strategies, and a backtesting framework for analysis.
License
Copyright (c) 2021-2024 Onur Cinar.
The source code is provided under GNU AGPLv3 License.
https://github.com/cinar/indicator
Disclaimer
The information provided on this project is strictly for informational purposes and is not to be construed as advice or solicitation to buy or sell any security.
Index
- Constants
- func RegisterReportBuilder(name string, builder ReportBuilderFunc)
- type Backtest
- type DataReport
- func NewDataReport() *DataReport
- func (d *DataReport) AssetBegin(name string, strategies []strategy.Strategy) error
- func (*DataReport) AssetEnd(_ string) error
- func (*DataReport) Begin(_ []string, _ []strategy.Strategy) error
- func (*DataReport) End() error
- func (d *DataReport) Write(assetName string, currentStrategy strategy.Strategy, snapshots <-chan *asset.Snapshot, actions <-chan strategy.Action, outcomes <-chan float64) error
- type DataStrategyResult
- type HTMLReport
- func NewHTMLReport(outputDir string) *HTMLReport
- func (h *HTMLReport) AssetBegin(name string, strategies []strategy.Strategy) error
- func (h *HTMLReport) AssetEnd(name string) error
- func (h *HTMLReport) Begin(assetNames []string, _ []strategy.Strategy) error
- func (h *HTMLReport) End() error
- func (h *HTMLReport) Write(assetName string, currentStrategy strategy.Strategy, snapshots <-chan *asset.Snapshot, actions <-chan strategy.Action, outcomes <-chan float64) error
- type Report
- type ReportBuilderFunc
Constants
const (
// DefaultBacktestWorkers is the default number of backtest workers.
DefaultBacktestWorkers = 1
// DefaultLastDays is the default number of days backtest should go back.
DefaultLastDays = 365
)
const (
// DefaultWriteStrategyReports is the default state of writing individual strategy reports.
DefaultWriteStrategyReports = true
)
const (
// HTMLReportBuilderName is the name for the HTML report builder.
HTMLReportBuilderName = "html"
)
func RegisterReportBuilder
func RegisterReportBuilder(name string, builder ReportBuilderFunc)
RegisterReportBuilder registers the given builder.
type Backtest
Backtest function rigorously evaluates the potential performance of the specified strategies applied to a defined set of assets. It generates comprehensive visual representations for each strategy-asset pairing.
type Backtest struct {
// Names is the names of the assets to backtest.
Names []string
// Strategies is the list of strategies to apply.
Strategies []strategy.Strategy
// Workers is the number of concurrent workers.
Workers int
// LastDays is the number of days backtest should go back.
LastDays int
// Logger is the slog logger instance.
Logger *slog.Logger
// contains filtered or unexported fields
}
func NewBacktest
func NewBacktest(repository asset.Repository, report Report) *Backtest
NewBacktest function initializes a new backtest instance.
func (*Backtest) Run
func (b *Backtest) Run() error
Run executes a comprehensive performance evaluation of the designated strategies, applied to a specified collection of assets. In the absence of explicitly defined assets, encompasses all assets within the repository. Likewise, in the absence of explicitly defined strategies, encompasses all the registered strategies.
type DataReport
DataReport is the bactest data report enablign programmatic access to the backtest results.
type DataReport struct {
// Results are the backtest results for the assets.
Results map[string][]*DataStrategyResult
}
func NewDataReport
func NewDataReport() *DataReport
NewDataReport initializes a new data report instance.
func (*DataReport) AssetBegin
func (d *DataReport) AssetBegin(name string, strategies []strategy.Strategy) error
AssetBegin is called when backtesting for the given asset begins.
func (*DataReport) AssetEnd
func (*DataReport) AssetEnd(_ string) error
AssetEnd is called when backtesting for the given asset ends.
func (*DataReport) Begin
func (*DataReport) Begin(_ []string, _ []strategy.Strategy) error
Begin is called when the backtest begins.
func (*DataReport) End
func (*DataReport) End() error
End is called when the backtest ends.
func (*DataReport) Write
func (d *DataReport) Write(assetName string, currentStrategy strategy.Strategy, snapshots <-chan *asset.Snapshot, actions <-chan strategy.Action, outcomes <-chan float64) error
Write writes the given strategy actions and outomes to the report.
type DataStrategyResult
DataStrategyResult is the strategy result.
type DataStrategyResult struct {
// Asset is the asset name.
Asset string
// Strategy is the strategy instnace.
Strategy strategy.Strategy
// Outcome is the strategy outcome.
Outcome float64
// Action is the final action recommended by the strategy.
Action strategy.Action
// Transactions are the action recommendations.
Transactions []strategy.Action
}
type HTMLReport
HTMLReport is the backtest HTML report.
type HTMLReport struct {
Report
// WriteStrategyReports indicates whether the individual strategy reports should be generated.
WriteStrategyReports bool
// DateFormat is the date format that is used in the reports.
DateFormat string
// Logger is the slog logger instance.
Logger *slog.Logger
// contains filtered or unexported fields
}
func NewHTMLReport
func NewHTMLReport(outputDir string) *HTMLReport
NewHTMLReport initializes a new HTML report instance.
func (*HTMLReport) AssetBegin
func (h *HTMLReport) AssetBegin(name string, strategies []strategy.Strategy) error
AssetBegin is called when backtesting for the given asset begins.
func (*HTMLReport) AssetEnd
func (h *HTMLReport) AssetEnd(name string) error
AssetEnd is called when backtesting for the given asset ends.
func (*HTMLReport) Begin
func (h *HTMLReport) Begin(assetNames []string, _ []strategy.Strategy) error
Begin is called when the backtest starts.
func (*HTMLReport) End
func (h *HTMLReport) End() error
End is called when the backtest ends.
func (*HTMLReport) Write
func (h *HTMLReport) Write(assetName string, currentStrategy strategy.Strategy, snapshots <-chan *asset.Snapshot, actions <-chan strategy.Action, outcomes <-chan float64) error
Write writes the given strategy actions and outomes to the report.
type Report
Report is the backtest report interface.
type Report interface {
// Begin is called when the backtest begins.
Begin(assetNames []string, strategies []strategy.Strategy) error
// AssetBegin is called when backtesting for the given asset begins.
AssetBegin(name string, strategies []strategy.Strategy) error
// Write writes the given strategy actions and outomes to the report.
Write(assetName string, currentStrategy strategy.Strategy, snapshots <-chan *asset.Snapshot, actions <-chan strategy.Action, outcomes <-chan float64) error
// AssetEnd is called when backtesting for the given asset ends.
AssetEnd(name string) error
// End is called when the backtest ends.
End() error
}
func NewReport
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.
type ReportBuilderFunc func(config string) (Report, error)
Generated by gomarkdoc