bbgo_origin/examples/binance-margin/main.go

129 lines
2.9 KiB
Go
Raw 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"
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)
}
2020-12-21 09:48:30 +00:00
marginAccount, err := exchange.QueryMarginAccount(ctx)
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: 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
},
}
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")
}
}