forked from HuobiRDCenter/huobi_Golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
368 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package algoorder | ||
|
||
type CancelOrdersRequest struct { | ||
ClientOrderIds []string `json:"clientOrderIds"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |