bbgo_origin/pkg/cmd/orders.go

266 lines
7.1 KiB
Go
Raw Permalink Normal View History

2021-03-07 04:47:11 +00:00
package cmd
import (
"context"
"fmt"
"strings"
2021-03-23 14:25:21 +00:00
"time"
2021-03-07 04:47:11 +00:00
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
2021-03-19 02:06:42 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint"
2021-03-07 04:47:11 +00:00
"github.com/c9s/bbgo/pkg/types"
)
2022-03-02 16:06:44 +00:00
var getOrderCmd = &cobra.Command{
Use: "get-order --session SESSION --order-id ORDER_ID",
Short: "Get order status",
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"order-id",
"symbol",
}),
2022-03-02 16:06:44 +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)
}
orderID, err := cmd.Flags().GetString("order-id")
if err != nil {
return fmt.Errorf("can't get the order-id from flags: %w", err)
}
symbol, err := cmd.Flags().GetString("symbol")
2022-03-02 16:06:44 +00:00
if err != nil {
return fmt.Errorf("can't get the symbol from flags: %w", err)
}
service, ok := session.Exchange.(types.ExchangeOrderQueryService)
if !ok {
return fmt.Errorf("query order status is not supported for exchange %T, interface types.ExchangeOrderQueryService is not implemented", session.Exchange)
}
order, err := service.QueryOrder(ctx, types.OrderQuery{
OrderID: orderID,
Symbol: symbol,
2022-03-02 16:06:44 +00:00
})
if err != nil {
return err
}
log.Infof("%+v", order)
return nil
},
}
2021-05-27 19:05:59 +00:00
// go run ./cmd/bbgo list-orders [open|closed] --session=ftx --symbol=BTCUSDT
2021-03-13 02:12:20 +00:00
var listOrdersCmd = &cobra.Command{
2022-03-02 16:06:44 +00:00
Use: "list-orders open|closed --session SESSION --symbol SYMBOL",
Short: "list user's open orders in exchange of a specific trading pair",
2022-03-02 16:06:44 +00:00
Args: cobra.OnlyValidArgs,
2021-03-07 04:47:11 +00:00
// default is open which means we query open orders if you haven't provided args.
ValidArgs: []string{"", "open", "closed"},
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"session",
"symbol",
}),
2021-03-07 04:47:11 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
2021-03-23 14:25:21 +00:00
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
sessionName, err := cmd.Flags().GetString("session")
2021-03-07 04:47:11 +00:00
if err != nil {
return err
}
2021-03-23 14:25:21 +00:00
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
2021-03-07 04:47:11 +00:00
symbol, err := cmd.Flags().GetString("symbol")
if err != nil {
return fmt.Errorf("can't get the symbol from flags: %w", err)
}
status := "open"
if len(args) != 0 {
status = args[0]
}
var os []types.Order
switch status {
case "open":
2021-03-23 14:25:21 +00:00
os, err = session.Exchange.QueryOpenOrders(ctx, symbol)
2021-03-07 04:47:11 +00:00
if err != nil {
return err
}
case "closed":
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 (listOrdersCmd)", session.Exchange.Name())
return nil
}
os, err = tradeHistoryService.QueryClosedOrders(ctx, symbol, time.Now().Add(-3*24*time.Hour), time.Now(), 0)
2021-03-23 14:25:21 +00:00
if err != nil {
return err
}
2021-03-07 04:47:11 +00:00
default:
return fmt.Errorf("invalid status %s", status)
}
log.Infof("%s ORDERS FROM %s SESSION", strings.ToUpper(status), session.Name)
2021-03-13 01:51:31 +00:00
for _, o := range os {
log.Infof("%+v", o)
2021-03-13 01:51:31 +00:00
}
return nil
},
}
2021-12-25 16:27:52 +00:00
// go run ./cmd/bbgo submit-order --session=ftx --symbol=BTCUSDT --side=buy --price=18000 --quantity=0.001
2021-05-14 03:53:07 +00:00
var submitOrderCmd = &cobra.Command{
2021-12-25 16:27:52 +00:00
Use: "submit-order --session SESSION --symbol SYMBOL --side SIDE --quantity QUANTITY [--price PRICE]",
2022-03-03 03:39:28 +00:00
Short: "place order to the exchange",
2021-05-14 03:53:07 +00:00
SilenceUsage: true,
PreRunE: cobraInitRequired([]string{
"session",
"symbol",
"side",
"quantity",
}),
2021-05-14 03:53:07 +00:00
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
sessionName, err := cmd.Flags().GetString("session")
if err != nil {
return err
}
2021-03-13 01:51:31 +00:00
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")
}
side, err := cmd.Flags().GetString("side")
if err != nil {
2022-03-03 03:39:28 +00:00
return fmt.Errorf("can not get side: %w", err)
2021-03-13 01:51:31 +00:00
}
2021-04-28 11:48:17 +00:00
2021-03-13 01:51:31 +00:00
price, err := cmd.Flags().GetString("price")
if err != nil {
2022-03-03 03:39:28 +00:00
return fmt.Errorf("can not get price: %w", err)
2021-03-13 01:51:31 +00:00
}
2021-04-28 11:48:17 +00:00
2022-09-06 18:17:56 +00:00
asMarketOrder, err := cmd.Flags().GetBool("market")
if err != nil {
return err
}
2021-03-13 01:51:31 +00:00
quantity, err := cmd.Flags().GetString("quantity")
if err != nil {
2022-03-03 03:39:28 +00:00
return fmt.Errorf("can not get quantity: %w", err)
2021-03-13 01:51:31 +00:00
}
marginOrderSideEffect, err := cmd.Flags().GetString("margin-side-effect")
if err != nil {
return fmt.Errorf("can not get quantity: %w", err)
}
2021-05-14 03:53:07 +00:00
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
if err := environ.Init(ctx); err != nil {
return err
}
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
2021-05-27 19:05:59 +00:00
market, ok := session.Market(symbol)
if !ok {
return fmt.Errorf("market definition %s not found", symbol)
}
2021-03-13 01:51:31 +00:00
so := types.SubmitOrder{
Symbol: symbol,
Side: types.SideType(strings.ToUpper(side)),
Type: types.OrderTypeLimit,
Quantity: fixedpoint.MustNewFromString(quantity),
Market: market,
MarginSideEffect: types.MarginOrderSideEffectType(marginOrderSideEffect),
2021-03-13 01:51:31 +00:00
}
2021-05-27 19:05:59 +00:00
2022-09-06 18:17:56 +00:00
if asMarketOrder {
so.Type = types.OrderTypeMarket
so.Price = fixedpoint.Zero
} else {
if len(price) == 0 {
return fmt.Errorf("price is required for limit order submission")
}
so.Type = types.OrderTypeLimit
so.Price = fixedpoint.MustNewFromString(price)
so.TimeInForce = types.TimeInForceGTC
}
co, err := session.Exchange.SubmitOrder(ctx, so)
2021-03-13 01:51:31 +00:00
if err != nil {
return err
}
log.Infof("submitted order: %+v\ncreated order: %+v", so, co)
2021-03-07 04:47:11 +00:00
return nil
},
}
func init() {
2021-03-13 02:12:20 +00:00
listOrdersCmd.Flags().String("session", "", "the exchange session name for sync")
listOrdersCmd.Flags().String("symbol", "", "the trading pair, like btcusdt")
2021-03-07 04:47:11 +00:00
2022-03-02 16:06:44 +00:00
getOrderCmd.Flags().String("session", "", "the exchange session name for sync")
getOrderCmd.Flags().String("symbol", "", "the trading pair, like btcusdt")
getOrderCmd.Flags().String("order-id", "", "order id")
submitOrderCmd.Flags().String("session", "", "the exchange session name for sync")
submitOrderCmd.Flags().String("symbol", "", "the trading pair, like btcusdt")
submitOrderCmd.Flags().String("side", "", "the trading side: buy or sell")
submitOrderCmd.Flags().String("price", "", "the trading price")
submitOrderCmd.Flags().String("quantity", "", "the trading quantity")
2022-09-06 18:17:56 +00:00
submitOrderCmd.Flags().Bool("market", false, "submit order as a market order")
submitOrderCmd.Flags().String("margin-side-effect", "", "margin order side effect")
2021-03-13 01:51:31 +00:00
2021-03-13 02:12:20 +00:00
RootCmd.AddCommand(listOrdersCmd)
2022-03-02 16:06:44 +00:00
RootCmd.AddCommand(getOrderCmd)
RootCmd.AddCommand(submitOrderCmd)
2021-03-07 04:47:11 +00:00
}