Files
gocryptotrader/backtester/common/common.go
Scott 017cdf1384 Backtester: Live trading upgrades (#1023)
* Modifications for a smoother live run

* Fixes data appending

* Successfully allows multi-currency live trading. Adds multiple currencies to live DCA strategy

* Attempting to get cash and carry working

* Poor attempts at sorting out data and appending it properly with USD in mind

* =designs new live data handler

* Updates cash and carry strat to work

* adds test coverage. begins closeallpositions function

* Updates cash and carry to work live

* New kline.Event type. Cancels orders on close. Rn types

* =Fixes USD funding issue

* =fixes tests

* fixes tests AGAIN

* adds coverage to close all orders

* crummy tests, should override

* more tests

* more tests

* more coverage

* removes scourge of currency.Pair maps. More tests

* missed currency stuff

* Fixes USD data issue & collateral issue. Needs to close ALL orders

* Now triggers updates on the very first data entry

* All my problems are solved now????

* fixes tests, extends coverage

* there is some really funky candle stuff going on

* my brain is melting

* better shutdown management, fixes freezing bug

* fixes data duplication issues, adds retries to requests

* reduces logging, adds verbose options

* expands coverage over all new functionality

* fixes fun bug from curr == curr to curr.Equal(curr)

* fixes setup issues and tests

* starts adding external wallet amounts for funding

* more setup for assets

* setup live fund calcs and placing orders

* successfully performs automated cash and carry

* merge fixes

* funding properly set at all times

* fixes some bugs, need to address currencystatistics still

* adds 'appeneded' trait, attempts to fix some stats

* fixes stat bugs, adds cool new fetchfees feature

* fixes terrible processing bugs

* tightens realorder stats, sadly loses some live stats

* this actually sets everything correctly for bothcd ..cd ..cd ..cd ..cd ..!

* fix tests

* coverage

* beautiful new test coverage

* docs

* adds new fee getter delayer

* commits from the correct directory

* Lint

* adds verbose to fund manager

* Fix bug in t2b2 strat. Update dca live config. Docs

* go mod tidy

* update buf

* buf + test improvement

* Post merge fixes

* fixes surprise offset bug

* fix sizing restrictions for cash and carry

* fix server lints

* merge fixes

* test fixesss

* lintle fixles

* slowloris

* rn run to task, bug fixes, close all on close

* rpc lint and fixes

* bugfix: order manager not processing orders properly

* somewhat addresses nits

* absolutely broken end of day commit

* absolutely massive knockon effects from nits

* massive knockon effects continue

* fixes things

* address remaining nits

* jk now fixes things

* addresses the easier nits

* more nit fixers

* more niterinos addressederinos

* refactors holdings and does some nits

* so buf

* addresses some nits, fixes holdings bugs

* cleanup

* attempts to fix alert chans to prevent many chans waiting?

* terrible code, will revert

* to be reviewed in detail tomorrow

* Fixes up channel system

* smashes those nits

* fixes extra candles, fixes collateral bug, tests

* fixes data races, introduces reflection

* more checks n tests

* Fixes cash and carry issues. Fixes more cool bugs

* fixes ~typer~ typo

* replace spot strats from ftx to binance

* fixes all the tests I just destroyed

* removes example path, rm verbose

* 1) what 2) removes FTX references from the Backtester

* renamed, non-working strategies

* Removes FTX references almost as fast as sbf removes funds

* regen docs, add contrib names,sort contrib names

* fixes merge renamings

* Addresses nits. Fixes setting API credentials. Fixes Binance limit retrieval

* Fixes live order bugs with real orders and without

* Apply suggestions from code review

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update backtester/engine/live.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update backtester/engine/live.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update backtester/config/strategyconfigbuilder/main.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* updates docs

* even better docs

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
2023-01-05 13:03:17 +11:00

228 lines
9.0 KiB
Go

package common
import (
"fmt"
"regexp"
"strings"
gctorder "github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/log"
)
// CanTransact checks whether an order side is valid
// to the backtester's standards
func CanTransact(side gctorder.Side) bool {
return side.IsLong() || side.IsShort() || side == gctorder.ClosePosition
}
// DataTypeToInt converts the config string value into an int
func DataTypeToInt(dataType string) (int64, error) {
switch dataType {
case CandleStr:
return DataCandle, nil
case TradeStr:
return DataTrade, nil
default:
return 0, fmt.Errorf("unrecognised dataType '%v'", dataType)
}
}
// GenerateFileName will convert a proposed filename into something that is more
// OS friendly
func GenerateFileName(fileName, extension string) (string, error) {
if fileName == "" {
return "", fmt.Errorf("%w missing filename", errCannotGenerateFileName)
}
if extension == "" {
return "", fmt.Errorf("%w missing filename extension", errCannotGenerateFileName)
}
reg := regexp.MustCompile(`[\w-]`)
parsedFileName := reg.FindAllString(fileName, -1)
parsedExtension := reg.FindAllString(extension, -1)
fileName = strings.Join(parsedFileName, "") + "." + strings.Join(parsedExtension, "")
return strings.ToLower(fileName), nil
}
// FitStringToLimit ensures a string is of the length of the limit
// either by truncating the string with ellipses or padding with the spacer
func FitStringToLimit(str, spacer string, limit int, upper bool) string {
if limit < 0 {
return str
}
if limit == 0 {
return ""
}
limResp := limit - len(str)
if upper {
str = strings.ToUpper(str)
}
if limResp < 0 {
if limit-3 > 0 {
return str[0:limit-3] + "..."
}
return str[0:limit]
}
spacerLen := len(spacer)
for i := 0; i < limResp; i++ {
str += spacer
for j := 0; j < spacerLen; j++ {
if j > 0 {
// prevent clever people from going beyond
// the limit by having a spacer longer than 1
i++
}
}
}
return str[0:limit]
}
// RegisterBacktesterSubLoggers sets up all custom Backtester sub-loggers
func RegisterBacktesterSubLoggers() error {
var err error
Backtester, err = log.NewSubLogger("Backtester")
if err != nil {
return err
}
LiveStrategy, err = log.NewSubLogger("LiveStrategy")
if err != nil {
return err
}
Setup, err = log.NewSubLogger("Setup")
if err != nil {
return err
}
Strategy, err = log.NewSubLogger("Strategy")
if err != nil {
return err
}
Report, err = log.NewSubLogger("Report")
if err != nil {
return err
}
Statistics, err = log.NewSubLogger("Statistics")
if err != nil {
return err
}
CurrencyStatistics, err = log.NewSubLogger("CurrencyStatistics")
if err != nil {
return err
}
FundingStatistics, err = log.NewSubLogger("FundingStatistics")
if err != nil {
return err
}
Holdings, err = log.NewSubLogger("Holdings")
if err != nil {
return err
}
Data, err = log.NewSubLogger("Data")
if err != nil {
return err
}
FundManager, err = log.NewSubLogger("FundManager")
if err != nil {
return err
}
// Set to existing registered sub-loggers
Config = log.ConfigMgr
Portfolio = log.PortfolioMgr
Exchange = log.ExchangeSys
Fill = log.Fill
return nil
}
// PurgeColours removes colour information
func PurgeColours() {
CMDColours.Green = ""
CMDColours.White = ""
CMDColours.Grey = ""
CMDColours.Default = ""
CMDColours.H1 = ""
CMDColours.H2 = ""
CMDColours.H3 = ""
CMDColours.H4 = ""
CMDColours.Success = ""
CMDColours.Info = ""
CMDColours.Debug = ""
CMDColours.Warn = ""
CMDColours.DarkGrey = ""
CMDColours.Error = ""
}
// SetColours sets cmd output colours at startup. Doing it at any other point
// risks races and this really isn't worth adding a mutex for
func SetColours(colours *Colours) {
if colours.Default != "" && colours.Default != CMDColours.Default {
CMDColours.Default = colours.Default
}
if colours.Green != "" && colours.Green != CMDColours.Green {
CMDColours.Green = colours.Green
}
if colours.Error != "" && colours.Error != CMDColours.Error {
CMDColours.Error = colours.Error
}
if colours.White != "" && colours.White != CMDColours.White {
CMDColours.White = colours.White
}
if colours.Grey != "" && colours.Grey != CMDColours.Grey {
CMDColours.Grey = colours.Grey
}
if colours.H1 != "" && colours.H1 != CMDColours.H1 {
CMDColours.H1 = colours.H1
}
if colours.H2 != "" && colours.H2 != CMDColours.H2 {
CMDColours.H2 = colours.H2
}
if colours.H3 != "" && colours.H3 != CMDColours.H3 {
CMDColours.H3 = colours.H3
}
if colours.H4 != "" && colours.H4 != CMDColours.H4 {
CMDColours.H4 = colours.H4
}
if colours.Success != "" && colours.Error != CMDColours.Success {
CMDColours.Success = colours.Success
}
if colours.Info != "" && colours.Info != CMDColours.Info {
CMDColours.Info = colours.Info
}
if colours.Debug != "" && colours.Debug != CMDColours.Debug {
CMDColours.Debug = colours.Debug
}
if colours.Warn != "" && colours.Warn != CMDColours.Warn {
CMDColours.Warn = colours.Warn
}
if colours.DarkGrey != "" && colours.DarkGrey != CMDColours.DarkGrey {
CMDColours.DarkGrey = colours.DarkGrey
}
}
// Logo returns the logo
func Logo() string {
sb := strings.Builder{}
sb.WriteString(" \n")
sb.WriteString(" " + CMDColours.White + "@@@@@@@@@@@@@@@@@ \n")
sb.WriteString(" " + CMDColours.White + "@@@@@@@@@@@@@@@@@@@@@@@ " + CMDColours.Grey + ",,,,,," + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.White + "@@@@@@@@" + CMDColours.Grey + ",,,,, " + CMDColours.White + "@@@@@@@@@" + CMDColours.Grey + ",,,,,,,," + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.White + "@@@@@@@@" + CMDColours.Grey + ",,,,,,, " + CMDColours.White + "@@@@@@@" + CMDColours.Grey + ",,,,,,," + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.White + "@@@@@@" + CMDColours.Grey + "(,,,,,,,, " + CMDColours.Grey + ",," + CMDColours.White + "@@@@@@@" + CMDColours.Grey + ",,,,,," + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.Grey + ",," + CMDColours.White + "@@@@@@" + CMDColours.Grey + ",,,,,,,,, #,,,,,,,,,,,,,,,,,," + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.Grey + ",,,,*" + CMDColours.White + "@@@@@@" + CMDColours.Grey + ",,,,,,,,,,,,,,,,,,,,,,,,,," + CMDColours.Green + "%%%%%%%" + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.Grey + ",,,,,,,*" + CMDColours.White + "@@@@@@" + CMDColours.Grey + ",,,,,,,,,,,,,," + CMDColours.Green + "%%%%%" + CMDColours.Grey + " ,,,,,," + CMDColours.Grey + "%" + CMDColours.Green + "%%%%%%" + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.Grey + ",,,,,,,,*" + CMDColours.White + "@@@@@@" + CMDColours.Grey + ",,,,,,,,,,," + CMDColours.Green + "%%%%%%%%%%%%%%%%%%" + CMDColours.Grey + "#" + CMDColours.Green + "%%" + CMDColours.Grey + " \n")
sb.WriteString(" " + CMDColours.Grey + ",,,,,,*" + CMDColours.White + "@@@@@@" + CMDColours.Grey + ",,,,,,,,," + CMDColours.Green + "%%%" + CMDColours.Grey + " ,,,,," + CMDColours.Green + "%%%%%%%%" + CMDColours.Grey + ",,,,, \n")
sb.WriteString(" " + CMDColours.Grey + ",,,*" + CMDColours.White + "@@@@@@" + CMDColours.Grey + ",,,,,," + CMDColours.Green + "%%" + CMDColours.Grey + ",, ,,,,,,," + CMDColours.White + "@" + CMDColours.Green + "*%%," + CMDColours.White + "@" + CMDColours.Grey + ",,,,,, \n")
sb.WriteString(" " + CMDColours.Grey + "*" + CMDColours.White + "@@@@@@" + CMDColours.Grey + ",,,,,,,,, " + CMDColours.Grey + ",,,,," + CMDColours.White + "@@@@@@" + CMDColours.Grey + ",,,,,," + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.White + "@@@@@@" + CMDColours.Grey + ",,,,,,,,, " + CMDColours.White + "@@@@@@@" + CMDColours.Grey + ",,,,,," + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.White + "@@@@@@@@" + CMDColours.Grey + ",,,,,,, " + CMDColours.White + "@@@@@@@" + CMDColours.Grey + ",,,,,,," + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.White + "@@@@@@@@@" + CMDColours.Grey + ",,,, " + CMDColours.White + "@@@@@@@@@" + CMDColours.Grey + "#,,,,,,," + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.White + "@@@@@@@@@@@@@@@@@@@@@@@ " + CMDColours.Grey + "*,,,," + CMDColours.White + " \n")
sb.WriteString(" " + CMDColours.White + "@@@@@@@@@@@@@@@@" + CMDColours.Default + " \n")
sb.WriteString(ASCIILogo)
return sb.String()
}