2021-05-23 04:11:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2021-05-23 07:41:49 +00:00
|
|
|
"time"
|
2021-05-23 04:11:27 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/exchange/okex/okexapi"
|
|
|
|
"github.com/joho/godotenv"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.PersistentFlags().String("okex-api-key", "", "okex api key")
|
|
|
|
rootCmd.PersistentFlags().String("okex-api-secret", "", "okex api secret")
|
|
|
|
rootCmd.PersistentFlags().String("okex-api-passphrase", "", "okex api secret")
|
|
|
|
rootCmd.PersistentFlags().String("symbol", "BNBUSDT", "symbol")
|
|
|
|
}
|
|
|
|
|
|
|
|
var rootCmd = &cobra.Command{
|
|
|
|
Use: "okex-book",
|
|
|
|
Short: "okex book",
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
|
|
|
symbol := viper.GetString("symbol")
|
|
|
|
if len(symbol) == 0 {
|
|
|
|
return errors.New("empty symbol")
|
|
|
|
}
|
|
|
|
|
|
|
|
key, secret, passphrase := viper.GetString("okex-api-key"),
|
|
|
|
viper.GetString("okex-api-secret"),
|
|
|
|
viper.GetString("okex-api-passphrase")
|
|
|
|
if len(key) == 0 || len(secret) == 0 {
|
|
|
|
return errors.New("empty key, secret or passphrase")
|
|
|
|
}
|
|
|
|
|
|
|
|
client := okexapi.NewClient()
|
|
|
|
client.Auth(key, secret, passphrase)
|
|
|
|
|
2021-05-24 17:35:54 +00:00
|
|
|
instruments, err := client.PublicDataService.NewGetInstrumentsRequest().
|
|
|
|
InstrumentType("SPOT").Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("instruments: %+v", instruments)
|
2021-05-24 18:22:08 +00:00
|
|
|
|
|
|
|
fundingRate, err := client.PublicDataService.NewGetFundingRate().InstrumentID("BTC-USDT-SWAP").Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof("funding rate: %+v", fundingRate)
|
2021-05-24 17:35:54 +00:00
|
|
|
|
2021-05-23 05:44:08 +00:00
|
|
|
log.Infof("ACCOUNT BALANCES:")
|
2021-05-27 17:14:11 +00:00
|
|
|
account, err := client.AccountBalances()
|
2021-05-23 04:11:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-27 17:14:11 +00:00
|
|
|
log.Infof("%+v", account)
|
|
|
|
|
2021-05-23 05:44:08 +00:00
|
|
|
log.Infof("ASSET BALANCES:")
|
|
|
|
assetBalances, err := client.AssetBalances()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, balance := range assetBalances {
|
|
|
|
log.Infof("%T%+v", balance, balance)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("ASSET CURRENCIES:")
|
|
|
|
currencies, err := client.AssetCurrencies()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, currency := range currencies {
|
|
|
|
log.Infof("%T%+v", currency, currency)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("MARKET TICKERS:")
|
2021-05-25 19:04:49 +00:00
|
|
|
tickers, err := client.MarketTickers(okexapi.InstrumentTypeSpot)
|
2021-05-23 05:44:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ticker := range tickers {
|
|
|
|
log.Infof("%T%+v", ticker, ticker)
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker, err := client.MarketTicker("ETH-USDT")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof("TICKER:")
|
|
|
|
log.Infof("%T%+v", ticker, ticker)
|
|
|
|
|
2021-05-23 07:31:18 +00:00
|
|
|
log.Infof("PLACING ORDER:")
|
2021-05-24 17:15:46 +00:00
|
|
|
placeResponse, err := client.TradeService.NewPlaceOrderRequest().
|
2021-05-23 07:31:18 +00:00
|
|
|
InstrumentID("LTC-USDT").
|
|
|
|
OrderType(okexapi.OrderTypeLimit).
|
|
|
|
Side(okexapi.SideTypeBuy).
|
|
|
|
Price("50.0").
|
|
|
|
Quantity("0.5").
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-23 07:41:49 +00:00
|
|
|
log.Infof("place order response: %+v", placeResponse)
|
|
|
|
time.Sleep(time.Second)
|
2021-05-23 07:31:18 +00:00
|
|
|
|
2021-05-24 17:15:46 +00:00
|
|
|
log.Infof("getting order detail...")
|
|
|
|
orderDetail, err := client.TradeService.NewGetOrderDetailsRequest().
|
|
|
|
InstrumentID("LTC-USDT").
|
|
|
|
OrderID(placeResponse.OrderID).
|
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("order detail: %+v", orderDetail)
|
2021-05-24 16:30:29 +00:00
|
|
|
|
2021-05-24 17:15:46 +00:00
|
|
|
cancelResponse, err := client.TradeService.NewCancelOrderRequest().
|
2021-05-23 07:31:18 +00:00
|
|
|
InstrumentID("LTC-USDT").
|
2021-05-23 07:41:49 +00:00
|
|
|
OrderID(placeResponse.OrderID).
|
2021-05-23 07:31:18 +00:00
|
|
|
Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-23 07:41:49 +00:00
|
|
|
log.Infof("cancel order response: %+v", cancelResponse)
|
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
log.Infof("BATCH PLACE ORDER:")
|
2021-05-24 17:15:46 +00:00
|
|
|
batchPlaceReq := client.TradeService.NewBatchPlaceOrderRequest()
|
|
|
|
batchPlaceReq.Add(client.TradeService.NewPlaceOrderRequest().
|
2021-05-23 07:41:49 +00:00
|
|
|
InstrumentID("LTC-USDT").
|
|
|
|
OrderType(okexapi.OrderTypeLimit).
|
|
|
|
Side(okexapi.SideTypeBuy).
|
|
|
|
Price("50.0").
|
|
|
|
Quantity("0.5"))
|
|
|
|
|
2021-05-24 17:15:46 +00:00
|
|
|
batchPlaceReq.Add(client.TradeService.NewPlaceOrderRequest().
|
2021-05-23 07:41:49 +00:00
|
|
|
InstrumentID("LTC-USDT").
|
|
|
|
OrderType(okexapi.OrderTypeLimit).
|
|
|
|
Side(okexapi.SideTypeBuy).
|
|
|
|
Price("30.0").
|
|
|
|
Quantity("0.5"))
|
|
|
|
|
|
|
|
batchPlaceResponse, err := batchPlaceReq.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("batch place order response: %+v", batchPlaceResponse)
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
2021-05-24 16:50:53 +00:00
|
|
|
log.Infof("getting pending orders...")
|
2021-05-24 17:15:46 +00:00
|
|
|
pendingOrders, err := client.TradeService.NewGetPendingOrderRequest().Do(ctx)
|
2021-05-24 16:50:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, pendingOrder := range pendingOrders {
|
|
|
|
log.Infof("pending order: %+v", pendingOrder)
|
|
|
|
}
|
|
|
|
|
2021-05-24 17:15:46 +00:00
|
|
|
cancelReq := client.TradeService.NewBatchCancelOrderRequest()
|
2021-05-23 07:41:49 +00:00
|
|
|
for _, resp := range batchPlaceResponse {
|
2021-05-24 17:15:46 +00:00
|
|
|
cancelReq.Add(client.TradeService.NewCancelOrderRequest().
|
2021-05-23 07:41:49 +00:00
|
|
|
InstrumentID("LTC-USDT").
|
|
|
|
OrderID(resp.OrderID))
|
|
|
|
}
|
|
|
|
|
|
|
|
batchCancelResponse, err := cancelReq.Do(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Infof("batch cancel order response: %+v", batchCancelResponse)
|
2021-05-23 07:31:18 +00:00
|
|
|
|
2021-05-23 04:11:27 +00:00
|
|
|
// cmdutil.WaitForSignal(ctx, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if _, err := os.Stat(".env.local"); err == nil {
|
|
|
|
if err := godotenv.Load(".env.local"); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|