use the profit struct to pass profit info

This commit is contained in:
c9s 2021-10-08 19:16:40 +08:00
parent fac14a8c7f
commit 45645d0a3d
2 changed files with 17 additions and 14 deletions

View File

@ -38,19 +38,19 @@ type ProfitStats struct {
TodaySince int64 `json:"todaySince,omitempty"`
}
func (s *ProfitStats) AddProfit(profit, netProfit fixedpoint.Value) {
s.AccumulatedPnL += profit
s.AccumulatedNetProfit += netProfit
func (s *ProfitStats) AddProfit(profit Profit) {
s.AccumulatedPnL += profit.Profit
s.AccumulatedNetProfit += profit.NetProfit
s.TodayPnL += profit
s.TodayNetProfit += netProfit
s.TodayPnL += profit.Profit
s.TodayNetProfit += profit.NetProfit
if profit < 0 {
s.AccumulatedLoss += profit
s.TodayLoss += profit
} else if profit > 0 {
s.AccumulatedProfit += profit
s.TodayProfit += profit
if profit.Profit < 0 {
s.AccumulatedLoss += profit.Profit
s.TodayLoss += profit.Profit
} else if profit.Profit > 0 {
s.AccumulatedProfit += profit.Profit
s.TodayProfit += profit.Profit
}
}

View File

@ -42,7 +42,7 @@ type State struct {
HedgePosition fixedpoint.Value `json:"hedgePosition"`
CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty"`
Position *bbgo.Position `json:"position,omitempty"`
ProfitStats bbgo.ProfitStats `json:"profitStats,omitempty"`
ProfitStats ProfitStats `json:"profitStats,omitempty"`
}
type ProfitStats struct {
@ -623,7 +623,10 @@ func (s *Strategy) processTrade(trade types.Trade) {
s.state.ProfitStats.AddTrade(trade)
if profit, netProfit, madeProfit := s.state.Position.AddTrade(trade); madeProfit {
s.state.ProfitStats.AddProfit(profit, netProfit)
s.state.ProfitStats.AddProfit(bbgo.Profit{
Profit: profit,
NetProfit: netProfit,
})
profitMargin := profit.DivFloat64(trade.QuoteQuantity)
netProfitMargin := netProfit.DivFloat64(trade.QuoteQuantity)