add net profit arg to the position insert method

This commit is contained in:
c9s 2024-11-15 16:41:16 +08:00
parent e26156d77b
commit 9d0898ecc6
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 9 additions and 5 deletions

View File

@ -587,7 +587,7 @@ func (environ *Environment) RecordPosition(position *types.Position, trade types
log.Infof("recordPosition: position = %s, trade = %+v, profit = %+v", position.Base.String(), trade, profit) log.Infof("recordPosition: position = %s, trade = %+v, profit = %+v", position.Base.String(), trade, profit)
if profit != nil { if profit != nil {
if err := environ.PositionService.Insert(position, trade, profit.Profit); err != nil { if err := environ.PositionService.Insert(position, trade, profit.Profit, profit.NetProfit); err != nil {
log.WithError(err).Errorf("can not insert position record") log.WithError(err).Errorf("can not insert position record")
} }
@ -595,7 +595,7 @@ func (environ *Environment) RecordPosition(position *types.Position, trade types
log.WithError(err).Errorf("can not insert profit record: %+v", profit) log.WithError(err).Errorf("can not insert profit record: %+v", profit)
} }
} else { } else {
if err := environ.PositionService.Insert(position, trade, fixedpoint.Zero); err != nil { if err := environ.PositionService.Insert(position, trade, fixedpoint.Zero, fixedpoint.Zero); err != nil {
log.WithError(err).Errorf("can not insert position record") log.WithError(err).Errorf("can not insert position record")
} }
} }

View File

@ -51,7 +51,11 @@ func (s *PositionService) scanRows(rows *sqlx.Rows) (positions []types.Position,
return positions, rows.Err() return positions, rows.Err()
} }
func (s *PositionService) Insert(position *types.Position, trade types.Trade, profit fixedpoint.Value) error { func (s *PositionService) Insert(
position *types.Position,
trade types.Trade,
profit, netProfit fixedpoint.Value,
) error {
_, err := s.DB.NamedExec(` _, err := s.DB.NamedExec(`
INSERT INTO positions ( INSERT INTO positions (
strategy, strategy,

View File

@ -34,7 +34,7 @@ func TestPositionService(t *testing.T) {
ChangedAt: time.Now(), ChangedAt: time.Now(),
}, types.Trade{ }, types.Trade{
Time: types.Time(time.Now()), Time: types.Time(time.Now()),
}, fixedpoint.Zero) }, fixedpoint.Zero, fixedpoint.Zero)
assert.NoError(t, err) assert.NoError(t, err)
}) })
@ -54,7 +54,7 @@ func TestPositionService(t *testing.T) {
Exchange: types.ExchangeBinance, Exchange: types.ExchangeBinance,
Side: types.SideTypeSell, Side: types.SideTypeSell,
Time: types.Time(time.Now()), Time: types.Time(time.Now()),
}, fixedpoint.NewFromFloat(10.9)) }, fixedpoint.NewFromFloat(10.9), fixedpoint.Zero)
assert.NoError(t, err) assert.NoError(t, err)
}) })