all: update profit struct fields

This commit is contained in:
c9s 2022-03-05 01:39:53 +08:00
parent 82e5520ee4
commit 197d750cb4
5 changed files with 75 additions and 31 deletions

View File

@ -30,11 +30,12 @@ func TestProfitService(t *testing.T) {
Profit: fixedpoint.NewFromFloat(1.01),
NetProfit: fixedpoint.NewFromFloat(0.98),
TradeID: 99,
Side: types.SideTypeSell,
Price: fixedpoint.NewFromFloat(44300),
Quantity: fixedpoint.NewFromFloat(0.001),
TradeAmount: fixedpoint.NewFromFloat(44.0),
QuoteQuantity: fixedpoint.NewFromFloat(44.0),
Exchange: types.ExchangeMax,
Time: time.Now(),
TradedAt: time.Now(),
})
assert.NoError(t, err)
}

View File

@ -575,12 +575,12 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
Symbol: s.Symbol,
Profit: profit,
NetProfit: netProfit,
TradeAmount: trade.QuoteQuantity,
QuoteQuantity: trade.QuoteQuantity,
ProfitMargin: profit.Div(trade.QuoteQuantity),
NetProfitMargin: netProfit.Div(trade.QuoteQuantity),
QuoteCurrency: s.state.Position.QuoteCurrency,
BaseCurrency: s.state.Position.BaseCurrency,
Time: trade.Time.Time(),
TradedAt: trade.Time.Time(),
}
s.state.ProfitStats.AddProfit(p)
s.Notify(&p)

View File

@ -767,12 +767,12 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
Symbol: s.Symbol,
Profit: profit,
NetProfit: netProfit,
TradeAmount: trade.QuoteQuantity,
QuoteQuantity: trade.QuoteQuantity,
ProfitMargin: profit.Div(trade.QuoteQuantity),
NetProfitMargin: netProfit.Div(trade.QuoteQuantity),
QuoteCurrency: s.state.Position.QuoteCurrency,
BaseCurrency: s.state.Position.BaseCurrency,
Time: trade.Time.Time(),
TradedAt: trade.Time.Time(),
}
s.state.ProfitStats.AddProfit(p)
s.Notify(&p)

View File

@ -53,6 +53,40 @@ type Position struct {
sync.Mutex
}
// NewProfit generates the profit object from the current position
func (p *Position) NewProfit(profit, netProfit fixedpoint.Value, trade Trade) *Profit {
return &Profit{
Symbol: p.Symbol,
QuoteCurrency: p.QuoteCurrency,
BaseCurrency: p.BaseCurrency,
AverageCost: p.AverageCost,
// profit related fields
Profit: profit,
NetProfit: netProfit,
ProfitMargin: profit.Div(trade.QuoteQuantity),
NetProfitMargin: netProfit.Div(trade.QuoteQuantity),
// trade related fields
TradeID: trade.ID,
Price: trade.Price,
Quantity: trade.Quantity,
QuoteQuantity: trade.QuoteQuantity,
IsMaker: trade.IsMaker,
IsBuyer: trade.IsBuyer,
Side: trade.Side,
Fee: trade.Fee,
FeeCurrency: trade.FeeCurrency,
TradedAt: trade.Time.Time(),
IsFutures: trade.IsFutures,
IsMargin: trade.IsMargin,
IsIsolated: trade.IsIsolated,
}
}
func (p *Position) NewClosePositionOrder(percentage fixedpoint.Value) *SubmitOrder {
base := p.GetBase()
quantity := base.Mul(percentage)

View File

@ -12,14 +12,21 @@ import (
// Profit struct stores the PnL information
type Profit struct {
// --- position related fields
// -------------------------------------------
// Symbol is the symbol of the position
Symbol string `json:"symbol"`
QuoteCurrency string `json:"quoteCurrency" db:"quote_currency"`
BaseCurrency string `json:"baseCurrency" db:"base_currency"`
AverageCost fixedpoint.Value `json:"averageCost" db:"average_cost"`
// profit related fields
// -------------------------------------------
// Profit is the profit of this trade made. negative profit means loss.
Profit fixedpoint.Value `json:"profit" db:"profit"`
// NetProfit is (profit - trading fee)
NetProfit fixedpoint.Value `json:"netProfit" db:"net_profit"`
AverageCost fixedpoint.Value `json:"averageCost" db:"average_cost"`
// ProfitMargin is a percentage of the profit and the capital amount
ProfitMargin fixedpoint.Value `json:"profitMargin" db:"profit_margin"`
@ -27,29 +34,31 @@ type Profit struct {
// NetProfitMargin is a percentage of the net profit and the capital amount
NetProfitMargin fixedpoint.Value `json:"netProfitMargin" db:"net_profit_margin"`
QuoteCurrency string `json:"quoteCurrency" db:"quote_currency"`
BaseCurrency string `json:"baseCurrency" db:"base_currency"`
// trade related fields
// --------------------------------------------
// TradeID is the exchange trade id of that trade
TradeID uint64 `json:"tradeID" db:"trade_id"`
Side string `json:"side" db:"side"`
Side SideType `json:"side" db:"side"`
IsBuyer bool `json:"isBuyer" db:"is_buyer"`
IsMaker bool `json:"isMaker" db:"is_maker"`
Price fixedpoint.Value `json:"price" db:"price"`
Quantity fixedpoint.Value `json:"quantity"`
TradeAmount fixedpoint.Value `json:"quoteQuantity" db:"quote_quantity"`
Quantity fixedpoint.Value `json:"quantity" db:"quantity"`
QuoteQuantity fixedpoint.Value `json:"quoteQuantity" db:"quote_quantity"`
// 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"`
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"`
Exchange ExchangeName `json:"exchange" db:"exchange"`
IsMargin bool `json:"isMargin" db:"is_margin"`
IsFutures bool `json:"isFutures" db:"is_futures"`
IsIsolated bool `json:"isIsolated" db:"is_isolated"`
TradedAt time.Time `json:"tradedAt" db:"traded_at"`
// strategy related fields
Strategy string `json:"strategy" db:"strategy"`
StrategyInstanceID string `json:"strategyInstanceID" db:"strategy_instance_id"`
}
func (p *Profit) SlackAttachment() slack.Attachment {
@ -84,10 +93,10 @@ func (p *Profit) SlackAttachment() slack.Attachment {
})
}
if !p.TradeAmount.IsZero() {
if !p.QuoteQuantity.IsZero() {
fields = append(fields, slack.AttachmentField{
Title: "Trade Amount",
Value: p.TradeAmount.String() + " " + p.QuoteCurrency,
Value: p.QuoteQuantity.String() + " " + p.QuoteCurrency,
Short: true,
})
}