diff --git a/pkg/cmd/pnl.go b/pkg/cmd/pnl.go index ccb0362a9..575405c18 100644 --- a/pkg/cmd/pnl.go +++ b/pkg/cmd/pnl.go @@ -21,6 +21,7 @@ func init() { PnLCmd.Flags().String("session", "", "target exchange") PnLCmd.Flags().String("symbol", "", "trading symbol") PnLCmd.Flags().Bool("include-transfer", false, "convert transfer records into trades") + PnLCmd.Flags().String("since", "", "query trades from a timepoint") PnLCmd.Flags().Int("limit", 0, "number of trades") RootCmd.AddCommand(PnLCmd) } @@ -67,7 +68,7 @@ var PnLCmd = &cobra.Command{ return fmt.Errorf("session %s not found", sessionName) } - if err := environ.SyncSession(ctx, session); err != nil { + if err := environ.SyncSession(ctx, session, symbol); err != nil { return err } @@ -82,7 +83,22 @@ var PnLCmd = &cobra.Command{ return fmt.Errorf("market config %s not found", symbol) } + // 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") diff --git a/pkg/types/time.go b/pkg/types/time.go index 2648d08af..8d819896c 100644 --- a/pkg/types/time.go +++ b/pkg/types/time.go @@ -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 } diff --git a/pkg/types/time_test.go b/pkg/types/time_test.go index ae21984cf..9fcb613c3 100644 --- a/pkg/types/time_test.go +++ b/pkg/types/time_test.go @@ -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