mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
backtester: shared exchange level funding, decimal implementation (#783)
* Better designed backtester funding concept * Fleshes out funding concepts further to allow two funding types * Adds types, finishes adding to portfolio and adds to exchange * Fixes a bug to reveal another * Fixes issues with purchasing * A partial conversion to using decimal.decimal for the backtester * Further decimal rollout. Can compile and output report * More cleanup * Fix rendering and initial funds issue. * Adds new concept for trading using the exchange level funding to see what happens * Fixes a bug in funding not being found * New strat config to test RSI and discover issues * Can run with pairs that contain 0 funding * Finally fixes the arrangement to share funds * Adds testing and funding transfer * end of day * More comments, more tests! * Improves item comparisons and completes testing * Initial attempt at new strategy which utilisies shared funding and transfers * end of day broken * Chronological output. Fixes output bug where multi currency. * End of day commit * Fixes bug where events were being overwritten in a simultaneous context * Begins transitioning from portfolio holdings to funding holdings. Am I doing the right thing * End of day run around * Likely fix for holding calculations * Improvement to template. Improvement to holdings * DARK MODE. Report upgrades. Even handling with funds. Fix output * Output funding to cmd * Add new trasnferred funds "side" * Fixing test run 1 * Test updates * Test updating * More test fixing * Fixes portfolio tests * More test fixes * Fixes remaining tests and lints * Fixes currencystatistics tests. Adds decimal math implementations * Fixes hilarious bug where there could only be on holding * Adds funding support for config. Minor fixes * Adds documentation * Finishes config builder support for funding * Logs inexact conversions, updates tests. adds config validation * The quest to understand a new funding bug begins. New strategy * Fixes bug where wrong funding was retrieved. Expands t2b2 strat * End of the day commit. Gotta revert the nulldecimal stuff * Fixes tests, adds extra funding transfer feature * Fixes initial total values, tries to add a grand total value * Rebase fixes, documentation updates, tests for strategy * Swaps the err statement for tests. Regenerates tests. Math warnings * Attempts to solve Live data problems. Fixes volume * Fixes live data missing * can trade at any interval. skip volume sizing. volume colours. * config regen. display fixes * test fixes, lint fixes * Anti-funky errors * docs * Rmbad * docs * docs update * Simplifies err handling. Updates readmes. Data type checks * docs. new field initial-base-funds. comment errs. config test coverage * minMaxing * testfix * Fixes fee calculation, re-bans minMax being equal * Crazy concepts to attempt to solve totals. Addresses nits * Adds in totals calculation for exchange level funding.Uses external API In future, this will be replaced by proper pricing supplied by the same exchange that is requested. This is an unknown price * rm dollar signs in cmd and report. rm bad error. fix chart decimal. padding * re-run docs post merge * Fixes oopsie for fee parsing Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io> Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
@@ -2,10 +2,17 @@ package math
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrNoNegativeResults is returned when no negative results are allowed
|
||||
ErrNoNegativeResults = errors.New("cannot calculate with no negative values")
|
||||
// ErrInexactConversion is returned when a decimal does not convert to float exactly
|
||||
ErrInexactConversion = errors.New("inexact conversion from decimal to float detected")
|
||||
errZeroValue = errors.New("cannot calculate average of no values")
|
||||
errNegativeValueOutOfRange = errors.New("received negative number less than -1")
|
||||
errGeometricNegative = errors.New("cannot calculate a geometric mean with negative values")
|
||||
@@ -180,7 +187,8 @@ func FinancialGeometricMean(values []float64) (float64, error) {
|
||||
modVal := values[i] + 1
|
||||
product *= modVal
|
||||
}
|
||||
geometricPower := math.Pow(product, 1/float64(len(values)))
|
||||
prod := 1 / float64(len(values))
|
||||
geometricPower := math.Pow(product, prod)
|
||||
if geometricPower > 0 {
|
||||
// we minus 1 because we manipulated the values to be non-zero/negative
|
||||
geometricPower--
|
||||
@@ -238,3 +246,232 @@ func SharpeRatio(movementPerCandle []float64, riskFreeRatePerInterval, average f
|
||||
|
||||
return (average - riskFreeRatePerInterval) / standardDeviation, nil
|
||||
}
|
||||
|
||||
// DecimalCompoundAnnualGrowthRate Calculates CAGR.
|
||||
// Using years, intervals per year would be 1 and number of intervals would be the number of years
|
||||
// Using days, intervals per year would be 365 and number of intervals would be the number of days
|
||||
func DecimalCompoundAnnualGrowthRate(openValue, closeValue, intervalsPerYear, numberOfIntervals decimal.Decimal) (decimal.Decimal, error) {
|
||||
if numberOfIntervals.IsZero() {
|
||||
return decimal.Zero, errCAGRNoIntervals
|
||||
}
|
||||
if openValue.IsZero() {
|
||||
return decimal.Zero, errCAGRZeroOpenValue
|
||||
}
|
||||
closeOverOpen := closeValue.Div(openValue)
|
||||
exp := intervalsPerYear.Div(numberOfIntervals)
|
||||
pow := DecimalPow(closeOverOpen, exp)
|
||||
k := pow.Sub(decimal.NewFromInt(1)).Mul(decimal.NewFromInt(100))
|
||||
return k, nil
|
||||
}
|
||||
|
||||
// DecimalCalmarRatio is a function of the average compounded annual rate of return versus its maximum drawdown.
|
||||
// The higher the Calmar ratio, the better it performed on a risk-adjusted basis during the given time frame, which is mostly commonly set at 36 months
|
||||
func DecimalCalmarRatio(highestPrice, lowestPrice, average, riskFreeRateForPeriod decimal.Decimal) (decimal.Decimal, error) {
|
||||
if highestPrice.IsZero() {
|
||||
return decimal.Zero, errCalmarHighest
|
||||
}
|
||||
drawdownDiff := highestPrice.Sub(lowestPrice).Div(highestPrice)
|
||||
if drawdownDiff.IsZero() {
|
||||
return decimal.Zero, nil
|
||||
}
|
||||
return average.Sub(riskFreeRateForPeriod).Div(drawdownDiff), nil
|
||||
}
|
||||
|
||||
// DecimalInformationRatio The information ratio (IR) is a measurement of portfolio returns beyond the returns of a benchmark,
|
||||
// usually an index, compared to the volatility of those returns.
|
||||
// The benchmark used is typically an index that represents the market or a particular sector or industry.
|
||||
func DecimalInformationRatio(returnsRates, benchmarkRates []decimal.Decimal, averageValues, averageComparison decimal.Decimal) (decimal.Decimal, error) {
|
||||
if len(benchmarkRates) != len(returnsRates) {
|
||||
return decimal.Zero, errInformationBadLength
|
||||
}
|
||||
var diffs []decimal.Decimal
|
||||
for i := range returnsRates {
|
||||
diffs = append(diffs, returnsRates[i].Sub(benchmarkRates[i]))
|
||||
}
|
||||
stdDev, err := DecimalPopulationStandardDeviation(diffs)
|
||||
if err != nil && !errors.Is(err, ErrInexactConversion) {
|
||||
return decimal.Zero, err
|
||||
}
|
||||
if stdDev.IsZero() {
|
||||
return decimal.Zero, nil
|
||||
}
|
||||
return averageValues.Sub(averageComparison).Div(stdDev), nil
|
||||
}
|
||||
|
||||
// DecimalPopulationStandardDeviation calculates standard deviation using population based calculation
|
||||
func DecimalPopulationStandardDeviation(values []decimal.Decimal) (decimal.Decimal, error) {
|
||||
if len(values) < 2 {
|
||||
return decimal.Zero, nil
|
||||
}
|
||||
valAvg, err := DecimalArithmeticMean(values)
|
||||
if err != nil {
|
||||
return decimal.Zero, err
|
||||
}
|
||||
diffs := make([]decimal.Decimal, len(values))
|
||||
for x := range values {
|
||||
val := values[x].Sub(valAvg)
|
||||
exp := decimal.NewFromInt(2)
|
||||
pow := DecimalPow(val, exp)
|
||||
diffs[x] = pow
|
||||
}
|
||||
var diffAvg decimal.Decimal
|
||||
diffAvg, err = DecimalArithmeticMean(diffs)
|
||||
if err != nil {
|
||||
return decimal.Zero, err
|
||||
}
|
||||
f, exact := diffAvg.Float64()
|
||||
err = nil
|
||||
if !exact {
|
||||
err = fmt.Errorf("%w from %v to %v", ErrInexactConversion, diffAvg, f)
|
||||
}
|
||||
resp := decimal.NewFromFloat(math.Sqrt(f))
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// DecimalSampleStandardDeviation standard deviation is a statistic that
|
||||
// measures the dispersion of a dataset relative to its mean and
|
||||
// is calculated as the square root of the variance
|
||||
func DecimalSampleStandardDeviation(values []decimal.Decimal) (decimal.Decimal, error) {
|
||||
if len(values) < 2 {
|
||||
return decimal.Zero, nil
|
||||
}
|
||||
mean, err := DecimalArithmeticMean(values)
|
||||
if err != nil {
|
||||
return decimal.Zero, err
|
||||
}
|
||||
var superMean []decimal.Decimal
|
||||
var combined decimal.Decimal
|
||||
for i := range values {
|
||||
pow := values[i].Sub(mean).Pow(decimal.NewFromInt(2))
|
||||
superMean = append(superMean, pow)
|
||||
combined.Add(pow)
|
||||
}
|
||||
avg := combined.Div(decimal.NewFromInt(int64(len(superMean))).Sub(decimal.NewFromInt(1)))
|
||||
f, exact := avg.Float64()
|
||||
err = nil
|
||||
if !exact {
|
||||
err = fmt.Errorf("%w from %v to %v", ErrInexactConversion, avg, f)
|
||||
}
|
||||
sqrt := math.Sqrt(f)
|
||||
return decimal.NewFromFloat(sqrt), err
|
||||
}
|
||||
|
||||
// DecimalGeometricMean is an average which indicates the central tendency or
|
||||
// typical value of a set of numbers by using the product of their values
|
||||
// The geometric average can only process positive numbers
|
||||
func DecimalGeometricMean(values []decimal.Decimal) (decimal.Decimal, error) {
|
||||
if len(values) == 0 {
|
||||
return decimal.Zero, errZeroValue
|
||||
}
|
||||
product := decimal.NewFromInt(1)
|
||||
for i := range values {
|
||||
if values[i].LessThanOrEqual(decimal.Zero) {
|
||||
// cannot use negative or zero values in geometric calculation
|
||||
return decimal.Zero, errGeometricNegative
|
||||
}
|
||||
product = product.Mul(values[i])
|
||||
}
|
||||
exp := decimal.NewFromInt(1).Div(decimal.NewFromInt(int64(len(values))))
|
||||
pow := DecimalPow(product, exp)
|
||||
geometricPower := pow
|
||||
return geometricPower, nil
|
||||
}
|
||||
|
||||
// DecimalPow is lovely because shopspring decimal cannot
|
||||
// handle ^0.x and instead returns 1
|
||||
func DecimalPow(x, y decimal.Decimal) decimal.Decimal {
|
||||
fX, _ := x.Float64()
|
||||
fY, _ := y.Float64()
|
||||
pow := math.Pow(fX, fY)
|
||||
return decimal.NewFromFloat(pow)
|
||||
}
|
||||
|
||||
// DecimalFinancialGeometricMean is a modified geometric average to assess
|
||||
// the negative returns of investments. It accepts It adds +1 to each
|
||||
// This does impact the final figures as it is modifying values
|
||||
// It is still ultimately calculating a geometric average
|
||||
// which should only be compared to other financial geometric averages
|
||||
func DecimalFinancialGeometricMean(values []decimal.Decimal) (decimal.Decimal, error) {
|
||||
if len(values) == 0 {
|
||||
return decimal.Zero, errZeroValue
|
||||
}
|
||||
product := 1.0
|
||||
for i := range values {
|
||||
if values[i].LessThan(decimal.NewFromInt(-1)) {
|
||||
// cannot lose more than 100%, figures are incorrect
|
||||
// losing exactly 100% will return a 0 value, but is not an error
|
||||
return decimal.Zero, errNegativeValueOutOfRange
|
||||
}
|
||||
// as we cannot have negative or zero value geometric numbers
|
||||
// adding a 1 to the percentage movements allows for differentiation between
|
||||
// negative numbers (eg -0.1 translates to 0.9) and positive numbers (eg 0.1 becomes 1.1)
|
||||
modVal, _ := values[i].Add(decimal.NewFromInt(1)).Float64()
|
||||
product *= modVal
|
||||
}
|
||||
prod := 1 / float64(len(values))
|
||||
geometricPower := math.Pow(product, prod)
|
||||
if geometricPower > 0 {
|
||||
// we minus 1 because we manipulated the values to be non-zero/negative
|
||||
geometricPower--
|
||||
}
|
||||
return decimal.NewFromFloat(geometricPower), nil
|
||||
}
|
||||
|
||||
// DecimalArithmeticMean is the basic form of calculating an average.
|
||||
// Divide the sum of all values by the length of values
|
||||
func DecimalArithmeticMean(values []decimal.Decimal) (decimal.Decimal, error) {
|
||||
if len(values) == 0 {
|
||||
return decimal.Zero, errZeroValue
|
||||
}
|
||||
var sumOfValues decimal.Decimal
|
||||
for x := range values {
|
||||
sumOfValues = sumOfValues.Add(values[x])
|
||||
}
|
||||
return sumOfValues.Div(decimal.NewFromInt(int64(len(values)))), nil
|
||||
}
|
||||
|
||||
// DecimalSortinoRatio returns sortino ratio of backtest compared to risk-free
|
||||
func DecimalSortinoRatio(movementPerCandle []decimal.Decimal, riskFreeRatePerInterval, average decimal.Decimal) (decimal.Decimal, error) {
|
||||
if len(movementPerCandle) == 0 {
|
||||
return decimal.Zero, errZeroValue
|
||||
}
|
||||
totalNegativeResultsSquared := decimal.Zero
|
||||
for x := range movementPerCandle {
|
||||
if movementPerCandle[x].Sub(riskFreeRatePerInterval).LessThan(decimal.Zero) {
|
||||
totalNegativeResultsSquared = totalNegativeResultsSquared.Add(movementPerCandle[x].Sub(riskFreeRatePerInterval).Pow(decimal.NewFromInt(2)))
|
||||
}
|
||||
}
|
||||
if totalNegativeResultsSquared.IsZero() {
|
||||
return decimal.Zero, ErrNoNegativeResults
|
||||
}
|
||||
f, exact := totalNegativeResultsSquared.Float64()
|
||||
var err error
|
||||
if !exact {
|
||||
err = fmt.Errorf("%w from %v to %v", ErrInexactConversion, totalNegativeResultsSquared, f)
|
||||
}
|
||||
fAverageDownsideDeviation := math.Sqrt(f / float64(len(movementPerCandle)))
|
||||
averageDownsideDeviation := decimal.NewFromFloat(fAverageDownsideDeviation)
|
||||
|
||||
return average.Sub(riskFreeRatePerInterval).Div(averageDownsideDeviation), err
|
||||
}
|
||||
|
||||
// DecimalSharpeRatio returns sharpe ratio of backtest compared to risk-free
|
||||
func DecimalSharpeRatio(movementPerCandle []decimal.Decimal, riskFreeRatePerInterval, average decimal.Decimal) (decimal.Decimal, error) {
|
||||
totalIntervals := decimal.NewFromInt(int64(len(movementPerCandle)))
|
||||
if totalIntervals.IsZero() {
|
||||
return decimal.Zero, errZeroValue
|
||||
}
|
||||
var excessReturns []decimal.Decimal
|
||||
for i := range movementPerCandle {
|
||||
excessReturns = append(excessReturns, movementPerCandle[i].Sub(riskFreeRatePerInterval))
|
||||
}
|
||||
standardDeviation, err := DecimalPopulationStandardDeviation(excessReturns)
|
||||
if err != nil && !errors.Is(err, ErrInexactConversion) {
|
||||
return decimal.Zero, err
|
||||
}
|
||||
if standardDeviation.IsZero() {
|
||||
return decimal.Zero, nil
|
||||
}
|
||||
|
||||
return average.Sub(riskFreeRatePerInterval).Div(standardDeviation), nil
|
||||
}
|
||||
|
||||
@@ -4,13 +4,15 @@ import (
|
||||
"errors"
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
func TestCalculateFee(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInput := float64(1)
|
||||
fee := float64(1)
|
||||
expectedOutput := float64(0.01)
|
||||
expectedOutput := 0.01
|
||||
actualResult := CalculateFee(originalInput, fee)
|
||||
if expectedOutput != actualResult {
|
||||
t.Errorf(
|
||||
@@ -22,7 +24,7 @@ func TestCalculateAmountWithFee(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalInput := float64(1)
|
||||
fee := float64(1)
|
||||
expectedOutput := float64(1.01)
|
||||
expectedOutput := 1.01
|
||||
actualResult := CalculateAmountWithFee(originalInput, fee)
|
||||
if expectedOutput != actualResult {
|
||||
t.Errorf(
|
||||
@@ -462,3 +464,416 @@ func TestArithmeticAverage(t *testing.T) {
|
||||
t.Error("expected 4.5")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalSortinoRatio(t *testing.T) {
|
||||
t.Parallel()
|
||||
rfr := decimal.NewFromFloat(0.001)
|
||||
figures := []decimal.Decimal{
|
||||
decimal.NewFromFloat(0.10),
|
||||
decimal.NewFromFloat(0.04),
|
||||
decimal.NewFromFloat(0.15),
|
||||
decimal.NewFromFloat(-0.05),
|
||||
decimal.NewFromFloat(0.20),
|
||||
decimal.NewFromFloat(-0.02),
|
||||
decimal.NewFromFloat(0.08),
|
||||
decimal.NewFromFloat(-0.06),
|
||||
decimal.NewFromFloat(0.13),
|
||||
decimal.NewFromFloat(0.23),
|
||||
}
|
||||
avg, err := DecimalArithmeticMean(figures)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
_, err = DecimalSortinoRatio(nil, rfr, avg)
|
||||
if !errors.Is(err, errZeroValue) {
|
||||
t.Errorf("expected: %v, received %v", errZeroValue, err)
|
||||
}
|
||||
|
||||
var r decimal.Decimal
|
||||
r, err = DecimalSortinoRatio(figures, rfr, avg)
|
||||
if err != nil && !errors.Is(err, ErrInexactConversion) {
|
||||
t.Error(err)
|
||||
}
|
||||
rf, exact := r.Float64()
|
||||
if !exact && rf != 3.0377875479459906 {
|
||||
t.Errorf("expected 3.0377875479459906, received %v", r)
|
||||
} else if rf != 3.0377875479459907 {
|
||||
t.Errorf("expected 3.0377875479459907, received %v", r)
|
||||
}
|
||||
|
||||
avg, err = DecimalFinancialGeometricMean(figures)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
r, err = DecimalSortinoRatio(figures, rfr, avg)
|
||||
if err != nil && !errors.Is(err, ErrInexactConversion) {
|
||||
t.Error(err)
|
||||
}
|
||||
if !r.Equal(decimal.NewFromFloat(2.8712802265603243)) {
|
||||
t.Errorf("expected 2.525203164136098, received %v", r)
|
||||
}
|
||||
|
||||
// this follows and matches the example calculation from
|
||||
// https://www.wallstreetmojo.com/sortino-ratio/
|
||||
example := []decimal.Decimal{
|
||||
decimal.NewFromFloat(0.1),
|
||||
decimal.NewFromFloat(0.12),
|
||||
decimal.NewFromFloat(0.07),
|
||||
decimal.NewFromFloat(-0.03),
|
||||
decimal.NewFromFloat(0.08),
|
||||
decimal.NewFromFloat(-0.04),
|
||||
decimal.NewFromFloat(0.15),
|
||||
decimal.NewFromFloat(0.2),
|
||||
decimal.NewFromFloat(0.12),
|
||||
decimal.NewFromFloat(0.06),
|
||||
decimal.NewFromFloat(-0.03),
|
||||
decimal.NewFromFloat(0.02),
|
||||
}
|
||||
avg, err = DecimalArithmeticMean(example)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
r, err = DecimalSortinoRatio(example, decimal.NewFromFloat(0.06), avg)
|
||||
if err != nil && !errors.Is(err, ErrInexactConversion) {
|
||||
t.Error(err)
|
||||
}
|
||||
rr := r.Round(1)
|
||||
if !rr.Equal(decimal.NewFromFloat(0.2)) {
|
||||
t.Errorf("expected 0.2, received %v", rr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalInformationRatio(t *testing.T) {
|
||||
t.Parallel()
|
||||
figures := []decimal.Decimal{
|
||||
decimal.NewFromFloat(0.0665),
|
||||
decimal.NewFromFloat(0.0283),
|
||||
decimal.NewFromFloat(0.0911),
|
||||
decimal.NewFromFloat(0.0008),
|
||||
decimal.NewFromFloat(-0.0203),
|
||||
decimal.NewFromFloat(-0.0978),
|
||||
decimal.NewFromFloat(0.0164),
|
||||
decimal.NewFromFloat(-0.0537),
|
||||
decimal.NewFromFloat(0.078),
|
||||
decimal.NewFromFloat(0.0032),
|
||||
decimal.NewFromFloat(0.0249),
|
||||
decimal.Zero,
|
||||
}
|
||||
comparisonFigures := []decimal.Decimal{
|
||||
decimal.NewFromFloat(0.0216),
|
||||
decimal.NewFromFloat(0.0048),
|
||||
decimal.NewFromFloat(0.036),
|
||||
decimal.NewFromFloat(0.0303),
|
||||
decimal.NewFromFloat(0.0043),
|
||||
decimal.NewFromFloat(-0.0694),
|
||||
decimal.NewFromFloat(0.0179),
|
||||
decimal.NewFromFloat(-0.0918),
|
||||
decimal.NewFromFloat(0.0787),
|
||||
decimal.NewFromFloat(0.0297),
|
||||
decimal.NewFromFloat(0.003),
|
||||
decimal.Zero,
|
||||
}
|
||||
avg, err := DecimalArithmeticMean(figures)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !avg.Equal(decimal.NewFromFloat(0.01145)) {
|
||||
t.Error(avg)
|
||||
}
|
||||
var avgComparison decimal.Decimal
|
||||
avgComparison, err = DecimalArithmeticMean(comparisonFigures)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !avgComparison.Equal(decimal.NewFromFloat(0.005425)) {
|
||||
t.Error(avgComparison)
|
||||
}
|
||||
|
||||
var eachDiff []decimal.Decimal
|
||||
for i := range figures {
|
||||
eachDiff = append(eachDiff, figures[i].Sub(comparisonFigures[i]))
|
||||
}
|
||||
stdDev, err := DecimalPopulationStandardDeviation(eachDiff)
|
||||
if err != nil && !errors.Is(err, ErrInexactConversion) {
|
||||
t.Error(err)
|
||||
}
|
||||
if !stdDev.Equal(decimal.NewFromFloat(0.028992588851865227)) {
|
||||
t.Error(stdDev)
|
||||
}
|
||||
information := avg.Sub(avgComparison).Div(stdDev)
|
||||
if !information.Equal(decimal.NewFromFloat(0.2078117283966652)) {
|
||||
t.Errorf("expected %v received %v", 0.2078117283966652, information)
|
||||
}
|
||||
var information2 decimal.Decimal
|
||||
information2, err = DecimalInformationRatio(figures, comparisonFigures, avg, avgComparison)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !information.Equal(information2) {
|
||||
t.Error(information2)
|
||||
}
|
||||
|
||||
_, err = DecimalInformationRatio(figures, []decimal.Decimal{decimal.NewFromInt(1)}, avg, avgComparison)
|
||||
if !errors.Is(err, errInformationBadLength) {
|
||||
t.Errorf("expected: %v, received %v", errInformationBadLength, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalCalmarRatio(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := DecimalCalmarRatio(decimal.Zero, decimal.Zero, decimal.Zero, decimal.Zero)
|
||||
if !errors.Is(err, errCalmarHighest) {
|
||||
t.Errorf("expected: %v, received %v", errCalmarHighest, err)
|
||||
}
|
||||
var ratio decimal.Decimal
|
||||
ratio, err = DecimalCalmarRatio(
|
||||
decimal.NewFromInt(50000),
|
||||
decimal.NewFromInt(15000),
|
||||
decimal.NewFromFloat(0.2),
|
||||
decimal.NewFromFloat(0.1))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !ratio.Equal(decimal.NewFromFloat(0.1428571428571429)) {
|
||||
t.Error(ratio)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalCalculateSharpeRatio(t *testing.T) {
|
||||
t.Parallel()
|
||||
result, err := DecimalSharpeRatio(nil, decimal.Zero, decimal.Zero)
|
||||
if !errors.Is(err, errZeroValue) {
|
||||
t.Error(err)
|
||||
}
|
||||
if !result.IsZero() {
|
||||
t.Error("expected 0")
|
||||
}
|
||||
|
||||
result, err = DecimalSharpeRatio([]decimal.Decimal{decimal.NewFromFloat(0.026)}, decimal.NewFromFloat(0.017), decimal.NewFromFloat(0.026))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !result.IsZero() {
|
||||
t.Error("expected 0")
|
||||
}
|
||||
|
||||
// this follows and matches the example calculation (without rounding) from
|
||||
// https://www.educba.com/sharpe-ratio-formula/
|
||||
returns := []decimal.Decimal{
|
||||
decimal.NewFromFloat(-0.0005),
|
||||
decimal.NewFromFloat(-0.0065),
|
||||
decimal.NewFromFloat(-0.0113),
|
||||
decimal.NewFromFloat(0.0031),
|
||||
decimal.NewFromFloat(-0.0112),
|
||||
decimal.NewFromFloat(0.0056),
|
||||
decimal.NewFromFloat(0.0156),
|
||||
decimal.NewFromFloat(0.0048),
|
||||
decimal.NewFromFloat(0.0012),
|
||||
decimal.NewFromFloat(0.0038),
|
||||
decimal.NewFromFloat(-0.0008),
|
||||
decimal.NewFromFloat(0.0032),
|
||||
decimal.Zero,
|
||||
decimal.NewFromFloat(-0.0128),
|
||||
decimal.NewFromFloat(-0.0058),
|
||||
decimal.NewFromFloat(0.003),
|
||||
decimal.NewFromFloat(0.0042),
|
||||
decimal.NewFromFloat(0.0055),
|
||||
decimal.NewFromFloat(0.0009),
|
||||
}
|
||||
var avg decimal.Decimal
|
||||
avg, err = DecimalArithmeticMean(returns)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
result, err = DecimalSharpeRatio(returns, decimal.NewFromFloat(-0.0017), avg)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
result = result.Round(2)
|
||||
if !result.Equal(decimal.NewFromFloat(0.26)) {
|
||||
t.Errorf("expected 0.26, received %v", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalStandardDeviation2(t *testing.T) {
|
||||
t.Parallel()
|
||||
r := []decimal.Decimal{
|
||||
decimal.NewFromInt(9),
|
||||
decimal.NewFromInt(2),
|
||||
decimal.NewFromInt(5),
|
||||
decimal.NewFromInt(4),
|
||||
decimal.NewFromInt(12),
|
||||
decimal.NewFromInt(7),
|
||||
}
|
||||
mean, err := DecimalArithmeticMean(r)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
var superMean []decimal.Decimal
|
||||
for i := range r {
|
||||
result := r[i].Sub(mean).Pow(decimal.NewFromInt(2))
|
||||
superMean = append(superMean, result)
|
||||
}
|
||||
superMeany := superMean[0].Add(superMean[1].Add(superMean[2].Add(superMean[3].Add(superMean[4].Add(superMean[5]))))).Div(decimal.NewFromInt(5))
|
||||
fSuperMeany, _ := superMeany.Float64()
|
||||
manualCalculation := decimal.NewFromFloat(math.Sqrt(fSuperMeany))
|
||||
var codeCalcu decimal.Decimal
|
||||
codeCalcu, err = DecimalSampleStandardDeviation(r)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !manualCalculation.Equal(codeCalcu) && codeCalcu.Equal(decimal.NewFromFloat(3.619)) {
|
||||
t.Error("expected 3.619")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalGeometricAverage(t *testing.T) {
|
||||
t.Parallel()
|
||||
values := []decimal.Decimal{
|
||||
decimal.NewFromInt(1),
|
||||
decimal.NewFromInt(2),
|
||||
decimal.NewFromInt(3),
|
||||
decimal.NewFromInt(4),
|
||||
decimal.NewFromInt(5),
|
||||
decimal.NewFromInt(6),
|
||||
decimal.NewFromInt(7),
|
||||
decimal.NewFromInt(8),
|
||||
}
|
||||
_, err := DecimalGeometricMean(nil)
|
||||
if !errors.Is(err, errZeroValue) {
|
||||
t.Error(err)
|
||||
}
|
||||
var mean decimal.Decimal
|
||||
mean, err = DecimalGeometricMean(values)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !mean.Equal(decimal.NewFromFloat(3.764350599503129)) {
|
||||
t.Errorf("expected %v, received %v", 3.95, mean)
|
||||
}
|
||||
|
||||
values = []decimal.Decimal{
|
||||
decimal.NewFromInt(15),
|
||||
decimal.NewFromInt(12),
|
||||
decimal.NewFromInt(13),
|
||||
decimal.NewFromInt(19),
|
||||
decimal.NewFromInt(10),
|
||||
}
|
||||
mean, err = DecimalGeometricMean(values)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !mean.Equal(decimal.NewFromFloat(13.477020583645698)) {
|
||||
t.Errorf("expected %v, received %v", 13.50, mean)
|
||||
}
|
||||
|
||||
values = []decimal.Decimal{
|
||||
decimal.NewFromInt(-1),
|
||||
decimal.NewFromInt(12),
|
||||
decimal.NewFromInt(13),
|
||||
decimal.NewFromInt(19),
|
||||
decimal.NewFromInt(10),
|
||||
}
|
||||
mean, err = DecimalGeometricMean(values)
|
||||
if !errors.Is(err, errGeometricNegative) {
|
||||
t.Error(err)
|
||||
}
|
||||
if !mean.Equal(decimal.Zero) {
|
||||
t.Errorf("expected %v, received %v", 0, mean)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalFinancialGeometricAverage(t *testing.T) {
|
||||
t.Parallel()
|
||||
values := []decimal.Decimal{
|
||||
decimal.NewFromInt(1),
|
||||
decimal.NewFromInt(2),
|
||||
decimal.NewFromInt(3),
|
||||
decimal.NewFromInt(4),
|
||||
decimal.NewFromInt(5),
|
||||
decimal.NewFromInt(6),
|
||||
decimal.NewFromInt(7),
|
||||
decimal.NewFromInt(8),
|
||||
}
|
||||
_, err := DecimalFinancialGeometricMean(nil)
|
||||
if !errors.Is(err, errZeroValue) {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
var mean decimal.Decimal
|
||||
mean, err = DecimalFinancialGeometricMean(values)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !mean.Equal(decimal.NewFromFloat(3.9541639996482028)) {
|
||||
t.Errorf("expected %v, received %v", 3.95, mean)
|
||||
}
|
||||
|
||||
values = []decimal.Decimal{
|
||||
decimal.NewFromInt(15),
|
||||
decimal.NewFromInt(12),
|
||||
decimal.NewFromInt(13),
|
||||
decimal.NewFromInt(19),
|
||||
decimal.NewFromInt(10),
|
||||
}
|
||||
mean, err = DecimalFinancialGeometricMean(values)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !mean.Equal(decimal.NewFromFloat(13.49849123325646)) {
|
||||
t.Errorf("expected %v, received %v", 13.50, mean)
|
||||
}
|
||||
|
||||
values = []decimal.Decimal{
|
||||
decimal.NewFromInt(-1),
|
||||
decimal.NewFromInt(12),
|
||||
decimal.NewFromInt(13),
|
||||
decimal.NewFromInt(19),
|
||||
decimal.NewFromInt(10),
|
||||
}
|
||||
mean, err = DecimalFinancialGeometricMean(values)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !mean.Equal(decimal.Zero) {
|
||||
t.Errorf("expected %v, received %v", 0, mean)
|
||||
}
|
||||
|
||||
values = []decimal.Decimal{
|
||||
decimal.NewFromInt(-2),
|
||||
decimal.NewFromInt(12),
|
||||
decimal.NewFromInt(13),
|
||||
decimal.NewFromInt(19),
|
||||
decimal.NewFromInt(10),
|
||||
}
|
||||
_, err = DecimalFinancialGeometricMean(values)
|
||||
if !errors.Is(err, errNegativeValueOutOfRange) {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecimalArithmeticAverage(t *testing.T) {
|
||||
values := []decimal.Decimal{
|
||||
decimal.NewFromInt(1),
|
||||
decimal.NewFromInt(2),
|
||||
decimal.NewFromInt(3),
|
||||
decimal.NewFromInt(4),
|
||||
decimal.NewFromInt(5),
|
||||
decimal.NewFromInt(6),
|
||||
decimal.NewFromInt(7),
|
||||
decimal.NewFromInt(8),
|
||||
}
|
||||
_, err := DecimalArithmeticMean(nil)
|
||||
if !errors.Is(err, errZeroValue) {
|
||||
t.Error(err)
|
||||
}
|
||||
var avg decimal.Decimal
|
||||
avg, err = DecimalArithmeticMean(values)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !avg.Equal(decimal.NewFromFloat(4.5)) {
|
||||
t.Error("expected 4.5")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user