Skip to content

Commit

Permalink
Add AlgoOrderClient and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
eynzhang committed Sep 15, 2020
1 parent 73530f6 commit 7948327
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 0 deletions.
133 changes: 133 additions & 0 deletions cmd/algoorderclientexample/algoorderclientexample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package algoorderclientexample

import (
"github.com/huobirdcenter/huobi_golang/config"
"github.com/huobirdcenter/huobi_golang/logging/applogger"
"github.com/huobirdcenter/huobi_golang/pkg/client"
"github.com/huobirdcenter/huobi_golang/pkg/model"
"github.com/huobirdcenter/huobi_golang/pkg/model/algoorder"
)

func RunAllExamples() {
placeOrder()
getOpenOrders()
getSpecificOrder()
cancelOder()
getHistoryOrders()
}

func placeOrder() {
client := new(client.AlgoOrderClient).Init(config.AccessKey, config.SecretKey, config.Host)
request := algoorder.PlaceOrderRequest{
AccountId: 11136102,
Symbol: "htusdt",
OrderPrice: "4.4",
OrderSide: "buy",
OrderSize: "2",
TimeInForce: "gtc",
OrderType: "limit",
ClientOrderId: "huobi1901",
StopPrice: "4",
}
resp, err := client.PlaceOrder(&request)
if err != nil {
applogger.Error(err.Error())
} else {
if resp.Code == 200 {
applogger.Info("Place algo order successfully, client order id: %s", resp.Data.ClientOrderId)
} else {
applogger.Error("Place algo order error, code: %d, message: %s", resp.Code, resp.Message)
}
}
}

func cancelOder() {
client := new(client.AlgoOrderClient).Init(config.AccessKey, config.SecretKey, config.Host)
request := algoorder.CancelOrdersRequest{
ClientOrderIds: []string{"huobi1901"},
}
resp, err := client.CancelOrder(&request)
if err != nil {
applogger.Error(err.Error())
} else {
if resp.Code == 200 {
if resp.Data.Accepted != nil {
for _, id := range resp.Data.Accepted {
applogger.Info("Cancelled client order id success: %s", id)
}
}
if resp.Data.Rejected != nil {
for _, id := range resp.Data.Rejected {
applogger.Error("Cancelled client order id error: %s", id)
}
}
} else {
applogger.Error("Cancel algo order error, code: %d, message: %s", resp.Code, resp.Message)
}
}
}

func getOpenOrders() {
client := new(client.AlgoOrderClient).Init(config.AccessKey, config.SecretKey, config.Host)
request := new(model.GetRequest).Init()
request.AddParam("accountId", config.AccountId)

resp, err := client.GetOpenOrders(request)
if err != nil {
applogger.Error(err.Error())
} else {
if resp.Code == 200 {
if resp.Data != nil {
applogger.Info("There are total %d open orders", len(resp.Data))
for _, o := range resp.Data {
applogger.Info("Open orders, cid: %s, symbol: %s, status: %s", o.ClientOrderId, o.Symbol, o.OrderStatus)
}
}
} else {
applogger.Error("Get open order error, code: %d, message: %s", resp.Code, resp.Message)
}
}
}

func getHistoryOrders() {
client := new(client.AlgoOrderClient).Init(config.AccessKey, config.SecretKey, config.Host)
request := new(model.GetRequest).Init()
request.AddParam("symbol", "htusdt")
request.AddParam("orderStatus", "canceled")

resp, err := client.GetHistoryOrders(request)
if err != nil {
applogger.Error(err.Error())
} else {
if resp.Code == 200 {
if resp.Data != nil {
applogger.Info("There are total %d history orders", len(resp.Data))
for _, o := range resp.Data {
applogger.Info("history orders, cid: %s, symbol: %s, status: %s", o.ClientOrderId, o.Symbol, o.OrderStatus)
}
}
} else {
applogger.Error("Get history order error, code: %d, message: %s", resp.Code, resp.Message)
}
}
}

func getSpecificOrder() {
client := new(client.AlgoOrderClient).Init(config.AccessKey, config.SecretKey, config.Host)
request := new(model.GetRequest).Init()
request.AddParam("clientOrderId", "huobi1901")

resp, err := client.GetSpecificOrder(request)
if err != nil {
applogger.Error(err.Error())
} else {
if resp.Code == 200 {
if resp.Data != nil {
o := resp.Data
applogger.Info("Get order, cid: %s, symbol: %s, status: %s", o.ClientOrderId, o.Symbol, o.OrderStatus)
}
} else {
applogger.Error("Get order error, code: %s, message: %s", resp.Code, resp.Message)
}
}
}
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"github.com/huobirdcenter/huobi_golang/cmd/accountclientexample"
"github.com/huobirdcenter/huobi_golang/cmd/accountwebsocketclientexample"
"github.com/huobirdcenter/huobi_golang/cmd/algoorderclientexample"
"github.com/huobirdcenter/huobi_golang/cmd/commonclientexample"
"github.com/huobirdcenter/huobi_golang/cmd/crossmarginclientexample"
"github.com/huobirdcenter/huobi_golang/cmd/etfclientexample"
Expand All @@ -25,6 +26,7 @@ func runAll() {
commonclientexample.RunAllExamples()
accountclientexample.RunAllExamples()
orderclientexample.RunAllExamples()
algoorderclientexample.RunAllExamples()
marketclientexample.RunAllExamples()
isolatedmarginclientexample.RunAllExamples()
crossmarginclientexample.RunAllExamples()
Expand Down
106 changes: 106 additions & 0 deletions pkg/client/algoorderclient.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package client

import (
"encoding/json"
"github.com/huobirdcenter/huobi_golang/internal"
"github.com/huobirdcenter/huobi_golang/internal/requestbuilder"
"github.com/huobirdcenter/huobi_golang/pkg/model"
"github.com/huobirdcenter/huobi_golang/pkg/model/algoorder"
)

// Responsible to operate algo order
type AlgoOrderClient struct {
privateUrlBuilder *requestbuilder.PrivateUrlBuilder
}

// Initializer
func (p *AlgoOrderClient) Init(accessKey string, secretKey string, host string) *AlgoOrderClient {
p.privateUrlBuilder = new(requestbuilder.PrivateUrlBuilder).Init(accessKey, secretKey, host)
return p
}

// Place a new order
func (p *AlgoOrderClient) PlaceOrder(request *algoorder.PlaceOrderRequest) (*algoorder.PlaceOrderResponse, error) {
postBody, jsonErr := model.ToJson(request)

url := p.privateUrlBuilder.Build("POST", "/v2/algo-orders", nil)
postResp, postErr := internal.HttpPost(url, postBody)
if postErr != nil {
return nil, postErr
}

result := algoorder.PlaceOrderResponse{}
jsonErr = json.Unmarshal([]byte(postResp), &result)
if jsonErr != nil {
return nil, jsonErr
}

return &result, nil
}

// Cancel orders by client order id
func (p *AlgoOrderClient) CancelOrder(request *algoorder.CancelOrdersRequest) (*algoorder.CancelOrdersResponse, error) {
postBody, jsonErr := model.ToJson(request)

url := p.privateUrlBuilder.Build("POST", "/v2/algo-orders/cancellation", nil)
postResp, postErr := internal.HttpPost(url, postBody)
if postErr != nil {
return nil, postErr
}

result := algoorder.CancelOrdersResponse{}
jsonErr = json.Unmarshal([]byte(postResp), &result)
if jsonErr != nil {
return nil, jsonErr
}

return &result, nil
}

func (p *AlgoOrderClient) GetOpenOrders(request *model.GetRequest) (*algoorder.GetOpenOrdersResponse, error) {
url := p.privateUrlBuilder.Build("GET", "/v2/algo-orders/opening", request)
getResp, getErr := internal.HttpGet(url)
if getErr != nil {
return nil, getErr
}

result := algoorder.GetOpenOrdersResponse{}
jsonErr := json.Unmarshal([]byte(getResp), &result)
if jsonErr != nil {
return nil, jsonErr
}

return &result, nil
}

func (p *AlgoOrderClient) GetHistoryOrders(request *model.GetRequest) (*algoorder.GetHistoryOrdersResponse, error) {
url := p.privateUrlBuilder.Build("GET", "/v2/algo-orders/history", request)
getResp, getErr := internal.HttpGet(url)
if getErr != nil {
return nil, getErr
}

result := algoorder.GetHistoryOrdersResponse{}
jsonErr := json.Unmarshal([]byte(getResp), &result)
if jsonErr != nil {
return nil, jsonErr
}

return &result, nil
}

func (p *AlgoOrderClient) GetSpecificOrder(request *model.GetRequest) (*algoorder.GetSpecificOrderResponse, error) {
url := p.privateUrlBuilder.Build("GET", "/v2/algo-orders/specific", request)
getResp, getErr := internal.HttpGet(url)
if getErr != nil {
return nil, getErr
}

result := algoorder.GetSpecificOrderResponse{}
jsonErr := json.Unmarshal([]byte(getResp), &result)
if jsonErr != nil {
return nil, jsonErr
}

return &result, nil
}
5 changes: 5 additions & 0 deletions pkg/model/algoorder/cancelordersrequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package algoorder

type CancelOrdersRequest struct {
ClientOrderIds []string `json:"clientOrderIds"`
}
12 changes: 12 additions & 0 deletions pkg/model/algoorder/cancelordersresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package algoorder

type CancelOrdersResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data CancelledResult `json:"data"`
}

type CancelledResult struct {
Accepted []string `json:"accepted"`
Rejected []string `json:"rejected"`
}
29 changes: 29 additions & 0 deletions pkg/model/algoorder/gethistoryordersresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package algoorder

type GetHistoryOrdersResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data []HistoryOrders `json:"data"`
NextId int64 `json:"nextId"`
}

type HistoryOrders struct {
AccountId int `json:"accountId"`
Source string `json:"source"`
ClientOrderId string `json:"clientOrderId"`
Symbol string `json:"symbol"`
OrderPrice string `json:"orderPrice"`
OrderSize string `json:"orderSize"`
OrderValue string `json:"orderValue"`
OrderSide string `json:"orderSide"`
TimeInForce string `json:"timeInForce"`
OrderType string `json:"orderType"`
StopPrice string `json:"stopPrice"`
TrailingRate string `json:"trailingRate"`
OrderOrigTime int64 `json:"orderOrigTime"`
LastActTime int64 `json:"lastActTime"`
OrderCreateTime int64 `json:"orderCreateTime"`
OrderStatus string `json:"orderStatus"`
ErrCode int `json:"errCode"`
ErrMessage string `json:"errMessage"`
}
26 changes: 26 additions & 0 deletions pkg/model/algoorder/getopenordersresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package algoorder

type GetOpenOrdersResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data []OpenOrders `json:"data"`
NextId int64 `json:"nextId"`
}

type OpenOrders struct {
AccountId int `json:"accountId"`
Source string `json:"source"`
ClientOrderId string `json:"clientOrderId"`
Symbol string `json:"symbol"`
OrderPrice string `json:"orderPrice"`
OrderSize string `json:"orderSize"`
OrderValue string `json:"orderValue"`
OrderSide string `json:"orderSide"`
TimeInForce string `json:"timeInForce"`
OrderType string `json:"orderType"`
StopPrice string `json:"stopPrice"`
TrailingRate string `json:"trailingRate"`
OrderOrigTime int64 `json:"orderOrigTime"`
LastActTime int64 `json:"lastActTime"`
OrderStatus string `json:"orderStatus"`
}
29 changes: 29 additions & 0 deletions pkg/model/algoorder/getspecificorderresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package algoorder

type GetSpecificOrderResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data *Order `json:"data"`
}

type Order struct {
AccountId int `json:"accountId"`
Source string `json:"source"`
ClientOrderId string `json:"clientOrderId"`
OrderId string `json:"orderId"`
Symbol string `json:"symbol"`
OrderPrice string `json:"orderPrice"`
OrderSize string `json:"orderSize"`
OrderValue string `json:"orderValue"`
OrderSide string `json:"orderSide"`
TimeInForce string `json:"timeInForce"`
OrderType string `json:"orderType"`
StopPrice string `json:"stopPrice"`
TrailingRate string `json:"trailingRate"`
OrderOrigTime int64 `json:"orderOrigTime"`
LastActTime int64 `json:"lastActTime"`
OrderCreateTime int64 `json:"orderCreateTime"`
OrderStatus string `json:"orderStatus"`
ErrCode int `json:"errCode"`
ErrMessage string `json:"errMessage"`
}
15 changes: 15 additions & 0 deletions pkg/model/algoorder/placeorderrequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package algoorder

type PlaceOrderRequest struct {
AccountId int `json:"accountId"`
Symbol string `json:"symbol"`
OrderPrice string `json:"orderPrice"`
OrderSide string `json:"orderSide"`
OrderSize string `json:"orderSize"`
OrderValue string `json:"orderValue"`
TimeInForce string `json:"timeInForce"`
OrderType string `json:"orderType"`
ClientOrderId string `json:"clientOrderId"`
StopPrice string `json:"stopPrice"`
TrailingRate string `json:"trailingRate"`
}
11 changes: 11 additions & 0 deletions pkg/model/algoorder/placeorderresponse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package algoorder

type PlaceOrderResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data ClientOrderIdData `json:"data"`
}

type ClientOrderIdData struct {
ClientOrderId string `json:"clientOrderId"`
}

0 comments on commit 7948327

Please sign in to comment.