From c13c33dcbb4edcbc03d3c307d69fe2df16fe9c22 Mon Sep 17 00:00:00 2001 From: c9s Date: Sat, 5 Oct 2024 14:30:19 +0800 Subject: [PATCH] bbgo: rename market value to totalValue --- pkg/bbgo/account_value_calc.go | 21 ++++++++++++++++----- pkg/bbgo/account_value_calc_test.go | 2 +- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pkg/bbgo/account_value_calc.go b/pkg/bbgo/account_value_calc.go index 6cd6f9fd0..a13e565ff 100644 --- a/pkg/bbgo/account_value_calc.go +++ b/pkg/bbgo/account_value_calc.go @@ -70,7 +70,7 @@ func (c *AccountValueCalculator) DebtValue() fixedpoint.Value { }) } -func (c *AccountValueCalculator) MarketValue() fixedpoint.Value { +func (c *AccountValueCalculator) TotalValue() fixedpoint.Value { balances := c.session.Account.Balances() return totalValueInQuote(balances, c.priceSolver, c.quoteCurrency, func( prev fixedpoint.Value, b types.Balance, price fixedpoint.Value, @@ -141,15 +141,26 @@ func (c *AccountValueCalculator) AvailableQuote() (fixedpoint.Value, error) { // MarginLevel calculates the margin level from the asset market value and the debt value // See https://www.binance.com/en/support/faq/360030493931 -func (c *AccountValueCalculator) MarginLevel() (fixedpoint.Value, error) { - marketValue := c.MarketValue() +func (c *AccountValueCalculator) MarginLevel() fixedpoint.Value { + marketValue := c.TotalValue() debtValue := c.DebtValue() if marketValue.IsZero() || debtValue.IsZero() { - return fixedpoint.NewFromFloat(999.0), nil + return fixedpoint.NewFromFloat(999.0) } - return marketValue.Div(debtValue), nil + return marketValue.Div(debtValue) +} + +func (c *AccountValueCalculator) Leverage() fixedpoint.Value { + netValue := c.NetValue() + debtValue := c.DebtValue() + + if debtValue.IsZero() || netValue.IsZero() { + return fixedpoint.One + } + + return debtValue.Div(netValue) } func aggregateUsdNetValue(balances types.BalanceMap) fixedpoint.Value { diff --git a/pkg/bbgo/account_value_calc_test.go b/pkg/bbgo/account_value_calc_test.go index f9c06a43f..be1fce1a1 100644 --- a/pkg/bbgo/account_value_calc_test.go +++ b/pkg/bbgo/account_value_calc_test.go @@ -133,7 +133,7 @@ func TestNewAccountValueCalculator_MarginLevel(t *testing.T) { cal := NewAccountValueCalculator(session, priceSolver, "USDT") assert.NotNil(t, cal) - marginLevel, err := cal.MarginLevel() + marginLevel := cal.MarginLevel() assert.NoError(t, err) // expected (21000 / 19000 * 1.003)