add UpdatePnL method for updating trade pnl field

This commit is contained in:
c9s 2021-02-15 20:51:25 +08:00
parent 777d673b14
commit f3d65b1281

View File

@ -1,6 +1,8 @@
package service
import (
"context"
"fmt"
"strconv"
"strings"
"time"
@ -12,6 +14,16 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
type QueryTradesOptions struct {
Exchange types.ExchangeName
Symbol string
LastGID int64
// ASC or DESC
Ordering string
Limit int
}
type TradingVolume struct {
Year int `db:"year" json:"year"`
Month int `db:"month" json:"month,omitempty"`
@ -80,7 +92,6 @@ func (s *TradeService) QueryTradingVolume(startTime time.Time, options TradingVo
return records, rows.Err()
}
func generateSqliteTradingVolumeSQL(options TradingVolumeQueryOptions) string {
var sel []string
var groupBys []string
@ -127,7 +138,6 @@ func generateSqliteTradingVolumeSQL(options TradingVolumeQueryOptions) string {
return sql
}
func generateMysqlTradingVolumeQuerySQL(options TradingVolumeQueryOptions) string {
var sel []string
var groupBys []string
@ -137,8 +147,6 @@ func generateMysqlTradingVolumeQuerySQL(options TradingVolumeQueryOptions) strin
switch options.GroupByPeriod {
case "month":
sel = append(sel, "YEAR(traded_at) AS year", "MONTH(traded_at) AS month")
groupBys = append([]string{"MONTH(traded_at)", "YEAR(traded_at)"}, groupBys...)
orderBys = append(orderBys, "year ASC", "month ASC")
@ -221,15 +229,6 @@ func (s *TradeService) QueryForTradingFeeCurrency(ex types.ExchangeName, symbol
return s.scanRows(rows)
}
type QueryTradesOptions struct {
Exchange types.ExchangeName
Symbol string
LastGID int64
// ASC or DESC
Ordering string
Limit int
}
func (s *TradeService) Query(options QueryTradesOptions) ([]types.Trade, error) {
sql := queryTradesSQL(options)
@ -249,6 +248,28 @@ func (s *TradeService) Query(options QueryTradesOptions) ([]types.Trade, error)
return s.scanRows(rows)
}
func (s *TradeService) UpdatePnL(ctx context.Context, id int64, pnl float64) error {
result, err := s.DB.NamedExecContext(ctx, "UPDATE `trades` SET `pnl` = :pnl WHERE `id` = :id", map[string]interface{}{
"id": id,
"pnl": pnl,
})
if err != nil {
return err
}
cnt, err := result.RowsAffected()
if err != nil {
return err
}
if cnt == 0 {
return fmt.Errorf("trade id:%d not found", id)
}
return nil
}
func queryTradesSQL(options QueryTradesOptions) string {
ordering := "ASC"
switch v := strings.ToUpper(options.Ordering); v {