diff --git a/config/marketcap.yaml b/config/marketcap.yaml index c59a9cf53..d1f8b41bf 100644 --- a/config/marketcap.yaml +++ b/config/marketcap.yaml @@ -13,9 +13,9 @@ exchangeStrategies: - on: max marketcap: interval: 1m - baseCurrency: TWD - baseWeight: 0% - targetCurrencies: + quoteCurrency: TWD + quoteCurrencyWeight: 0% + baseCurrencies: - BTC - ETH - MATIC diff --git a/config/rebalance.yaml b/config/rebalance.yaml index 2038a70d2..d18ce1819 100644 --- a/config/rebalance.yaml +++ b/config/rebalance.yaml @@ -12,7 +12,7 @@ exchangeStrategies: - on: max rebalance: interval: 1d - baseCurrency: TWD + quoteCurrency: TWD targetWeights: BTC: 40% ETH: 20% diff --git a/doc/strategy/marketcap.md b/doc/strategy/marketcap.md index d7eaf5259..aae4623c6 100644 --- a/doc/strategy/marketcap.md +++ b/doc/strategy/marketcap.md @@ -10,18 +10,18 @@ Setup your `COINMARKETCAP_API_KEY` in your environment variables. - `interval` - The interval to rebalance your portfolio, e.g., `5m`, `1h` -- `baseCurrency` - - The base currency of your portfolio, e.g., `USDT`, `TWD`. -- `baseWeight` - - The weight of the base currency in your portfolio. The rest of the weight will be distributed to other currencies by market capitalization. -- `targetCurrencies` +- `quoteCurrency` + - The quote currency of your portfolio, e.g., `USDT`, `TWD`. +- `quoteCurrencyWeight` + - The weight of the quote currency in your portfolio. The rest of the weight will be distributed to other currencies by market capitalization. +- `baseCurrencies` - A list of currencies you want to hold in your portfolio. - `threshold` - The threshold of the difference between the current weight and the target weight to trigger rebalancing. For example, if the threshold is `1%` and the current weight of `BTC` is `52%` and the target weight is `50%` then the strategy will sell `BTC` until it reaches `50%`. - `dryRun` - If `true`, then the strategy will not place orders. - `maxAmount` - - The maximum amount of each order in base currency. + - The maximum amount of each order in quote currency. #### Examples diff --git a/pkg/strategy/marketcap/strategy.go b/pkg/strategy/marketcap/strategy.go index b04592266..83800f573 100644 --- a/pkg/strategy/marketcap/strategy.go +++ b/pkg/strategy/marketcap/strategy.go @@ -26,12 +26,12 @@ type Strategy struct { datasource *coinmarketcap.DataSource // interval to rebalance the portfolio - Interval types.Interval `json:"interval"` - BaseCurrency string `json:"baseCurrency"` - BaseWeight fixedpoint.Value `json:"baseWeight"` - TargetCurrencies []string `json:"targetCurrencies"` - Threshold fixedpoint.Value `json:"threshold"` - DryRun bool `json:"dryRun"` + Interval types.Interval `json:"interval"` + QuoteCurrency string `json:"quoteCurrency"` + QuoteCurrencyWeight fixedpoint.Value `json:"quoteCurrencyWeight"` + BaseCurrencies []string `json:"baseCurrencies"` + Threshold fixedpoint.Value `json:"threshold"` + DryRun bool `json:"dryRun"` // max amount to buy or sell per order MaxAmount fixedpoint.Value `json:"maxAmount"` // interval to query marketcap data from coinmarketcap @@ -47,7 +47,7 @@ func (s *Strategy) Initialize() error { s.datasource = coinmarketcap.New(apiKey) // select one symbol to subscribe - s.subscribeSymbol = s.TargetCurrencies[0] + s.BaseCurrency + s.subscribeSymbol = s.BaseCurrencies[0] + s.QuoteCurrency s.activeOrderBook = bbgo.NewActiveOrderBook("") s.targetWeights = types.ValueMap{} @@ -59,12 +59,12 @@ func (s *Strategy) ID() string { } func (s *Strategy) Validate() error { - if len(s.TargetCurrencies) == 0 { + if len(s.BaseCurrencies) == 0 { return fmt.Errorf("taretCurrencies should not be empty") } - for _, c := range s.TargetCurrencies { - if c == s.BaseCurrency { + for _, c := range s.BaseCurrencies { + if c == s.QuoteCurrency { return fmt.Errorf("targetCurrencies contain baseCurrency") } } @@ -81,7 +81,7 @@ func (s *Strategy) Validate() error { } func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { - symbol := s.TargetCurrencies[0] + s.BaseCurrency + symbol := s.BaseCurrencies[0] + s.QuoteCurrency session.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: s.Interval}) session.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: s.QueryInterval}) } @@ -131,10 +131,10 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context, session *bbgo.Excha currentWeights := marketValues.Normalize() for currency, targetWeight := range s.targetWeights { - if currency == s.BaseCurrency { + if currency == s.QuoteCurrency { continue } - symbol := currency + s.BaseCurrency + symbol := currency + s.QuoteCurrency currentWeight := currentWeights[currency] currentPrice := prices[currency] @@ -198,7 +198,7 @@ func (s *Strategy) updateTargetWeights(ctx context.Context) { log.WithError(err).Error("failed to query market cap") } - for _, currency := range s.TargetCurrencies { + for _, currency := range s.BaseCurrencies { m[currency] = marketcaps[currency] } @@ -206,10 +206,10 @@ func (s *Strategy) updateTargetWeights(ctx context.Context) { m = m.Normalize() // rescale by 1 - baseWeight - m = m.MulScalar(1.0 - s.BaseWeight.Float64()) + m = m.MulScalar(1.0 - s.QuoteCurrencyWeight.Float64()) // append base weight - m[s.BaseCurrency] = s.BaseWeight.Float64() + m[s.QuoteCurrency] = s.QuoteCurrencyWeight.Float64() // convert to types.ValueMap for currency, weight := range m { @@ -227,12 +227,12 @@ func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) ty } prices := types.ValueMap{} - for _, currency := range s.TargetCurrencies { - prices[currency] = tickers[currency+s.BaseCurrency].Last + for _, currency := range s.BaseCurrencies { + prices[currency] = tickers[currency+s.QuoteCurrency].Last } // append base currency price - prices[s.BaseCurrency] = fixedpoint.One + prices[s.QuoteCurrency] = fixedpoint.One return prices } @@ -249,14 +249,14 @@ func (s *Strategy) quantities(session *bbgo.ExchangeSession) types.ValueMap { } func (s *Strategy) symbols() (symbols []string) { - for _, currency := range s.TargetCurrencies { - symbols = append(symbols, currency+s.BaseCurrency) + for _, currency := range s.BaseCurrencies { + symbols = append(symbols, currency+s.QuoteCurrency) } return symbols } func (s *Strategy) currencies() (currencies []string) { - currencies = append(currencies, s.TargetCurrencies...) - currencies = append(currencies, s.BaseCurrency) + currencies = append(currencies, s.BaseCurrencies...) + currencies = append(currencies, s.QuoteCurrency) return currencies } diff --git a/pkg/strategy/rebalance/strategy.go b/pkg/strategy/rebalance/strategy.go index 45ce699b1..fba79f296 100644 --- a/pkg/strategy/rebalance/strategy.go +++ b/pkg/strategy/rebalance/strategy.go @@ -21,7 +21,7 @@ func init() { type Strategy struct { Interval types.Interval `json:"interval"` - BaseCurrency string `json:"baseCurrency"` + QuoteCurrency string `json:"quoteCurrency"` TargetWeights types.ValueMap `json:"targetWeights"` Threshold fixedpoint.Value `json:"threshold"` DryRun bool `json:"dryRun"` @@ -121,11 +121,11 @@ func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) ty } for currency := range s.TargetWeights { - if currency == s.BaseCurrency { - m[s.BaseCurrency] = fixedpoint.One + if currency == s.QuoteCurrency { + m[s.QuoteCurrency] = fixedpoint.One continue } - m[currency] = tickers[currency+s.BaseCurrency].Last + m[currency] = tickers[currency+s.QuoteCurrency].Last } return m @@ -148,11 +148,11 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context, session *bbgo.Excha currentWeights := marketValues.Normalize() for currency, targetWeight := range s.TargetWeights { - if currency == s.BaseCurrency { + if currency == s.QuoteCurrency { continue } - symbol := currency + s.BaseCurrency + symbol := currency + s.QuoteCurrency currentWeight := currentWeights[currency] currentPrice := prices[currency] @@ -211,10 +211,10 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context, session *bbgo.Excha func (s *Strategy) symbols() (symbols []string) { for currency := range s.TargetWeights { - if currency == s.BaseCurrency { + if currency == s.QuoteCurrency { continue } - symbols = append(symbols, currency+s.BaseCurrency) + symbols = append(symbols, currency+s.QuoteCurrency) } return symbols }