mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
all: update profit struct fields
This commit is contained in:
parent
82e5520ee4
commit
197d750cb4
|
@ -23,18 +23,19 @@ func TestProfitService(t *testing.T) {
|
||||||
service := &ProfitService{DB: xdb}
|
service := &ProfitService{DB: xdb}
|
||||||
|
|
||||||
err = service.Insert(types.Profit{
|
err = service.Insert(types.Profit{
|
||||||
Symbol: "BTCUSDT",
|
Symbol: "BTCUSDT",
|
||||||
BaseCurrency: "BTC",
|
BaseCurrency: "BTC",
|
||||||
QuoteCurrency: "USDT",
|
QuoteCurrency: "USDT",
|
||||||
AverageCost: fixedpoint.NewFromFloat(44000),
|
AverageCost: fixedpoint.NewFromFloat(44000),
|
||||||
Profit: fixedpoint.NewFromFloat(1.01),
|
Profit: fixedpoint.NewFromFloat(1.01),
|
||||||
NetProfit: fixedpoint.NewFromFloat(0.98),
|
NetProfit: fixedpoint.NewFromFloat(0.98),
|
||||||
TradeID: 99,
|
TradeID: 99,
|
||||||
|
Side: types.SideTypeSell,
|
||||||
Price: fixedpoint.NewFromFloat(44300),
|
Price: fixedpoint.NewFromFloat(44300),
|
||||||
Quantity: fixedpoint.NewFromFloat(0.001),
|
Quantity: fixedpoint.NewFromFloat(0.001),
|
||||||
TradeAmount: fixedpoint.NewFromFloat(44.0),
|
QuoteQuantity: fixedpoint.NewFromFloat(44.0),
|
||||||
Exchange: types.ExchangeMax,
|
Exchange: types.ExchangeMax,
|
||||||
Time: time.Now(),
|
TradedAt: time.Now(),
|
||||||
})
|
})
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -575,12 +575,12 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
Symbol: s.Symbol,
|
Symbol: s.Symbol,
|
||||||
Profit: profit,
|
Profit: profit,
|
||||||
NetProfit: netProfit,
|
NetProfit: netProfit,
|
||||||
TradeAmount: trade.QuoteQuantity,
|
QuoteQuantity: trade.QuoteQuantity,
|
||||||
ProfitMargin: profit.Div(trade.QuoteQuantity),
|
ProfitMargin: profit.Div(trade.QuoteQuantity),
|
||||||
NetProfitMargin: netProfit.Div(trade.QuoteQuantity),
|
NetProfitMargin: netProfit.Div(trade.QuoteQuantity),
|
||||||
QuoteCurrency: s.state.Position.QuoteCurrency,
|
QuoteCurrency: s.state.Position.QuoteCurrency,
|
||||||
BaseCurrency: s.state.Position.BaseCurrency,
|
BaseCurrency: s.state.Position.BaseCurrency,
|
||||||
Time: trade.Time.Time(),
|
TradedAt: trade.Time.Time(),
|
||||||
}
|
}
|
||||||
s.state.ProfitStats.AddProfit(p)
|
s.state.ProfitStats.AddProfit(p)
|
||||||
s.Notify(&p)
|
s.Notify(&p)
|
||||||
|
|
|
@ -767,12 +767,12 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order
|
||||||
Symbol: s.Symbol,
|
Symbol: s.Symbol,
|
||||||
Profit: profit,
|
Profit: profit,
|
||||||
NetProfit: netProfit,
|
NetProfit: netProfit,
|
||||||
TradeAmount: trade.QuoteQuantity,
|
QuoteQuantity: trade.QuoteQuantity,
|
||||||
ProfitMargin: profit.Div(trade.QuoteQuantity),
|
ProfitMargin: profit.Div(trade.QuoteQuantity),
|
||||||
NetProfitMargin: netProfit.Div(trade.QuoteQuantity),
|
NetProfitMargin: netProfit.Div(trade.QuoteQuantity),
|
||||||
QuoteCurrency: s.state.Position.QuoteCurrency,
|
QuoteCurrency: s.state.Position.QuoteCurrency,
|
||||||
BaseCurrency: s.state.Position.BaseCurrency,
|
BaseCurrency: s.state.Position.BaseCurrency,
|
||||||
Time: trade.Time.Time(),
|
TradedAt: trade.Time.Time(),
|
||||||
}
|
}
|
||||||
s.state.ProfitStats.AddProfit(p)
|
s.state.ProfitStats.AddProfit(p)
|
||||||
s.Notify(&p)
|
s.Notify(&p)
|
||||||
|
|
|
@ -53,6 +53,40 @@ type Position struct {
|
||||||
sync.Mutex
|
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 {
|
func (p *Position) NewClosePositionOrder(percentage fixedpoint.Value) *SubmitOrder {
|
||||||
base := p.GetBase()
|
base := p.GetBase()
|
||||||
quantity := base.Mul(percentage)
|
quantity := base.Mul(percentage)
|
||||||
|
|
|
@ -12,14 +12,21 @@ import (
|
||||||
|
|
||||||
// Profit struct stores the PnL information
|
// Profit struct stores the PnL information
|
||||||
type Profit struct {
|
type Profit struct {
|
||||||
Symbol string `json:"symbol"`
|
// --- 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 is the profit of this trade made. negative profit means loss.
|
||||||
Profit fixedpoint.Value `json:"profit" db:"profit"`
|
Profit fixedpoint.Value `json:"profit" db:"profit"`
|
||||||
|
|
||||||
// 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_cost"`
|
|
||||||
|
|
||||||
// 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"`
|
||||||
|
@ -27,29 +34,31 @@ type Profit struct {
|
||||||
// NetProfitMargin is a percentage of the net profit and the capital amount
|
// NetProfitMargin is a percentage of the net profit and the capital amount
|
||||||
NetProfitMargin fixedpoint.Value `json:"netProfitMargin" db:"net_profit_margin"`
|
NetProfitMargin fixedpoint.Value `json:"netProfitMargin" db:"net_profit_margin"`
|
||||||
|
|
||||||
QuoteCurrency string `json:"quoteCurrency" db:"quote_currency"`
|
// trade related fields
|
||||||
BaseCurrency string `json:"baseCurrency" db:"base_currency"`
|
// --------------------------------------------
|
||||||
|
// TradeID is the exchange trade id of that trade
|
||||||
|
TradeID uint64 `json:"tradeID" db:"trade_id"`
|
||||||
|
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" db:"quantity"`
|
||||||
|
QuoteQuantity fixedpoint.Value `json:"quoteQuantity" db:"quote_quantity"`
|
||||||
|
|
||||||
TradeID uint64 `json:"tradeID" db:"trade_id"`
|
|
||||||
Side string `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"`
|
|
||||||
// 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"`
|
||||||
Fee fixedpoint.Value `json:"fee" db:"fee"`
|
Fee fixedpoint.Value `json:"fee" db:"fee"`
|
||||||
FeeCurrency string `json:"feeCurrency" db:"fee_currency"`
|
FeeCurrency string `json:"feeCurrency" db:"fee_currency"`
|
||||||
Time time.Time `json:"tradedAt" db:"traded_at"`
|
Exchange ExchangeName `json:"exchange" db:"exchange"`
|
||||||
Strategy string `json:"strategy" db:"strategy"`
|
IsMargin bool `json:"isMargin" db:"is_margin"`
|
||||||
StrategyInstanceID string `json:"strategyInstanceID" db:"strategy_instance_id"`
|
IsFutures bool `json:"isFutures" db:"is_futures"`
|
||||||
|
IsIsolated bool `json:"isIsolated" db:"is_isolated"`
|
||||||
|
TradedAt time.Time `json:"tradedAt" db:"traded_at"`
|
||||||
|
|
||||||
Exchange ExchangeName `json:"exchange" db:"exchange"`
|
// strategy related fields
|
||||||
IsMargin bool `json:"isMargin" db:"is_margin"`
|
Strategy string `json:"strategy" db:"strategy"`
|
||||||
IsFutures bool `json:"isFutures" db:"is_futures"`
|
StrategyInstanceID string `json:"strategyInstanceID" db:"strategy_instance_id"`
|
||||||
IsIsolated bool `json:"isIsolated" db:"is_isolated"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Profit) SlackAttachment() slack.Attachment {
|
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{
|
fields = append(fields, slack.AttachmentField{
|
||||||
Title: "Trade Amount",
|
Title: "Trade Amount",
|
||||||
Value: p.TradeAmount.String() + " " + p.QuoteCurrency,
|
Value: p.QuoteQuantity.String() + " " + p.QuoteCurrency,
|
||||||
Short: true,
|
Short: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user