types: use passed time to reset today pnl

This commit is contained in:
c9s 2022-09-19 09:32:26 +08:00
parent 26cf048c84
commit f9f2df29e7
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 6 additions and 5 deletions

View File

@ -2,6 +2,7 @@ package xmaker
import (
"sync"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
@ -57,7 +58,7 @@ func (s *ProfitStats) AddTrade(trade types.Trade) {
}
func (s *ProfitStats) ResetToday() {
s.ProfitStats.ResetToday()
s.ProfitStats.ResetToday(time.Now())
s.lock.Lock()
s.TodayMakerVolume = fixedpoint.Zero

View File

@ -198,7 +198,7 @@ func (s *ProfitStats) Init(market Market) {
func (s *ProfitStats) AddProfit(profit Profit) {
if s.IsOver24Hours() {
s.ResetToday()
s.ResetToday(profit.TradedAt)
}
// since field guard
@ -227,7 +227,7 @@ func (s *ProfitStats) AddProfit(profit Profit) {
func (s *ProfitStats) AddTrade(trade Trade) {
if s.IsOver24Hours() {
s.ResetToday()
s.ResetToday(trade.Time.Time())
}
s.AccumulatedVolume = s.AccumulatedVolume.Add(trade.Quantity)
@ -238,13 +238,13 @@ func (s *ProfitStats) IsOver24Hours() bool {
return time.Since(time.Unix(s.TodaySince, 0)) >= 24*time.Hour
}
func (s *ProfitStats) ResetToday() {
func (s *ProfitStats) ResetToday(t time.Time) {
s.TodayPnL = fixedpoint.Zero
s.TodayNetProfit = fixedpoint.Zero
s.TodayGrossProfit = fixedpoint.Zero
s.TodayGrossLoss = fixedpoint.Zero
var beginningOfTheDay = BeginningOfTheDay(time.Now().Local())
var beginningOfTheDay = BeginningOfTheDay(t.Local())
s.TodaySince = beginningOfTheDay.Unix()
}