bbgo_origin/pkg/cmd/trades.go

147 lines
3.6 KiB
Go
Raw Permalink Normal View History

2021-03-25 08:57:54 +00:00
package cmd
import (
"context"
"fmt"
2021-03-28 06:42:54 +00:00
"syscall"
2021-03-25 08:57:54 +00:00
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/c9s/bbgo/pkg/bbgo"
2021-03-28 06:42:54 +00:00
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
2021-03-25 08:57:54 +00:00
"github.com/c9s/bbgo/pkg/types"
)
// go run ./cmd/bbgo trades --session=ftx --symbol="BTC/USD"
2021-03-25 08:57:54 +00:00
var tradesCmd = &cobra.Command{
Use: "trades --session=[exchange_name] --symbol=[pair_name]",
Short: "Query trading history",
2021-03-25 08:57:54 +00:00
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"session",
"symbol",
}),
2021-03-25 08:57:54 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
symbol, err := cmd.Flags().GetString("symbol")
if err != nil {
return fmt.Errorf("can't get the symbol from flags: %w", err)
}
if symbol == "" {
return fmt.Errorf("symbol is not found")
}
2021-05-19 09:54:30 +00:00
limit, err := cmd.Flags().GetInt64("limit")
if err != nil {
return err
}
now := time.Now()
since := now.Add(-24 * time.Hour)
tradeHistoryService, ok := session.Exchange.(types.ExchangeTradeHistoryService)
if !ok {
// skip exchanges that does not support trading history services
log.Warnf("exchange %s does not implement ExchangeTradeHistoryService, skip syncing closed orders (tradesCmd)", session.Exchange.Name())
return nil
}
trades, err := tradeHistoryService.QueryTrades(ctx, symbol, &types.TradeQueryOptions{
2021-03-25 08:57:54 +00:00
StartTime: &since,
2021-05-19 09:54:30 +00:00
Limit: limit,
2021-03-25 08:57:54 +00:00
LastTradeID: 0,
})
if err != nil {
return err
}
log.Infof("%d trades", len(trades))
2021-05-19 09:54:30 +00:00
for _, trade := range trades {
2022-02-07 03:40:30 +00:00
log.Infof("TRADE %s %s %4s %s @ %s orderID %d %s amount %v , fee %v %s ",
2021-05-19 09:54:30 +00:00
trade.Exchange.String(),
trade.Symbol,
trade.Side,
2022-02-07 03:40:30 +00:00
trade.Quantity.FormatString(4),
trade.Price.FormatString(3),
2021-05-19 09:54:30 +00:00
trade.OrderID,
trade.Time.Time().Format(time.StampMilli),
2021-12-13 23:15:18 +00:00
trade.QuoteQuantity,
trade.Fee,
trade.FeeCurrency)
2021-03-25 08:57:54 +00:00
}
return nil
},
}
2021-03-28 06:42:54 +00:00
// go run ./cmd/bbgo tradeupdate --session=ftx
var tradeUpdateCmd = &cobra.Command{
Use: "tradeupdate --session=[exchange_name]",
Short: "Listen to trade update events",
PreRunE: cobraInitRequired([]string{
"session",
}),
2021-03-28 06:42:54 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
s := session.Exchange.NewStream()
s.OnTradeUpdate(func(trade types.Trade) {
log.Infof("trade update: %+v", trade)
})
log.Infof("connecting...")
if err := s.Connect(ctx); err != nil {
return fmt.Errorf("failed to connect to %s", sessionName)
}
log.Infof("connected")
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
return nil
},
}
func init() {
tradesCmd.Flags().String("session", "", "the exchange session name for querying balances")
tradesCmd.Flags().String("symbol", "", "the trading pair, like btcusdt")
2021-05-19 09:54:30 +00:00
tradesCmd.Flags().Int64("limit", 100, "limit")
2021-03-28 06:42:54 +00:00
tradeUpdateCmd.Flags().String("session", "", "the exchange session name for querying balances")
RootCmd.AddCommand(tradesCmd)
RootCmd.AddCommand(tradeUpdateCmd)
}