Skip to content

Commit

Permalink
change variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
narumiruna committed Oct 13, 2022
1 parent 1d9cc54 commit 9330b9f
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 41 deletions.
6 changes: 3 additions & 3 deletions config/marketcap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ exchangeStrategies:
- on: max
marketcap:
interval: 1m
baseCurrency: TWD
baseWeight: 0%
targetCurrencies:
quoteCurrency: TWD
quoteCurrencyWeight: 0%
baseCurrencies:
- BTC
- ETH
- MATIC
Expand Down
2 changes: 1 addition & 1 deletion config/rebalance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exchangeStrategies:
- on: max
rebalance:
interval: 1d
baseCurrency: TWD
quoteCurrency: TWD
targetWeights:
BTC: 40%
ETH: 20%
Expand Down
12 changes: 6 additions & 6 deletions doc/strategy/marketcap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
46 changes: 23 additions & 23 deletions pkg/strategy/marketcap/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand All @@ -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")
}
}
Expand All @@ -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})
}
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -198,18 +198,18 @@ 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]
}

// normalize
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 {
Expand All @@ -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
}
Expand All @@ -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
}
16 changes: 8 additions & 8 deletions pkg/strategy/rebalance/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand All @@ -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]

Expand Down Expand Up @@ -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
}

0 comments on commit 9330b9f

Please sign in to comment.