perf

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Jun 19, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	// DailyToAnnualFactor is the factor to scale daily observations to annual.
	// Commonly defined as the number of public market trading days in a year.
	DailyToAnnualFactor = 252

	// SharpeDefaultAnnualRiskFreeRate is the default risk free rate for Sharpe Ratio.
	SharpeDefaultAnnualRiskFreeRate = 0.0

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

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 HistVolAnn added in v0.0.26

func HistVolAnn(daily []float64) float64

HistVolAnn is the annualized historic volatility of daily returns.

func KellyCriterion

func KellyCriterion(profitFactor, winP float64) float64

KellyCriterion is the famous method for trade sizing.

func OptimalF

func OptimalF(roundturns []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 roundturns 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 roundturns.

func PlotHist added in v0.0.26

func PlotHist(series []float64, writer io.Writer, nBins int, title string, format string) (int64, error)

PlotHist renders a histogram plot to a writer. Param format can be anything supported by gonum.org/v1/plot/vg

func PlotHistStdNormDist added in v0.0.26

func PlotHistStdNormDist(series []float64, writer io.Writer, nBins int, title string, format string) (int64, error)

PlotHistStdNormDist renders a histogram plot to a writer. Normalizes the area under the series to 1 and adds a std normal distribution plot. Param format can be anything supported by gonum.org/v1/plot/vg

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-roundturns-required-for-backtesting-results-to-be-trusted.356588/page-2

func WriteEquitySeriesToCSV added in v0.0.20

func WriteEquitySeriesToCSV(filename string, series broker.EquitySeries) error

WriteEquitySeriesToCSV writes an equity curve to a CSV file.

func WritePerformanceReportToCSV added in v0.0.20

func WritePerformanceReportToCSV(filename string, report *PerformanceReport) error

WritePerformanceReportToCSV writes a performance report to a CSV file.

func WriteRoundTurnsToCSV added in v0.0.25

func WriteRoundTurnsToCSV(filename string, roundturns []broker.RoundTurn) error

WriteRoundTurnsToCSV writes a slice of roundturns to a CSV file.

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"`
	Asset           market.Asset     `csv:"asset_,inline"`
	TradeReport     *TradeReport     `csv:",inline"`
	PortfolioReport *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 roundturns (aka roundrtrip / roundturn).

- PorfolioReport reports metrics related to the portfolio equity curve.

func NewPerformanceReport

func NewPerformanceReport(roundturns []broker.RoundTurn, 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

	// HistVolAnn is the historic volatility of the equity curve as annualized std dev.
	HistVolAnn float64

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

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

	// EquityCurve is the source from which the report fields are generated.
	EquityCurve broker.EquitySeries `csv:"-"`

	// DailyReturns is the series of pct diff returns extracted from the EquityCurve.
	// This is the series used to calculate Sharpe and HistVolAnn.
	// Intended for use plotting returns distribution
	DailyReturns []float64 `csv:"-"`
	// 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
	RoundTurnCount 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

	RoundTurns []broker.RoundTurn `csv:"-"`
	// contains filtered or unexported fields
}

TradeReport is a report on the trade metrics. It is generated by the NewTradeReport function using the roundturns and roundturn data generated by an algo.

func NewTradeReport

func NewTradeReport(roundturns []broker.RoundTurn) *TradeReport

NewTradeReport creates a new TradeReport from a sequence of roundturns.

Jump to

Keyboard shortcuts

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