implement profit insert

This commit is contained in:
c9s 2022-03-04 19:24:40 +08:00
parent bb52fcb48f
commit 09dea3938d
4 changed files with 117 additions and 58 deletions

View File

@ -1,60 +1,67 @@
-- +up -- +up
CREATE TABLE `profits` CREATE TABLE `profits`
( (
`gid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `gid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`symbol` VARCHAR(8) NOT NULL, `strategy` VARCHAR(32) NOT NULL,
`strategy_instance_id` VARCHAR(64) NOT NULL,
`symbol` VARCHAR(8) NOT NULL,
-- average_cost is the position average cost -- average_cost is the position average cost
`average_cost` DECIMAL(16, 8) UNSIGNED NOT NULL, `average_cost` DECIMAL(16, 8) UNSIGNED NOT NULL,
-- profit is the pnl (profit and loss) -- profit is the pnl (profit and loss)
`profit` DECIMAL(16, 8) NOT NULL, `profit` DECIMAL(16, 8) NOT NULL,
-- net_profit is the pnl (profit and loss) -- net_profit is the pnl (profit and loss)
`net_profit` DECIMAL(16, 8) NOT NULL, `net_profit` DECIMAL(16, 8) NOT NULL,
-- profit_margin is the pnl (profit and loss) -- profit_margin is the pnl (profit and loss)
`profit_margin` DECIMAL(16, 8) NOT NULL, `profit_margin` DECIMAL(16, 8) NOT NULL,
-- net_profit_margin is the pnl (profit and loss) -- net_profit_margin is the pnl (profit and loss)
`net_profit_margin` DECIMAL(16, 8) NOT NULL, `net_profit_margin` DECIMAL(16, 8) NOT NULL,
`quote_currency` VARCHAR(10) NOT NULL, `quote_currency` VARCHAR(10) NOT NULL,
`base_currency` VARCHAR(10) NOT NULL, `base_currency` VARCHAR(10) NOT NULL,
-- ------------------------------------------------------- -- -------------------------------------------------------
-- embedded trade data -- -- embedded trade data --
-- ------------------------------------------------------- -- -------------------------------------------------------
`exchange` VARCHAR(24) NOT NULL DEFAULT '', `exchange` VARCHAR(24) NOT NULL DEFAULT '',
`is_futures` BOOLEAN NOT NULL DEFAULT FALSE, `is_futures` BOOLEAN NOT NULL DEFAULT FALSE,
`is_margin` BOOLEAN NOT NULL DEFAULT FALSE, `is_margin` BOOLEAN NOT NULL DEFAULT FALSE,
`is_isolated` BOOLEAN NOT NULL DEFAULT FALSE, `is_isolated` BOOLEAN NOT NULL DEFAULT FALSE,
`trade_id` BIGINT UNSIGNED NOT NULL, `trade_id` BIGINT UNSIGNED NOT NULL,
-- side is the side of the trade that makes profit -- side is the side of the trade that makes profit
`side` VARCHAR(4) NOT NULL DEFAULT '', `side` VARCHAR(4) NOT NULL DEFAULT '',
`is_buyer` BOOLEAN NOT NULL DEFAULT FALSE,
`is_maker` BOOLEAN NOT NULL DEFAULT FALSE,
-- price is the price of the trade that makes profit -- price is the price of the trade that makes profit
`price` DECIMAL(16, 8) UNSIGNED NOT NULL, `price` DECIMAL(16, 8) UNSIGNED NOT NULL,
-- quantity is the quantity of the trade that makes profit -- quantity is the quantity of the trade that makes profit
`quantity` DECIMAL(16, 8) UNSIGNED NOT NULL, `quantity` DECIMAL(16, 8) UNSIGNED NOT NULL,
-- trade_amount is the quote quantity of the trade that makes profit -- quote_quantity is the quote quantity of the trade that makes profit
`trade_amount` DECIMAL(16, 8) UNSIGNED NOT NULL, `quote_quantity` DECIMAL(16, 8) UNSIGNED NOT NULL,
`traded_at` DATETIME(3) NOT NULL, `traded_at` DATETIME(3) NOT NULL,
-- fee -- fee
`fee_in_usd` DECIMAL(16, 8) UNSIGNED NOT NULL, `fee_in_usd` DECIMAL(16, 8) UNSIGNED,
`fee` DECIMAL(16, 8) UNSIGNED NOT NULL, `fee` DECIMAL(16, 8) UNSIGNED NOT NULL,
`fee_currency` VARCHAR(10) NOT NULL, `fee_currency` VARCHAR(10) NOT NULL,
PRIMARY KEY (`gid`), PRIMARY KEY (`gid`),
UNIQUE KEY `trade_id` (`trade_id`) UNIQUE KEY `trade_id` (`trade_id`)

View File

@ -1,60 +1,67 @@
-- +up -- +up
CREATE TABLE `profits` CREATE TABLE `profits`
( (
`gid` INTEGER PRIMARY KEY AUTOINCREMENT, `gid` INTEGER PRIMARY KEY AUTOINCREMENT,
`symbol` VARCHAR(8) NOT NULL, `strategy` VARCHAR(32) NOT NULL,
`strategy_instance_id` VARCHAR(64) NOT NULL,
`symbol` VARCHAR(8) NOT NULL,
-- average_cost is the position average cost -- average_cost is the position average cost
`average_cost` DECIMAL(16, 8) NOT NULL, `average_cost` DECIMAL(16, 8) NOT NULL,
-- profit is the pnl (profit and loss) -- profit is the pnl (profit and loss)
`profit` DECIMAL(16, 8) NOT NULL, `profit` DECIMAL(16, 8) NOT NULL,
-- net_profit is the pnl (profit and loss) -- net_profit is the pnl (profit and loss)
`net_profit` DECIMAL(16, 8) NOT NULL, `net_profit` DECIMAL(16, 8) NOT NULL,
-- profit_margin is the pnl (profit and loss) -- profit_margin is the pnl (profit and loss)
`profit_margin` DECIMAL(16, 8) NOT NULL, `profit_margin` DECIMAL(16, 8) NOT NULL,
-- net_profit_margin is the pnl (profit and loss) -- net_profit_margin is the pnl (profit and loss)
`net_profit_margin` DECIMAL(16, 8) NOT NULL, `net_profit_margin` DECIMAL(16, 8) NOT NULL,
`quote_currency` VARCHAR(10) NOT NULL, `quote_currency` VARCHAR(10) NOT NULL,
`base_currency` VARCHAR(10) NOT NULL, `base_currency` VARCHAR(10) NOT NULL,
-- ------------------------------------------------------- -- -------------------------------------------------------
-- embedded trade data -- -- embedded trade data --
-- ------------------------------------------------------- -- -------------------------------------------------------
`exchange` VARCHAR(24) NOT NULL DEFAULT '', `exchange` VARCHAR(24) NOT NULL DEFAULT '',
`is_futures` BOOLEAN NOT NULL DEFAULT FALSE, `is_futures` BOOLEAN NOT NULL DEFAULT FALSE,
`is_margin` BOOLEAN NOT NULL DEFAULT FALSE, `is_margin` BOOLEAN NOT NULL DEFAULT FALSE,
`is_isolated` BOOLEAN NOT NULL DEFAULT FALSE, `is_isolated` BOOLEAN NOT NULL DEFAULT FALSE,
`trade_id` BIGINT NOT NULL, `trade_id` BIGINT NOT NULL,
-- side is the side of the trade that makes profit -- side is the side of the trade that makes profit
`side` VARCHAR(4) NOT NULL DEFAULT '', `side` VARCHAR(4) NOT NULL DEFAULT '',
`is_buyer` BOOLEAN NOT NULL DEFAULT FALSE,
`is_maker` BOOLEAN NOT NULL DEFAULT FALSE,
-- price is the price of the trade that makes profit -- price is the price of the trade that makes profit
`price` DECIMAL(16, 8) NOT NULL, `price` DECIMAL(16, 8) NOT NULL,
-- quantity is the quantity of the trade that makes profit -- quantity is the quantity of the trade that makes profit
`quantity` DECIMAL(16, 8) NOT NULL, `quantity` DECIMAL(16, 8) NOT NULL,
-- trade_amount is the quote quantity of the trade that makes profit -- trade_amount is the quote quantity of the trade that makes profit
`trade_amount` DECIMAL(16, 8) NOT NULL, `quote_quantity` DECIMAL(16, 8) NOT NULL,
`traded_at` DATETIME(3) NOT NULL, `traded_at` DATETIME(3) NOT NULL,
-- fee -- fee
`fee_in_usd` DECIMAL(16, 8) NOT NULL, `fee_in_usd` DECIMAL(16, 8),
`fee` DECIMAL(16, 8) NOT NULL, `fee` DECIMAL(16, 8) NOT NULL,
`fee_currency` VARCHAR(10) NOT NULL `fee_currency` VARCHAR(10) NOT NULL
); );
-- +down -- +down

View File

@ -37,23 +37,64 @@ func (s *ProfitService) Load(ctx context.Context, id int64) (*types.Trade, error
return nil, errors.Wrapf(ErrTradeNotFound, "trade id:%d not found", id) return nil, errors.Wrapf(ErrTradeNotFound, "trade id:%d not found", id)
} }
func (s *ProfitService) scanRows(rows *sqlx.Rows) (trades []types.Trade, err error) { func (s *ProfitService) scanRows(rows *sqlx.Rows) (profits []types.Profit, err error) {
for rows.Next() { for rows.Next() {
var trade types.Trade var profit types.Profit
if err := rows.StructScan(&trade); err != nil { if err := rows.StructScan(&profit); err != nil {
return trades, err return profits, err
} }
trades = append(trades, trade) profits = append(profits, profit)
} }
return trades, rows.Err() return profits, rows.Err()
} }
func (s *ProfitService) Insert(trade types.Trade) error { func (s *ProfitService) Insert(profit types.Profit) error {
_, err := s.DB.NamedExec(` _, err := s.DB.NamedExec(`
INSERT INTO profits (id, exchange, symbol, trade_id, average_cost, profit, price, quantity, quote_quantity, side, traded_at, is_margin, is_futures, is_isolated) INSERT INTO profits (
VALUES (:id, :exchange, :order_id, :symbol, :price, :quantity, :quote_quantity, :side, :is_buyer, :is_maker, :fee, :fee_currency, :traded_at, :is_margin, :is_futures, :is_isolated)`, strategy,
trade) strategy_instance_id,
symbol,
average_cost,
profit,
trade_id,
price,
quantity,
quote_quantity,
side,
is_buyer,
is_maker,
fee,
fee_currency,
fee_in_usd,
traded_at,
exchange,
is_margin,
is_futures,
is_isolated
) VALUES (
:strategy,
:strategy_instance_id,
:symbol,
:average_cost,
:profit,
:trade_id,
:price,
:quantity,
:quote_quantity,
:side,
:is_buyer,
:is_maker,
:fee,
:fee_currency,
:fee_in_usd,
:traded_at,
:exchange,
:is_margin,
:is_futures,
:is_isolated
)`,
profit)
return err return err
} }

View File

@ -20,8 +20,7 @@ type Profit struct {
// NetProfit is (profit - trading fee) // NetProfit is (profit - trading fee)
NetProfit fixedpoint.Value `json:"netProfit" db:"net_profit"` NetProfit fixedpoint.Value `json:"netProfit" db:"net_profit"`
AverageCost fixedpoint.Value `json:"averageCost" db:"average_ost"` AverageCost fixedpoint.Value `json:"averageCost" db:"average_ost"`
TradeAmount fixedpoint.Value `json:"tradeAmount" db:"quote_quantity"`
TradeAmount fixedpoint.Value `json:"tradeAmount" db:"trade_amount"`
// ProfitMargin is a percentage of the profit and the capital amount // ProfitMargin is a percentage of the profit and the capital amount
ProfitMargin fixedpoint.Value `json:"profitMargin" db:"profit_margin"` ProfitMargin fixedpoint.Value `json:"profitMargin" db:"profit_margin"`
@ -32,10 +31,15 @@ type Profit struct {
QuoteCurrency string `json:"quoteCurrency" db:"quote_currency"` QuoteCurrency string `json:"quoteCurrency" db:"quote_currency"`
BaseCurrency string `json:"baseCurrency" db:"base_currency"` BaseCurrency string `json:"baseCurrency" db:"base_currency"`
IsBuyer bool `json:"isBuyer" db:"is_buyer"`
IsMaker bool `json:"isMaker" db:"is_maker"`
// FeeInUSD is the summed fee of this profit, // FeeInUSD is the summed fee of this profit,
// you will need to convert the trade fee into USD since the fee currencies can be different. // you will need to convert the trade fee into USD since the fee currencies can be different.
FeeInUSD fixedpoint.Value `json:"feeInUSD" db:"fee_in_usd"` FeeInUSD fixedpoint.Value `json:"feeInUSD" db:"fee_in_usd"`
Time time.Time `json:"time" db:"time"` Fee fixedpoint.Value `json:"fee" db:"fee"`
FeeCurrency string `json:"feeCurrency" db:"fee_currency"`
Time time.Time `json:"tradedAt" db:"traded_at"`
Strategy string `json:"strategy" db:"strategy"` Strategy string `json:"strategy" db:"strategy"`
StrategyInstanceID string `json:"strategyInstanceID" db:"strategy_instance_id"` StrategyInstanceID string `json:"strategyInstanceID" db:"strategy_instance_id"`
} }