mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
risk: add margin level calculator
This commit is contained in:
parent
b53da177c2
commit
a9f9fc4e5e
|
@ -140,6 +140,22 @@ func (c *AccountValueCalculator) NetValue(ctx context.Context) (fixedpoint.Value
|
||||||
return accountValue, nil
|
return accountValue, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *AccountValueCalculator) MarginLevel(ctx context.Context) (fixedpoint.Value, error) {
|
||||||
|
marginLevel := fixedpoint.Zero
|
||||||
|
marketValue, err := c.MarketValue(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return marginLevel, err
|
||||||
|
}
|
||||||
|
|
||||||
|
debtValue, err := c.DebtValue(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return marginLevel, err
|
||||||
|
}
|
||||||
|
|
||||||
|
marginLevel = marketValue.Div(debtValue)
|
||||||
|
return marginLevel, nil
|
||||||
|
}
|
||||||
|
|
||||||
func CalculateBaseQuantity(session *bbgo.ExchangeSession, market types.Market, price, quantity, leverage fixedpoint.Value) (fixedpoint.Value, error) {
|
func CalculateBaseQuantity(session *bbgo.ExchangeSession, market types.Market, price, quantity, leverage fixedpoint.Value) (fixedpoint.Value, error) {
|
||||||
// default leverage guard
|
// default leverage guard
|
||||||
if leverage.IsZero() {
|
if leverage.IsZero() {
|
||||||
|
|
|
@ -111,3 +111,44 @@ func TestAccountValueCalculator_NetValue(t *testing.T) {
|
||||||
assert.Equal(t, "2000", netValue.String()) // 21000-19000
|
assert.Equal(t, "2000", netValue.String()) // 21000-19000
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestNewAccountValueCalculator_MarginLevel(t *testing.T) {
|
||||||
|
mockCtrl := gomock.NewController(t)
|
||||||
|
defer mockCtrl.Finish()
|
||||||
|
|
||||||
|
mockEx := mocks.NewMockExchange(mockCtrl)
|
||||||
|
// for market data stream and user data stream
|
||||||
|
mockEx.EXPECT().NewStream().Return(&types.StandardStream{}).Times(2)
|
||||||
|
mockEx.EXPECT().QueryTickers(gomock.Any(), []string{"BTCUSDT"}).Return(map[string]types.Ticker{
|
||||||
|
"BTCUSDT": newTestTicker(),
|
||||||
|
}, nil)
|
||||||
|
|
||||||
|
session := bbgo.NewExchangeSession("test", mockEx)
|
||||||
|
session.Account.UpdateBalances(types.BalanceMap{
|
||||||
|
"BTC": {
|
||||||
|
Currency: "BTC",
|
||||||
|
Available: fixedpoint.Zero,
|
||||||
|
Locked: fixedpoint.Zero,
|
||||||
|
Borrowed: fixedpoint.NewFromFloat(1.0),
|
||||||
|
Interest: fixedpoint.NewFromFloat(0.003),
|
||||||
|
NetAsset: fixedpoint.Zero,
|
||||||
|
},
|
||||||
|
"USDT": {
|
||||||
|
Currency: "USDT",
|
||||||
|
Available: fixedpoint.NewFromFloat(21000.0),
|
||||||
|
Locked: fixedpoint.Zero,
|
||||||
|
Borrowed: fixedpoint.Zero,
|
||||||
|
Interest: fixedpoint.Zero,
|
||||||
|
NetAsset: fixedpoint.Zero,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
assert.NotNil(t, session)
|
||||||
|
|
||||||
|
cal := NewAccountValueCalculator(session, "USDT")
|
||||||
|
assert.NotNil(t, cal)
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
marginLevel, err := cal.MarginLevel(ctx)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, "1.10195728", marginLevel.String()) // 21000 / 19000 * 1.003
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user