bbgo_origin/examples/exchange-api/binance-margin/main.go

130 lines
3.0 KiB
Go
Raw Permalink Normal View History

package main
import (
"context"
2021-01-15 23:37:42 +00:00
"fmt"
"strings"
"syscall"
2021-01-15 23:37:42 +00:00
"time"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/exchange/binance"
"github.com/c9s/bbgo/pkg/fixedpoint"
2021-01-15 23:37:42 +00:00
"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")
2021-01-15 23:37:42 +00:00
rootCmd.PersistentFlags().Float64("price", 20.0, "order price")
rootCmd.PersistentFlags().Float64("quantity", 10.0, "order quantity")
}
var rootCmd = &cobra.Command{
2020-12-21 09:48:30 +00:00
Use: "binance-margin",
Short: "binance margin",
// SilenceUsage is an option to silence usage when an error occurs.
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
key, secret := viper.GetString("binance-api-key"), viper.GetString("binance-api-secret")
if len(key) == 0 || len(secret) == 0 {
return errors.New("empty key or secret")
}
2020-12-21 09:48:30 +00:00
symbol, err := cmd.Flags().GetString("symbol")
if err != nil {
return err
}
2021-01-15 23:37:42 +00:00
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)
2020-12-21 09:48:30 +00:00
exchange.UseIsolatedMargin(symbol)
2021-01-15 23:37:42 +00:00
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.QueryCrossMarginAccount(ctx)
2020-12-21 09:48:30 +00:00
if err != nil {
return err
}
log.Infof("margin account: %+v", marginAccount)
2020-12-21 09:48:30 +00:00
isolatedMarginAccount, err := exchange.QueryIsolatedMarginAccount(ctx)
if err != nil {
return err
}
2020-12-21 09:48:30 +00:00
log.Infof("isolated margin account: %+v", isolatedMarginAccount)
stream := exchange.NewStream()
log.Info("connecting websocket...")
if err := stream.Connect(ctx); err != nil {
log.Fatal(err)
}
2021-01-15 23:37:42 +00:00
time.Sleep(time.Second)
createdOrders, err := exchange.SubmitOrders(ctx, types.SubmitOrder{
Symbol: symbol,
Market: market,
Side: types.SideTypeBuy,
Type: types.OrderTypeLimit,
Price: fixedpoint.NewFromFloat(price),
Quantity: fixedpoint.NewFromFloat(quantity),
2021-01-15 23:37:42 +00:00
MarginSideEffect: types.SideEffectTypeMarginBuy,
TimeInForce: "GTC",
})
if err != nil {
return err
}
log.Info(createdOrders)
cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
return nil
},
}
func main() {
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
if err := viper.BindPFlags(rootCmd.PersistentFlags()); err != nil {
log.WithError(err).Error("bind pflags error")
}
if err := rootCmd.ExecuteContext(context.Background()); err != nil {
log.WithError(err).Error("cmd error")
}
}