add more margin info columns

This commit is contained in:
c9s 2022-05-04 14:40:35 +08:00
parent 01273f7c4c
commit 8a93f0921f
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 32 additions and 7 deletions

View File

@ -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")
}
}

View File

@ -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

View File

@ -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"),