perf

package
v0.0.17 Latest Latest
Warning

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

Go to latest
Published: May 29, 2022 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package perf provides performance metrics for a trading algo, using a record of trades and the equity curve from a Dealer in package broker.

Index

Constants

View Source
const (
	// SharpeDefaultAnnualRiskFreeRate is the default risk free rate for Sharpe Ratio.
	SharpeDefaultAnnualRiskFreeRate = 0.0

	// SharpeDailyToAnnualFactor is the factor to convert daily Sharpe to annual Sharpe.
	SharpeDailyToAnnualFactor = 252

	// SharpeDefaultDailyRiskFreeRate is the daily rate based on the default annual rate
	SharpeDefaultDailyRiskFreeRate = SharpeDefaultAnnualRiskFreeRate / SharpeDailyToAnnualFactor
)

Variables

This section is empty.

Functions

func CAGR

func CAGR(initial, final float64, days int) float64

CAGR Compound Annual Growth Rate

func CalmarRatio

func CalmarRatio(cagr, mdd float64) float64

CalmarRatio relates the capaital growth rate to the maximum drawdown.

func DiffPctReturns

func DiffPctReturns(curve broker.EquitySeries) []float64

DiffPctReturns converts an equity curve of absolute amounts into a series of percentage differences.

func KellyCriterion

func KellyCriterion(profitFactor, winP float64) float64

KellyCriterion is the famous method for trade sizing.

func OptimalF

func OptimalF(trades []float64) float64

OptimalF is a function that returns the 'OptimalF' for a series of trade returns as defined by Ralph Vince. It is a method for sizing positions to maximize geometric return whilst accounting for biggest trading loss. See: https://www.investopedia.com/terms/o/optimalf.asp Param trades is the series of profits (-ve amount for losses) for each trade

func PRR

func PRR(profit, loss, winningN, losingN float64) float64

PRR (Pessimistic Return Ratio) is the profit factor with a penalty for a lower number of trades.

func PrintSummary

func PrintSummary(r PerformanceReport)

PrintSummary prints a summary of the performance report to stdout.

func ReduceEOD

func ReduceEOD(curve broker.EquitySeries) broker.EquitySeries

ReduceEOD filters the equity curve to the end of day values. End of day is defined as equity point with hour and minute 0.

func SE

func SE(xs []float64) float64

SE (Standard Error)

func SharpeRatio

func SharpeRatio(daily []float64, riskFreeRate float64) float64

SharpeRatio is the annualised value using a daily risk free rate and daily returns. Param daily is the percentage daily returns from the portfolio.

func StatN

func StatN(xs []float64) float64

StatN returns the statistically significant number of samples required based on the distribution of a series. From: https://www.elitetrader.com/et/threads/minimum-number-of-trades-required-for-backtesting-results-to-be-trusted.356588/page-2

Types

type Drawdown

type Drawdown struct {
	HighAt  time.Time
	LowAt   time.Time
	StartAt time.Time
	EndAt   time.Time

	High decimal.Decimal
	Low  decimal.Decimal

	Recovery time.Duration

	Amount decimal.Decimal
	Pct    float64

	IsOpen bool
}

Drawdown is a feature of the equity curve representing a peak to valley bottom and recovery to prior peak.

func Drawdowns

func Drawdowns(curve broker.EquitySeries) []Drawdown

Drawdowns extracts all the drawdowns from the equity curve.

func MaxDrawdown

func MaxDrawdown(dds []Drawdown) (max Drawdown)

MaxDrawdown finds the largest drawdown based on the percentage amount.

type PerformanceReport

type PerformanceReport struct {
	ID         string           `csv:"id"`
	Trade      *TradeReport     `csv:",inline"`
	Portfolio  *PortfolioReport `csv:",inline"`
	Properties map[string]any   `csv:"properties"`
}

PerformanceReport is a report on the performance of a trading algo. It contains a TradeReport and a PortfolioReport.

- TradeReport reports metrics related to the discrete trades (aka roundrtrip / roundturn).

- PorfolioReport reports metrics related to the portfolio equity curve.

func NewPerformanceReport

func NewPerformanceReport(trades []broker.Trade, equity broker.EquitySeries) PerformanceReport

NewPerformanceReport creates a new PerformanceReport.

type PortfolioReport

type PortfolioReport struct {

	// PeriodStart is the start time of the equity curve.
	PeriodStart time.Time

	// PeriodEnd is the end time of the equity curve.
	PeriodEnd time.Time

	// Period is the duration of the equity curve.
	Period time.Duration

	// StartEquity is the starting equity (amount) of the equity curve.
	StartEquity float64

	// EndEquity is the ending equity (amount) of the equity curve.
	EndEquity float64

	// EquityReturn is the percentage return of the equity curve.
	EquityReturn float64

	// CAGR is the Compound Annual Growth Rate of the equity curve.
	CAGR float64

	// MaxDrawdown is the maximum percentage drawdown of the equity curve
	MaxDrawdown float64

	// MDDRecovery is the recovery time of the maximum drawdown of the equity curve.
	MDDRecovery time.Duration

	// Sharpe is the Sharpe ratio of the equity curve.
	Sharpe float64

	// Calmar is the Calmar ratio of the equity curve.
	Calmar float64
	// contains filtered or unexported fields
}

PortfolioReport is report on the portfolio metrics. It is generated by the NewPortfolioReport function using an equity curve, typically the output of algo execution using a Dealer in package broker.

func NewPortfolioReport

func NewPortfolioReport(curve broker.EquitySeries) *PortfolioReport

NewPortfolioReport creates a new PortfolioReport from a given equity curve.

type TradeReport

type TradeReport struct {
	TradeCount     float64
	TotalNetProfit float64
	AvgNetProfit   float64
	GrossProfit    float64
	GrossLoss      float64
	ProfitFactor   float64
	PRR            float64

	PercentProfitable  float64
	MaxProfit, MaxLoss float64

	AvgProfit float64
	AvgLoss   float64

	MaxLossStreak int

	Kelly    float64
	OptimalF float64
	StatN    float64

	TotalTimeInMarketSec float64
	AvgHoldSec           float64
	// contains filtered or unexported fields
}

TradeReport is a report on the trade metrics. It is generated by the NewTradeReport function using the trades generated by an algo. Trade is a roundtrip / roundturn i.e. the outcome of opening and closing a position.

func NewTradeReport

func NewTradeReport(trades []broker.Trade) *TradeReport

NewTradeReport creates a new TradeReport from a sequence of trades.

Jump to

Keyboard shortcuts

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