optimizer

package
v1.51.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 2, 2023 License: AGPL-3.0 Imports: 27 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// HpOptimizerObjectiveEquity optimize the parameters to maximize equity gain
	HpOptimizerObjectiveEquity = "equity"
	// HpOptimizerObjectiveProfit optimize the parameters to maximize trading profit
	HpOptimizerObjectiveProfit = "profit"
	// HpOptimizerObjectiveVolume optimize the parameters to maximize trading volume
	HpOptimizerObjectiveVolume = "volume"
	// HpOptimizerObjectiveProfitFactor optimize the parameters to maximize profit factor
	HpOptimizerObjectiveProfitFactor = "profitfactor"
)

WARNING: the text here could only be lower cases

View Source
const (
	// HpOptimizerAlgorithmTPE is the implementation of Tree-structured Parzen Estimators
	HpOptimizerAlgorithmTPE = "tpe"
	// HpOptimizerAlgorithmCMAES is the implementation Covariance Matrix Adaptation Evolution Strategy
	HpOptimizerAlgorithmCMAES = "cmaes"
	// HpOptimizerAlgorithmSOBOL is the implementation Quasi-monte carlo sampling based on Sobol sequence
	HpOptimizerAlgorithmSOBOL = "sobol"
	// HpOptimizerAlgorithmRandom is the implementation random search
	HpOptimizerAlgorithmRandom = "random"
)

Variables

View Source
var ProfitFactorMetricValueFunc = func(summaryReport *backtest.SummaryReport) float64 {
	if len(summaryReport.SymbolReports) == 0 {
		return 0
	}
	if len(summaryReport.SymbolReports) > 1 {
		panic("multiple symbols' profitfactor optimization not supported")
	}
	report := summaryReport.SymbolReports[0]
	pf := report.ProfitFactor.Float64()
	win := report.WinningRatio.Float64()
	return pf*0.9 + win*0.1
}
View Source
var TotalEquityDiff = func(summaryReport *backtest.SummaryReport) float64 {
	if len(summaryReport.SymbolReports) == 0 {
		return 0
	}

	initEquity := summaryReport.InitialEquityValue.Float64()
	finalEquity := summaryReport.FinalEquityValue.Float64()
	return finalEquity - initEquity
}
View Source
var TotalProfitMetricValueFunc = func(summaryReport *backtest.SummaryReport) float64 {
	return summaryReport.TotalProfit.Float64()
}
View Source
var TotalVolume = func(summaryReport *backtest.SummaryReport) float64 {
	if len(summaryReport.SymbolReports) == 0 {
		return 0
	}

	buyVolume := summaryReport.SymbolReports[0].PnL.BuyVolume.Float64()
	sellVolume := summaryReport.SymbolReports[0].PnL.SellVolume.Float64()
	return buyVolume + sellVolume
}

Functions

func FormatMetricsTsv added in v1.39.0

func FormatMetricsTsv(writer io.WriteCloser, metrics map[string][]Metric) error

func FormatResultsTsv added in v1.39.0

func FormatResultsTsv(writer io.WriteCloser, labelPaths map[string]string, results []*HyperparameterOptimizeTrialResult) error

Types

type AsyncHandle added in v1.36.0

type AsyncHandle struct {
	Error  error
	Report *backtest.SummaryReport
	Done   chan struct{}
}

type BacktestTask added in v1.36.0

type BacktestTask struct {
	ConfigJson []byte
	Params     []interface{}
	Labels     []string
	Report     *backtest.SummaryReport
	Error      error
}

type Config

type Config struct {
	Executor      *ExecutorConfig  `json:"executor" yaml:"executor"`
	MaxThread     int              `yaml:"maxThread,omitempty"`
	Matrix        []SelectorConfig `yaml:"matrix"`
	Algorithm     string           `yaml:"algorithm,omitempty"`
	Objective     string           `yaml:"objectiveBy,omitempty"`
	MaxEvaluation int              `yaml:"maxEvaluation"`
}

func LoadConfig

func LoadConfig(yamlConfigFileName string) (*Config, error)

type Executor

type Executor interface {
	Execute(configJson []byte) (*backtest.SummaryReport, error)
	Run(ctx context.Context, taskC chan BacktestTask, bar *pb.ProgressBar) (chan BacktestTask, error)
}

type ExecutorConfig added in v1.36.0

type ExecutorConfig struct {
	Type                string               `json:"type" yaml:"type"`
	LocalExecutorConfig *LocalExecutorConfig `json:"local" yaml:"local"`
}

type GridOptimizer

type GridOptimizer struct {
	Config *Config

	ParamLabels   []string
	CurrentParams []interface{}
}

func (*GridOptimizer) Run

func (o *GridOptimizer) Run(executor Executor, configJson []byte) (map[string][]Metric, error)

type HyperparameterOptimizeReport added in v1.39.0

type HyperparameterOptimizeReport struct {
	Name       string                               `json:"studyName"`
	Objective  string                               `json:"objective"`
	Parameters map[string]string                    `json:"domains"`
	Best       *HyperparameterOptimizeTrialResult   `json:"best"`
	Trials     []*HyperparameterOptimizeTrialResult `json:"trials,omitempty"`
}

type HyperparameterOptimizeTrialResult added in v1.39.0

type HyperparameterOptimizeTrialResult struct {
	Value      fixedpoint.Value       `json:"value"`
	Parameters map[string]interface{} `json:"parameters"`
	ID         *int                   `json:"id,omitempty"`
	State      string                 `json:"state,omitempty"`
}

type HyperparameterOptimizer added in v1.39.0

type HyperparameterOptimizer struct {
	SessionName string
	Config      *Config
	// contains filtered or unexported fields
}

func (*HyperparameterOptimizer) Run added in v1.39.0

func (o *HyperparameterOptimizer) Run(ctx context.Context, executor Executor, configJson []byte) (*HyperparameterOptimizeReport, error)

type LocalExecutorConfig added in v1.36.0

type LocalExecutorConfig struct {
	MaxNumberOfProcesses int `json:"maxNumberOfProcesses" yaml:"maxNumberOfProcesses"`
}

type LocalProcessExecutor

type LocalProcessExecutor struct {
	Config    *LocalExecutorConfig
	Bin       string
	WorkDir   string
	ConfigDir string
	OutputDir string
}

func (*LocalProcessExecutor) Execute

func (e *LocalProcessExecutor) Execute(configJson []byte) (*backtest.SummaryReport, error)

Execute runs the config json and returns the summary report. This is a blocking operation.

func (*LocalProcessExecutor) ExecuteAsync added in v1.36.0

func (e *LocalProcessExecutor) ExecuteAsync(configJson []byte) *AsyncHandle

func (*LocalProcessExecutor) Prepare added in v1.37.0

func (e *LocalProcessExecutor) Prepare(configJson []byte) error

Prepare prepares the environment for the following back tests this is a blocking operation

func (*LocalProcessExecutor) Run added in v1.36.0

func (e *LocalProcessExecutor) Run(ctx context.Context, taskC chan BacktestTask, bar *pb.ProgressBar) (chan BacktestTask, error)

type Metric

type Metric struct {
	// Labels is the labels of the given parameters
	Labels []string `json:"labels,omitempty"`

	// Params is the parameters used to output the metrics result
	Params []interface{} `json:"params,omitempty"`

	// Key is the metric name
	Key string `json:"key"`

	// Value is the metric value of the metric
	Value float64 `json:"value,omitempty"`
}

type MetricValueFunc

type MetricValueFunc func(summaryReport *backtest.SummaryReport) float64

type OpFunc

type OpFunc func(configJson []byte, next func(configJson []byte) error) error

type SelectorConfig

type SelectorConfig struct {
	Type   string           `json:"type" yaml:"type"`
	Label  string           `json:"label,omitempty" yaml:"label,omitempty"`
	Path   string           `json:"path" yaml:"path"`
	Values []string         `json:"values,omitempty" yaml:"values,omitempty"`
	Min    fixedpoint.Value `json:"min,omitempty" yaml:"min,omitempty"`
	Max    fixedpoint.Value `json:"max,omitempty" yaml:"max,omitempty"`
	Step   fixedpoint.Value `json:"step,omitempty" yaml:"step,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL