diff --git a/README.md b/README.md index e90fc29..608b64f 100644 --- a/README.md +++ b/README.md @@ -164,9 +164,9 @@ if err != nil { return } -ch1 := kumex.NewSubscribeMessage("/contractMarket/ticker:XBTUSDM", false) -ch2 := kumex.NewSubscribeMessage("/contractMarket/ticker:XBTUSDM", false) -uch := kumex.NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDM", false) +ch1 := kumex.NewSubscribeMessage("/contractMarket/ticker:XBTUSDTM", false) +ch2 := kumex.NewSubscribeMessage("/contractMarket/ticker:XBTUSDTM", false) +uch := kumex.NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDTM", false) if err := c.Subscribe(ch1, ch2); err != nil { // Handle error @@ -191,7 +191,7 @@ for { log.Printf("Ticker: %s, %s, %s, %s", msg.Topic, t.Sequence, t.Price, t.Size) i++ if i == 5 { - log.Println("Unsubscribe XBTUSDM") + log.Println("Unsubscribe XBTUSDTM") if err = c.Unsubscribe(uch); err != nil { log.Printf("Error: %s", err.Error()) // Handle error @@ -199,7 +199,7 @@ for { } } if i == 10 { - log.Println("Subscribe XBTUSDM") + log.Println("Subscribe XBTUSDTM") if err = c.Subscribe(ch2); err != nil { log.Printf("Error: %s", err.Error()) // Handle error diff --git a/contracts_test.go b/contracts_test.go index e2cc057..33d6806 100644 --- a/contracts_test.go +++ b/contracts_test.go @@ -36,7 +36,7 @@ func TestApiService_Contracts(t *testing.T) { t.SkipNow() s := NewApiServiceFromEnv() - rsp, err := s.Contracts("XBTUSDM") + rsp, err := s.Contracts("XBTUSDTM") if err != nil { t.Fatal(err) } diff --git a/examples/main.go b/examples/main.go index 25c34f9..53e06e7 100644 --- a/examples/main.go +++ b/examples/main.go @@ -95,9 +95,9 @@ func publicWebsocket(s *kumex.ApiService) { return } - ch1 := kumex.NewSubscribeMessage("/contract/instrument:XBTUSDM", false) - ch2 := kumex.NewSubscribeMessage("/contractMarket/level3:XBTUSDM", false) - //uch := kumex.NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDM", false) + ch1 := kumex.NewSubscribeMessage("/contract/instrument:XBTUSDTM", false) + ch2 := kumex.NewSubscribeMessage("/contractMarket/level3:XBTUSDTM", false) + //uch := kumex.NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDTM", false) if err := c.Subscribe(ch1, ch2); err != nil { // Handle error @@ -138,7 +138,7 @@ func privateWebsocket(s *kumex.ApiService) { return } - ch1 := kumex.NewSubscribeMessage("/contract/position:XBTUSDM", false) + ch1 := kumex.NewSubscribeMessage("/contract/position:XBTUSDTM", false) ch2 := kumex.NewSubscribeMessage("/contractAccount/wallet", false) log.Println(kumex.ToJsonString(ch1)) diff --git a/fill_test.go b/fill_test.go index b970de4..529583c 100644 --- a/fill_test.go +++ b/fill_test.go @@ -69,7 +69,7 @@ func TestApiService_OpenOrderStatistics(t *testing.T) { t.SkipNow() s := NewApiServiceFromEnv() - rsp, err := s.OpenOrderStatistics("XBTUSDM") + rsp, err := s.OpenOrderStatistics("XBTUSDTM") if err != nil { t.Fatal(err) } diff --git a/funding_test.go b/funding_test.go index 3c09e03..71aaa2e 100644 --- a/funding_test.go +++ b/funding_test.go @@ -4,7 +4,7 @@ import "testing" func TestApiService_FundingHistory(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.FundingHistory(map[string]string{"symbol": "XBTUSDM"}) + rsp, err := s.FundingHistory(map[string]string{"symbol": "XBTUSDTM"}) if err != nil { t.Fatal(err) } diff --git a/kline_test.go b/kline_test.go index ee7821e..d141f28 100644 --- a/kline_test.go +++ b/kline_test.go @@ -7,7 +7,7 @@ import ( func TestApiService_KLines(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.KLines("XBTUSDM", "5", time.Now().UnixNano()/1e6-7*24*3600, time.Now().UnixNano()/1e6) + rsp, err := s.KLines("XBTUSDTM", "5", time.Now().UnixNano()/1e6-7*24*3600, time.Now().UnixNano()/1e6) if err != nil { t.Fatal(err) } diff --git a/market.go b/market.go index 297809a..5c963b8 100644 --- a/market.go +++ b/market.go @@ -238,3 +238,25 @@ func (as *ApiService) TradeStatistics() (*ApiResponse, error) { req := NewRequest(http.MethodGet, "/api/v1/trade-statistics", nil) return as.Call(req) } + +type AllTickersModel []TickerItem + +type TickerItem struct { + Sequence int64 `json:"sequence"` + Symbol string `json:"symbol"` + Side string `json:"side"` + Size int `json:"size"` + TradeID string `json:"tradeId"` + Price string `json:"price"` + BestBidPrice string `json:"bestBidPrice"` + BestBidSize int `json:"bestBidSize"` + BestAskPrice string `json:"bestAskPrice"` + BestAskSize int `json:"bestAskSize"` + Timestamp int64 `json:"ts"` +} + +// AllTickers Get Latest Ticker for All Contracts +func (as *ApiService) AllTickers() (*ApiResponse, error) { + req := NewRequest(http.MethodGet, "/api/v1/allTickers", nil) + return as.Call(req) +} diff --git a/market_test.go b/market_test.go index d076021..c459292 100644 --- a/market_test.go +++ b/market_test.go @@ -6,7 +6,7 @@ import ( func TestApiService_TickerLevel1(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.Ticker("XBTUSDM") + rsp, err := s.Ticker("XBTUSDTM") if err != nil { t.Fatal(err) } @@ -35,7 +35,7 @@ func TestApiService_TickerLevel1(t *testing.T) { func TestApiService_Level2Snapshot(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.Level2Snapshot("XBTUSDM") + rsp, err := s.Level2Snapshot("XBTUSDTM") if err != nil { t.Fatal(err) } @@ -62,7 +62,7 @@ func TestApiService_Level2Snapshot(t *testing.T) { func TestApiService_Level2MessageQuery(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.Level2MessageQuery("XBTUSDM", 1, 20) + rsp, err := s.Level2MessageQuery("XBTUSDTM", 1, 20) if err != nil { t.Fatal(err) } @@ -85,7 +85,7 @@ func TestApiService_Level2MessageQuery(t *testing.T) { func TestApiService_Level3Snapshot(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.Level3Snapshot("XBTUSDM") + rsp, err := s.Level3Snapshot("XBTUSDTM") if err != nil { t.Fatal(err) } @@ -116,7 +116,7 @@ func TestApiService_Level3Snapshot(t *testing.T) { func TestApiService_Level3SnapshotV2(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.Level3SnapshotV2("XBTUSDM") + rsp, err := s.Level3SnapshotV2("XBTUSDTM") if err != nil { t.Fatal(err) } @@ -133,7 +133,7 @@ func TestApiService_Level3SnapshotV2(t *testing.T) { func TestApiService_Level3MessageQuery(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.Level3MessageQuery("XBTUSDM", 1, 20) + rsp, err := s.Level3MessageQuery("XBTUSDTM", 1, 20) if err != nil { t.Fatal(err) } @@ -156,7 +156,7 @@ func TestApiService_Level3MessageQuery(t *testing.T) { func TestApiService_TradeHistory(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.TradeHistory("XBTUSDM") + rsp, err := s.TradeHistory("XBTUSDTM") if err != nil { t.Fatal(err) } @@ -213,7 +213,7 @@ func TestApiService_InterestQuery(t *testing.T) { func TestApiService_IndexQuery(t *testing.T) { s := NewApiServiceFromEnv() p := map[string]string{} - p["symbol"] = "XBTUSDM" + p["symbol"] = "XBTUSDTM" pp := &PaginationParam{CurrentPage: 1, PageSize: 10} rsp, err := s.IndexQuery(p, pp) if err != nil { @@ -240,7 +240,7 @@ func TestApiService_IndexQuery(t *testing.T) { func TestApiService_MarkPrice(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.MarkPrice("XBTUSDM") + rsp, err := s.MarkPrice("XBTUSDTM") if err != nil { t.Fatal(err) } @@ -264,7 +264,7 @@ func TestApiService_MarkPrice(t *testing.T) { func TestApiService_PremiumQuery(t *testing.T) { s := NewApiServiceFromEnv() p := map[string]string{} - p["symbol"] = "XBTUSDM" + p["symbol"] = "XBTUSDTM" pp := &PaginationParam{CurrentPage: 1, PageSize: 10} rsp, err := s.PremiumQuery(p, pp) if err != nil { @@ -291,7 +291,7 @@ func TestApiService_PremiumQuery(t *testing.T) { func TestApiService_FundingRate(t *testing.T) { s := NewApiServiceFromEnv() - rsp, err := s.FundingRate(".XBTUSDMFPI8H") + rsp, err := s.FundingRate(".XBTUSDTMFPI8H") if err != nil { t.Fatal(err) } @@ -324,3 +324,16 @@ func TestApiService_TradeStatistics(t *testing.T) { } t.Log(ToJsonString(tk)) } + +func TestApiService_AllTickers(t *testing.T) { + s := NewApiServiceFromEnv() + resp, err := s.AllTickers() + if err != nil { + t.Fatal(err) + } + m := &AllTickersModel{} + if err := resp.ReadData(m); err != nil { + t.Fatal(err) + } + t.Log(ToJsonString(m)) +} diff --git a/order.go b/order.go index 7e73126..f999969 100644 --- a/order.go +++ b/order.go @@ -4,7 +4,8 @@ import "net/http" // A CreateOrderResultModel represents the result of CreateOrder(). type CreateOrderResultModel struct { - OrderId string `json:"orderId"` + OrderId string `json:"orderId"` + ClientOid string `json:"clientOid"` } // CreateOrder places a new order. @@ -181,3 +182,38 @@ func (as *ApiService) CreateMultiOrders(p []*CreateOrderReq) (*ApiResponse, erro req := NewRequest(http.MethodPost, "/api/v1/orders/multi", p) return as.Call(req) } + +type STOrderReq struct { + ClientOid string `json:"clientOid"` + Side string `json:"side"` + Symbol string `json:"symbol"` + Leverage string `json:"leverage"` + Type string `json:"type"` + Remark string `json:"remark"` + TriggerStopUpPrice string `json:"triggerStopUpPrice"` + StopPriceType string `json:"stopPriceType"` + TriggerStopDownPrice string `json:"triggerStopDownPrice"` + ReduceOnly bool `json:"reduceOnly"` + CloseOrder bool `json:"closeOrder"` + ForceHold bool `json:"forceHold"` + Stp string `json:"stp"` + Price string `json:"price"` + Size int `json:"size"` + TimeInForce string `json:"timeInForce"` + PostOnly bool `json:"postOnly"` + Hidden bool `json:"hidden"` + Iceberg bool `json:"iceberg"` + VisibleSize int `json:"visibleSize"` + MarginMode string `json:"marginMode"` +} + +type STOrderRes struct { + OrderId string `json:"orderId"` + ClientOid string `json:"clientOid"` +} + +// CreateSTOrder Place take profit and stop loss order +func (as *ApiService) CreateSTOrder(p *STOrderReq) (*ApiResponse, error) { + req := NewRequest(http.MethodPost, "/api/v1/st-orders", p) + return as.Call(req) +} diff --git a/order_test.go b/order_test.go index 418a491..5f59222 100644 --- a/order_test.go +++ b/order_test.go @@ -293,3 +293,30 @@ func TestApiService_CreateMultiOrders(t *testing.T) { } t.Log(ToJsonString(o)) } + +func TestApiService_CreateSTOrders(t *testing.T) { + s := NewApiServiceFromEnv() + req := STOrderReq{ + ClientOid: IntToString(time.Now().UnixNano()), + Side: "buy", + Symbol: "XBTUSDM", + Leverage: "20", + Type: "limit", + Price: "8000", + Size: 1, + StopPriceType: "TP", + MarginMode: "ISOLATED", + TriggerStopUpPrice: "9000", + TriggerStopDownPrice: "8000", + TimeInForce: "GTC", + } + rsp, err := s.CreateSTOrder(&req) + if err != nil { + t.Fatal(err) + } + o := &STOrderRes{} + if err := rsp.ReadData(o); err != nil { + t.Fatal(err) + } + t.Log(ToJsonString(o)) +} diff --git a/position.go b/position.go index 86dbf19..cdbcb80 100644 --- a/position.go +++ b/position.go @@ -146,3 +146,25 @@ func (as *ApiService) GetPositionsHistoryV1(r *GetPositionsHistoryV1Req) (*ApiRe req := NewRequest(http.MethodGet, "/api/v1/history-positions", v) return as.Call(req) } + +type GetMaxOpenSizeReq struct { + Symbol string `url:"symbol"` + Price string `url:"price"` + Leverage string `url:"leverage"` +} + +type GetMaxOpenSizeResp struct { + Symbol string `json:"symbol"` + MaxBuyOpenSize int `json:"maxBuyOpenSize"` + MaxSellOpenSize int `json:"leverage"` +} + +// GetMaxOpenSize Get Maximum Open Position Size +func (as *ApiService) GetMaxOpenSize(r *GetMaxOpenSizeReq) (*ApiResponse, error) { + v, err := query.Values(r) + if err != nil { + return nil, err + } + req := NewRequest(http.MethodGet, "/api/v2/getMaxOpenSize", v) + return as.Call(req) +} diff --git a/position_test.go b/position_test.go index 483edaa..9f20afe 100644 --- a/position_test.go +++ b/position_test.go @@ -9,7 +9,7 @@ func TestApiService_Position(t *testing.T) { t.SkipNow() s := NewApiServiceFromEnv() - rsp, err := s.Position("XBTUSDM") + rsp, err := s.Position("XBTUSDTM") if err != nil { t.Fatal(err) } @@ -65,7 +65,7 @@ func TestApiService_autoDepositStatus(t *testing.T) { s := NewApiServiceFromEnv() p := map[string]string{ - "symbol": "XBTUSDM", + "symbol": "XBTUSDTM", "status": "true", } rsp, err := s.AutoDepositStatus(p) @@ -88,7 +88,7 @@ func TestApiService_DepositMargin(t *testing.T) { s := NewApiServiceFromEnv() p := map[string]string{ - "symbol": "XBTUSDM", + "symbol": "XBTUSDTM", "margin": "0.1111", "bizNo": IntToString(time.Now().UnixNano()), } @@ -155,3 +155,22 @@ func TestApiService_GetPositionsHistoryV1(t *testing.T) { } t.Log(ToJsonString(data)) } + +func TestApiService_GetMaxOpenSize(t *testing.T) { + s := NewApiServiceFromEnv() + + req := GetMaxOpenSizeReq{} + req.Symbol = "PEPEUSDTM" + req.Price = "0.0000000001" + req.Leverage = "10" + + resp, err := s.GetMaxOpenSize(&req) + if err != nil { + t.Fatal(err) + } + m := &GetMaxOpenSizeResp{} + if err := resp.ReadData(m); err != nil { + t.Fatal(err) + } + t.Log(ToJsonString(m)) +} diff --git a/websocket_test.go b/websocket_test.go index 5d3241b..c8c3c1c 100644 --- a/websocket_test.go +++ b/websocket_test.go @@ -102,9 +102,9 @@ func TestWebSocketClient_Subscribe(t *testing.T) { t.Fatal(err) } - ch1 := NewSubscribeMessage("/contractMarket/ticker:XBTUSDM", false) - ch2 := NewSubscribeMessage("/contractMarket/ticker:XBTUSDM", false) - uch := NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDM", false) + ch1 := NewSubscribeMessage("/contractMarket/ticker:XBTUSDTM", false) + ch2 := NewSubscribeMessage("/contractMarket/ticker:XBTUSDTM", false) + uch := NewUnsubscribeMessage("/contractMarket/ticker:XBTUSDTM", false) if err := c.Subscribe(ch1, ch2); err != nil { t.Fatal(err) @@ -120,13 +120,13 @@ func TestWebSocketClient_Subscribe(t *testing.T) { t.Log(ToJsonString(msg)) i++ if i == 5 { - t.Log("Unsubscribe XBTUSDM") + t.Log("Unsubscribe XBTUSDTM") if err = c.Unsubscribe(uch); err != nil { t.Fatal(err) } } if i == 10 { - t.Log("Subscribe XBTUSDM") + t.Log("Subscribe XBTUSDTM") if err = c.Subscribe(ch2); err != nil { t.Fatal(err) }