mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
Merge pull request #588 from c9s/fix-net-asset
fix: add interest and fix net asset column
This commit is contained in:
commit
bfb61c3129
19
migrations/mysql/20220504184155_fix_net_asset_column.sql
Normal file
19
migrations/mysql/20220504184155_fix_net_asset_column.sql
Normal file
|
@ -0,0 +1,19 @@
|
|||
-- +up
|
||||
-- +begin
|
||||
ALTER TABLE `nav_history_details`
|
||||
MODIFY COLUMN `net_asset` DECIMAL(32, 8) DEFAULT 0.00000000 NOT NULL,
|
||||
CHANGE COLUMN `balance_in_usd` `net_asset_in_usd` DECIMAL(32, 2) DEFAULT 0.00000000 NOT NULL,
|
||||
CHANGE COLUMN `balance_in_btc` `net_asset_in_btc` DECIMAL(32, 20) DEFAULT 0.00000000 NOT NULL;
|
||||
-- +end
|
||||
|
||||
-- +begin
|
||||
ALTER TABLE `nav_history_details`
|
||||
ADD COLUMN `interest` DECIMAL(32, 20) UNSIGNED DEFAULT 0.00000000 NOT NULL;
|
||||
-- +end
|
||||
|
||||
-- +down
|
||||
|
||||
-- +begin
|
||||
ALTER TABLE `nav_history_details`
|
||||
DROP COLUMN `interest`;
|
||||
-- +end
|
|
@ -2,16 +2,16 @@
|
|||
-- +begin
|
||||
CREATE TABLE `nav_history_details`
|
||||
(
|
||||
gid bigint unsigned auto_increment PRIMARY KEY,
|
||||
`exchange` VARCHAR NOT NULL DEFAULT '',
|
||||
`subaccount` VARCHAR NOT NULL DEFAULT '',
|
||||
time DATETIME(3) NOT NULL DEFAULT (strftime('%s','now')),
|
||||
currency VARCHAR NOT NULL,
|
||||
balance_in_usd DECIMAL DEFAULT 0.00000000 NOT NULL,
|
||||
balance_in_btc DECIMAL DEFAULT 0.00000000 NOT NULL,
|
||||
balance DECIMAL DEFAULT 0.00000000 NOT NULL,
|
||||
available DECIMAL DEFAULT 0.00000000 NOT NULL,
|
||||
locked DECIMAL DEFAULT 0.00000000 NOT NULL
|
||||
`gid` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
`exchange` VARCHAR(30) NOT NULL DEFAULT '',
|
||||
`subaccount` VARCHAR(30) NOT NULL DEFAULT '',
|
||||
`time` DATETIME(3) NOT NULL DEFAULT (strftime('%s', 'now')),
|
||||
`currency` VARCHAR(30) NOT NULL,
|
||||
`net_asset_in_usd` DECIMAL DEFAULT 0.00000000 NOT NULL,
|
||||
`net_asset_in_btc` DECIMAL DEFAULT 0.00000000 NOT NULL,
|
||||
`balance` DECIMAL DEFAULT 0.00000000 NOT NULL,
|
||||
`available` DECIMAL DEFAULT 0.00000000 NOT NULL,
|
||||
`locked` DECIMAL DEFAULT 0.00000000 NOT NULL
|
||||
);
|
||||
-- +end
|
||||
-- +begin
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
-- +up
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `session` VARCHAR(50) NOT NULL;
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `borrowed` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `net_asset` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `price_in_usd` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `borrowed` DECIMAL DEFAULT 0.00000000 NOT NULL;
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `net_asset` DECIMAL DEFAULT 0.00000000 NOT NULL;
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `price_in_usd` DECIMAL DEFAULT 0.00000000 NOT NULL;
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `is_margin` BOOL DEFAULT FALSE NOT NULL;
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `is_isolated` BOOL DEFAULT FALSE NOT NULL;
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `isolated_symbol` VARCHAR(30) DEFAULT '' NOT NULL;
|
||||
|
|
11
migrations/sqlite3/20220504184155_fix_net_asset_column.sql
Normal file
11
migrations/sqlite3/20220504184155_fix_net_asset_column.sql
Normal file
|
@ -0,0 +1,11 @@
|
|||
-- +up
|
||||
-- +begin
|
||||
ALTER TABLE `nav_history_details` ADD COLUMN `interest` DECIMAL DEFAULT 0.00000000 NOT NULL;
|
||||
-- +end
|
||||
|
||||
|
||||
-- +down
|
||||
|
||||
-- +begin
|
||||
SELECT 1;
|
||||
-- +end
|
39
pkg/migrations/mysql/20220504184155_fix_net_asset_column.go
Normal file
39
pkg/migrations/mysql/20220504184155_fix_net_asset_column.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/c9s/rockhopper"
|
||||
)
|
||||
|
||||
func init() {
|
||||
AddMigration(upFixNetAssetColumn, downFixNetAssetColumn)
|
||||
|
||||
}
|
||||
|
||||
func upFixNetAssetColumn(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
|
||||
// This code is executed when the migration is applied.
|
||||
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details`\n MODIFY COLUMN `net_asset` DECIMAL(32, 8) DEFAULT 0.00000000 NOT NULL,\n CHANGE COLUMN `balance_in_usd` `net_asset_in_usd` DECIMAL(32, 2) DEFAULT 0.00000000 NOT NULL,\n CHANGE COLUMN `balance_in_btc` `net_asset_in_btc` DECIMAL(32, 20) DEFAULT 0.00000000 NOT NULL;")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details`\n ADD COLUMN `interest` DECIMAL(32, 20) UNSIGNED DEFAULT 0.00000000 NOT NULL;")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func downFixNetAssetColumn(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
|
||||
// This code is executed when the migration is rolled back.
|
||||
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details`\n DROP COLUMN `interest`;")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
|
@ -14,7 +14,7 @@ func init() {
|
|||
func upAddNavHistoryDetails(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
|
||||
// This code is executed when the migration is applied.
|
||||
|
||||
_, err = tx.ExecContext(ctx, "CREATE TABLE `nav_history_details`\n(\n gid bigint unsigned auto_increment PRIMARY KEY,\n `exchange` VARCHAR NOT NULL DEFAULT '',\n `subaccount` VARCHAR NOT NULL DEFAULT '',\n time DATETIME(3) NOT NULL DEFAULT (strftime('%s','now')),\n currency VARCHAR NOT NULL,\n balance_in_usd DECIMAL DEFAULT 0.00000000 NOT NULL,\n balance_in_btc DECIMAL DEFAULT 0.00000000 NOT NULL,\n balance DECIMAL DEFAULT 0.00000000 NOT NULL,\n available DECIMAL DEFAULT 0.00000000 NOT NULL,\n locked DECIMAL DEFAULT 0.00000000 NOT NULL\n);")
|
||||
_, err = tx.ExecContext(ctx, "CREATE TABLE `nav_history_details`\n(\n `gid` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,\n `exchange` VARCHAR(30) NOT NULL DEFAULT '',\n `subaccount` VARCHAR(30) NOT NULL DEFAULT '',\n `time` DATETIME(3) NOT NULL DEFAULT (strftime('%s', 'now')),\n `currency` VARCHAR(30) NOT NULL,\n `net_asset_in_usd` DECIMAL DEFAULT 0.00000000 NOT NULL,\n `net_asset_in_btc` DECIMAL DEFAULT 0.00000000 NOT NULL,\n `balance` DECIMAL DEFAULT 0.00000000 NOT NULL,\n `available` DECIMAL DEFAULT 0.00000000 NOT NULL,\n `locked` DECIMAL DEFAULT 0.00000000 NOT NULL\n);")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -19,17 +19,17 @@ func upAddMarginInfoToNav(ctx context.Context, tx rockhopper.SQLExecutor) (err e
|
|||
return err
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `borrowed` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;")
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `borrowed` DECIMAL DEFAULT 0.00000000 NOT NULL;")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `net_asset` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;")
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `net_asset` DECIMAL DEFAULT 0.00000000 NOT NULL;")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `price_in_usd` DECIMAL UNSIGNED DEFAULT 0.00000000 NOT NULL;")
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `price_in_usd` DECIMAL DEFAULT 0.00000000 NOT NULL;")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -0,0 +1,34 @@
|
|||
package sqlite3
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/c9s/rockhopper"
|
||||
)
|
||||
|
||||
func init() {
|
||||
AddMigration(upFixNetAssetColumn, downFixNetAssetColumn)
|
||||
|
||||
}
|
||||
|
||||
func upFixNetAssetColumn(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
|
||||
// This code is executed when the migration is applied.
|
||||
|
||||
_, err = tx.ExecContext(ctx, "ALTER TABLE `nav_history_details` ADD COLUMN `interest` DECIMAL DEFAULT 0.00000000 NOT NULL;")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func downFixNetAssetColumn(ctx context.Context, tx rockhopper.SQLExecutor) (err error) {
|
||||
// This code is executed when the migration is rolled back.
|
||||
|
||||
_, err = tx.ExecContext(ctx, "SELECT 1;")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
|
@ -31,8 +31,8 @@ func (s *AccountService) InsertAsset(time time.Time, session string, name types.
|
|||
subaccount,
|
||||
time,
|
||||
currency,
|
||||
balance_in_usd,
|
||||
balance_in_btc,
|
||||
net_asset_in_usd,
|
||||
net_asset_in_btc,
|
||||
balance,
|
||||
available,
|
||||
locked,
|
||||
|
|
|
@ -13,13 +13,19 @@ import (
|
|||
type Asset struct {
|
||||
Currency string `json:"currency" db:"currency"`
|
||||
Total fixedpoint.Value `json:"total" db:"total"`
|
||||
InUSD fixedpoint.Value `json:"inUSD" db:"in_usd"`
|
||||
InBTC fixedpoint.Value `json:"inBTC" db:"in_btc"`
|
||||
|
||||
NetAsset fixedpoint.Value `json:"netAsset" db:"net_asset"`
|
||||
|
||||
// InUSD is net asset in USD
|
||||
InUSD fixedpoint.Value `json:"inUSD" db:"net_asset_in_usd"`
|
||||
|
||||
// InBTC is net asset in BTC
|
||||
InBTC fixedpoint.Value `json:"inBTC" db:"net_asset_in_btc"`
|
||||
|
||||
Time time.Time `json:"time" db:"time"`
|
||||
Locked fixedpoint.Value `json:"lock" db:"lock" `
|
||||
Available fixedpoint.Value `json:"available" db:"available"`
|
||||
Borrowed fixedpoint.Value `json:"borrowed" db:"borrowed"`
|
||||
NetAsset fixedpoint.Value `json:"netAsset" db:"net_asset"`
|
||||
PriceInUSD fixedpoint.Value `json:"priceInUSD" db:"price_in_usd"`
|
||||
}
|
||||
|
||||
|
|
|
@ -31,8 +31,9 @@ func (b Balance) Net() fixedpoint.Value {
|
|||
total := b.Total()
|
||||
netAsset := b.NetAsset
|
||||
if netAsset.IsZero() {
|
||||
netAsset = total.Sub(b.Borrowed)
|
||||
netAsset = total.Sub(b.Borrowed).Sub(b.Interest)
|
||||
}
|
||||
|
||||
return netAsset
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user