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 package service
import ( import (
"context"
"fmt"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -12,6 +14,16 @@ import (
"github.com/c9s/bbgo/pkg/types" "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 { type TradingVolume struct {
Year int `db:"year" json:"year"` Year int `db:"year" json:"year"`
Month int `db:"month" json:"month,omitempty"` Month int `db:"month" json:"month,omitempty"`
@ -80,7 +92,6 @@ func (s *TradeService) QueryTradingVolume(startTime time.Time, options TradingVo
return records, rows.Err() return records, rows.Err()
} }
func generateSqliteTradingVolumeSQL(options TradingVolumeQueryOptions) string { func generateSqliteTradingVolumeSQL(options TradingVolumeQueryOptions) string {
var sel []string var sel []string
var groupBys []string var groupBys []string
@ -127,7 +138,6 @@ func generateSqliteTradingVolumeSQL(options TradingVolumeQueryOptions) string {
return sql return sql
} }
func generateMysqlTradingVolumeQuerySQL(options TradingVolumeQueryOptions) string { func generateMysqlTradingVolumeQuerySQL(options TradingVolumeQueryOptions) string {
var sel []string var sel []string
var groupBys []string var groupBys []string
@ -137,8 +147,6 @@ func generateMysqlTradingVolumeQuerySQL(options TradingVolumeQueryOptions) strin
switch options.GroupByPeriod { switch options.GroupByPeriod {
case "month": case "month":
sel = append(sel, "YEAR(traded_at) AS year", "MONTH(traded_at) AS month") sel = append(sel, "YEAR(traded_at) AS year", "MONTH(traded_at) AS month")
groupBys = append([]string{"MONTH(traded_at)", "YEAR(traded_at)"}, groupBys...) groupBys = append([]string{"MONTH(traded_at)", "YEAR(traded_at)"}, groupBys...)
orderBys = append(orderBys, "year ASC", "month ASC") orderBys = append(orderBys, "year ASC", "month ASC")
@ -221,15 +229,6 @@ func (s *TradeService) QueryForTradingFeeCurrency(ex types.ExchangeName, symbol
return s.scanRows(rows) 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) { func (s *TradeService) Query(options QueryTradesOptions) ([]types.Trade, error) {
sql := queryTradesSQL(options) sql := queryTradesSQL(options)
@ -249,6 +248,28 @@ func (s *TradeService) Query(options QueryTradesOptions) ([]types.Trade, error)
return s.scanRows(rows) 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 { func queryTradesSQL(options QueryTradesOptions) string {
ordering := "ASC" ordering := "ASC"
switch v := strings.ToUpper(options.Ordering); v { switch v := strings.ToUpper(options.Ordering); v {