improve baseline calculator

This commit is contained in:
c9s 2020-10-12 17:07:50 +08:00
parent 64c9960882
commit 931ce434ef

View File

@ -13,19 +13,17 @@ import (
func init() { func init() {
transferHistoryCmd.Flags().String("exchange", "", "target exchange") transferHistoryCmd.Flags().String("exchange", "", "target exchange")
transferHistoryCmd.Flags().String("asset", "BTC", "trading symbol") transferHistoryCmd.Flags().String("asset", "", "trading symbol")
transferHistoryCmd.Flags().String("since", "", "since time") transferHistoryCmd.Flags().String("since", "", "since time")
RootCmd.AddCommand(transferHistoryCmd) RootCmd.AddCommand(transferHistoryCmd)
} }
type timeRecord struct {
type TimeRecord struct {
Record interface{} Record interface{}
Time time.Time Time time.Time
} }
type timeSlice []TimeRecord type timeSlice []timeRecord
func (p timeSlice) Len() int { func (p timeSlice) Len() int {
return len(p) return len(p)
@ -39,13 +37,9 @@ func (p timeSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i] p[i], p[j] = p[j], p[i]
} }
var transferHistoryCmd = &cobra.Command{ var transferHistoryCmd = &cobra.Command{
Use: "transfer-history", Use: "transfer-history",
Short: "show transfer history", Short: "show transfer history",
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@ -89,7 +83,6 @@ var transferHistoryCmd = &cobra.Command{
} }
} }
exchange := newExchange(exchangeName) exchange := newExchange(exchangeName)
var records timeSlice var records timeSlice
@ -99,7 +92,7 @@ var transferHistoryCmd = &cobra.Command{
return err return err
} }
for _, d := range deposits { for _, d := range deposits {
records = append(records, TimeRecord{ records = append(records, timeRecord{
Record: d, Record: d,
Time: d.EffectiveTime(), Time: d.EffectiveTime(),
}) })
@ -110,7 +103,7 @@ var transferHistoryCmd = &cobra.Command{
return err return err
} }
for _, w := range withdraws { for _, w := range withdraws {
records = append(records, TimeRecord{ records = append(records, timeRecord{
Record: w, Record: w,
Time: w.EffectiveTime(), Time: w.EffectiveTime(),
}) })
@ -134,39 +127,63 @@ var transferHistoryCmd = &cobra.Command{
} }
stats := calBaselineStats(asset, deposits, withdraws) stats := calBaselineStats(asset, deposits, withdraws)
log.Infof("total %s deposit: %f (x %d)", asset, stats.TotalDeposit, stats.NumOfDeposit) for asset, quantity := range stats.TotalDeposit {
log.Infof("total %s withdraw: %f (x %d)", asset, stats.TotalWithdraw, stats.NumOfWithdraw) log.Infof("total %s deposit: %f", asset, quantity)
log.Infof("baseline %s balance: %f", asset, stats.BaselineBalance) }
for asset, quantity := range stats.TotalWithdraw {
log.Infof("total %s withdraw: %f", asset, quantity)
}
for asset, quantity := range stats.BaselineBalance {
log.Infof("baseline %s balance: %f", asset, quantity)
}
return nil return nil
}, },
} }
type BaselineStats struct { type BaselineStats struct {
Asset string Asset string
NumOfDeposit int TotalDeposit map[string]float64
NumOfWithdraw int TotalWithdraw map[string]float64
TotalDeposit float64 BaselineBalance map[string]float64
TotalWithdraw float64
BaselineBalance float64
} }
func calBaselineStats(asset string, deposits []types.Deposit, withdraws []types.Withdraw) (stats BaselineStats) { func calBaselineStats(asset string, deposits []types.Deposit, withdraws []types.Withdraw) (stats BaselineStats) {
stats.Asset = asset stats.Asset = asset
stats.NumOfDeposit = len(deposits) stats.TotalDeposit = make(map[string]float64)
stats.NumOfWithdraw = len(withdraws) stats.TotalWithdraw = make(map[string]float64)
stats.BaselineBalance = make(map[string]float64)
for _, deposit := range deposits { for _, deposit := range deposits {
if deposit.Status == types.DepositSuccess { if deposit.Status == types.DepositSuccess {
stats.TotalDeposit += deposit.Amount if _, ok := stats.TotalDeposit[deposit.Asset]; !ok {
stats.TotalDeposit[deposit.Asset] = 0.0
}
stats.TotalDeposit[deposit.Asset] += deposit.Amount
} }
} }
for _, withdraw := range withdraws { for _, withdraw := range withdraws {
if withdraw.Status == "completed" { if withdraw.Status == "completed" {
stats.TotalWithdraw += withdraw.Amount if _, ok := stats.TotalWithdraw[withdraw.Asset]; !ok {
stats.TotalWithdraw[withdraw.Asset] = 0.0
}
stats.TotalWithdraw[withdraw.Asset] += withdraw.Amount
} }
} }
stats.BaselineBalance = stats.TotalDeposit - stats.TotalWithdraw for asset, deposit := range stats.TotalDeposit {
withdraw, ok := stats.TotalWithdraw[asset]
if !ok {
withdraw = 0.0
}
stats.BaselineBalance[asset] = deposit - withdraw
}
return stats return stats
} }