Skip to content

Commit

Permalink
add balance type
Browse files Browse the repository at this point in the history
  • Loading branch information
narumiruna committed Mar 4, 2024
1 parent 449c0ab commit 75d856b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 32 deletions.
1 change: 1 addition & 0 deletions config/rebalance.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ exchangeStrategies:
maxAmount: 1_000 # max amount to buy or sell per order
orderType: LIMIT_MAKER # LIMIT, LIMIT_MAKER or MARKET
priceType: MAKER # LAST, MID, TAKER or MAKER
balanceType: TOTAL
dryRun: true
onStart: true
30 changes: 17 additions & 13 deletions pkg/strategy/rebalance/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ type Strategy struct {

Environment *bbgo.Environment

Schedule string `json:"schedule"`
QuoteCurrency string `json:"quoteCurrency"`
TargetWeights types.ValueMap `json:"targetWeights"`
Threshold fixedpoint.Value `json:"threshold"`
MaxAmount fixedpoint.Value `json:"maxAmount"` // max amount to buy or sell per order
OrderType types.OrderType `json:"orderType"`
PriceType types.PriceType `json:"priceType"`
DryRun bool `json:"dryRun"`
OnStart bool `json:"onStart"` // rebalance on start
Schedule string `json:"schedule"`
QuoteCurrency string `json:"quoteCurrency"`
TargetWeights types.ValueMap `json:"targetWeights"`
Threshold fixedpoint.Value `json:"threshold"`
MaxAmount fixedpoint.Value `json:"maxAmount"` // max amount to buy or sell per order
OrderType types.OrderType `json:"orderType"`
PriceType types.PriceType `json:"priceType"`
BalanceType types.BalanceType `json:"balanceType"`
DryRun bool `json:"dryRun"`
OnStart bool `json:"onStart"` // rebalance on start

symbols []string
markets map[string]types.Market
Expand All @@ -51,6 +52,10 @@ func (s *Strategy) Defaults() error {
if s.PriceType == "" {
s.PriceType = types.PriceTypeMaker
}

if s.BalanceType == "" {
s.BalanceType = types.BalanceTypeAvailable
}
return nil
}

Expand Down Expand Up @@ -216,7 +221,7 @@ func (s *Strategy) generateOrder(ctx context.Context) (*types.SubmitOrder, error
return nil, err
}

values := prices.Mul(toValueMap(balances))
values := prices.Mul(s.toValueMap(balances))
weights := values.Normalize()

for symbol, market := range s.markets {
Expand Down Expand Up @@ -286,11 +291,10 @@ func (s *Strategy) generateOrder(ctx context.Context) (*types.SubmitOrder, error
return nil, nil
}

func toValueMap(balances types.BalanceMap) types.ValueMap {
func (s *Strategy) toValueMap(balances types.BalanceMap) types.ValueMap {
m := make(types.ValueMap)
for _, b := range balances {
// m[b.Currency] = b.Net()
m[b.Currency] = b.Available
m[b.Currency] = s.BalanceType.Map(b)
}
return m
}
73 changes: 73 additions & 0 deletions pkg/types/balance_type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package types

import (
"encoding/json"
"strings"

"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/pkg/errors"
)

type BalanceType string

const (
BalanceTypeAvailable BalanceType = "AVAILABLE"
BalanceTypeLocked BalanceType = "LOCKED"
BalanceTypeBorrowed BalanceType = "BORROWED"
BalanceTypeInterest BalanceType = "INTEREST"
BalanceTypeNet BalanceType = "NET"
BalanceTypeTotal BalanceType = "TOTAL"
BalanceTypeDebt BalanceType = "DEBT"
)

var ErrInvalidBalanceType = errors.New("invalid balance type")

func (b BalanceType) Validate() error {
switch b {
case BalanceTypeAvailable:
case BalanceTypeLocked:
case BalanceTypeBorrowed:
case BalanceTypeInterest:
case BalanceTypeNet:
case BalanceTypeTotal:
case BalanceTypeDebt:
default:
return ErrInvalidBalanceType
}
return nil
}

func (p *BalanceType) UnmarshalJSON(data []byte) error {
var s string

if err := json.Unmarshal(data, &s); err != nil {
return err
}

*p = BalanceType(strings.ToUpper(s))
if err := p.Validate(); err != nil {
return err
}

return nil
}

func (b BalanceType) Map(balance Balance) fixedpoint.Value {
switch b {
case BalanceTypeAvailable:
return balance.Available
case BalanceTypeLocked:
return balance.Locked
case BalanceTypeBorrowed:
return balance.Borrowed
case BalanceTypeInterest:
return balance.Interest
case BalanceTypeNet:
return balance.Net()
case BalanceTypeTotal:
return balance.Total()
case BalanceTypeDebt:
return balance.Debt()
}
return fixedpoint.Zero
}
31 changes: 12 additions & 19 deletions pkg/types/price_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,18 @@ const (

var ErrInvalidPriceType = errors.New("invalid price type")

func StrToPriceType(s string) (price PriceType, err error) {
switch strings.ToLower(s) {
case "last":
price = PriceTypeLast
case "buy":
price = PriceTypeBuy
case "sell":
price = PriceTypeSell
case "mid":
price = PriceTypeMid
case "maker":
price = PriceTypeMaker
case "taker":
price = PriceTypeTaker
func (p PriceType) Validate() error {
switch p {
case PriceTypeLast:
case PriceTypeBuy:
case PriceTypeSell:
case PriceTypeMid:
case PriceTypeMaker:
case PriceTypeTaker:
default:
err = ErrInvalidPriceType
return ErrInvalidPriceType
}
return price, err
return nil
}

func (p *PriceType) UnmarshalJSON(data []byte) error {
Expand All @@ -48,12 +42,11 @@ func (p *PriceType) UnmarshalJSON(data []byte) error {
return err
}

t, err := StrToPriceType(s)
if err != nil {
*p = PriceType(strings.ToUpper(s))
if err := p.Validate(); err != nil {
return err
}

*p = t
return nil
}

Expand Down

0 comments on commit 75d856b

Please sign in to comment.