mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
support margin order creation
This commit is contained in:
parent
3eda64641e
commit
ad4226f35b
|
@ -2,8 +2,10 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -12,12 +14,15 @@ import (
|
|||
|
||||
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
|
||||
"github.com/c9s/bbgo/pkg/exchange/binance"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
func init() {
|
||||
rootCmd.PersistentFlags().String("binance-api-key", "", "binance api key")
|
||||
rootCmd.PersistentFlags().String("binance-api-secret", "", "binance api secret")
|
||||
rootCmd.PersistentFlags().String("symbol", "BNBUSDT", "symbol")
|
||||
rootCmd.PersistentFlags().Float64("price", 20.0, "order price")
|
||||
rootCmd.PersistentFlags().Float64("quantity", 10.0, "order quantity")
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
|
@ -41,10 +46,30 @@ var rootCmd = &cobra.Command{
|
|||
return err
|
||||
}
|
||||
|
||||
price, err := cmd.Flags().GetFloat64("price")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
quantity, err := cmd.Flags().GetFloat64("quantity")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var exchange = binance.New(key, secret)
|
||||
|
||||
exchange.UseIsolatedMargin(symbol)
|
||||
|
||||
markets, err := exchange.QueryMarkets(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
market, ok := markets[symbol]
|
||||
if !ok {
|
||||
return fmt.Errorf("market %s is not defined", symbol)
|
||||
}
|
||||
|
||||
marginAccount, err := exchange.QueryMarginAccount(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -66,6 +91,24 @@ var rootCmd = &cobra.Command{
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
createdOrders, err := exchange.SubmitOrders(ctx, types.SubmitOrder{
|
||||
Symbol: symbol,
|
||||
Market: market,
|
||||
Side: types.SideTypeBuy,
|
||||
Type: types.OrderTypeLimit,
|
||||
Price: price,
|
||||
Quantity: quantity,
|
||||
MarginSideEffect: types.SideEffectTypeMarginBuy,
|
||||
TimeInForce: "GTC",
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info(createdOrders)
|
||||
|
||||
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
||||
return nil
|
||||
},
|
||||
|
|
|
@ -400,10 +400,20 @@ func (e *Exchange) submitMarginOrder(ctx context.Context, order types.SubmitOrde
|
|||
req.SideEffectType(binance.SideEffectType(order.MarginSideEffect))
|
||||
}
|
||||
|
||||
req.Quantity(order.QuantityString)
|
||||
if len(order.QuantityString) > 0 {
|
||||
req.Quantity(order.QuantityString)
|
||||
} else if order.Market.Symbol != "" {
|
||||
req.Quantity(order.Market.FormatQuantity(order.Quantity))
|
||||
} else {
|
||||
req.Quantity(strconv.FormatFloat(order.Quantity, 'f', 8, 64))
|
||||
}
|
||||
|
||||
if len(order.PriceString) > 0 {
|
||||
req.Price(order.PriceString)
|
||||
} else if order.Market.Symbol != "" {
|
||||
req.Price(order.Market.FormatPrice(order.Price))
|
||||
} else {
|
||||
req.Price(strconv.FormatFloat(order.Price, 'f', 8, 64))
|
||||
}
|
||||
|
||||
switch order.Type {
|
||||
|
@ -415,6 +425,7 @@ func (e *Exchange) submitMarginOrder(ctx context.Context, order types.SubmitOrde
|
|||
req.StopPrice(order.StopPriceString)
|
||||
}
|
||||
|
||||
// could be IOC or FOK
|
||||
if len(order.TimeInForce) > 0 {
|
||||
// TODO: check the TimeInForce value
|
||||
req.TimeInForce(binance.TimeInForceType(order.TimeInForce))
|
||||
|
|
|
@ -15,6 +15,16 @@ func init() {
|
|||
_ = PlainText(&Order{})
|
||||
}
|
||||
|
||||
// MarginOrderSideEffectType define side effect type for orders
|
||||
type MarginOrderSideEffectType string
|
||||
|
||||
var (
|
||||
SideEffectTypeNoSideEffect MarginOrderSideEffectType = "NO_SIDE_EFFECT"
|
||||
SideEffectTypeMarginBuy MarginOrderSideEffectType = "MARGIN_BUY"
|
||||
SideEffectTypeAutoRepay MarginOrderSideEffectType = "AUTO_REPAY"
|
||||
)
|
||||
|
||||
|
||||
// OrderType define order type
|
||||
type OrderType string
|
||||
|
||||
|
@ -69,7 +79,7 @@ type SubmitOrder struct {
|
|||
|
||||
TimeInForce string `json:"timeInForce" db:"time_in_force"` // GTC, IOC, FOK
|
||||
|
||||
MarginSideEffect string `json:"marginSideEffect"` // AUTO_REPAY = repay, MARGIN_BUY = borrow, defaults to NO_SIDE_EFFECT
|
||||
MarginSideEffect MarginOrderSideEffectType `json:"marginSideEffect"` // AUTO_REPAY = repay, MARGIN_BUY = borrow, defaults to NO_SIDE_EFFECT
|
||||
}
|
||||
|
||||
func (o *SubmitOrder) String() string {
|
||||
|
|
Loading…
Reference in New Issue
Block a user