cmd: add marginInterestsCmd

This commit is contained in:
c9s 2022-05-29 12:01:20 +08:00
parent 4a4699a4bc
commit 11075b0d1a
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 85 additions and 7 deletions

View File

@ -24,6 +24,10 @@ func init() {
marginRepaysCmd.Flags().String("session", "", "exchange session name") marginRepaysCmd.Flags().String("session", "", "exchange session name")
marginCmd.AddCommand(marginRepaysCmd) marginCmd.AddCommand(marginRepaysCmd)
marginInterestsCmd.Flags().String("asset", "", "asset")
marginInterestsCmd.Flags().String("session", "", "exchange session name")
marginCmd.AddCommand(marginInterestsCmd)
RootCmd.AddCommand(marginCmd) RootCmd.AddCommand(marginCmd)
} }
@ -77,7 +81,7 @@ var marginLoansCmd = &cobra.Command{
asset, err := cmd.Flags().GetString("asset") asset, err := cmd.Flags().GetString("asset")
if err != nil { if err != nil {
return fmt.Errorf("can't get the symbol from flags: %w", err) return err
} }
if selectedSession == nil { if selectedSession == nil {
@ -114,6 +118,45 @@ var marginRepaysCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background() ctx := context.Background()
asset, err := cmd.Flags().GetString("asset")
if err != nil {
return err
}
if selectedSession == nil {
return errors.New("session is not set")
}
marginHistoryService, ok := selectedSession.Exchange.(types.MarginHistory)
if !ok {
return fmt.Errorf("exchange %s does not support MarginHistory service", selectedSession.ExchangeName)
}
now := time.Now()
startTime := now.AddDate(0, -5, 0)
endTime := now
repays, err := marginHistoryService.QueryLoanHistory(ctx, asset, &startTime, &endTime)
if err != nil {
return err
}
log.Infof("%d repays", len(repays))
for _, repay := range repays {
log.Infof("REPAY %+v", repay)
}
return nil
},
}
// go run ./cmd/bbgo margin interests --session=binance
var marginInterestsCmd = &cobra.Command{
Use: "interests --session=SESSION_NAME --asset=ASSET",
Short: "query interests history",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
asset, err := cmd.Flags().GetString("asset") asset, err := cmd.Flags().GetString("asset")
if err != nil { if err != nil {
return fmt.Errorf("can't get the symbol from flags: %w", err) return fmt.Errorf("can't get the symbol from flags: %w", err)
@ -131,14 +174,14 @@ var marginRepaysCmd = &cobra.Command{
now := time.Now() now := time.Now()
startTime := now.AddDate(0, -5, 0) startTime := now.AddDate(0, -5, 0)
endTime := now endTime := now
loans, err := marginHistoryService.QueryLoanHistory(ctx, asset, &startTime, &endTime) interests, err := marginHistoryService.QueryInterestHistory(ctx, asset, &startTime, &endTime)
if err != nil { if err != nil {
return err return err
} }
log.Infof("%d loans", len(loans)) log.Infof("%d interests", len(interests))
for _, loan := range loans { for _, interest := range interests {
log.Infof("LOAN %+v", loan) log.Infof("INTEREST %+v", interest)
} }
return nil return nil

View File

@ -132,9 +132,23 @@ func (e *Exchange) QueryLiquidationHistory(ctx context.Context, startTime, endTi
func (e *Exchange) QueryInterestHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginInterest, error) { func (e *Exchange) QueryInterestHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.MarginInterest, error) {
req := e.client2.NewGetMarginInterestHistoryRequest() req := e.client2.NewGetMarginInterestHistoryRequest()
req.Asset(asset) req.Asset(asset)
req.Size(100)
if startTime != nil { if startTime != nil {
req.StartTime(*startTime) req.StartTime(*startTime)
// 6 months
if time.Since(*startTime) > time.Hour*24*30*6 {
req.Archived(true)
}
}
if startTime != nil && endTime != nil {
duration := endTime.Sub(*startTime)
if duration > time.Hour*24*30 {
t := startTime.Add(time.Hour * 24 * 30)
endTime = &t
}
} }
if endTime != nil { if endTime != nil {
@ -145,6 +159,27 @@ func (e *Exchange) QueryInterestHistory(ctx context.Context, asset string, start
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol) req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
} }
_, err := req.Do(ctx) records, err := req.Do(ctx)
if err != nil {
return nil, err return nil, err
} }
var interests []types.MarginInterest
for _, record := range records {
interests = append(interests, toGlobalInterest(record))
}
return interests, err
}
func toGlobalInterest(record binanceapi.MarginInterest) types.MarginInterest {
return types.MarginInterest{
Asset: record.Asset,
Principle: record.Principal,
Interest: record.Interest,
InterestRate: record.InterestRate,
IsolatedSymbol: record.IsolatedSymbol,
Time: types.Time(record.InterestAccuredTime),
}
}