From 8a93f0921fbcff894b5021990e97d2526296e6a4 Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 4 May 2022 14:40:35 +0800 Subject: [PATCH] add more margin info columns --- pkg/bbgo/environment.go | 12 ++++++++++-- pkg/service/account.go | 25 +++++++++++++++++++++---- pkg/service/account_test.go | 2 +- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index be1cd0d3a..e6542895d 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -649,7 +649,7 @@ func (environ *Environment) Sync(ctx context.Context, userConfig ...*Config) err return nil } -func (environ *Environment) RecordAsset(t time.Time, sessionName string, exchangeName types.ExchangeName, account string, assets types.AssetMap) { +func (environ *Environment) RecordAsset(t time.Time, session *ExchangeSession, assets types.AssetMap) { // skip for back-test if environ.BacktestService != nil { return @@ -659,7 +659,15 @@ func (environ *Environment) RecordAsset(t time.Time, sessionName string, exchang return } - if err := environ.AccountService.InsertAsset(t, sessionName, exchangeName, account, assets); err != nil { + if err := environ.AccountService.InsertAsset( + t, + session.Name, + session.ExchangeName, + session.SubAccount, + session.Margin, + session.IsolatedMargin, + session.IsolatedMarginSymbol, + assets); err != nil { log.WithError(err).Errorf("can not insert asset record") } } diff --git a/pkg/service/account.go b/pkg/service/account.go index 5244cc45a..d64cdc176 100644 --- a/pkg/service/account.go +++ b/pkg/service/account.go @@ -15,7 +15,8 @@ func NewAccountService(db *sqlx.DB) *AccountService { return &AccountService{DB: db} } -func (s *AccountService) InsertAsset(time time.Time, session string, name types.ExchangeName, account string, assets types.AssetMap) error { +// TODO: should pass bbgo.ExchangeSession to this function, but that might cause cyclic import +func (s *AccountService) InsertAsset(time time.Time, session string, name types.ExchangeName, account string, isMargin bool, isIsolatedMargin bool, isolatedMarginSymbol string, assets types.AssetMap) error { if s.DB == nil { // skip db insert when no db connection setting. return nil @@ -37,9 +38,25 @@ func (s *AccountService) InsertAsset(time time.Time, session string, name types. locked, borrowed, net_asset, - price_in_usd) - values (?,?,?,?,?,?,?,?,?,?,?,?,?); - `, session, name, account, time, v.Currency, v.InUSD, v.InBTC, v.Total, v.Available, v.Locked, v.Borrowed, v.NetAsset, v.PriceInUSD) + price_in_usd, + is_margin, is_isolated, isolated_symbol) + values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);`, + session, + name, + account, + time, + v.Currency, + v.InUSD, + v.InBTC, + v.Total, + v.Available, + v.Locked, + v.Borrowed, + v.NetAsset, + v.PriceInUSD, + isMargin, + isIsolatedMargin, + isolatedMarginSymbol) err = multierr.Append(err, _err) // successful request diff --git a/pkg/service/account_test.go b/pkg/service/account_test.go index d6433ca7a..89c0fa98c 100644 --- a/pkg/service/account_test.go +++ b/pkg/service/account_test.go @@ -23,7 +23,7 @@ func TestAccountService(t *testing.T) { service := &AccountService{DB: xdb} t1 := time.Now() - err = service.InsertAsset(t1, "binance", types.ExchangeBinance, "main", types.AssetMap{ + err = service.InsertAsset(t1, "binance", types.ExchangeBinance, "main", false, false, "", types.AssetMap{ "BTC": types.Asset{ Currency: "BTC", Total: fixedpoint.MustNewFromString("1.0"),