-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
main.go
214 lines (175 loc) · 5.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"context"
"os"
"strings"
"time"
"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)
instruments, err := client.PublicDataService.NewGetInstrumentsRequest().
InstrumentType("SPOT").Do(ctx)
if err != nil {
return err
}
log.Infof("instruments: %+v", instruments)
fundingRate, err := client.PublicDataService.NewGetFundingRate().InstrumentID("BTC-USDT-SWAP").Do(ctx)
if err != nil {
return err
}
log.Infof("funding rate: %+v", fundingRate)
log.Infof("ACCOUNT BALANCES:")
account, err := client.AccountBalances()
if err != nil {
return err
}
log.Infof("%+v", account)
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:")
tickers, err := client.MarketTickers(okexapi.InstrumentTypeSpot)
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)
log.Infof("PLACING ORDER:")
placeResponse, err := client.TradeService.NewPlaceOrderRequest().
InstrumentID("LTC-USDT").
OrderType(okexapi.OrderTypeLimit).
Side(okexapi.SideTypeBuy).
Price("50.0").
Quantity("0.5").
Do(ctx)
if err != nil {
return err
}
log.Infof("place order response: %+v", placeResponse)
time.Sleep(time.Second)
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)
cancelResponse, err := client.TradeService.NewCancelOrderRequest().
InstrumentID("LTC-USDT").
OrderID(placeResponse.OrderID).
Do(ctx)
if err != nil {
return err
}
log.Infof("cancel order response: %+v", cancelResponse)
time.Sleep(time.Second)
log.Infof("BATCH PLACE ORDER:")
batchPlaceReq := client.TradeService.NewBatchPlaceOrderRequest()
batchPlaceReq.Add(client.TradeService.NewPlaceOrderRequest().
InstrumentID("LTC-USDT").
OrderType(okexapi.OrderTypeLimit).
Side(okexapi.SideTypeBuy).
Price("50.0").
Quantity("0.5"))
batchPlaceReq.Add(client.TradeService.NewPlaceOrderRequest().
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)
log.Infof("getting pending orders...")
pendingOrders, err := client.TradeService.NewGetPendingOrderRequest().Do(ctx)
if err != nil {
return err
}
for _, pendingOrder := range pendingOrders {
log.Infof("pending order: %+v", pendingOrder)
}
cancelReq := client.TradeService.NewBatchCancelOrderRequest()
for _, resp := range batchPlaceResponse {
cancelReq.Add(client.TradeService.NewCancelOrderRequest().
InstrumentID("LTC-USDT").
OrderID(resp.OrderID))
}
batchCancelResponse, err := cancelReq.Do(ctx)
if err != nil {
return err
}
log.Infof("batch cancel order response: %+v", batchCancelResponse)
// 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")
}
}