From 41d4001872ac3ad0c970ea9b0ee089bd55561447 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=82=8B=E3=81=BF?= Date: Wed, 22 Dec 2021 01:41:48 +0800 Subject: [PATCH 1/3] Add log --- config/rebalance.yaml | 6 +++--- pkg/strategy/rebalance/strategy.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/config/rebalance.yaml b/config/rebalance.yaml index 45acab891..91d92b522 100644 --- a/config/rebalance.yaml +++ b/config/rebalance.yaml @@ -13,9 +13,9 @@ exchangeStrategies: targetWeights: BTC: 40% ETH: 20% - MAX: 20% - USDT: 10% - TWD: 10% + MAX: 10% + USDT: 15% + TWD: 15% threshold: 2% # max amount to buy or sell per order maxAmount: 10_000 diff --git a/pkg/strategy/rebalance/strategy.go b/pkg/strategy/rebalance/strategy.go index 5306b724e..9b744b166 100644 --- a/pkg/strategy/rebalance/strategy.go +++ b/pkg/strategy/rebalance/strategy.go @@ -103,6 +103,7 @@ func (s *Strategy) rebalance(ctx context.Context, orderExecutor bbgo.OrderExecut orders := s.generateSubmitOrders(prices, marketValues) _, err = orderExecutor.SubmitOrders(ctx, orders...) if err != nil { + log.WithError(err).Error("submit order error") return } } @@ -156,11 +157,21 @@ func (s *Strategy) generateSubmitOrders(prices, marketValues map[string]fixedpoi symbol := currency + s.BaseCurrency currentWeight := currentWeights[currency] currentPrice := prices[currency] + log.Infof("%s price: %f, current weight: %f, target weight: %f", + symbol, + currentPrice.Float64(), + currentWeight.Float64(), + targetWeight.Float64()) // calculate the difference between current weight and target weight // if the difference is less than threshold, then we will not create the order weightDifference := targetWeight.Sub(currentWeight) if weightDifference.Abs() < s.Threshold { + log.Infof("%s weight distance |%f - %f| = |%f| less than the threshold: %f", + currentWeight.Float64(), + targetWeight.Float64(), + weightDifference.Float64(), + s.Threshold.Float64()) continue } From 2999e41ef06d33df2a224110a477c73471ea949b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=82=8B=E3=81=BF?= Date: Wed, 22 Dec 2021 01:50:27 +0800 Subject: [PATCH 2/3] Validate config --- pkg/strategy/rebalance/strategy.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/strategy/rebalance/strategy.go b/pkg/strategy/rebalance/strategy.go index 9b744b166..feeaa1d5a 100644 --- a/pkg/strategy/rebalance/strategy.go +++ b/pkg/strategy/rebalance/strategy.go @@ -2,6 +2,7 @@ package kline import ( "context" + "fmt" "time" "github.com/sirupsen/logrus" @@ -67,6 +68,32 @@ func (s *Strategy) ID() string { return ID } +func (s *Strategy) Validate() error { + if s.Interval == 0 { + return fmt.Errorf("interval shoud not be 0") + } + + if len(s.TargetWeights) == 0 { + return fmt.Errorf("targetWeights should not be empty") + } + + for currency, weight := range s.TargetWeights { + if weight.Float64() < 0 { + return fmt.Errorf("%s weight: %f should not less than 0", currency, weight.Float64()) + } + } + + if s.Threshold < 0 { + return fmt.Errorf("threshold should not less than 0") + } + + if s.MaxAmount < 0 { + return fmt.Errorf("maxAmount shoud not less than 0") + } + + return nil +} + func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {} func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { From 4a8be9cc1a4ff6bd75c1c0647b759d82aa9d76c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=AA=E3=82=8B=E3=81=BF?= Date: Wed, 22 Dec 2021 02:04:44 +0800 Subject: [PATCH 3/3] Fix log --- pkg/strategy/rebalance/strategy.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/strategy/rebalance/strategy.go b/pkg/strategy/rebalance/strategy.go index feeaa1d5a..7e23a65c4 100644 --- a/pkg/strategy/rebalance/strategy.go +++ b/pkg/strategy/rebalance/strategy.go @@ -195,6 +195,7 @@ func (s *Strategy) generateSubmitOrders(prices, marketValues map[string]fixedpoi weightDifference := targetWeight.Sub(currentWeight) if weightDifference.Abs() < s.Threshold { log.Infof("%s weight distance |%f - %f| = |%f| less than the threshold: %f", + symbol, currentWeight.Float64(), targetWeight.Float64(), weightDifference.Float64(),