mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
Merge pull request #387 from narumiruna/narumi/rebalance/validate
strategy: rebalance: validate parameters
This commit is contained in:
commit
b2ffcb7993
|
@ -13,9 +13,9 @@ exchangeStrategies:
|
||||||
targetWeights:
|
targetWeights:
|
||||||
BTC: 40%
|
BTC: 40%
|
||||||
ETH: 20%
|
ETH: 20%
|
||||||
MAX: 20%
|
MAX: 10%
|
||||||
USDT: 10%
|
USDT: 15%
|
||||||
TWD: 10%
|
TWD: 15%
|
||||||
threshold: 2%
|
threshold: 2%
|
||||||
# max amount to buy or sell per order
|
# max amount to buy or sell per order
|
||||||
maxAmount: 10_000
|
maxAmount: 10_000
|
||||||
|
|
|
@ -2,6 +2,7 @@ package kline
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
@ -67,6 +68,32 @@ func (s *Strategy) ID() string {
|
||||||
return ID
|
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) Subscribe(session *bbgo.ExchangeSession) {}
|
||||||
|
|
||||||
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
||||||
|
@ -103,6 +130,7 @@ func (s *Strategy) rebalance(ctx context.Context, orderExecutor bbgo.OrderExecut
|
||||||
orders := s.generateSubmitOrders(prices, marketValues)
|
orders := s.generateSubmitOrders(prices, marketValues)
|
||||||
_, err = orderExecutor.SubmitOrders(ctx, orders...)
|
_, err = orderExecutor.SubmitOrders(ctx, orders...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.WithError(err).Error("submit order error")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -156,11 +184,22 @@ func (s *Strategy) generateSubmitOrders(prices, marketValues map[string]fixedpoi
|
||||||
symbol := currency + s.BaseCurrency
|
symbol := currency + s.BaseCurrency
|
||||||
currentWeight := currentWeights[currency]
|
currentWeight := currentWeights[currency]
|
||||||
currentPrice := prices[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
|
// calculate the difference between current weight and target weight
|
||||||
// if the difference is less than threshold, then we will not create the order
|
// if the difference is less than threshold, then we will not create the order
|
||||||
weightDifference := targetWeight.Sub(currentWeight)
|
weightDifference := targetWeight.Sub(currentWeight)
|
||||||
if weightDifference.Abs() < s.Threshold {
|
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(),
|
||||||
|
s.Threshold.Float64())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user