Documentation
¶
Index ¶
- Constants
- type Component
- type ComponentReturnsProvider
- type Policy
- type Specification
- func (pf *Specification) AssetReturns(ctx context.Context) (returns.Table, error)
- func (pf *Specification) Backtest(ctx context.Context, ...) (backtest.Result, error)
- func (pf *Specification) BacktestWithStartAndEndTime(ctx context.Context, start, end time.Time, ...) (backtest.Result, error)
- func (pf *Specification) ParseValues(q url.Values) error
- func (pf *Specification) RemoveAsset(index int) error
- func (pf *Specification) Validate() error
- func (pf *Specification) Values() url.Values
Examples ¶
Constants ¶
const ( // ServerURLEnvironmentVariableName is used in testing to override the host // and scheme for server calls. ServerURLEnvironmentVariableName = "PORTFOLIO_TREE_URL" // DefaultURL is the scheme and host for the API calls. DefaultURL = "https://portfoliotree.com" )
const ( PolicyAlgorithmEqualWeights = "EqualWeights" PolicyAlgorithmConstantWeights = "ConstantWeights" )
const (
ReturnsURLPath = "/api/returns"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ComponentReturnsProvider ¶
type ComponentReturnsProvider interface {
ComponentReturnsList(ctx context.Context, component Component) (returns.List, error)
ComponentReturnsTable(ctx context.Context, component ...Component) (returns.Table, error)
}
ComponentReturnsProvider is currently used for tests.
type Policy ¶
type Policy struct {
RebalancingInterval backtestconfig.Interval `yaml:"rebalancing_interval,omitempty"`
Weights []float64 `yaml:"weights,omitempty"`
WeightsAlgorithm string `yaml:"weights_algorithm,omitempty"`
WeightsAlgorithmLookBack backtestconfig.Window `yaml:"weights_algorithm_look_back_window,omitempty"`
WeightsUpdatingInterval backtestconfig.Interval `yaml:"weights_updating_interval,omitempty"`
}
type Specification ¶
type Specification struct {
Name string `yaml:"name"`
Benchmark Component `yaml:"benchmark"`
Assets []Component `yaml:"assets"`
Policy Policy `yaml:"policy"`
Filepath string `yaml:"-"`
FileIndex int `yaml:"-"`
}
Specification models a portfolio.
func ParseOneSpecification ¶
func ParseOneSpecification(in string) (Specification, error)
ParseOneSpecification decodes the contents of in to a Specification It supports a string containing YAML. The resulting Specification may have default values for unset fields.
func ParseSpecificationFile ¶
func ParseSpecificationFile(specificationFilePath string) ([]Specification, error)
ParseSpecificationFile opens a file and parses the contents into a Specification It supports YAML files at the moment but may support other encodings in the future.
func ParseSpecifications ¶
func ParseSpecifications(r io.Reader) ([]Specification, error)
ParseSpecifications decodes the contents of in to a list of Specifications The resulting Specification may have default values for unset fields.
func WalkDirectoryAndParseSpecificationFiles ¶
func WalkDirectoryAndParseSpecificationFiles(dir fs.FS) ([]Specification, error)
func (*Specification) AssetReturns ¶
func (*Specification) Backtest ¶
func (pf *Specification) Backtest(ctx context.Context, weightsAlgorithm backtestconfig.PolicyWeightCalculatorFunc) (backtest.Result, error)
Example ¶
package main
import (
"context"
"fmt"
"time"
"github.com/portfoliotree/portfolio"
)
func main() {
// language=yaml
const portfolioSpecYAML = `---
type: Portfolio
spec:
name: 60/40
benchmark: BIGPX
assets: [ACWI, AGG]
policy:
weights: [60, 40]
weights_algorithm: ConstantWeights
rebalancing_interval: Quarterly
`
pf, err := portfolio.ParseOneSpecification(portfolioSpecYAML)
if err != nil {
panic(err)
}
ctx := context.Background()
result, err := pf.Backtest(ctx, nil)
if err != nil {
panic(err)
}
portfolioReturns := result.Returns()
fmt.Printf("Annualized Risk: %.2f%%\n", portfolioReturns.AnnualizedRisk()*100)
fmt.Printf("Annualized Return: %.2f%%\n", portfolioReturns.AnnualizedArithmeticReturn()*100)
fmt.Printf("Backtest start date: %s\n", result.ReturnsTable.FirstTime().Format(time.DateOnly))
fmt.Printf("Backtest end date: %s\n", result.ReturnsTable.LastTime().Format(time.DateOnly))
}
Output: Annualized Risk: 11.46% Annualized Return: 5.10% Backtest start date: 2008-03-31 Backtest end date: 2023-06-14
func (*Specification) BacktestWithStartAndEndTime ¶
func (pf *Specification) BacktestWithStartAndEndTime(ctx context.Context, start, end time.Time, weightsFn backtestconfig.PolicyWeightCalculatorFunc) (backtest.Result, error)
func (*Specification) ParseValues ¶
func (pf *Specification) ParseValues(q url.Values) error
func (*Specification) RemoveAsset ¶
func (pf *Specification) RemoveAsset(index int) error
func (*Specification) Validate ¶
func (pf *Specification) Validate() error
Validate does some simple validations. Server you should do additional validations.
func (*Specification) Values ¶
func (pf *Specification) Values() url.Values
Directories
¶
| Path | Synopsis |
|---|---|
|
Package backtest calculates portfolio returns and asset weights from historic asset returns.
|
Package backtest calculates portfolio returns and asset weights from historic asset returns. |
|
Package calculations implements various finance equations used in the rest of the package.
|
Package calculations implements various finance equations used in the rest of the package. |
|
internal
|
|
|
Package returns implements data structures and convenience methods for interacting with a list of returns or sets of date-aligned returns.
|
Package returns implements data structures and convenience methods for interacting with a list of returns or sets of date-aligned returns. |