mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
bbgo: refactor profit stats
This commit is contained in:
parent
9e1d28f3b3
commit
d058125f78
60
pkg/bbgo/profitstats.go
Normal file
60
pkg/bbgo/profitstats.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package bbgo
|
||||
|
||||
import (
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"github.com/c9s/bbgo/pkg/util"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ProfitStats struct {
|
||||
MakerExchange types.ExchangeName `json:"makerExchange"`
|
||||
|
||||
AccumulatedPnL fixedpoint.Value `json:"accumulatedPnL,omitempty"`
|
||||
AccumulatedNetProfit fixedpoint.Value `json:"accumulatedNetProfit,omitempty"`
|
||||
AccumulatedProfit fixedpoint.Value `json:"accumulatedProfit,omitempty"`
|
||||
AccumulatedLoss fixedpoint.Value `json:"accumulatedLoss,omitempty"`
|
||||
AccumulatedSince int64 `json:"accumulatedSince,omitempty"`
|
||||
|
||||
TodayPnL fixedpoint.Value `json:"todayPnL,omitempty"`
|
||||
TodayNetProfit fixedpoint.Value `json:"todayNetProfit,omitempty"`
|
||||
TodayProfit fixedpoint.Value `json:"todayProfit,omitempty"`
|
||||
TodayLoss fixedpoint.Value `json:"todayLoss,omitempty"`
|
||||
TodaySince int64 `json:"todaySince,omitempty"`
|
||||
}
|
||||
|
||||
func (s *ProfitStats) AddProfit(profit, netProfit fixedpoint.Value) {
|
||||
s.AccumulatedPnL += profit
|
||||
s.AccumulatedNetProfit += netProfit
|
||||
|
||||
s.TodayPnL += profit
|
||||
s.TodayNetProfit += netProfit
|
||||
|
||||
if profit < 0 {
|
||||
s.AccumulatedLoss += profit
|
||||
s.TodayLoss += profit
|
||||
} else if profit > 0 {
|
||||
s.AccumulatedProfit += profit
|
||||
s.TodayProfit += profit
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ProfitStats) AddTrade(trade types.Trade) {
|
||||
if s.IsOver24Hours() {
|
||||
s.ResetToday()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ProfitStats) IsOver24Hours() bool {
|
||||
return time.Since(time.Unix(s.TodaySince, 0)) > 24*time.Hour
|
||||
}
|
||||
|
||||
func (s *ProfitStats) ResetToday() {
|
||||
s.TodayPnL = 0
|
||||
s.TodayNetProfit = 0
|
||||
s.TodayProfit = 0
|
||||
s.TodayLoss = 0
|
||||
|
||||
var beginningOfTheDay = util.BeginningOfTheDay(time.Now().Local())
|
||||
s.TodaySince = beginningOfTheDay.Unix()
|
||||
}
|
|
@ -14,7 +14,6 @@ import (
|
|||
"github.com/c9s/bbgo/pkg/indicator"
|
||||
"github.com/c9s/bbgo/pkg/service"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"github.com/c9s/bbgo/pkg/util"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
@ -42,52 +41,26 @@ func init() {
|
|||
type State struct {
|
||||
HedgePosition fixedpoint.Value `json:"hedgePosition"`
|
||||
CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty"`
|
||||
Position *bbgo.Position `json:"position,omitempty"`
|
||||
ProfitStats ProfitStats `json:"profitStats,omitempty"`
|
||||
Position *bbgo.Position `json:"position,omitempty"`
|
||||
ProfitStats bbgo.ProfitStats `json:"profitStats,omitempty"`
|
||||
}
|
||||
|
||||
type ProfitStats struct {
|
||||
bbgo.ProfitStats
|
||||
|
||||
MakerExchange types.ExchangeName `json:"makerExchange"`
|
||||
|
||||
AccumulatedMakerVolume fixedpoint.Value `json:"accumulatedMakerVolume,omitempty"`
|
||||
AccumulatedMakerBidVolume fixedpoint.Value `json:"accumulatedMakerBidVolume,omitempty"`
|
||||
AccumulatedMakerAskVolume fixedpoint.Value `json:"accumulatedMakerAskVolume,omitempty"`
|
||||
AccumulatedPnL fixedpoint.Value `json:"accumulatedPnL,omitempty"`
|
||||
AccumulatedNetProfit fixedpoint.Value `json:"accumulatedNetProfit,omitempty"`
|
||||
AccumulatedProfit fixedpoint.Value `json:"accumulatedProfit,omitempty"`
|
||||
AccumulatedLoss fixedpoint.Value `json:"accumulatedLoss,omitempty"`
|
||||
AccumulatedSince int64 `json:"accumulatedSince,omitempty"`
|
||||
|
||||
TodayMakerVolume fixedpoint.Value `json:"todayMakerVolume,omitempty"`
|
||||
TodayMakerBidVolume fixedpoint.Value `json:"todayMakerBidVolume,omitempty"`
|
||||
TodayMakerAskVolume fixedpoint.Value `json:"todayMakerAskVolume,omitempty"`
|
||||
TodayPnL fixedpoint.Value `json:"todayPnL,omitempty"`
|
||||
TodayNetProfit fixedpoint.Value `json:"todayNetProfit,omitempty"`
|
||||
TodayProfit fixedpoint.Value `json:"todayProfit,omitempty"`
|
||||
TodayLoss fixedpoint.Value `json:"todayLoss,omitempty"`
|
||||
TodaySince int64 `json:"todaySince,omitempty"`
|
||||
}
|
||||
|
||||
func (s *ProfitStats) AddProfit(profit, netProfit fixedpoint.Value) {
|
||||
s.AccumulatedPnL += profit
|
||||
s.AccumulatedNetProfit += netProfit
|
||||
|
||||
s.TodayPnL += profit
|
||||
s.TodayNetProfit += netProfit
|
||||
|
||||
if profit < 0 {
|
||||
s.AccumulatedLoss += profit
|
||||
s.TodayLoss += profit
|
||||
} else if profit > 0 {
|
||||
s.AccumulatedProfit += profit
|
||||
s.TodayProfit += profit
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ProfitStats) AddTrade(trade types.Trade) {
|
||||
if s.IsOver24Hours() {
|
||||
s.ResetToday()
|
||||
}
|
||||
s.ProfitStats.AddTrade(trade)
|
||||
|
||||
if trade.Exchange == s.MakerExchange {
|
||||
s.AccumulatedMakerVolume.AtomicAdd(fixedpoint.NewFromFloat(trade.Quantity))
|
||||
|
@ -107,21 +80,11 @@ func (s *ProfitStats) AddTrade(trade types.Trade) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *ProfitStats) IsOver24Hours() bool {
|
||||
return time.Since(time.Unix(s.TodaySince, 0)) > 24*time.Hour
|
||||
}
|
||||
|
||||
func (s *ProfitStats) ResetToday() {
|
||||
s.ProfitStats.ResetToday()
|
||||
s.TodayMakerVolume = 0
|
||||
s.TodayMakerBidVolume = 0
|
||||
s.TodayMakerAskVolume = 0
|
||||
s.TodayPnL = 0
|
||||
s.TodayNetProfit = 0
|
||||
s.TodayProfit = 0
|
||||
s.TodayLoss = 0
|
||||
|
||||
var beginningOfTheDay = util.BeginningOfTheDay(time.Now().Local())
|
||||
s.TodaySince = beginningOfTheDay.Unix()
|
||||
}
|
||||
|
||||
type Strategy struct {
|
||||
|
|
Loading…
Reference in New Issue
Block a user