Backtester: USD tracking (#818)

* Initial concept for creating price tracking pairs

* Completes coverage, even with a slow test

* I dont know what point to hook this stuff up

* Bit of a broken way of handling tracking pairs

* Correctly calculates USD rates against all currencies

* Removes dependency on GCT config

* Failed currency statistics redesign

* initial Update chart to use highcharts

* Minor changes to stats

* Creats funding stats to handle the stat calculations. Needs more work

* tracks USD snapshots and BREAKS THINGS FURTHER

* Fixed!

* Adds ratio calculations and such, but its WRONG. do it at totals level dummy

* End of day basic lint

* Remaining lints

* USD totals statistics

* Minor panic fixes

* Printing of funding stats, but its bad

* Properly calculates overall benchmark, moves funding stat output

* Adds some template charge, removes duplicate fields

* New charts!

* Darkcharts. funding protection when disabled

* Now works with usd tracking/funding disabled!

* Attempting to only show working stats based on settings.

* Spruces up the goose/reporting

* Completes report HTML rendering

* lint and test fixes

* funding statistics testing

* slightly more test coverage

* Test coverage

* Initial documentation

* Fixes tests

* Database testing and rendering improvements and breakages

* report and cmd rendering, linting. fix comma output. rm gct cfg

* PR mode 🎉 Path field, config builder support,testing,linting,docs

* minor calculation improvement

* Secret lint that did not show up locally

* Disable USD tracking for example configs

* ShazNitNoScope

* Forgotten errors

* ""

* literally Logarithmically logically renders the date 👀

* Fixes typos, fixes parallel test, fixes chart gui and exporting
This commit is contained in:
Scott
2021-11-08 12:10:15 +11:00
committed by GitHub
parent 77d90a1a6c
commit 6eaa2e4073
81 changed files with 4691 additions and 2254 deletions

View File

@@ -4,7 +4,10 @@ import (
"fmt"
"math"
"strconv"
"strings"
"time"
"github.com/shopspring/decimal"
)
// FloatFromString format
@@ -81,3 +84,83 @@ func BoolPtr(condition bool) *bool {
b := condition
return &b
}
// IntToHumanFriendlyString converts an int to a comma separated string at the thousand point
// eg 1000 becomes 1,000
func IntToHumanFriendlyString(number int64, thousandsSep string) string {
neg := false
if number < 0 {
number = -number
neg = true
}
str := fmt.Sprintf("%v", number)
return numberToHumanFriendlyString(str, 0, "", thousandsSep, neg)
}
// FloatToHumanFriendlyString converts a float to a comma separated string at the thousand point
// eg 1000 becomes 1,000
func FloatToHumanFriendlyString(number float64, decimals uint, decPoint, thousandsSep string) string {
neg := false
if number < 0 {
number = -number
neg = true
}
dec := int(decimals)
str := fmt.Sprintf("%."+strconv.Itoa(dec)+"F", number)
return numberToHumanFriendlyString(str, dec, decPoint, thousandsSep, neg)
}
// DecimalToHumanFriendlyString converts a decimal number to a comma separated string at the thousand point
// eg 1000 becomes 1,000
func DecimalToHumanFriendlyString(number decimal.Decimal, rounding int, decPoint, thousandsSep string) string {
neg := false
if number.LessThan(decimal.Zero) {
number = number.Abs()
neg = true
}
str := number.String()
rnd := strings.Split(str, ".")
if len(rnd) == 1 {
rounding = 0
} else if len(rnd[1]) < rounding {
rounding = len(rnd[1])
}
return numberToHumanFriendlyString(number.StringFixed(int32(rounding)), rounding, decPoint, thousandsSep, neg)
}
func numberToHumanFriendlyString(str string, dec int, decPoint, thousandsSep string, neg bool) string {
var prefix, suffix string
if len(str)-(dec+1) < 0 {
dec = 0
}
if dec > 0 {
prefix = str[:len(str)-(dec+1)]
suffix = str[len(str)-dec:]
} else {
prefix = str
}
sep := []byte(thousandsSep)
n, l1, l2 := 0, len(prefix), len(sep)
// thousands sep num
c := (l1 - 1) / 3
tmp := make([]byte, l2*c+l1)
pos := len(tmp) - 1
for i := l1 - 1; i >= 0; i, n, pos = i-1, n+1, pos-1 {
if l2 > 0 && n > 0 && n%3 == 0 {
for j := range sep {
tmp[pos] = sep[l2-j-1]
pos--
}
}
tmp[pos] = prefix[i]
}
s := string(tmp)
if dec > 0 {
s += decPoint + suffix
}
if neg {
s = "-" + s
}
return s
}

View File

@@ -1,8 +1,11 @@
package convert
import (
"strings"
"testing"
"time"
"github.com/shopspring/decimal"
)
func TestFloatFromString(t *testing.T) {
@@ -150,3 +153,132 @@ func TestBoolPtr(t *testing.T) {
t.Fatal("false expected received true")
}
}
func TestFloatToHumanFriendlyString(t *testing.T) {
t.Parallel()
test := FloatToHumanFriendlyString(0, 3, ".", ",")
if strings.Contains(test, ",") {
t.Error("unexpected ','")
}
test = FloatToHumanFriendlyString(100, 3, ".", ",")
if strings.Contains(test, ",") {
t.Error("unexpected ','")
}
test = FloatToHumanFriendlyString(1000, 3, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = FloatToHumanFriendlyString(-1000, 3, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = FloatToHumanFriendlyString(-1000, 10, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = FloatToHumanFriendlyString(1000.1337, 1, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec := strings.Split(test, ".")
if len(dec) == 1 {
t.Error("expected decimal place")
}
if dec[1] != "1" {
t.Error("expected decimal place")
}
}
func TestDecimalToHumanFriendlyString(t *testing.T) {
t.Parallel()
test := DecimalToHumanFriendlyString(decimal.Zero, 0, ".", ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromInt(100), 0, ".", ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromInt(1000), 0, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(1000.1337), 1, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec := strings.Split(test, ".")
if len(dec) == 1 {
t.Error("expected decimal place")
}
if dec[1] != "1" {
t.Error("expected decimal place")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(-1000.1337), 1, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(-1000.1337), 100000, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = DecimalToHumanFriendlyString(decimal.NewFromFloat(1000.1), 10, ".", ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec = strings.Split(test, ".")
if len(dec) == 1 {
t.Error("expected decimal place")
}
if dec[1] != "1" {
t.Error("expected decimal place")
}
}
func TestIntToHumanFriendlyString(t *testing.T) {
t.Parallel()
test := IntToHumanFriendlyString(0, ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = IntToHumanFriendlyString(100, ",")
if strings.Contains(test, ",") {
t.Log(test)
t.Error("unexpected ','")
}
test = IntToHumanFriendlyString(1000, ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = IntToHumanFriendlyString(-1000, ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
test = IntToHumanFriendlyString(1000000, ",")
if !strings.Contains(test, ",") {
t.Error("expected ','")
}
dec := strings.Split(test, ",")
if len(dec) <= 2 {
t.Error("expected two commas place")
}
}
func TestNumberToHumanFriendlyString(t *testing.T) {
resp := numberToHumanFriendlyString("1", 1337, ".", ",", false)
if strings.Contains(resp, ".") {
t.Error("expected no comma")
}
}