From 02dab542c47d063a6b5e660162c2ca7ca4214921 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 14 Sep 2022 02:17:21 +0800 Subject: [PATCH] bbgo: add USDTTWD price test case --- pkg/bbgo/risk.go | 3 +-- pkg/bbgo/risk_test.go | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/bbgo/risk.go b/pkg/bbgo/risk.go index abc49ebee..633ae42e2 100644 --- a/pkg/bbgo/risk.go +++ b/pkg/bbgo/risk.go @@ -140,8 +140,7 @@ func calculateNetValueInQuote(balances types.BalanceMap, prices types.PriceMap, if price, ok := prices[symbol]; ok { accountValue = accountValue.Add(b.Net().Mul(price)) } else if priceReverse, ok2 := prices[symbolReverse]; ok2 { - price2 := one.Div(priceReverse) - accountValue = accountValue.Add(b.Net().Mul(price2)) + accountValue = accountValue.Add(b.Net().Div(priceReverse)) } } diff --git a/pkg/bbgo/risk_test.go b/pkg/bbgo/risk_test.go index 1382b4adf..f33f5845d 100644 --- a/pkg/bbgo/risk_test.go +++ b/pkg/bbgo/risk_test.go @@ -256,6 +256,26 @@ func Test_calculateNetValueInQuote(t *testing.T) { }, wantAccountValue: number(19000.0*0.01 + 100.0 + 80.0 + 70.0), }, + { + name: "reversed usdt price", + args: args{ + balances: types.BalanceMap{ + "USDC": types.Balance{Currency: "USDC", Available: number(70.0)}, + "TWD": types.Balance{Currency: "TWD", Available: number(3000.0)}, + "USDT": types.Balance{Currency: "USDT", Available: number(100.0)}, + "BUSD": types.Balance{Currency: "BUSD", Available: number(80.0)}, + "BTC": types.Balance{Currency: "BTC", Available: number(0.01)}, + }, + prices: types.PriceMap{ + "USDTTWD": number(30.0), + "USDCUSDT": number(1.0), + "BUSDUSDT": number(1.0), + "BTCUSDT": number(19000.0), + }, + quoteCurrency: "USDT", + }, + wantAccountValue: number(19000.0*0.01 + 100.0 + 80.0 + 70.0 + (3000.0 / 30.0)), + }, { name: "borrow base asset", args: args{