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

View File

@ -41,8 +41,8 @@ func init() {
type State struct { type State struct {
HedgePosition fixedpoint.Value `json:"hedgePosition"` HedgePosition fixedpoint.Value `json:"hedgePosition"`
CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty"` CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty"`
Position *bbgo.Position `json:"position,omitempty"` Position *bbgo.Position `json:"position,omitempty"`
ProfitStats bbgo.ProfitStats `json:"profitStats,omitempty"` ProfitStats ProfitStats `json:"profitStats,omitempty"`
} }
type ProfitStats struct { type ProfitStats struct {
@ -623,7 +623,10 @@ func (s *Strategy) processTrade(trade types.Trade) {
s.state.ProfitStats.AddTrade(trade) s.state.ProfitStats.AddTrade(trade)
if profit, netProfit, madeProfit := s.state.Position.AddTrade(trade); madeProfit { 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) profitMargin := profit.DivFloat64(trade.QuoteQuantity)
netProfitMargin := netProfit.DivFloat64(trade.QuoteQuantity) netProfitMargin := netProfit.DivFloat64(trade.QuoteQuantity)