Skip to content

Commit

Permalink
新增jrpc说明
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqiang478 committed Apr 20, 2021
1 parent 57096f4 commit c6f56ca
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 7 deletions.
73 changes: 67 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func wsPriv() {
log.Println(err)
return
}

defer r.Stop()
var res bool

res, _, err = r.Login(apikey, secretKey, passphrase)
Expand Down Expand Up @@ -118,7 +118,7 @@ func wsPub() {
log.Println(err)
return
}

defer r.Stop()
// 订阅产品频道
var args []map[string]string
arg := make(map[string]string)
Expand Down Expand Up @@ -147,11 +147,72 @@ func wsPub() {
}
}

// websocket交易
func wsJrpc() {
ep := "wss://ws.okex.com:8443/ws/v5/private?brokerId=9999"

// 填写您自己的APIKey信息
apikey := "xxxx"
secretKey := "xxxxx"
passphrase := "xxxxx"

var res bool
var req_id string

// 创建ws客户端
r, err := NewWsClient(ep)
if err != nil {
log.Println(err)
return
}

// 设置连接超时
r.SetDailTimeout(time.Second * 2)
err = r.Start()
if err != nil {
log.Println(err)
return
}

defer r.Stop()

res, _, err = r.Login(apikey, secretKey, passphrase)
if res {
fmt.Println("登录成功!")
} else {
fmt.Println("登录失败!", err)
return
}

start := time.Now()
param := map[string]interface{}{}
param["instId"] = "BTC-USDT"
param["tdMode"] = "cash"
param["side"] = "buy"
param["ordType"] = "market"
param["sz"] = "200"
req_id = "00001"

res, _, err = r.PlaceOrder(req_id, param)
if res {
usedTime := time.Since(start)
fmt.Println("下单成功!", usedTime.String())
} else {
usedTime := time.Since(start)
fmt.Println("下单失败!", usedTime.String(), err)
}
}

func main() {
// 公共订阅
wsPub()
// // 私有订阅
// wsPriv()
// // rest请求
// REST()

// 私有订阅
wsPriv()

// websocket交易
wsJrpc()

// rest请求
REST()
}
65 changes: 64 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ OKEX go版本的v5sdk,仅供学习交流使用。
```
更多示例请查看rest/rest_test.go

## WS订阅
## websocket订阅

### 私有频道
```go
Expand All @@ -55,6 +55,7 @@ OKEX go版本的v5sdk,仅供学习交流使用。
log.Println(err)
return
}
defer r.Stop()

var res bool

Expand Down Expand Up @@ -104,6 +105,7 @@ OKEX go版本的v5sdk,仅供学习交流使用。
return
}


// 设置连接超时
r.SetDailTimeout(time.Second * 2)
err = r.Start()
Expand All @@ -112,6 +114,8 @@ OKEX go版本的v5sdk,仅供学习交流使用。
return
}

defer r.Stop()

// 订阅产品频道
var args []map[string]string
arg := make(map[string]string)
Expand Down Expand Up @@ -141,6 +145,65 @@ OKEX go版本的v5sdk,仅供学习交流使用。
```
更多示例请查看ws/ws_pub_channel_test.go

## websocket交易
```go
ep := "wss://ws.okex.com:8443/ws/v5/private?brokerId=9999"

// 填写您自己的APIKey信息
apikey := "xxxx"
secretKey := "xxxxx"
passphrase := "xxxxx"

var res bool
var req_id string

// 创建ws客户端
r, err := NewWsClient(ep)
if err != nil {
log.Println(err)
return
}

// 设置连接超时
r.SetDailTimeout(time.Second * 2)
err = r.Start()
if err != nil {
log.Println(err)
return
}

defer r.Stop()

res, _, err = r.Login(apikey, secretKey, passphrase)
if res {
fmt.Println("登录成功!")
} else {
fmt.Println("登录失败!", err)
return
}

start := time.Now()
param := map[string]interface{}{}
param["instId"] = "BTC-USDT"
param["tdMode"] = "cash"
param["side"] = "buy"
param["ordType"] = "market"
param["sz"] = "200"
req_id = "00001"

// 单个下单
res, _, err = r.PlaceOrder(req_id, param)
if res {
usedTime := time.Since(start)
fmt.Println("下单成功!", usedTime.String())
} else {
usedTime := time.Since(start)
fmt.Println("下单失败!", usedTime.String(), err)
}

```
更多示例请查看ws/ws_jrpc_test.go

# 联系方式
邮箱:caron_co@163.com
微信:caron_co
33 changes: 33 additions & 0 deletions ws/ws_jrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import (
. "v5sdk_go/ws/wImpl"
)

/*
websocket交易 通用请求
参数说明:
evtId:封装的事件类型
id: 请求ID
op: 请求参数op
params: 请求参数
timeOut: 超时时间
*/
func (a *WsClient) jrpcReq(evtId Event, op string, id string, params []map[string]interface{}, timeOut ...int) (res bool, detail *ProcessDetail, err error) {
res = true
tm := 5000
Expand Down Expand Up @@ -48,6 +57,10 @@ func (a *WsClient) jrpcReq(evtId Event, op string, id string, params []map[strin

/*
单个下单
参数说明:
id: 请求ID
params: 请求参数
timeOut: 超时时间
*/
func (a *WsClient) PlaceOrder(id string, param map[string]interface{}, timeOut ...int) (res bool, detail *ProcessDetail, err error) {
op := "order"
Expand All @@ -62,6 +75,10 @@ func (a *WsClient) PlaceOrder(id string, param map[string]interface{}, timeOut .

/*
批量下单
参数说明:
id: 请求ID
params: 请求参数
timeOut: 超时时间
*/
func (a *WsClient) BatchPlaceOrders(id string, params []map[string]interface{}, timeOut ...int) (res bool, detail *ProcessDetail, err error) {

Expand All @@ -73,6 +90,10 @@ func (a *WsClient) BatchPlaceOrders(id string, params []map[string]interface{},

/*
单个撤单
参数说明:
id: 请求ID
params: 请求参数
timeOut: 超时时间
*/
func (a *WsClient) CancelOrder(id string, param map[string]interface{}, timeOut ...int) (res bool, detail *ProcessDetail, err error) {

Expand All @@ -88,6 +109,10 @@ func (a *WsClient) CancelOrder(id string, param map[string]interface{}, timeOut

/*
批量撤单
参数说明:
id: 请求ID
params: 请求参数
timeOut: 超时时间
*/
func (a *WsClient) BatchCancelOrders(id string, params []map[string]interface{}, timeOut ...int) (res bool, detail *ProcessDetail, err error) {

Expand All @@ -99,6 +124,10 @@ func (a *WsClient) BatchCancelOrders(id string, params []map[string]interface{},

/*
单个改单
参数说明:
id: 请求ID
params: 请求参数
timeOut: 超时时间
*/
func (a *WsClient) AmendOrder(id string, param map[string]interface{}, timeOut ...int) (res bool, detail *ProcessDetail, err error) {

Expand All @@ -114,6 +143,10 @@ func (a *WsClient) AmendOrder(id string, param map[string]interface{}, timeOut .

/*
批量改单
参数说明:
id: 请求ID
params: 请求参数
timeOut: 超时时间
*/
func (a *WsClient) BatchAmendOrders(id string, params []map[string]interface{}, timeOut ...int) (res bool, detail *ProcessDetail, err error) {

Expand Down

0 comments on commit c6f56ca

Please sign in to comment.