bbgo_origin/pkg/cmd/orders.go

171 lines
4.4 KiB
Go
Raw Normal View History

2021-03-07 04:47:11 +00:00
package cmd
import (
"context"
"fmt"
2021-03-19 02:06:42 +00:00
"os"
2021-03-07 04:47:11 +00:00
2021-03-13 01:51:31 +00:00
"github.com/google/uuid"
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"
2021-03-13 01:51:31 +00:00
"github.com/c9s/bbgo/pkg/exchange/ftx"
2021-03-07 04:47:11 +00:00
"github.com/c9s/bbgo/pkg/types"
2021-03-13 01:51:31 +00:00
"github.com/c9s/bbgo/pkg/util"
2021-03-07 04:47:11 +00:00
)
2021-03-13 02:12:20 +00:00
// go run ./cmd/bbgo listorders [open|closed] --session=ftx --symbol=BTC/USDT
var listOrdersCmd = &cobra.Command{
Use: "listorders [status]",
2021-03-07 04:47:11 +00:00
Args: cobra.OnlyValidArgs,
// default is open which means we query open orders if you haven't provided args.
ValidArgs: []string{"", "open", "closed"},
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
session, err := cmd.Flags().GetString("session")
if err != nil {
return fmt.Errorf("can't get session from flags: %w", err)
}
ex, err := newExchange(session)
if err != nil {
return err
}
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")
}
status := "open"
if len(args) != 0 {
status = args[0]
}
var os []types.Order
switch status {
case "open":
os, err = ex.QueryOpenOrders(ctx, symbol)
if err != nil {
return err
}
case "closed":
2021-03-13 02:12:20 +00:00
panic("not implemented")
2021-03-07 04:47:11 +00:00
default:
return fmt.Errorf("invalid status %s", status)
}
2021-03-13 01:51:31 +00:00
for _, o := range os {
log.Infof("%s orders: %+v", status, o)
}
return nil
},
}
// go run ./cmd/bbgo placeorder --session=ftx --symbol=BTC/USDT --side=buy --price=<price> --quantity=<quantity>
var placeOrderCmd = &cobra.Command{
Use: "placeorder",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
2021-03-19 02:06:42 +00:00
configFile, err := cmd.Flags().GetString("config")
2021-03-13 01:51:31 +00:00
if err != nil {
2021-03-19 02:06:42 +00:00
return err
2021-03-13 01:51:31 +00:00
}
2021-03-19 02:06:42 +00:00
if len(configFile) == 0 {
return fmt.Errorf("--config option is required")
}
// if config file exists, use the config loaded from the config file.
// otherwise, use a empty config object
var userConfig *bbgo.Config
if _, err := os.Stat(configFile); err == nil {
// load successfully
userConfig, err = bbgo.Load(configFile, false)
if err != nil {
return err
}
} else if os.IsNotExist(err) {
// config file doesn't exist
userConfig = &bbgo.Config{}
} else {
// other error
return err
}
environ := bbgo.NewEnvironment()
if err := environ.ConfigureExchangeSessions(userConfig); err != nil {
return err
}
sessionName, err := cmd.Flags().GetString("session")
2021-03-13 01:51:31 +00:00
if err != nil {
return err
}
2021-03-19 02:06:42 +00:00
session, ok := environ.Session(sessionName)
if !ok {
return fmt.Errorf("session %s not found", sessionName)
}
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 {
return fmt.Errorf("can't get side: %w", err)
}
price, err := cmd.Flags().GetString("price")
if err != nil {
return fmt.Errorf("can't get price: %w", err)
}
quantity, err := cmd.Flags().GetString("quantity")
if err != nil {
return fmt.Errorf("can't get quantity: %w", err)
}
so := types.SubmitOrder{
ClientOrderID: uuid.New().String(),
Symbol: symbol,
Side: types.SideType(ftx.TrimUpperString(side)),
Type: types.OrderTypeLimit,
Quantity: util.MustParseFloat(quantity),
Price: util.MustParseFloat(price),
Market: types.Market{Symbol: symbol},
TimeInForce: "GTC",
}
2021-03-19 02:06:42 +00:00
co, err := session.Exchange.SubmitOrders(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[0])
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
2021-03-13 01:51:31 +00:00
placeOrderCmd.Flags().String("session", "", "the exchange session name for sync")
placeOrderCmd.Flags().String("symbol", "", "the trading pair, like btcusdt")
placeOrderCmd.Flags().String("side", "", "the trading side: buy or sell")
placeOrderCmd.Flags().String("price", "", "the trading price")
placeOrderCmd.Flags().String("quantity", "", "the trading quantity")
2021-03-13 02:12:20 +00:00
RootCmd.AddCommand(listOrdersCmd)
2021-03-13 01:51:31 +00:00
RootCmd.AddCommand(placeOrderCmd)
2021-03-07 04:47:11 +00:00
}