mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
cmd: add marginInterestsCmd
This commit is contained in:
parent
4a4699a4bc
commit
11075b0d1a
|
@ -24,6 +24,10 @@ func init() {
|
|||
marginRepaysCmd.Flags().String("session", "", "exchange session name")
|
||||
marginCmd.AddCommand(marginRepaysCmd)
|
||||
|
||||
marginInterestsCmd.Flags().String("asset", "", "asset")
|
||||
marginInterestsCmd.Flags().String("session", "", "exchange session name")
|
||||
marginCmd.AddCommand(marginInterestsCmd)
|
||||
|
||||
RootCmd.AddCommand(marginCmd)
|
||||
}
|
||||
|
||||
|
@ -77,7 +81,7 @@ var marginLoansCmd = &cobra.Command{
|
|||
|
||||
asset, err := cmd.Flags().GetString("asset")
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't get the symbol from flags: %w", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if selectedSession == nil {
|
||||
|
@ -114,6 +118,45 @@ var marginRepaysCmd = &cobra.Command{
|
|||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
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")
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't get the symbol from flags: %w", err)
|
||||
|
@ -131,14 +174,14 @@ var marginRepaysCmd = &cobra.Command{
|
|||
now := time.Now()
|
||||
startTime := now.AddDate(0, -5, 0)
|
||||
endTime := now
|
||||
loans, err := marginHistoryService.QueryLoanHistory(ctx, asset, &startTime, &endTime)
|
||||
interests, err := marginHistoryService.QueryInterestHistory(ctx, asset, &startTime, &endTime)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Infof("%d loans", len(loans))
|
||||
for _, loan := range loans {
|
||||
log.Infof("LOAN %+v", loan)
|
||||
log.Infof("%d interests", len(interests))
|
||||
for _, interest := range interests {
|
||||
log.Infof("INTEREST %+v", interest)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -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) {
|
||||
req := e.client2.NewGetMarginInterestHistoryRequest()
|
||||
req.Asset(asset)
|
||||
req.Size(100)
|
||||
|
||||
if startTime != nil {
|
||||
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 {
|
||||
|
@ -145,6 +159,27 @@ func (e *Exchange) QueryInterestHistory(ctx context.Context, asset string, start
|
|||
req.IsolatedSymbol(e.MarginSettings.IsolatedMarginSymbol)
|
||||
}
|
||||
|
||||
_, err := req.Do(ctx)
|
||||
records, err := req.Do(ctx)
|
||||
if err != nil {
|
||||
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),
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user