From 8b17d78a48fdf7c3b972d318e617bd6e222335c9 Mon Sep 17 00:00:00 2001 From: Lan Phan Date: Wed, 2 Oct 2024 22:40:28 +0700 Subject: [PATCH] solved all deprecated, comment all unused variables and functions --- pkg/accounting/cost_distribution_test.go | 4 ++-- pkg/backtest/exchange.go | 6 ------ pkg/backtest/matching.go | 1 + pkg/backtest/report.go | 5 ++--- pkg/bbgo/activeorderbook.go | 2 +- pkg/bbgo/builder.go | 5 ++--- pkg/bbgo/config.go | 6 +++--- pkg/bbgo/config_test.go | 4 ++-- pkg/bbgo/environment.go | 7 ++----- pkg/bbgo/interact.go | 15 --------------- pkg/bbgo/order_executor_simple.go | 3 --- pkg/bbgo/reporter.go | 5 ++--- pkg/bbgo/session.go | 3 +++ pkg/cache/cache.go | 5 ++--- pkg/cmd/hoptimize.go | 12 ++++++------ pkg/cmd/margin.go | 4 ++-- pkg/cmd/optimize.go | 3 +-- pkg/cmd/root.go | 10 ++++------ pkg/cmd/run.go | 3 +-- pkg/cmd/utils.go | 5 ++--- pkg/exchange/binance/parse.go | 2 +- pkg/optimizer/config.go | 4 ++-- pkg/server/routes.go | 6 ++---- pkg/service/persistence_json.go | 5 ++--- pkg/strategy/grid2/grid_recover_test.go | 3 +-- pkg/util/http_response.go | 4 ++-- pkg/util/http_response_test.go | 4 ++-- pkg/util/json.go | 3 +-- utils/embed/main.go | 5 ++--- 29 files changed, 53 insertions(+), 91 deletions(-) diff --git a/pkg/accounting/cost_distribution_test.go b/pkg/accounting/cost_distribution_test.go index 3addf28af..a2f980dd8 100644 --- a/pkg/accounting/cost_distribution_test.go +++ b/pkg/accounting/cost_distribution_test.go @@ -2,7 +2,7 @@ package accounting import ( "encoding/json" - "io/ioutil" + "os" "testing" "github.com/stretchr/testify/assert" @@ -16,7 +16,7 @@ func TestStockManager(t *testing.T) { t.Run("testdata", func(t *testing.T) { var trades []types.Trade - out, err := ioutil.ReadFile("testdata/btcusdt-trades.json") + out, err := os.ReadFile("testdata/btcusdt-trades.json") assert.NoError(t, err) err = json.Unmarshal(out, &trades) diff --git a/pkg/backtest/exchange.go b/pkg/backtest/exchange.go index 328cd6ffe..a5d63e4b2 100644 --- a/pkg/backtest/exchange.go +++ b/pkg/backtest/exchange.go @@ -136,12 +136,6 @@ func (e *Exchange) resetMatchingBooks() { e.matchingBooksMutex.Unlock() } -func (e *Exchange) addMatchingBook(symbol string, market types.Market) { - e.matchingBooksMutex.Lock() - e._addMatchingBook(symbol, market) - e.matchingBooksMutex.Unlock() -} - func (e *Exchange) _addMatchingBook(symbol string, market types.Market) { matching := &SimplePriceMatching{ currentTime: e.currentTime, diff --git a/pkg/backtest/matching.go b/pkg/backtest/matching.go index 57fe493a5..6de90a2c8 100644 --- a/pkg/backtest/matching.go +++ b/pkg/backtest/matching.go @@ -48,6 +48,7 @@ func init() { } // SimplePriceMatching implements a simple kline data driven matching engine for backtest +// //go:generate callbackgen -type SimplePriceMatching type SimplePriceMatching struct { Symbol string diff --git a/pkg/backtest/report.go b/pkg/backtest/report.go index bce827699..dc05b5987 100644 --- a/pkg/backtest/report.go +++ b/pkg/backtest/report.go @@ -3,7 +3,6 @@ package backtest import ( "encoding/json" "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -55,7 +54,7 @@ type SummaryReport struct { } func ReadSummaryReport(filename string) (*SummaryReport, error) { - o, err := ioutil.ReadFile(filename) + o, err := os.ReadFile(filename) if err != nil { return nil, err } @@ -247,7 +246,7 @@ func loadReportIndexLocked(indexFilePath string) (*ReportIndex, error) { if fileInfo, err := os.Stat(indexFilePath); err != nil { return nil, err } else if fileInfo.Size() != 0 { - o, err := ioutil.ReadFile(indexFilePath) + o, err := os.ReadFile(indexFilePath) if err != nil { return nil, err } diff --git a/pkg/bbgo/activeorderbook.go b/pkg/bbgo/activeorderbook.go index 7cfab2dfd..8ab40e36b 100644 --- a/pkg/bbgo/activeorderbook.go +++ b/pkg/bbgo/activeorderbook.go @@ -70,7 +70,7 @@ func (b *ActiveOrderBook) BindStream(stream types.Stream) { stream.OnOrderUpdate(b.orderUpdateHandler) } -func (b *ActiveOrderBook) waitClear( +func (b *ActiveOrderBook) waitOrderClear( ctx context.Context, order types.Order, waitTime, timeout time.Duration, ) (bool, error) { if !b.orders.Exists(order.OrderID) { diff --git a/pkg/bbgo/builder.go b/pkg/bbgo/builder.go index f23babf92..68870b2eb 100644 --- a/pkg/bbgo/builder.go +++ b/pkg/bbgo/builder.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "fmt" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -51,7 +50,7 @@ func generateRunFile(filepath string, config *Config, imports []string) error { return err } - return ioutil.WriteFile(filepath, buf.Bytes(), 0644) + return os.WriteFile(filepath, buf.Bytes(), 0644) } // compilePackage generates the main.go file of the wrapper package @@ -80,7 +79,7 @@ func Build(ctx context.Context, userConfig *Config, targetConfig BuildTargetConf buildDir = "build" } - packageDir, err := ioutil.TempDir(buildDir, "bbgow-") // with prefix bbgow + packageDir, err := os.MkdirTemp(buildDir, "bbgow-") // with prefix bbgow if err != nil { return "", err } diff --git a/pkg/bbgo/config.go b/pkg/bbgo/config.go index 2d9812912..697a733e2 100644 --- a/pkg/bbgo/config.go +++ b/pkg/bbgo/config.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "fmt" - "io/ioutil" + "os" "reflect" "runtime" "strings" @@ -496,7 +496,7 @@ func loadStash(config []byte) (Stash, error) { func LoadBuildConfig(configFile string) (*Config, error) { var config Config - content, err := ioutil.ReadFile(configFile) + content, err := os.ReadFile(configFile) if err != nil { return nil, err } @@ -526,7 +526,7 @@ func LoadBuildConfig(configFile string) (*Config, error) { func Load(configFile string, loadStrategies bool) (*Config, error) { var config Config - content, err := ioutil.ReadFile(configFile) + content, err := os.ReadFile(configFile) if err != nil { return nil, err } diff --git a/pkg/bbgo/config_test.go b/pkg/bbgo/config_test.go index 2598a52fc..0b15546a4 100644 --- a/pkg/bbgo/config_test.go +++ b/pkg/bbgo/config_test.go @@ -2,7 +2,7 @@ package bbgo import ( "context" - "io/ioutil" + "os" "testing" "github.com/stretchr/testify/assert" @@ -128,7 +128,7 @@ func TestLoadConfig(t *testing.T) { yamlText, err := config.YAML() assert.NoError(t, err) - yamlTextSource, err := ioutil.ReadFile("testdata/strategy.yaml") + yamlTextSource, err := os.ReadFile("testdata/strategy.yaml") assert.NoError(t, err) var sourceMap map[string]interface{} diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 07893aaad..fc8d245d2 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "image/png" - "io/ioutil" stdlog "log" "math/rand" "os" @@ -34,7 +33,7 @@ import ( func init() { // randomize pulling - rand.Seed(time.Now().UnixNano()) + rand.New(rand.NewSource(time.Now().UnixNano())) } var defaultSyncBufferPeriod = 30 * time.Minute @@ -69,8 +68,6 @@ func RegisterStrategy(key string, s interface{}) { } } -var emptyTime time.Time - type SyncStatus int const ( @@ -971,7 +968,7 @@ func writeOTPKeyAsQRCodePNG(key *otp.Key, imagePath string) error { return err } - if err := ioutil.WriteFile(imagePath, buf.Bytes(), 0644); err != nil { + if err := os.WriteFile(imagePath, buf.Bytes(), 0644); err != nil { return err } diff --git a/pkg/bbgo/interact.go b/pkg/bbgo/interact.go index 133c1b13f..6cb84247d 100644 --- a/pkg/bbgo/interact.go +++ b/pkg/bbgo/interact.go @@ -5,8 +5,6 @@ import ( "fmt" "path" "reflect" - "strconv" - "strings" "github.com/c9s/bbgo/pkg/dynamic" "github.com/c9s/bbgo/pkg/fixedpoint" @@ -556,19 +554,6 @@ func getStrategySignature(strategy SingleExchangeStrategy) (string, error) { return signature, nil } -func parseFloatPercent(s string, bitSize int) (f float64, err error) { - i := strings.Index(s, "%") - if i < 0 { - return strconv.ParseFloat(s, bitSize) - } - - f, err = strconv.ParseFloat(s[:i], bitSize) - if err != nil { - return 0, err - } - return f / 100.0, nil -} - func getStrategySignatures(exchangeStrategies map[string]SingleExchangeStrategy) []string { var strategies []string for signature := range exchangeStrategies { diff --git a/pkg/bbgo/order_executor_simple.go b/pkg/bbgo/order_executor_simple.go index 123cd03f1..ea61b3ccc 100644 --- a/pkg/bbgo/order_executor_simple.go +++ b/pkg/bbgo/order_executor_simple.go @@ -3,7 +3,6 @@ package bbgo import ( "context" - log "github.com/sirupsen/logrus" "go.uber.org/multierr" "github.com/c9s/bbgo/pkg/core" @@ -14,8 +13,6 @@ import ( // This order executor does not handle position and profit stats update type SimpleOrderExecutor struct { BaseOrderExecutor - - logger log.FieldLogger } func NewSimpleOrderExecutor(session *ExchangeSession) *SimpleOrderExecutor { diff --git a/pkg/bbgo/reporter.go b/pkg/bbgo/reporter.go index 910f1ba4f..fb1c02c2c 100644 --- a/pkg/bbgo/reporter.go +++ b/pkg/bbgo/reporter.go @@ -11,9 +11,8 @@ type PnLReporter interface { } type baseReporter struct { - notifier Notifier - cron *cron.Cron - environment *Environment + notifier Notifier + cron *cron.Cron } type PnLReporterManager struct { diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index 22bcdc608..73adc39f5 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -209,6 +209,9 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment) var err error if util.SetEnvVarBool("DISABLE_MARKETS_CACHE", &disableMarketsCache); disableMarketsCache { markets, err = session.Exchange.QueryMarkets(ctx) + if err != nil { + return err + } } else { markets, err = cache.LoadExchangeMarketsWithCache(ctx, session.Exchange) if err != nil { diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 81b688d19..2d864e17b 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "os" "path" "reflect" @@ -99,7 +98,7 @@ func WithCache(key string, obj interface{}, fetcher DataFetcher) error { return err } - if err := ioutil.WriteFile(cacheFile, out, 0666); err != nil { + if err := os.WriteFile(cacheFile, out, 0666); err != nil { return err } @@ -113,7 +112,7 @@ func WithCache(key string, obj interface{}, fetcher DataFetcher) error { } else { log.Debugf("cache %s found", cacheFile) - data, err := ioutil.ReadFile(cacheFile) + data, err := os.ReadFile(cacheFile) if err != nil { return err } diff --git a/pkg/cmd/hoptimize.go b/pkg/cmd/hoptimize.go index 195ae3963..003b085e5 100644 --- a/pkg/cmd/hoptimize.go +++ b/pkg/cmd/hoptimize.go @@ -4,16 +4,16 @@ import ( "context" "encoding/json" "fmt" + "os" + "os/signal" + "syscall" + "time" + "github.com/c9s/bbgo/pkg/optimizer" "github.com/fatih/color" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "gopkg.in/yaml.v3" - "io/ioutil" - "os" - "os/signal" - "syscall" - "time" ) func init() { @@ -69,7 +69,7 @@ var hoptimizeCmd = &cobra.Command{ return err } - yamlBody, err := ioutil.ReadFile(configFile) + yamlBody, err := os.ReadFile(configFile) if err != nil { return err } diff --git a/pkg/cmd/margin.go b/pkg/cmd/margin.go index 542a905dd..7a5e97d95 100644 --- a/pkg/cmd/margin.go +++ b/pkg/cmd/margin.go @@ -37,11 +37,11 @@ var marginCmd = &cobra.Command{ Short: "margin related history", SilenceUsage: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := cobraLoadDotenv(cmd, args); err != nil { + if err := cobraLoadDotenv(cmd); err != nil { return err } - if err := cobraLoadConfig(cmd, args); err != nil { + if err := cobraLoadConfig(cmd); err != nil { return err } diff --git a/pkg/cmd/optimize.go b/pkg/cmd/optimize.go index a9af7f4e2..792f460e3 100644 --- a/pkg/cmd/optimize.go +++ b/pkg/cmd/optimize.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "io/ioutil" "os" "github.com/spf13/cobra" @@ -60,7 +59,7 @@ var optimizeCmd = &cobra.Command{ return err } - yamlBody, err := ioutil.ReadFile(configFile) + yamlBody, err := os.ReadFile(configFile) if err != nil { return err } diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index b9fe59f53..2b0ded1df 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -22,8 +22,6 @@ import ( "github.com/c9s/bbgo/pkg/util" _ "time/tzdata" - - _ "github.com/go-sql-driver/mysql" ) var cpuProfileFile *os.File @@ -38,7 +36,7 @@ var RootCmd = &cobra.Command{ SilenceUsage: true, PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - if err := cobraLoadDotenv(cmd, args); err != nil { + if err := cobraLoadDotenv(cmd); err != nil { return err } @@ -101,7 +99,7 @@ var RootCmd = &cobra.Command{ } } - return cobraLoadConfig(cmd, args) + return cobraLoadConfig(cmd) }, PersistentPostRunE: func(cmd *cobra.Command, args []string) error { pprof.StopCPUProfile() @@ -116,7 +114,7 @@ var RootCmd = &cobra.Command{ }, } -func cobraLoadDotenv(cmd *cobra.Command, args []string) error { +func cobraLoadDotenv(cmd *cobra.Command) error { disableDotEnv, err := cmd.Flags().GetBool("no-dotenv") if err != nil { return err @@ -137,7 +135,7 @@ func cobraLoadDotenv(cmd *cobra.Command, args []string) error { return nil } -func cobraLoadConfig(cmd *cobra.Command, args []string) error { +func cobraLoadConfig(cmd *cobra.Command) error { configFile, err := cmd.Flags().GetString("config") if err != nil { return errors.Wrapf(err, "failed to get the config flag") diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 92e1bbe8a..11cafc12c 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -2,7 +2,6 @@ package cmd import ( "context" - "io/ioutil" "os" "os/exec" "path/filepath" @@ -308,7 +307,7 @@ func runWrapperBinary(ctx context.Context, cmd *cobra.Command, userConfig *bbgo. // buildAndRun builds the package natively and run the binary with the given args func buildAndRun(ctx context.Context, userConfig *bbgo.Config, args ...string) (*exec.Cmd, error) { - packageDir, err := ioutil.TempDir("build", "bbgow") + packageDir, err := os.MkdirTemp("build", "bbgow") if err != nil { return nil, err } diff --git a/pkg/cmd/utils.go b/pkg/cmd/utils.go index eac567af2..3485cec48 100644 --- a/pkg/cmd/utils.go +++ b/pkg/cmd/utils.go @@ -1,11 +1,10 @@ package cmd import ( - log "github.com/sirupsen/logrus" - "github.com/spf13/cobra" - "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" ) func cobraInitRequired(required []string) func(cmd *cobra.Command, args []string) error { diff --git a/pkg/exchange/binance/parse.go b/pkg/exchange/binance/parse.go index 48fe18012..891b74cac 100644 --- a/pkg/exchange/binance/parse.go +++ b/pkg/exchange/binance/parse.go @@ -867,7 +867,7 @@ type MarkPriceUpdateEvent struct { type ContinuousKLineEvent struct { EventBase Symbol string `json:"ps"` - ct string `json:"ct"` + CT string `json:"ct"` KLine KLine `json:"k,omitempty"` } diff --git a/pkg/optimizer/config.go b/pkg/optimizer/config.go index 4d5e0515b..3f319fd20 100644 --- a/pkg/optimizer/config.go +++ b/pkg/optimizer/config.go @@ -2,7 +2,7 @@ package optimizer import ( "fmt" - "io/ioutil" + "os" "strings" "gopkg.in/yaml.v3" @@ -57,7 +57,7 @@ var defaultLocalExecutorConfig = &LocalExecutorConfig{ } func LoadConfig(yamlConfigFileName string) (*Config, error) { - configYaml, err := ioutil.ReadFile(yamlConfigFileName) + configYaml, err := os.ReadFile(yamlConfigFileName) if err != nil { return nil, err } diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 6502c17e1..6968a40fd 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -3,12 +3,10 @@ package server import ( "context" "fmt" - "io/ioutil" "math/rand" "net" "net/http" "os" - "regexp" "strconv" "time" @@ -525,7 +523,7 @@ func (s *Server) setupSaveConfig(c *gin.Context) { return } - if err := ioutil.WriteFile(filename, out, 0666); err != nil { + if err := os.WriteFile(filename, out, 0666); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } @@ -533,7 +531,7 @@ func (s *Server) setupSaveConfig(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"success": true}) } -var pageRoutePattern = regexp.MustCompile("/[a-z]+$") +// var pageRoutePattern = regexp.MustCompile("/[a-z]+$") func moveFileToBackup(filename string) error { stat, err := os.Stat(filename) diff --git a/pkg/service/persistence_json.go b/pkg/service/persistence_json.go index 3bea74556..a2595504c 100644 --- a/pkg/service/persistence_json.go +++ b/pkg/service/persistence_json.go @@ -2,7 +2,6 @@ package service import ( "encoding/json" - "io/ioutil" "os" "path/filepath" ) @@ -49,7 +48,7 @@ func (store JsonStore) Load(val interface{}) error { return ErrPersistenceNotExists } - data, err := ioutil.ReadFile(p) + data, err := os.ReadFile(p) if err != nil { return err } @@ -74,5 +73,5 @@ func (store JsonStore) Save(val interface{}) error { } p := filepath.Join(store.Directory, store.ID) + ".json" - return ioutil.WriteFile(p, data, 0666) + return os.WriteFile(p, data, 0666) } diff --git a/pkg/strategy/grid2/grid_recover_test.go b/pkg/strategy/grid2/grid_recover_test.go index 997cac9e5..d22e91f39 100644 --- a/pkg/strategy/grid2/grid_recover_test.go +++ b/pkg/strategy/grid2/grid_recover_test.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "os" "sort" "strconv" @@ -108,7 +107,7 @@ func NewTestDataService(t *TestData) *TestDataService { } func readSpec(fileName string) (*TestData, error) { - content, err := ioutil.ReadFile(fileName) + content, err := os.ReadFile(fileName) if err != nil { return nil, err } diff --git a/pkg/util/http_response.go b/pkg/util/http_response.go index 392f14b6f..1ddffb3d7 100644 --- a/pkg/util/http_response.go +++ b/pkg/util/http_response.go @@ -2,7 +2,7 @@ package util import ( "encoding/json" - "io/ioutil" + "io" "net/http" ) @@ -17,7 +17,7 @@ type Response struct { // NewResponse is a wrapper of the http.Response instance, it reads the response body and close the file. func NewResponse(r *http.Response) (response *Response, err error) { - body, err := ioutil.ReadAll(r.Body) + body, err := io.ReadAll(r.Body) if err != nil { return nil, err } diff --git a/pkg/util/http_response_test.go b/pkg/util/http_response_test.go index af864f825..6c3d55fe8 100644 --- a/pkg/util/http_response_test.go +++ b/pkg/util/http_response_test.go @@ -2,7 +2,7 @@ package util import ( "bytes" - "io/ioutil" + "io" "net/http" "testing" @@ -14,7 +14,7 @@ func TestResponse_DecodeJSON(t *testing.T) { Name string `json:"name"` } json := `{"name":"Test Name","a":"a"}` - reader := ioutil.NopCloser(bytes.NewReader([]byte(json))) + reader := io.NopCloser(bytes.NewReader([]byte(json))) resp, err := NewResponse(&http.Response{ StatusCode: 200, Body: reader, diff --git a/pkg/util/json.go b/pkg/util/json.go index b11dd89b6..1d31f7308 100644 --- a/pkg/util/json.go +++ b/pkg/util/json.go @@ -3,7 +3,6 @@ package util import ( "encoding/json" "io" - "io/ioutil" "os" ) @@ -13,7 +12,7 @@ func WriteJsonFile(p string, obj interface{}) error { return err } - return ioutil.WriteFile(p, out, 0644) + return os.WriteFile(p, out, 0644) } func ReadJsonFile(file string, obj interface{}) error { diff --git a/utils/embed/main.go b/utils/embed/main.go index 1d45db115..6f23354b7 100644 --- a/utils/embed/main.go +++ b/utils/embed/main.go @@ -4,7 +4,6 @@ import ( "bytes" "flag" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -119,14 +118,14 @@ func Embed(file string, dirs ...string) error { log.Printf("packing %s...", path) - b, err := ioutil.ReadFile(path) + b, err := os.ReadFile(path) if err != nil { return err } path = filepath.ToSlash(path) fmt.Fprintf(w, ` assets[%q] = []byte{`, strings.TrimPrefix(path, dir)) - fmt.Fprintf(w, formatBytes(b)) + fmt.Fprint(w, formatBytes(b)) fmt.Fprintln(w, `}`) return nil })