Merge pull request #727 from narumiruna/marketcap/init

strategy: add marketcap strategy
This commit is contained in:
Yo-An Lin 2022-06-17 01:13:21 +08:00 committed by GitHub
commit d3d685b4e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 481 additions and 149 deletions

20
config/marketcap.yaml Normal file
View File

@ -0,0 +1,20 @@
---
notifications:
slack:
defaultChannel: "bbgo"
errorChannel: "bbgo-error"
exchangeStrategies:
- on: max
marketcap:
interval: 1m
baseCurrency: TWD
baseWeight: 2%
targetCurrencies:
- BTC
- ETH
- MATIC
threshold: 2%
# max amount to buy or sell per order
maxAmount: 1_000
dryRun: true

View File

@ -15,6 +15,7 @@ import (
_ "github.com/c9s/bbgo/pkg/strategy/funding"
_ "github.com/c9s/bbgo/pkg/strategy/grid"
_ "github.com/c9s/bbgo/pkg/strategy/kline"
_ "github.com/c9s/bbgo/pkg/strategy/marketcap"
_ "github.com/c9s/bbgo/pkg/strategy/pivotshort"
_ "github.com/c9s/bbgo/pkg/strategy/pricealert"
_ "github.com/c9s/bbgo/pkg/strategy/pricedrop"

View File

@ -1,123 +0,0 @@
package fixedpoint
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_ValueMap_Eq(t *testing.T) {
m1 := ValueMap{
"A": NewFromFloat(3.0),
"B": NewFromFloat(4.0),
}
m2 := ValueMap{}
m3 := ValueMap{"A": NewFromFloat(5.0)}
m4 := ValueMap{
"A": NewFromFloat(6.0),
"B": NewFromFloat(7.0),
}
m5 := ValueMap{
"A": NewFromFloat(3.0),
"B": NewFromFloat(4.0),
}
assert.True(t, m1.Eq(m1))
assert.False(t, m1.Eq(m2))
assert.False(t, m1.Eq(m3))
assert.False(t, m1.Eq(m4))
assert.True(t, m1.Eq(m5))
}
func Test_ValueMap_Add(t *testing.T) {
m1 := ValueMap{
"A": NewFromFloat(3.0),
"B": NewFromFloat(4.0),
}
m2 := ValueMap{
"A": NewFromFloat(5.0),
"B": NewFromFloat(6.0),
}
m3 := ValueMap{
"A": NewFromFloat(8.0),
"B": NewFromFloat(10.0),
}
m4 := ValueMap{"A": NewFromFloat(8.0)}
assert.Equal(t, m3, m1.Add(m2))
assert.Panics(t, func() { m1.Add(m4) })
}
func Test_ValueMap_AddScalar(t *testing.T) {
x := NewFromFloat(5.0)
m1 := ValueMap{
"A": NewFromFloat(3.0),
"B": NewFromFloat(4.0),
}
m2 := ValueMap{
"A": NewFromFloat(3.0).Add(x),
"B": NewFromFloat(4.0).Add(x),
}
assert.Equal(t, m2, m1.AddScalar(x))
}
func Test_ValueMap_DivScalar(t *testing.T) {
x := NewFromFloat(5.0)
m1 := ValueMap{
"A": NewFromFloat(3.0),
"B": NewFromFloat(4.0),
}
m2 := ValueMap{
"A": NewFromFloat(3.0).Div(x),
"B": NewFromFloat(4.0).Div(x),
}
assert.Equal(t, m2, m1.DivScalar(x))
}
func Test_ValueMap_Sum(t *testing.T) {
m := ValueMap{
"A": NewFromFloat(3.0),
"B": NewFromFloat(4.0),
}
assert.Equal(t, NewFromFloat(7.0), m.Sum())
}
func Test_ValueMap_Normalize(t *testing.T) {
a := NewFromFloat(3.0)
b := NewFromFloat(4.0)
m := ValueMap{
"A": a,
"B": b,
}
n := ValueMap{
"A": a.Div(a.Add(b)),
"B": b.Div(a.Add(b)),
}
assert.True(t, m.Normalize().Eq(n))
}
func Test_ValueMap_Normalize_zero_sum(t *testing.T) {
m := ValueMap{
"A": Zero,
"B": Zero,
}
assert.Panics(t, func() { m.Normalize() })
}

View File

@ -0,0 +1,244 @@
package marketcap
import (
"context"
"fmt"
"os"
"github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/datasource/glassnode"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
const ID = "marketcap"
var log = logrus.WithField("strategy", ID)
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type Strategy struct {
Notifiability *bbgo.Notifiability
glassnode *glassnode.DataSource
Interval types.Interval `json:"interval"`
BaseCurrency string `json:"baseCurrency"`
BaseWeight fixedpoint.Value `json:"baseWeight"`
TargetCurrencies []string `json:"targetCurrencies"`
Threshold fixedpoint.Value `json:"threshold"`
Verbose bool `json:"verbose"`
DryRun bool `json:"dryRun"`
// max amount to buy or sell per order
MaxAmount fixedpoint.Value `json:"maxAmount"`
activeOrderBook *bbgo.ActiveOrderBook
}
func (s *Strategy) Initialize() error {
apiKey := os.Getenv("GLASSNODE_API_KEY")
s.glassnode = glassnode.New(apiKey)
return nil
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Validate() error {
if len(s.TargetCurrencies) == 0 {
return fmt.Errorf("taretCurrencies should not be empty")
}
for _, c := range s.TargetCurrencies {
if c == s.BaseCurrency {
return fmt.Errorf("targetCurrencies contain baseCurrency")
}
}
if s.Threshold.Sign() < 0 {
return fmt.Errorf("threshold should not less than 0")
}
if s.MaxAmount.Sign() < 0 {
return fmt.Errorf("maxAmount shoud not less than 0")
}
return nil
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
for _, symbol := range s.symbols() {
session.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: s.Interval})
}
}
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
s.activeOrderBook = bbgo.NewActiveOrderBook("")
s.activeOrderBook.BindStream(session.UserDataStream)
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
s.rebalance(ctx, orderExecutor, session)
})
return nil
}
func (s *Strategy) rebalance(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
if err := orderExecutor.CancelOrders(ctx, s.activeOrderBook.Orders()...); err != nil {
log.WithError(err).Error("failed to cancel orders")
}
submitOrders := s.generateSubmitOrders(ctx, session)
for _, submitOrder := range submitOrders {
log.Infof("generated submit order: %s", submitOrder.String())
}
if s.DryRun {
return
}
createdOrders, err := orderExecutor.SubmitOrders(ctx, submitOrders...)
if err != nil {
log.WithError(err).Error("failed to submit orders")
return
}
s.activeOrderBook.Add(createdOrders...)
}
func (s *Strategy) generateSubmitOrders(ctx context.Context, session *bbgo.ExchangeSession) (submitOrders []types.SubmitOrder) {
targetWeights := s.getTargetWeights(ctx)
prices := s.prices(ctx, session)
marketValues := prices.Mul(s.quantities(session))
currentWeights := marketValues.Normalize()
for currency, targetWeight := range targetWeights {
symbol := currency + s.BaseCurrency
currentWeight := currentWeights[currency]
currentPrice := prices[currency]
log.Infof("%s price: %v, current weight: %v, target weight: %v",
symbol,
currentPrice,
currentWeight,
targetWeight)
// calculate the difference between current weight and target weight
// if the difference is less than threshold, then we will not create the order
weightDifference := targetWeight.Sub(currentWeight)
if weightDifference.Abs().Compare(s.Threshold) < 0 {
log.Infof("%s weight distance |%v - %v| = |%v| less than the threshold: %v",
symbol,
currentWeight,
targetWeight,
weightDifference,
s.Threshold)
continue
}
quantity := weightDifference.Mul(marketValues.Sum()).Div(currentPrice)
side := types.SideTypeBuy
if quantity.Sign() < 0 {
side = types.SideTypeSell
quantity = quantity.Abs()
}
if s.MaxAmount.Sign() > 0 {
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, currentPrice, s.MaxAmount)
log.Infof("adjust the quantity %v (%s %s @ %v) by max amount %v",
quantity,
symbol,
side.String(),
currentPrice,
s.MaxAmount)
}
order := types.SubmitOrder{
Symbol: symbol,
Side: side,
Type: types.OrderTypeLimit,
Quantity: quantity,
Price: currentPrice,
}
submitOrders = append(submitOrders, order)
}
return submitOrders
}
func (s *Strategy) getTargetWeights(ctx context.Context) types.ValueMap {
m := types.FloatMap{}
// get market cap values
for _, currency := range s.TargetCurrencies {
marketCap, err := s.glassnode.QueryMarketCapInUSD(ctx, currency)
if err != nil {
log.WithError(err).Error("failed to query market cap")
return nil
}
m[currency] = marketCap
}
// normalize
m = m.Normalize()
// rescale by 1 - baseWeight
m = m.MulScalar(1.0 - s.BaseWeight.Float64())
// append base weight
m[s.BaseCurrency] = s.BaseWeight.Float64()
// convert to types.ValueMap
targetWeights := types.ValueMap{}
for currency, weight := range m {
targetWeights[currency] = fixedpoint.NewFromFloat(weight)
}
return targetWeights
}
func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) types.ValueMap {
tickers, err := session.Exchange.QueryTickers(ctx, s.symbols()...)
if err != nil {
log.WithError(err).Error("failed to query tickers")
return nil
}
prices := types.ValueMap{}
for _, currency := range s.TargetCurrencies {
prices[currency] = tickers[currency+s.BaseCurrency].Last
}
// append base currency price
prices[s.BaseCurrency] = fixedpoint.One
return prices
}
func (s *Strategy) quantities(session *bbgo.ExchangeSession) types.ValueMap {
balances := session.Account.Balances()
quantities := types.ValueMap{}
for _, currency := range s.currencies() {
quantities[currency] = balances[currency].Total()
}
return quantities
}
func (s *Strategy) symbols() (symbols []string) {
for _, currency := range s.TargetCurrencies {
symbols = append(symbols, currency+s.BaseCurrency)
}
return symbols
}
func (s *Strategy) currencies() (currencies []string) {
currencies = append(currencies, s.TargetCurrencies...)
currencies = append(currencies, s.BaseCurrency)
return currencies
}

View File

@ -24,7 +24,7 @@ type Strategy struct {
Interval types.Interval `json:"interval"`
BaseCurrency string `json:"baseCurrency"`
TargetWeights fixedpoint.ValueMap `json:"targetWeights"`
TargetWeights types.ValueMap `json:"targetWeights"`
Threshold fixedpoint.Value `json:"threshold"`
DryRun bool `json:"dryRun"`
// max amount to buy or sell per order
@ -88,6 +88,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
return nil
}
func (s *Strategy) rebalance(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) {
// cancel active orders before rebalance
if err := session.Exchange.CancelOrders(ctx, s.activeOrderBook.Orders()...); err != nil {
@ -112,8 +113,8 @@ func (s *Strategy) rebalance(ctx context.Context, orderExecutor bbgo.OrderExecut
s.activeOrderBook.Add(createdOrders...)
}
func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) fixedpoint.ValueMap {
m := make(fixedpoint.ValueMap)
func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) types.ValueMap {
m := make(types.ValueMap)
tickers, err := session.Exchange.QueryTickers(ctx, s.symbols()...)
if err != nil {
@ -132,8 +133,8 @@ func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) fi
return m
}
func (s *Strategy) quantities(session *bbgo.ExchangeSession) fixedpoint.ValueMap {
m := make(fixedpoint.ValueMap)
func (s *Strategy) quantities(session *bbgo.ExchangeSession) types.ValueMap {
m := make(types.ValueMap)
balances := session.GetAccount().Balances()
for currency := range s.TargetWeights {

42
pkg/types/float_map.go Normal file
View File

@ -0,0 +1,42 @@
package types
type FloatMap map[string]float64
func (m FloatMap) Sum() float64 {
sum := 0.0
for _, v := range m {
sum += v
}
return sum
}
func (m FloatMap) MulScalar(x float64) FloatMap {
o := FloatMap{}
for k, v := range m {
o[k] = v * x
}
return o
}
func (m FloatMap) DivScalar(x float64) FloatMap {
o := FloatMap{}
for k, v := range m {
o[k] = v / x
}
return o
}
func (m FloatMap) Normalize() FloatMap {
sum := m.Sum()
if sum == 0 {
panic("zero sum")
}
o := FloatMap{}
for k, v := range m {
o[k] = v / sum
}
return o
}

View File

@ -1,6 +1,8 @@
package fixedpoint
package types
type ValueMap map[string]Value
import "github.com/c9s/bbgo/pkg/fixedpoint"
type ValueMap map[string]fixedpoint.Value
func (m ValueMap) Eq(n ValueMap) bool {
if len(m) != len(n) {
@ -28,8 +30,13 @@ func (m ValueMap) Add(n ValueMap) ValueMap {
o := ValueMap{}
for k, v := range m {
o[k] = v.Add(n[k])
for m_k, m_v := range m {
n_v, ok := n[m_k]
if !ok {
panic("key not found")
}
o[m_k] = m_v.Add(n_v)
}
return o
@ -42,8 +49,13 @@ func (m ValueMap) Sub(n ValueMap) ValueMap {
o := ValueMap{}
for k, v := range m {
o[k] = v.Sub(n[k])
for m_k, m_v := range m {
n_v, ok := n[m_k]
if !ok {
panic("key not found")
}
o[m_k] = m_v.Sub(n_v)
}
return o
@ -56,8 +68,13 @@ func (m ValueMap) Mul(n ValueMap) ValueMap {
o := ValueMap{}
for k, v := range m {
o[k] = v.Mul(n[k])
for m_k, m_v := range m {
n_v, ok := n[m_k]
if !ok {
panic("key not found")
}
o[m_k] = m_v.Mul(n_v)
}
return o
@ -70,14 +87,19 @@ func (m ValueMap) Div(n ValueMap) ValueMap {
o := ValueMap{}
for k, v := range m {
o[k] = v.Div(n[k])
for m_k, m_v := range m {
n_v, ok := n[m_k]
if !ok {
panic("key not found")
}
o[m_k] = m_v.Div(n_v)
}
return o
}
func (m ValueMap) AddScalar(x Value) ValueMap {
func (m ValueMap) AddScalar(x fixedpoint.Value) ValueMap {
o := ValueMap{}
for k, v := range m {
@ -87,7 +109,7 @@ func (m ValueMap) AddScalar(x Value) ValueMap {
return o
}
func (m ValueMap) SubScalar(x Value) ValueMap {
func (m ValueMap) SubScalar(x fixedpoint.Value) ValueMap {
o := ValueMap{}
for k, v := range m {
@ -97,7 +119,7 @@ func (m ValueMap) SubScalar(x Value) ValueMap {
return o
}
func (m ValueMap) MulScalar(x Value) ValueMap {
func (m ValueMap) MulScalar(x fixedpoint.Value) ValueMap {
o := ValueMap{}
for k, v := range m {
@ -107,7 +129,7 @@ func (m ValueMap) MulScalar(x Value) ValueMap {
return o
}
func (m ValueMap) DivScalar(x Value) ValueMap {
func (m ValueMap) DivScalar(x fixedpoint.Value) ValueMap {
o := ValueMap{}
for k, v := range m {
@ -117,8 +139,8 @@ func (m ValueMap) DivScalar(x Value) ValueMap {
return o
}
func (m ValueMap) Sum() Value {
var sum Value
func (m ValueMap) Sum() fixedpoint.Value {
var sum fixedpoint.Value
for _, v := range m {
sum = sum.Add(v)
}
@ -127,7 +149,7 @@ func (m ValueMap) Sum() Value {
func (m ValueMap) Normalize() ValueMap {
sum := m.Sum()
if sum.Eq(Zero) {
if sum.Eq(fixedpoint.Zero) {
panic("zero sum")
}

125
pkg/types/value_map_test.go Normal file
View File

@ -0,0 +1,125 @@
package types
import (
"testing"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/stretchr/testify/assert"
)
func Test_ValueMap_Eq(t *testing.T) {
m1 := ValueMap{
"A": fixedpoint.NewFromFloat(3.0),
"B": fixedpoint.NewFromFloat(4.0),
}
m2 := ValueMap{}
m3 := ValueMap{"A": fixedpoint.NewFromFloat(5.0)}
m4 := ValueMap{
"A": fixedpoint.NewFromFloat(6.0),
"B": fixedpoint.NewFromFloat(7.0),
}
m5 := ValueMap{
"A": fixedpoint.NewFromFloat(3.0),
"B": fixedpoint.NewFromFloat(4.0),
}
assert.True(t, m1.Eq(m1))
assert.False(t, m1.Eq(m2))
assert.False(t, m1.Eq(m3))
assert.False(t, m1.Eq(m4))
assert.True(t, m1.Eq(m5))
}
func Test_ValueMap_Add(t *testing.T) {
m1 := ValueMap{
"A": fixedpoint.NewFromFloat(3.0),
"B": fixedpoint.NewFromFloat(4.0),
}
m2 := ValueMap{
"A": fixedpoint.NewFromFloat(5.0),
"B": fixedpoint.NewFromFloat(6.0),
}
m3 := ValueMap{
"A": fixedpoint.NewFromFloat(8.0),
"B": fixedpoint.NewFromFloat(10.0),
}
m4 := ValueMap{"A": fixedpoint.NewFromFloat(8.0)}
assert.Equal(t, m3, m1.Add(m2))
assert.Panics(t, func() { m1.Add(m4) })
}
func Test_ValueMap_AddScalar(t *testing.T) {
x := fixedpoint.NewFromFloat(5.0)
m1 := ValueMap{
"A": fixedpoint.NewFromFloat(3.0),
"B": fixedpoint.NewFromFloat(4.0),
}
m2 := ValueMap{
"A": fixedpoint.NewFromFloat(3.0).Add(x),
"B": fixedpoint.NewFromFloat(4.0).Add(x),
}
assert.Equal(t, m2, m1.AddScalar(x))
}
func Test_ValueMap_DivScalar(t *testing.T) {
x := fixedpoint.NewFromFloat(5.0)
m1 := ValueMap{
"A": fixedpoint.NewFromFloat(3.0),
"B": fixedpoint.NewFromFloat(4.0),
}
m2 := ValueMap{
"A": fixedpoint.NewFromFloat(3.0).Div(x),
"B": fixedpoint.NewFromFloat(4.0).Div(x),
}
assert.Equal(t, m2, m1.DivScalar(x))
}
func Test_ValueMap_Sum(t *testing.T) {
m := ValueMap{
"A": fixedpoint.NewFromFloat(3.0),
"B": fixedpoint.NewFromFloat(4.0),
}
assert.Equal(t, fixedpoint.NewFromFloat(7.0), m.Sum())
}
func Test_ValueMap_Normalize(t *testing.T) {
a := fixedpoint.NewFromFloat(3.0)
b := fixedpoint.NewFromFloat(4.0)
c := a.Add(b)
m := ValueMap{
"A": a,
"B": b,
}
n := ValueMap{
"A": a.Div(c),
"B": b.Div(c),
}
assert.True(t, m.Normalize().Eq(n))
}
func Test_ValueMap_Normalize_zero_sum(t *testing.T) {
m := ValueMap{
"A": fixedpoint.Zero,
"B": fixedpoint.Zero,
}
assert.Panics(t, func() { m.Normalize() })
}