types: group profits by order id

This commit is contained in:
c9s 2022-09-07 11:59:08 +08:00
parent 4c3334c482
commit 6b4661783d
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 9 additions and 2 deletions

View File

@ -104,7 +104,9 @@ func (p *Position) NewProfit(trade Trade, profit, netProfit fixedpoint.Value) Pr
NetProfit: netProfit, NetProfit: netProfit,
ProfitMargin: profit.Div(trade.QuoteQuantity), ProfitMargin: profit.Div(trade.QuoteQuantity),
NetProfitMargin: netProfit.Div(trade.QuoteQuantity), NetProfitMargin: netProfit.Div(trade.QuoteQuantity),
// trade related fields // trade related fields
Trade: &trade,
TradeID: trade.ID, TradeID: trade.ID,
OrderID: trade.OrderID, OrderID: trade.OrderID,
Side: trade.Side, Side: trade.Side,

View File

@ -37,6 +37,7 @@ type Profit struct {
// trade related fields // trade related fields
// -------------------------------------------- // --------------------------------------------
// TradeID is the exchange trade id of that trade // TradeID is the exchange trade id of that trade
Trade *Trade `json:"trade,omitempty" db:"-"`
TradeID uint64 `json:"tradeID" db:"trade_id"` TradeID uint64 `json:"tradeID" db:"trade_id"`
OrderID uint64 `json:"orderID,omitempty"` OrderID uint64 `json:"orderID,omitempty"`
Side SideType `json:"side" db:"side"` Side SideType `json:"side" db:"side"`

View File

@ -169,7 +169,7 @@ type TradeStats struct {
Profits []fixedpoint.Value `json:"profits,omitempty" yaml:"profits,omitempty"` Profits []fixedpoint.Value `json:"profits,omitempty" yaml:"profits,omitempty"`
Losses []fixedpoint.Value `json:"losses,omitempty" yaml:"losses,omitempty"` Losses []fixedpoint.Value `json:"losses,omitempty" yaml:"losses,omitempty"`
orderProfits map[uint64]*Profit orderProfits map[uint64][]*Profit
LargestProfitTrade fixedpoint.Value `json:"largestProfitTrade,omitempty" yaml:"largestProfitTrade"` LargestProfitTrade fixedpoint.Value `json:"largestProfitTrade,omitempty" yaml:"largestProfitTrade"`
LargestLossTrade fixedpoint.Value `json:"largestLossTrade,omitempty" yaml:"largestLossTrade"` LargestLossTrade fixedpoint.Value `json:"largestLossTrade,omitempty" yaml:"largestLossTrade"`
@ -244,7 +244,11 @@ func (s *TradeStats) Add(profit *Profit) {
} }
if s.orderProfits == nil { if s.orderProfits == nil {
s.orderProfits = make(map[uint64]*Profit) s.orderProfits = make(map[uint64][]*Profit)
}
if profit.OrderID > 0 {
s.orderProfits[profit.OrderID] = append(s.orderProfits[profit.OrderID], profit)
} }
s.add(profit.Profit) s.add(profit.Profit)