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)
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")
}
@ -595,7 +595,7 @@ func (environ *Environment) RecordPosition(position *types.Position, trade types
log.WithError(err).Errorf("can not insert profit record: %+v", profit)
}
} 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")
}
}

View File

@ -51,7 +51,11 @@ func (s *PositionService) scanRows(rows *sqlx.Rows) (positions []types.Position,
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(`
INSERT INTO positions (
strategy,

View File

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