mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
Merge pull request #758 from c9s/improve/pnl-cmd
improve: add pnl cmd options and fix trade query
This commit is contained in:
commit
7398afbde7
140
pkg/cmd/pnl.go
140
pkg/cmd/pnl.go
|
@ -18,10 +18,12 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
PnLCmd.Flags().String("session", "", "target exchange")
|
||||
PnLCmd.Flags().StringArray("session", []string{}, "target exchange sessions")
|
||||
PnLCmd.Flags().String("symbol", "", "trading symbol")
|
||||
PnLCmd.Flags().Bool("include-transfer", false, "convert transfer records into trades")
|
||||
PnLCmd.Flags().Int("limit", 0, "number of trades")
|
||||
PnLCmd.Flags().Bool("sync", false, "sync before loading trades")
|
||||
PnLCmd.Flags().String("since", "", "query trades from a time point")
|
||||
PnLCmd.Flags().Uint64("limit", 0, "number of trades")
|
||||
RootCmd.AddCommand(PnLCmd)
|
||||
}
|
||||
|
||||
|
@ -33,7 +35,16 @@ var PnLCmd = &cobra.Command{
|
|||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
ctx := context.Background()
|
||||
|
||||
sessionName, err := cmd.Flags().GetString("session")
|
||||
sessionNames, err := cmd.Flags().GetStringArray("session")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(sessionNames) == 0 {
|
||||
return errors.New("--session [SESSION] is required")
|
||||
}
|
||||
|
||||
wantSync, err := cmd.Flags().GetBool("sync")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -47,7 +58,30 @@ var PnLCmd = &cobra.Command{
|
|||
return errors.New("--symbol [SYMBOL] is required")
|
||||
}
|
||||
|
||||
limit, err := cmd.Flags().GetInt("limit")
|
||||
// this is the default since
|
||||
since := time.Now().AddDate(-1, 0, 0)
|
||||
|
||||
sinceOpt, err := cmd.Flags().GetString("since")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if sinceOpt != "" {
|
||||
lt, err := types.ParseLooseFormatTime(sinceOpt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
since = lt.Time()
|
||||
}
|
||||
|
||||
until := time.Now()
|
||||
|
||||
includeTransfer, err := cmd.Flags().GetBool("include-transfer")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
limit, err := cmd.Flags().GetUint64("limit")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -62,64 +96,58 @@ var PnLCmd = &cobra.Command{
|
|||
return err
|
||||
}
|
||||
|
||||
session, ok := environ.Session(sessionName)
|
||||
if !ok {
|
||||
return fmt.Errorf("session %s not found", sessionName)
|
||||
}
|
||||
for _, sessionName := range sessionNames {
|
||||
session, ok := environ.Session(sessionName)
|
||||
if !ok {
|
||||
return fmt.Errorf("session %s not found", sessionName)
|
||||
}
|
||||
|
||||
if err := environ.SyncSession(ctx, session); err != nil {
|
||||
return err
|
||||
if wantSync {
|
||||
if err := environ.SyncSession(ctx, session, symbol); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if includeTransfer {
|
||||
exchange := session.Exchange
|
||||
market, _ := session.Market(symbol)
|
||||
transferService, ok := exchange.(types.ExchangeTransferService)
|
||||
if !ok {
|
||||
return fmt.Errorf("session exchange %s does not implement transfer service", sessionName)
|
||||
}
|
||||
|
||||
deposits, err := transferService.QueryDepositHistory(ctx, market.BaseCurrency, since, until)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = deposits
|
||||
|
||||
withdrawals, err := transferService.QueryWithdrawHistory(ctx, market.BaseCurrency, since, until)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sort.Slice(withdrawals, func(i, j int) bool {
|
||||
a := withdrawals[i].ApplyTime.Time()
|
||||
b := withdrawals[j].ApplyTime.Time()
|
||||
return a.Before(b)
|
||||
})
|
||||
|
||||
// we need the backtest klines for the daily prices
|
||||
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
|
||||
if err := backtestService.Sync(ctx, exchange, symbol, types.Interval1d, since, until); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err = environ.Init(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
session, _ := environ.Session(sessionNames[0])
|
||||
exchange := session.Exchange
|
||||
|
||||
market, ok := session.Market(symbol)
|
||||
if !ok {
|
||||
return fmt.Errorf("market config %s not found", symbol)
|
||||
}
|
||||
|
||||
since := time.Now().AddDate(-1, 0, 0)
|
||||
until := time.Now()
|
||||
|
||||
includeTransfer, err := cmd.Flags().GetBool("include-transfer")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if includeTransfer {
|
||||
transferService, ok := exchange.(types.ExchangeTransferService)
|
||||
if !ok {
|
||||
return fmt.Errorf("session exchange %s does not implement transfer service", sessionName)
|
||||
}
|
||||
|
||||
deposits, err := transferService.QueryDepositHistory(ctx, market.BaseCurrency, since, until)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = deposits
|
||||
|
||||
withdrawals, err := transferService.QueryWithdrawHistory(ctx, market.BaseCurrency, since, until)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sort.Slice(withdrawals, func(i, j int) bool {
|
||||
a := withdrawals[i].ApplyTime.Time()
|
||||
b := withdrawals[j].ApplyTime.Time()
|
||||
return a.Before(b)
|
||||
})
|
||||
|
||||
// we need the backtest klines for the daily prices
|
||||
backtestService := &service.BacktestService{DB: environ.DatabaseService.DB}
|
||||
if err := backtestService.Sync(ctx, exchange, symbol, types.Interval1d, since, until); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var trades []types.Trade
|
||||
tradingFeeCurrency := exchange.PlatformFeeCurrency()
|
||||
if strings.HasPrefix(symbol, tradingFeeCurrency) {
|
||||
|
@ -127,8 +155,10 @@ var PnLCmd = &cobra.Command{
|
|||
trades, err = environ.TradeService.QueryForTradingFeeCurrency(exchange.Name(), symbol, tradingFeeCurrency)
|
||||
} else {
|
||||
trades, err = environ.TradeService.Query(service.QueryTradesOptions{
|
||||
Symbol: symbol,
|
||||
Limit: limit,
|
||||
Symbol: symbol,
|
||||
Limit: limit,
|
||||
Sessions: sessionNames,
|
||||
Since: &since,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -20,12 +20,14 @@ var ErrTradeNotFound = errors.New("trade not found")
|
|||
|
||||
type QueryTradesOptions struct {
|
||||
Exchange types.ExchangeName
|
||||
Sessions []string
|
||||
Symbol string
|
||||
LastGID int64
|
||||
Since *time.Time
|
||||
|
||||
// ASC or DESC
|
||||
Ordering string
|
||||
Limit int
|
||||
Limit uint64
|
||||
}
|
||||
|
||||
type TradingVolume struct {
|
||||
|
@ -295,13 +297,43 @@ func (s *TradeService) QueryForTradingFeeCurrency(ex types.ExchangeName, symbol
|
|||
}
|
||||
|
||||
func (s *TradeService) Query(options QueryTradesOptions) ([]types.Trade, error) {
|
||||
sql := queryTradesSQL(options)
|
||||
args := map[string]interface{}{
|
||||
"exchange": options.Exchange,
|
||||
"symbol": options.Symbol,
|
||||
sel := sq.Select("*").
|
||||
From("trades")
|
||||
|
||||
if options.Since != nil {
|
||||
sel = sel.Where(sq.GtOrEq{"traded_at": options.Since})
|
||||
}
|
||||
|
||||
rows, err := s.DB.NamedQuery(sql, args)
|
||||
sel = sel.Where(sq.Eq{"symbol": options.Symbol})
|
||||
|
||||
if options.Exchange != "" {
|
||||
sel = sel.Where(sq.Eq{"exchange": options.Exchange})
|
||||
}
|
||||
|
||||
if len(options.Sessions) > 0 {
|
||||
// FIXME: right now we only have the exchange field in the db, we might need to add the session field too.
|
||||
sel = sel.Where(sq.Eq{"exchange": options.Sessions})
|
||||
}
|
||||
|
||||
if options.Ordering != "" {
|
||||
sel = sel.OrderBy("traded_at " + options.Ordering)
|
||||
} else {
|
||||
sel = sel.OrderBy("traded_at ASC")
|
||||
}
|
||||
|
||||
if options.Limit > 0 {
|
||||
sel = sel.Limit(options.Limit)
|
||||
}
|
||||
|
||||
sql, args, err := sel.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Debug(sql)
|
||||
log.Debug(args)
|
||||
|
||||
rows, err := s.DB.Queryx(sql, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -408,7 +440,7 @@ func queryTradesSQL(options QueryTradesOptions) string {
|
|||
sql += ` ORDER BY gid ` + ordering
|
||||
|
||||
if options.Limit > 0 {
|
||||
sql += ` LIMIT ` + strconv.Itoa(options.Limit)
|
||||
sql += ` LIMIT ` + strconv.FormatUint(options.Limit, 10)
|
||||
}
|
||||
|
||||
return sql
|
||||
|
|
|
@ -92,7 +92,7 @@ func (s *Strategy) InstanceID() string {
|
|||
return fmt.Sprintf("%s:%s", ID, s.Symbol)
|
||||
}
|
||||
|
||||
func (s *Strategy) Run(ctx context.Context, session *bbgo.ExchangeSession) error {
|
||||
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
||||
if s.BudgetQuota.IsZero() {
|
||||
s.BudgetQuota = s.Budget
|
||||
}
|
||||
|
|
|
@ -402,7 +402,7 @@ func (s *Strategy) adjustOrderQuantity(submitOrder types.SubmitOrder) types.Subm
|
|||
return submitOrder
|
||||
}
|
||||
|
||||
func (s *Strategy) Run(ctx context.Context, session *bbgo.ExchangeSession) error {
|
||||
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
||||
instanceID := fmt.Sprintf("%s-%s", ID, s.Symbol)
|
||||
|
||||
s.status = types.StrategyStatusRunning
|
||||
|
|
|
@ -239,18 +239,47 @@ var looseTimeFormats = []string{
|
|||
// LooseFormatTime parses date time string with a wide range of formats.
|
||||
type LooseFormatTime time.Time
|
||||
|
||||
func ParseLooseFormatTime(s string) (LooseFormatTime, error) {
|
||||
var t time.Time
|
||||
switch s {
|
||||
case "now":
|
||||
t = time.Now()
|
||||
return LooseFormatTime(t), nil
|
||||
|
||||
case "yesterday":
|
||||
t = time.Now().AddDate(0, 0, -1)
|
||||
return LooseFormatTime(t), nil
|
||||
|
||||
case "last month":
|
||||
t = time.Now().AddDate(0, -1, 0)
|
||||
return LooseFormatTime(t), nil
|
||||
|
||||
case "last year":
|
||||
t = time.Now().AddDate(-1, 0, 0)
|
||||
return LooseFormatTime(t), nil
|
||||
|
||||
}
|
||||
|
||||
tv, err := util.ParseTimeWithFormats(s, looseTimeFormats)
|
||||
if err != nil {
|
||||
return LooseFormatTime{}, err
|
||||
}
|
||||
|
||||
return LooseFormatTime(tv), nil
|
||||
}
|
||||
|
||||
func (t *LooseFormatTime) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
||||
var str string
|
||||
if err := unmarshal(&str); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
tv, err := util.ParseTimeWithFormats(str, looseTimeFormats)
|
||||
lt, err := ParseLooseFormatTime(str)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*t = LooseFormatTime(tv)
|
||||
*t = lt
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,22 @@ import (
|
|||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestParseLooseFormatTime_alias_now(t *testing.T) {
|
||||
lt, err := ParseLooseFormatTime("now")
|
||||
assert.NoError(t, err)
|
||||
|
||||
now := time.Now()
|
||||
assert.True(t, now.Sub(lt.Time()) < 10*time.Millisecond)
|
||||
}
|
||||
|
||||
func TestParseLooseFormatTime_alias_yesterday(t *testing.T) {
|
||||
lt, err := ParseLooseFormatTime("yesterday")
|
||||
assert.NoError(t, err)
|
||||
|
||||
tt := time.Now().AddDate(0, 0, -1)
|
||||
assert.True(t, tt.Sub(lt.Time()) < 10*time.Millisecond)
|
||||
}
|
||||
|
||||
func TestLooseFormatTime_UnmarshalJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
Loading…
Reference in New Issue
Block a user