Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 187 additions & 0 deletions v2/algo_order_cancel_service_ws.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package binance

import (
"encoding/json"
"time"

"github.com/adshao/go-binance/v2/common"
"github.com/adshao/go-binance/v2/common/websocket"
"github.com/adshao/go-binance/v2/futures"
)

// AlgoOrderCancelWsService cancels algo order using WebSocket API
type AlgoOrderCancelWsService struct {
c websocket.Client
ApiKey string
SecretKey string
KeyType string
TimeOffset int64
}

// NewAlgoOrderCancelWsService init AlgoOrderCancelWsService
func NewAlgoOrderCancelWsService(apiKey, secretKey string) (*AlgoOrderCancelWsService, error) {
conn, err := websocket.NewConnection(futures.WsApiInitReadWriteConn, futures.WebsocketKeepalive, futures.WebsocketTimeoutReadWriteConnection)
if err != nil {
return nil, err
}

client, err := websocket.NewClient(conn)
if err != nil {
return nil, err
}

return &AlgoOrderCancelWsService{
c: client,
ApiKey: apiKey,
SecretKey: secretKey,
KeyType: common.KeyTypeHmac,
}, nil
}

// AlgoOrderCancelWsRequest parameters for 'algoOrder.cancel' websocket API
type AlgoOrderCancelWsRequest struct {
algoId *int64
clientAlgoId *string
recvWindow *int64
}

// NewAlgoOrderCancelWsRequest init AlgoOrderCancelWsRequest
func NewAlgoOrderCancelWsRequest() *AlgoOrderCancelWsRequest {
return &AlgoOrderCancelWsRequest{}
}

// AlgoID set algoID
func (s *AlgoOrderCancelWsRequest) AlgoID(algoID int64) *AlgoOrderCancelWsRequest {
s.algoId = &algoID
return s
}

// ClientAlgoID set clientAlgoID
func (s *AlgoOrderCancelWsRequest) ClientAlgoID(clientAlgoID string) *AlgoOrderCancelWsRequest {
s.clientAlgoId = &clientAlgoID
return s
}

// RecvWindow set recvWindow
func (s *AlgoOrderCancelWsRequest) RecvWindow(recvWindow int64) *AlgoOrderCancelWsRequest {
s.recvWindow = &recvWindow
return s
}

// buildParams builds params
func (s *AlgoOrderCancelWsRequest) buildParams() map[string]interface{} {
m := map[string]interface{}{}

if s.algoId != nil {
m["algoid"] = *s.algoId
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter name should be "algoId" (camelCase) instead of "algoid" to match the API specification and maintain consistency with the REST API implementation in futures/algo_order_service.go which uses "algoId".

Suggested change
m["algoid"] = *s.algoId
m["algoId"] = *s.algoId

Copilot uses AI. Check for mistakes.
}

if s.clientAlgoId != nil {
m["clientalgoid"] = *s.clientAlgoId
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parameter name should be "clientAlgoId" (camelCase) instead of "clientalgoid" to match the API specification and maintain consistency with the REST API implementation in futures/algo_order_service.go which uses "clientAlgoId".

Suggested change
m["clientalgoid"] = *s.clientAlgoId
m["clientAlgoId"] = *s.clientAlgoId

Copilot uses AI. Check for mistakes.
}

if s.recvWindow != nil {
m["recvWindow"] = *s.recvWindow
}

return m
}

// CancelAlgoOrderResult define algo order cancel result
type CancelAlgoOrderResult struct {
AlgoId int64 `json:"algoId"`
ClientAlgoId string `json:"clientAlgoId"`
Code string `json:"code"`
Message string `json:"msg"`
}

// CancelAlgoOrderWsResponse define 'algoOrder.cancel' websocket API response
type CancelAlgoOrderWsResponse struct {
Id string `json:"id"`
Status int `json:"status"`
Result CancelAlgoOrderResult `json:"result"`

// error response
Error *common.APIError `json:"error,omitempty"`
}

// Do - sends 'algoOrder.cancel' request
func (s *AlgoOrderCancelWsService) Do(requestID string, request *AlgoOrderCancelWsRequest) error {
// Use custom method "algoOrder.cancel"
method := websocket.WsApiMethodType("algoOrder.cancel")

rawData, err := websocket.CreateRequest(
websocket.NewRequestData(
requestID,
s.ApiKey,
s.SecretKey,
s.TimeOffset,
s.KeyType,
),
method,
request.buildParams(),
)
if err != nil {
return err
}

if err := s.c.Write(requestID, rawData); err != nil {
return err
}

return nil
}

// SyncDo - sends 'algoOrder.cancel' request and receives response
func (s *AlgoOrderCancelWsService) SyncDo(requestID string, request *AlgoOrderCancelWsRequest) (*CancelAlgoOrderWsResponse, error) {
// Use custom method "algoOrder.cancel"
method := websocket.WsApiMethodType("algoOrder.cancel")

rawData, err := websocket.CreateRequest(
websocket.NewRequestData(
requestID,
s.ApiKey,
s.SecretKey,
s.TimeOffset,
s.KeyType,
),
method,
request.buildParams(),
)
if err != nil {
return nil, err
}

response, err := s.c.WriteSync(requestID, rawData, websocket.WriteSyncWsTimeout)
if err != nil {
return nil, err
}

cancelAlgoOrderWsResponse := &CancelAlgoOrderWsResponse{}
if err := json.Unmarshal(response, cancelAlgoOrderWsResponse); err != nil {
return nil, err
}

return cancelAlgoOrderWsResponse, nil
}

// ReceiveAllDataBeforeStop waits until all responses will be received from websocket until timeout expired
func (s *AlgoOrderCancelWsService) ReceiveAllDataBeforeStop(timeout time.Duration) {
s.c.Wait(timeout)
}

// GetReadChannel returns channel with API response data (including API errors)
func (s *AlgoOrderCancelWsService) GetReadChannel() <-chan []byte {
return s.c.GetReadChannel()
}

// GetReadErrorChannel returns channel with errors which are occurred while reading websocket connection
func (s *AlgoOrderCancelWsService) GetReadErrorChannel() <-chan error {
return s.c.GetReadErrorChannel()
}

// GetReconnectCount returns count of reconnect attempts by client
func (s *AlgoOrderCancelWsService) GetReconnectCount() int64 {
return s.c.GetReconnectCount()
}

Comment on lines +1 to +187
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new AlgoOrderCancelWsService lacks test coverage. Other similar WebSocket services in this codebase (e.g., order_service_ws_create.go, sor_order_place_service_ws.go, order_list_cancel_service_ws.go) all have corresponding test files. Consider adding comprehensive tests to verify the service initialization, request building, parameter handling, and response parsing.

Copilot uses AI. Check for mistakes.
Loading
Loading