types: move valuemap and floatmap to types

This commit is contained in:
なるみ 2022-06-16 16:30:55 +08:00
parent 5799497a09
commit 50fbf0727e
6 changed files with 182 additions and 159 deletions

View File

@ -1,124 +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)
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": Zero,
"B": Zero,
}
assert.Panics(t, func() { m.Normalize() })
}

View File

@ -170,8 +170,8 @@ func (s *Strategy) generateSubmitOrders(ctx context.Context, session *bbgo.Excha
return submitOrders return submitOrders
} }
func (s *Strategy) getTargetWeights(ctx context.Context) fixedpoint.ValueMap { func (s *Strategy) getTargetWeights(ctx context.Context) types.ValueMap {
m := FloatMap{} m := types.FloatMap{}
// get market cap values // get market cap values
for _, currency := range s.TargetCurrencies { for _, currency := range s.TargetCurrencies {
@ -192,8 +192,8 @@ func (s *Strategy) getTargetWeights(ctx context.Context) fixedpoint.ValueMap {
// append base weight // append base weight
m[s.BaseCurrency] = s.BaseWeight.Float64() m[s.BaseCurrency] = s.BaseWeight.Float64()
// convert to fixedpoint.ValueMap // convert to types.ValueMap
targetWeights := fixedpoint.ValueMap{} targetWeights := types.ValueMap{}
for currency, weight := range m { for currency, weight := range m {
targetWeights[currency] = fixedpoint.NewFromFloat(weight) targetWeights[currency] = fixedpoint.NewFromFloat(weight)
} }
@ -201,14 +201,14 @@ func (s *Strategy) getTargetWeights(ctx context.Context) fixedpoint.ValueMap {
return targetWeights return targetWeights
} }
func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) fixedpoint.ValueMap { func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) types.ValueMap {
tickers, err := session.Exchange.QueryTickers(ctx, s.symbols()...) tickers, err := session.Exchange.QueryTickers(ctx, s.symbols()...)
if err != nil { if err != nil {
log.WithError(err).Error("failed to query tickers") log.WithError(err).Error("failed to query tickers")
return nil return nil
} }
prices := fixedpoint.ValueMap{} prices := types.ValueMap{}
for _, currency := range s.TargetCurrencies { for _, currency := range s.TargetCurrencies {
prices[currency] = tickers[currency+s.BaseCurrency].Last prices[currency] = tickers[currency+s.BaseCurrency].Last
} }
@ -219,10 +219,10 @@ func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) fi
return prices return prices
} }
func (s *Strategy) quantities(session *bbgo.ExchangeSession) fixedpoint.ValueMap { func (s *Strategy) quantities(session *bbgo.ExchangeSession) types.ValueMap {
balances := session.Account.Balances() balances := session.Account.Balances()
quantities := fixedpoint.ValueMap{} quantities := types.ValueMap{}
for _, currency := range s.currencies() { for _, currency := range s.currencies() {
quantities[currency] = balances[currency].Total() quantities[currency] = balances[currency].Total()
} }

View File

@ -22,11 +22,11 @@ func init() {
type Strategy struct { type Strategy struct {
Notifiability *bbgo.Notifiability Notifiability *bbgo.Notifiability
Interval types.Interval `json:"interval"` Interval types.Interval `json:"interval"`
BaseCurrency string `json:"baseCurrency"` BaseCurrency string `json:"baseCurrency"`
TargetWeights fixedpoint.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"`
// 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"`
@ -113,8 +113,8 @@ func (s *Strategy) rebalance(ctx context.Context, orderExecutor bbgo.OrderExecut
s.activeOrderBook.Add(createdOrders...) s.activeOrderBook.Add(createdOrders...)
} }
func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) fixedpoint.ValueMap { func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) types.ValueMap {
m := make(fixedpoint.ValueMap) m := make(types.ValueMap)
tickers, err := session.Exchange.QueryTickers(ctx, s.symbols()...) tickers, err := session.Exchange.QueryTickers(ctx, s.symbols()...)
if err != nil { if err != nil {
@ -133,8 +133,8 @@ func (s *Strategy) prices(ctx context.Context, session *bbgo.ExchangeSession) fi
return m return m
} }
func (s *Strategy) quantities(session *bbgo.ExchangeSession) fixedpoint.ValueMap { func (s *Strategy) quantities(session *bbgo.ExchangeSession) types.ValueMap {
m := make(fixedpoint.ValueMap) m := make(types.ValueMap)
balances := session.GetAccount().Balances() balances := session.GetAccount().Balances()
for currency := range s.TargetWeights { for currency := range s.TargetWeights {

View File

@ -1,4 +1,4 @@
package marketcap package types
type FloatMap map[string]float64 type FloatMap map[string]float64

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 { func (m ValueMap) Eq(n ValueMap) bool {
if len(m) != len(n) { if len(m) != len(n) {
@ -28,8 +30,13 @@ func (m ValueMap) Add(n ValueMap) ValueMap {
o := ValueMap{} o := ValueMap{}
for k, v := range m { for m_k, m_v := range m {
o[k] = v.Add(n[k]) n_v, ok := n[m_k]
if !ok {
panic("key not found")
}
o[m_k] = m_v.Add(n_v)
} }
return o return o
@ -42,8 +49,13 @@ func (m ValueMap) Sub(n ValueMap) ValueMap {
o := ValueMap{} o := ValueMap{}
for k, v := range m { for m_k, m_v := range m {
o[k] = v.Sub(n[k]) n_v, ok := n[m_k]
if !ok {
panic("key not found")
}
o[m_k] = m_v.Sub(n_v)
} }
return o return o
@ -56,8 +68,13 @@ func (m ValueMap) Mul(n ValueMap) ValueMap {
o := ValueMap{} o := ValueMap{}
for k, v := range m { for m_k, m_v := range m {
o[k] = v.Mul(n[k]) n_v, ok := n[m_k]
if !ok {
panic("key not found")
}
o[m_k] = m_v.Mul(n_v)
} }
return o return o
@ -70,14 +87,19 @@ func (m ValueMap) Div(n ValueMap) ValueMap {
o := ValueMap{} o := ValueMap{}
for k, v := range m { for m_k, m_v := range m {
o[k] = v.Div(n[k]) n_v, ok := n[m_k]
if !ok {
panic("key not found")
}
o[m_k] = m_v.Div(n_v)
} }
return o return o
} }
func (m ValueMap) AddScalar(x Value) ValueMap { func (m ValueMap) AddScalar(x fixedpoint.Value) ValueMap {
o := ValueMap{} o := ValueMap{}
for k, v := range m { for k, v := range m {
@ -87,7 +109,7 @@ func (m ValueMap) AddScalar(x Value) ValueMap {
return o return o
} }
func (m ValueMap) SubScalar(x Value) ValueMap { func (m ValueMap) SubScalar(x fixedpoint.Value) ValueMap {
o := ValueMap{} o := ValueMap{}
for k, v := range m { for k, v := range m {
@ -97,7 +119,7 @@ func (m ValueMap) SubScalar(x Value) ValueMap {
return o return o
} }
func (m ValueMap) MulScalar(x Value) ValueMap { func (m ValueMap) MulScalar(x fixedpoint.Value) ValueMap {
o := ValueMap{} o := ValueMap{}
for k, v := range m { for k, v := range m {
@ -107,7 +129,7 @@ func (m ValueMap) MulScalar(x Value) ValueMap {
return o return o
} }
func (m ValueMap) DivScalar(x Value) ValueMap { func (m ValueMap) DivScalar(x fixedpoint.Value) ValueMap {
o := ValueMap{} o := ValueMap{}
for k, v := range m { for k, v := range m {
@ -117,8 +139,8 @@ func (m ValueMap) DivScalar(x Value) ValueMap {
return o return o
} }
func (m ValueMap) Sum() Value { func (m ValueMap) Sum() fixedpoint.Value {
var sum Value var sum fixedpoint.Value
for _, v := range m { for _, v := range m {
sum = sum.Add(v) sum = sum.Add(v)
} }
@ -127,7 +149,7 @@ func (m ValueMap) Sum() Value {
func (m ValueMap) Normalize() ValueMap { func (m ValueMap) Normalize() ValueMap {
sum := m.Sum() sum := m.Sum()
if sum.Eq(Zero) { if sum.Eq(fixedpoint.Zero) {
panic("zero sum") 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() })
}