From 889318ddcba255b3176e0ba7241fda794d41249b Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 7 Sep 2022 02:17:56 +0800 Subject: [PATCH] cmd/order: add market order support --- pkg/cmd/orders.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/pkg/cmd/orders.go b/pkg/cmd/orders.go index 04f891698..f5dbf496c 100644 --- a/pkg/cmd/orders.go +++ b/pkg/cmd/orders.go @@ -300,7 +300,6 @@ var submitOrderCmd = &cobra.Command{ "session", "symbol", "side", - "price", "quantity", }), RunE: func(cmd *cobra.Command, args []string) error { @@ -329,6 +328,11 @@ var submitOrderCmd = &cobra.Command{ return fmt.Errorf("can not get price: %w", err) } + asMarketOrder, err := cmd.Flags().GetBool("market") + if err != nil { + return err + } + quantity, err := cmd.Flags().GetString("quantity") if err != nil { return fmt.Errorf("can not get quantity: %w", err) @@ -363,12 +367,23 @@ var submitOrderCmd = &cobra.Command{ Side: types.SideType(strings.ToUpper(side)), Type: types.OrderTypeLimit, Quantity: fixedpoint.MustNewFromString(quantity), - Price: fixedpoint.MustNewFromString(price), Market: market, - TimeInForce: "GTC", MarginSideEffect: types.MarginOrderSideEffectType(marginOrderSideEffect), } + 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.SubmitOrders(ctx, so) if err != nil { return err @@ -392,6 +407,7 @@ func init() { submitOrderCmd.Flags().String("side", "", "the trading side: buy or sell") submitOrderCmd.Flags().String("price", "", "the trading price") submitOrderCmd.Flags().String("quantity", "", "the trading quantity") + submitOrderCmd.Flags().Bool("market", false, "submit order as a market order") submitOrderCmd.Flags().String("margin-side-effect", "", "margin order side effect") executeOrderCmd.Flags().String("session", "", "the exchange session name for sync")