bbgo_origin/pkg/cmd/transfer.go

203 lines
4.6 KiB
Go
Raw Normal View History

package cmd
import (
"context"
2021-02-17 06:42:49 +00:00
"fmt"
"sort"
"time"
2020-10-23 06:28:07 +00:00
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
2021-02-17 06:42:49 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
2022-02-07 03:40:30 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)
func init() {
2021-02-17 06:42:49 +00:00
TransferHistoryCmd.Flags().String("session", "", "target exchange session")
2020-10-23 06:28:07 +00:00
TransferHistoryCmd.Flags().String("asset", "", "trading symbol")
TransferHistoryCmd.Flags().String("since", "", "since time")
RootCmd.AddCommand(TransferHistoryCmd)
}
2020-10-12 09:07:50 +00:00
type timeRecord struct {
Record interface{}
2020-10-12 09:07:50 +00:00
Time time.Time
}
2020-10-12 09:07:50 +00:00
type timeSlice []timeRecord
func (p timeSlice) Len() int {
return len(p)
}
func (p timeSlice) Less(i, j int) bool {
return p[i].Time.Before(p[j].Time)
}
func (p timeSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
2020-10-23 06:28:07 +00:00
var TransferHistoryCmd = &cobra.Command{
2020-10-12 09:07:50 +00:00
Use: "transfer-history",
Short: "show transfer history",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
2021-02-17 06:42:49 +00:00
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
2021-02-17 06:42:49 +00:00
return err
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
asset, err := cmd.Flags().GetString("asset")
if err != nil {
return err
}
2021-02-17 06:42:49 +00:00
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
// default
var now = time.Now()
var since = now.AddDate(-1, 0, 0)
var until = now
sinceStr, err := cmd.Flags().GetString("since")
if err != nil {
return err
}
if len(sinceStr) > 0 {
loc, err := time.LoadLocation("Asia/Taipei")
if err != nil {
return err
}
since, err = time.ParseInLocation("2006-01-02", sinceStr, loc)
if err != nil {
return err
}
}
var records timeSlice
2021-03-14 03:04:56 +00:00
exchange, ok := session.Exchange.(types.ExchangeTransferService)
if !ok {
return fmt.Errorf("exchange session %s does not implement transfer service", sessionName)
}
logrus.Infof("querying deposit history...")
deposits, err := exchange.QueryDepositHistory(ctx, asset, since, until)
if err != nil {
return err
}
for _, d := range deposits {
2020-10-12 09:07:50 +00:00
records = append(records, timeRecord{
Record: d,
Time: d.EffectiveTime(),
})
}
withdraws, err := exchange.QueryWithdrawHistory(ctx, asset, since, until)
if err != nil {
return err
}
for _, w := range withdraws {
2020-10-12 09:07:50 +00:00
records = append(records, timeRecord{
Record: w,
Time: w.EffectiveTime(),
})
}
sort.Sort(records)
for _, record := range records {
switch record := record.Record.(type) {
case types.Deposit:
2022-02-07 03:40:30 +00:00
logrus.Infof("%s: <--- DEPOSIT %v %s [%s]", record.Time, record.Amount, record.Asset, record.Status)
case types.Withdraw:
2022-02-07 03:40:30 +00:00
logrus.Infof("%s: ---> WITHDRAW %v %s [%s]", record.ApplyTime, record.Amount, record.Asset, record.Status)
default:
2020-10-23 06:28:07 +00:00
logrus.Infof("unknown record: %+v", record)
}
}
stats := calBaselineStats(asset, deposits, withdraws)
2020-10-12 09:07:50 +00:00
for asset, quantity := range stats.TotalDeposit {
2022-02-07 03:40:30 +00:00
logrus.Infof("total %s deposit: %v", asset, quantity)
2020-10-12 09:07:50 +00:00
}
for asset, quantity := range stats.TotalWithdraw {
2022-02-07 03:40:30 +00:00
logrus.Infof("total %s withdraw: %v", asset, quantity)
2020-10-12 09:07:50 +00:00
}
for asset, quantity := range stats.BaselineBalance {
2022-02-07 03:40:30 +00:00
logrus.Infof("baseline %s balance: %v", asset, quantity)
2020-10-12 09:07:50 +00:00
}
return nil
},
}
type BaselineStats struct {
2020-10-12 09:07:50 +00:00
Asset string
2022-02-07 03:40:30 +00:00
TotalDeposit map[string]fixedpoint.Value
TotalWithdraw map[string]fixedpoint.Value
BaselineBalance map[string]fixedpoint.Value
}
func calBaselineStats(asset string, deposits []types.Deposit, withdraws []types.Withdraw) (stats BaselineStats) {
stats.Asset = asset
2022-02-07 03:40:30 +00:00
stats.TotalDeposit = make(map[string]fixedpoint.Value)
stats.TotalWithdraw = make(map[string]fixedpoint.Value)
stats.BaselineBalance = make(map[string]fixedpoint.Value)
for _, deposit := range deposits {
if deposit.Status == types.DepositSuccess {
2020-10-12 09:07:50 +00:00
if _, ok := stats.TotalDeposit[deposit.Asset]; !ok {
2022-02-07 03:40:30 +00:00
stats.TotalDeposit[deposit.Asset] = fixedpoint.Zero
2020-10-12 09:07:50 +00:00
}
2022-02-07 03:40:30 +00:00
stats.TotalDeposit[deposit.Asset] = stats.TotalDeposit[deposit.Asset].Add(deposit.Amount)
}
}
for _, withdraw := range withdraws {
if withdraw.Status == "completed" {
2020-10-12 09:07:50 +00:00
if _, ok := stats.TotalWithdraw[withdraw.Asset]; !ok {
2022-02-07 03:40:30 +00:00
stats.TotalWithdraw[withdraw.Asset] = fixedpoint.Zero
2020-10-12 09:07:50 +00:00
}
2022-02-07 03:40:30 +00:00
stats.TotalWithdraw[withdraw.Asset] = stats.TotalWithdraw[withdraw.Asset].Add(withdraw.Amount)
}
}
2020-10-12 09:07:50 +00:00
for asset, deposit := range stats.TotalDeposit {
withdraw, ok := stats.TotalWithdraw[asset]
if !ok {
2022-02-07 03:40:30 +00:00
withdraw = fixedpoint.Zero
2020-10-12 09:07:50 +00:00
}
2022-02-07 03:40:30 +00:00
stats.BaselineBalance[asset] = deposit.Sub(withdraw)
2020-10-12 09:07:50 +00:00
}
return stats
}