Version: v0.1.1 Latest Latest

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

Go to latest
Published: Jan 16, 2023 License: MIT Imports: 6 Imported by: 0



Package backtest calculates portfolio returns and asset weights from historic asset returns. It re-balances asset weights and updates policies based on provided functions. See Run.

DailyRebalancedWithStaticWeights is a simplified "back-tester" for calculating daily rebalanced returns of a portfolio given static policy asset weights.

Please remember, investing carries inherent risks including but not limited to the potential loss of principal. Past performance is no guarantee of future results. The data, equations, and calculations in these docs and code are for informational purposes only and should not be considered financial advice. It is important to carefully consider your own financial situation before making any investment decisions. You should seek the advice of a licensed financial professional before making any investment decisions. You should seek code review of an experienced software developer before consulting this library (or any library that imports it) to make investment decisions.



This section is empty.


This section is empty.


func DailyRebalancedWithStaticWeights

func DailyRebalancedWithStaticWeights(assets returns.Table, weights []float64) (returns.List, error)


type ErrorNotEnoughData

type ErrorNotEnoughData struct{}

func (ErrorNotEnoughData) Error

func (err ErrorNotEnoughData) Error() string

type PolicyWeightCalculator added in v0.1.1

type PolicyWeightCalculator interface {
	PolicyWeights(ctx context.Context, today time.Time, assets returns.Table, currentWeights []float64) ([]float64, error)

type Result

type Result struct {
	ReturnsTable       returns.Table `json:"returnsTable"       bson:"returnsTable"`
	Weights            [][]float64   `json:"weights"            bson:"weights"`
	FinalPolicyWeights []float64     `json:"policyWeights"      bson:"policyWeights"`
	RebalanceTimes     []time.Time   `json:"rebalanceDates"     bson:"rebalanceDates"`
	PolicyUpdateTimes  []time.Time   `json:"policyUpdatesDates" bson:"policyUpdatesDates"`

func Run

func Run(ctx context.Context, end, start time.Time, assetReturns returns.Table,
	alg PolicyWeightCalculator,
	lookBackWindow WindowFunc,
	shouldRebalanceAssetWeights TriggerFunc,
) (Result, error)

Run runs a portfolio back-test. It calls function parameters for policy updates and to check when a policy update or rebalancing is required. Generally you should call Run or RunWithFullData.

func (Result) DailyRebalancedReturns

func (result Result) DailyRebalancedReturns() returns.List

func (Result) Returns

func (result Result) Returns() returns.List

type TriggerFunc

type TriggerFunc func(t time.Time, currentWeights []float64) bool

type WeightSnapshot

type WeightSnapshot struct {
	Time              time.Time `json:"time"`
	Weights           []float64 `json:"weights"`
	IsRebalanceDay    bool      `json:"r" bson:"r,omitempty"`
	IsPolicyUpdateDay bool      `json:"p" bson:"p,omitempty"`

type WeightSnapshotList

type WeightSnapshotList []WeightSnapshot

WeightSnapshotList should be ordered where the WeightSnapshot at index 0 is the most recent and at len(list)-1 least recent.

func (WeightSnapshotList) AverageWeightForIndex

func (list WeightSnapshotList) AverageWeightForIndex(index int) (float64, error)

func (WeightSnapshotList) Times

func (list WeightSnapshotList) Times() []time.Time

type WindowFunc

type WindowFunc func(today time.Time, table returns.Table) returns.Table


Path Synopsis

Jump to

Keyboard shortcuts

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