mirror of
https://github.com/c9s/bbgo.git
synced 2024-09-20 08:11:08 +00:00
implement profit insert
This commit is contained in:
parent
bb52fcb48f
commit
09dea3938d
|
@ -3,6 +3,9 @@ CREATE TABLE `profits`
|
|||
(
|
||||
`gid` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
|
||||
`strategy` VARCHAR(32) NOT NULL,
|
||||
`strategy_instance_id` VARCHAR(64) NOT NULL,
|
||||
|
||||
`symbol` VARCHAR(8) NOT NULL,
|
||||
|
||||
-- average_cost is the position average cost
|
||||
|
@ -40,19 +43,23 @@ CREATE TABLE `profits`
|
|||
-- side is the side of the trade that makes profit
|
||||
`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` DECIMAL(16, 8) UNSIGNED NOT NULL,
|
||||
|
||||
-- quantity is the quantity of the trade that makes profit
|
||||
`quantity` DECIMAL(16, 8) UNSIGNED NOT NULL,
|
||||
|
||||
-- trade_amount is the quote quantity of the trade that makes profit
|
||||
`trade_amount` DECIMAL(16, 8) UNSIGNED NOT NULL,
|
||||
-- quote_quantity is the quote quantity of the trade that makes profit
|
||||
`quote_quantity` DECIMAL(16, 8) UNSIGNED NOT NULL,
|
||||
|
||||
`traded_at` DATETIME(3) NOT NULL,
|
||||
|
||||
-- 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_currency` VARCHAR(10) NOT NULL,
|
||||
|
||||
|
|
|
@ -3,6 +3,9 @@ CREATE TABLE `profits`
|
|||
(
|
||||
`gid` INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
|
||||
`strategy` VARCHAR(32) NOT NULL,
|
||||
`strategy_instance_id` VARCHAR(64) NOT NULL,
|
||||
|
||||
`symbol` VARCHAR(8) NOT NULL,
|
||||
|
||||
-- average_cost is the position average cost
|
||||
|
@ -40,6 +43,10 @@ CREATE TABLE `profits`
|
|||
-- side is the side of the trade that makes profit
|
||||
`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` DECIMAL(16, 8) NOT NULL,
|
||||
|
||||
|
@ -47,12 +54,12 @@ CREATE TABLE `profits`
|
|||
`quantity` DECIMAL(16, 8) NOT NULL,
|
||||
|
||||
-- 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,
|
||||
|
||||
-- fee
|
||||
`fee_in_usd` DECIMAL(16, 8) NOT NULL,
|
||||
`fee_in_usd` DECIMAL(16, 8),
|
||||
`fee` DECIMAL(16, 8) NOT NULL,
|
||||
`fee_currency` VARCHAR(10) NOT NULL
|
||||
);
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
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() {
|
||||
var trade types.Trade
|
||||
if err := rows.StructScan(&trade); err != nil {
|
||||
return trades, err
|
||||
var profit types.Profit
|
||||
if err := rows.StructScan(&profit); err != nil {
|
||||
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(`
|
||||
INSERT INTO profits (id, exchange, symbol, trade_id, average_cost, profit, price, quantity, quote_quantity, side, traded_at, is_margin, is_futures, is_isolated)
|
||||
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)`,
|
||||
trade)
|
||||
INSERT INTO profits (
|
||||
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
|
||||
) 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
|
||||
}
|
||||
|
|
|
@ -20,8 +20,7 @@ type Profit struct {
|
|||
// NetProfit is (profit - trading fee)
|
||||
NetProfit fixedpoint.Value `json:"netProfit" db:"net_profit"`
|
||||
AverageCost fixedpoint.Value `json:"averageCost" db:"average_ost"`
|
||||
|
||||
TradeAmount fixedpoint.Value `json:"tradeAmount" db:"trade_amount"`
|
||||
TradeAmount fixedpoint.Value `json:"tradeAmount" db:"quote_quantity"`
|
||||
|
||||
// ProfitMargin is a percentage of the profit and the capital amount
|
||||
ProfitMargin fixedpoint.Value `json:"profitMargin" db:"profit_margin"`
|
||||
|
@ -32,10 +31,15 @@ type Profit struct {
|
|||
QuoteCurrency string `json:"quoteCurrency" db:"quote_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,
|
||||
// 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"`
|
||||
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"`
|
||||
StrategyInstanceID string `json:"strategyInstanceID" db:"strategy_instance_id"`
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user