mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
change variable names
This commit is contained in:
parent
1d9cc54ea7
commit
9330b9fde5
|
@ -13,9 +13,9 @@ exchangeStrategies:
|
||||||
- on: max
|
- on: max
|
||||||
marketcap:
|
marketcap:
|
||||||
interval: 1m
|
interval: 1m
|
||||||
baseCurrency: TWD
|
quoteCurrency: TWD
|
||||||
baseWeight: 0%
|
quoteCurrencyWeight: 0%
|
||||||
targetCurrencies:
|
baseCurrencies:
|
||||||
- BTC
|
- BTC
|
||||||
- ETH
|
- ETH
|
||||||
- MATIC
|
- MATIC
|
||||||
|
|
|
@ -12,7 +12,7 @@ exchangeStrategies:
|
||||||
- on: max
|
- on: max
|
||||||
rebalance:
|
rebalance:
|
||||||
interval: 1d
|
interval: 1d
|
||||||
baseCurrency: TWD
|
quoteCurrency: TWD
|
||||||
targetWeights:
|
targetWeights:
|
||||||
BTC: 40%
|
BTC: 40%
|
||||||
ETH: 20%
|
ETH: 20%
|
||||||
|
|
|
@ -10,18 +10,18 @@ Setup your `COINMARKETCAP_API_KEY` in your environment variables.
|
||||||
|
|
||||||
- `interval`
|
- `interval`
|
||||||
- The interval to rebalance your portfolio, e.g., `5m`, `1h`
|
- The interval to rebalance your portfolio, e.g., `5m`, `1h`
|
||||||
- `baseCurrency`
|
- `quoteCurrency`
|
||||||
- The base currency of your portfolio, e.g., `USDT`, `TWD`.
|
- The quote currency of your portfolio, e.g., `USDT`, `TWD`.
|
||||||
- `baseWeight`
|
- `quoteCurrencyWeight`
|
||||||
- The weight of the base currency in your portfolio. The rest of the weight will be distributed to other currencies by market capitalization.
|
- The weight of the quote currency in your portfolio. The rest of the weight will be distributed to other currencies by market capitalization.
|
||||||
- `targetCurrencies`
|
- `baseCurrencies`
|
||||||
- A list of currencies you want to hold in your portfolio.
|
- A list of currencies you want to hold in your portfolio.
|
||||||
- `threshold`
|
- `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%`.
|
- 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`
|
- `dryRun`
|
||||||
- If `true`, then the strategy will not place orders.
|
- If `true`, then the strategy will not place orders.
|
||||||
- `maxAmount`
|
- `maxAmount`
|
||||||
- The maximum amount of each order in base currency.
|
- The maximum amount of each order in quote currency.
|
||||||
|
|
||||||
#### Examples
|
#### Examples
|
||||||
|
|
||||||
|
|
|
@ -26,12 +26,12 @@ type Strategy struct {
|
||||||
datasource *coinmarketcap.DataSource
|
datasource *coinmarketcap.DataSource
|
||||||
|
|
||||||
// interval to rebalance the portfolio
|
// interval to rebalance the portfolio
|
||||||
Interval types.Interval `json:"interval"`
|
Interval types.Interval `json:"interval"`
|
||||||
BaseCurrency string `json:"baseCurrency"`
|
QuoteCurrency string `json:"quoteCurrency"`
|
||||||
BaseWeight fixedpoint.Value `json:"baseWeight"`
|
QuoteCurrencyWeight fixedpoint.Value `json:"quoteCurrencyWeight"`
|
||||||
TargetCurrencies []string `json:"targetCurrencies"`
|
BaseCurrencies []string `json:"baseCurrencies"`
|
||||||
Threshold fixedpoint.Value `json:"threshold"`
|
Threshold fixedpoint.Value `json:"threshold"`
|
||||||
DryRun bool `json:"dryRun"`
|
DryRun bool `json:"dryRun"`
|
||||||
// max amount to buy or sell per order
|
// max amount to buy or sell per order
|
||||||
MaxAmount fixedpoint.Value `json:"maxAmount"`
|
MaxAmount fixedpoint.Value `json:"maxAmount"`
|
||||||
// interval to query marketcap data from coinmarketcap
|
// interval to query marketcap data from coinmarketcap
|
||||||
|
@ -47,7 +47,7 @@ func (s *Strategy) Initialize() error {
|
||||||
s.datasource = coinmarketcap.New(apiKey)
|
s.datasource = coinmarketcap.New(apiKey)
|
||||||
|
|
||||||
// select one symbol to subscribe
|
// select one symbol to subscribe
|
||||||
s.subscribeSymbol = s.TargetCurrencies[0] + s.BaseCurrency
|
s.subscribeSymbol = s.BaseCurrencies[0] + s.QuoteCurrency
|
||||||
|
|
||||||
s.activeOrderBook = bbgo.NewActiveOrderBook("")
|
s.activeOrderBook = bbgo.NewActiveOrderBook("")
|
||||||
s.targetWeights = types.ValueMap{}
|
s.targetWeights = types.ValueMap{}
|
||||||
|
@ -59,12 +59,12 @@ func (s *Strategy) ID() string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) Validate() error {
|
func (s *Strategy) Validate() error {
|
||||||
if len(s.TargetCurrencies) == 0 {
|
if len(s.BaseCurrencies) == 0 {
|
||||||
return fmt.Errorf("taretCurrencies should not be empty")
|
return fmt.Errorf("taretCurrencies should not be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range s.TargetCurrencies {
|
for _, c := range s.BaseCurrencies {
|
||||||
if c == s.BaseCurrency {
|
if c == s.QuoteCurrency {
|
||||||
return fmt.Errorf("targetCurrencies contain baseCurrency")
|
return fmt.Errorf("targetCurrencies contain baseCurrency")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,7 +81,7 @@ func (s *Strategy) Validate() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
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.Interval})
|
||||||
session.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: s.QueryInterval})
|
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()
|
currentWeights := marketValues.Normalize()
|
||||||
|
|
||||||
for currency, targetWeight := range s.targetWeights {
|
for currency, targetWeight := range s.targetWeights {
|
||||||
if currency == s.BaseCurrency {
|
if currency == s.QuoteCurrency {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
symbol := currency + s.BaseCurrency
|
symbol := currency + s.QuoteCurrency
|
||||||
currentWeight := currentWeights[currency]
|
currentWeight := currentWeights[currency]
|
||||||
currentPrice := prices[currency]
|
currentPrice := prices[currency]
|
||||||
|
|
||||||
|
@ -198,7 +198,7 @@ func (s *Strategy) updateTargetWeights(ctx context.Context) {
|
||||||
log.WithError(err).Error("failed to query market cap")
|
log.WithError(err).Error("failed to query market cap")
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, currency := range s.TargetCurrencies {
|
for _, currency := range s.BaseCurrencies {
|
||||||
m[currency] = marketcaps[currency]
|
m[currency] = marketcaps[currency]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -206,10 +206,10 @@ func (s *Strategy) updateTargetWeights(ctx context.Context) {
|
||||||
m = m.Normalize()
|
m = m.Normalize()
|
||||||
|
|
||||||
// rescale by 1 - baseWeight
|
// rescale by 1 - baseWeight
|
||||||
m = m.MulScalar(1.0 - s.BaseWeight.Float64())
|
m = m.MulScalar(1.0 - s.QuoteCurrencyWeight.Float64())
|
||||||
|
|
||||||
// append base weight
|
// append base weight
|
||||||
m[s.BaseCurrency] = s.BaseWeight.Float64()
|
m[s.QuoteCurrency] = s.QuoteCurrencyWeight.Float64()
|
||||||
|
|
||||||
// convert to types.ValueMap
|
// convert to types.ValueMap
|
||||||
for currency, weight := range m {
|
for currency, weight := range m {
|
||||||
|
@ -227,12 +227,12 @@ func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) ty
|
||||||
}
|
}
|
||||||
|
|
||||||
prices := types.ValueMap{}
|
prices := types.ValueMap{}
|
||||||
for _, currency := range s.TargetCurrencies {
|
for _, currency := range s.BaseCurrencies {
|
||||||
prices[currency] = tickers[currency+s.BaseCurrency].Last
|
prices[currency] = tickers[currency+s.QuoteCurrency].Last
|
||||||
}
|
}
|
||||||
|
|
||||||
// append base currency price
|
// append base currency price
|
||||||
prices[s.BaseCurrency] = fixedpoint.One
|
prices[s.QuoteCurrency] = fixedpoint.One
|
||||||
|
|
||||||
return prices
|
return prices
|
||||||
}
|
}
|
||||||
|
@ -249,14 +249,14 @@ func (s *Strategy) quantities(session *bbgo.ExchangeSession) types.ValueMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) symbols() (symbols []string) {
|
func (s *Strategy) symbols() (symbols []string) {
|
||||||
for _, currency := range s.TargetCurrencies {
|
for _, currency := range s.BaseCurrencies {
|
||||||
symbols = append(symbols, currency+s.BaseCurrency)
|
symbols = append(symbols, currency+s.QuoteCurrency)
|
||||||
}
|
}
|
||||||
return symbols
|
return symbols
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) currencies() (currencies []string) {
|
func (s *Strategy) currencies() (currencies []string) {
|
||||||
currencies = append(currencies, s.TargetCurrencies...)
|
currencies = append(currencies, s.BaseCurrencies...)
|
||||||
currencies = append(currencies, s.BaseCurrency)
|
currencies = append(currencies, s.QuoteCurrency)
|
||||||
return currencies
|
return currencies
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ func init() {
|
||||||
|
|
||||||
type Strategy struct {
|
type Strategy struct {
|
||||||
Interval types.Interval `json:"interval"`
|
Interval types.Interval `json:"interval"`
|
||||||
BaseCurrency string `json:"baseCurrency"`
|
QuoteCurrency string `json:"quoteCurrency"`
|
||||||
TargetWeights types.ValueMap `json:"targetWeights"`
|
TargetWeights types.ValueMap `json:"targetWeights"`
|
||||||
Threshold fixedpoint.Value `json:"threshold"`
|
Threshold fixedpoint.Value `json:"threshold"`
|
||||||
DryRun bool `json:"dryRun"`
|
DryRun bool `json:"dryRun"`
|
||||||
|
@ -121,11 +121,11 @@ func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) ty
|
||||||
}
|
}
|
||||||
|
|
||||||
for currency := range s.TargetWeights {
|
for currency := range s.TargetWeights {
|
||||||
if currency == s.BaseCurrency {
|
if currency == s.QuoteCurrency {
|
||||||
m[s.BaseCurrency] = fixedpoint.One
|
m[s.QuoteCurrency] = fixedpoint.One
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
m[currency] = tickers[currency+s.BaseCurrency].Last
|
m[currency] = tickers[currency+s.QuoteCurrency].Last
|
||||||
}
|
}
|
||||||
|
|
||||||
return m
|
return m
|
||||||
|
@ -148,11 +148,11 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context, session *bbgo.Excha
|
||||||
currentWeights := marketValues.Normalize()
|
currentWeights := marketValues.Normalize()
|
||||||
|
|
||||||
for currency, targetWeight := range s.TargetWeights {
|
for currency, targetWeight := range s.TargetWeights {
|
||||||
if currency == s.BaseCurrency {
|
if currency == s.QuoteCurrency {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
symbol := currency + s.BaseCurrency
|
symbol := currency + s.QuoteCurrency
|
||||||
currentWeight := currentWeights[currency]
|
currentWeight := currentWeights[currency]
|
||||||
currentPrice := prices[currency]
|
currentPrice := prices[currency]
|
||||||
|
|
||||||
|
@ -211,10 +211,10 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context, session *bbgo.Excha
|
||||||
|
|
||||||
func (s *Strategy) symbols() (symbols []string) {
|
func (s *Strategy) symbols() (symbols []string) {
|
||||||
for currency := range s.TargetWeights {
|
for currency := range s.TargetWeights {
|
||||||
if currency == s.BaseCurrency {
|
if currency == s.QuoteCurrency {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
symbols = append(symbols, currency+s.BaseCurrency)
|
symbols = append(symbols, currency+s.QuoteCurrency)
|
||||||
}
|
}
|
||||||
return symbols
|
return symbols
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user