com.binance.connector.client.impl.websocketapi.WebSocketApiTrade Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of binance-connector-java Show documentation
Show all versions of binance-connector-java Show documentation
lightweight connector to API
package com.binance.connector.client.impl.websocketapi;
import org.json.JSONObject;
import com.binance.connector.client.utils.JSONParser;
import com.binance.connector.client.utils.ParameterChecker;
import com.binance.connector.client.utils.websocketapi.WebSocketApiRequestHandler;
/**
* Trading Requests
* All requests under the
* Trading requests
* section of the WebSocket API documentation will be implemented in this class.
*
* Response will be returned as callback.
*/
public class WebSocketApiTrade {
private WebSocketApiRequestHandler handler;
public WebSocketApiTrade(WebSocketApiRequestHandler handler) {
this.handler = handler;
}
/**
* Send in a new order.
*
* Additional mandatory parameters (*) are determined by the new order type.
*
* @param symbol String
* @param side String -- BUY or SELL
* @param type String
* @param parameters JSONObject composed by key-value pairs:
*
* timeInForce -- optional/String
* price -- optional/double
* quantity -- optional/double
* quoteOrderQty -- optional/double
* newClientOrderId -- optional/String -- Arbitrary unique ID among open orders. Automatically generated if not sent
* newOrderRespType -- optional/String -- Select response format: ACK, RESULT, FULL. MARKET and LIMIT orders use FULL by default, other order types default to ACK.
* stopPrice -- optional/double
* trailingDelta -- optional/int -- see https://github.com/binance/binance-spot-api-docs/blob/master/faqs/trailing-stop-faq.md
* icebergQty -- optional/double
* strategyId -- optional/int -- Arbitrary numeric value identifying the order within an order strategy.
* strategyType -- optional/int -- Arbitrary numeric value identifying the order strategy. Values smaller than 1000000 are reserved and cannot be used.
* selfTradePreventionMode -- optional/String -- The allowed enums is dependent on what is configured on the symbol. The possible supported values are EXPIRE_TAKER, EXPIRE_MAKER, EXPIRE_BOTH, NONE.
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#place-new-order-trade
*/
public void newOrder(String symbol, String side, String type, JSONObject parameters) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
ParameterChecker.checkParameterType(side, String.class, "side");
ParameterChecker.checkParameterType(type, String.class, "type");
parameters = JSONParser.addKeyValue(parameters, "symbol", symbol);
parameters = JSONParser.addKeyValue(parameters, "side", side);
parameters = JSONParser.addKeyValue(parameters, "type", type);
this.handler.signedRequest("order.place", parameters);
}
/**
* Test order placement.
*
* Validates new order parameters and verifies your signature but does not send the order into the matching engine.
* Additional mandatory parameters (*) are determined by the new order type.
*
* @param symbol String
* @param side String -- BUY or SELL
* @param type String
* @param parameters JSONObject composed by key-value pairs:
*
* timeInForce -- optional/String
* price -- optional/double
* quantity -- optional/double
* quoteOrderQty -- optional/double
* newClientOrderId -- optional/String -- Arbitrary unique ID among open orders. Automatically generated if not sent
* newOrderRespType -- optional/String -- Select response format: ACK, RESULT, FULL. MARKET and LIMIT orders use FULL by default, other order types default to ACK.
* stopPrice -- optional/double
* trailingDelta -- optional/int -- see https://github.com/binance/binance-spot-api-docs/blob/master/faqs/trailing-stop-faq.md
* icebergQty -- optional/double
* strategyId -- optional/int -- Arbitrary numeric value identifying the order within an order strategy.
* strategyType -- optional/int -- Arbitrary numeric value identifying the order strategy. Values smaller than 1000000 are reserved and cannot be used.
* selfTradePreventionMode -- optional/String -- The allowed enums is dependent on what is configured on the symbol. The possible supported values are EXPIRE_TAKER, EXPIRE_MAKER, EXPIRE_BOTH, NONE.
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#test-new-order-trade
*/
public void testNewOrder(String symbol, String side, String type, JSONObject parameters) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
ParameterChecker.checkParameterType(side, String.class, "side");
ParameterChecker.checkParameterType(type, String.class, "type");
parameters = JSONParser.addKeyValue(parameters, "symbol", symbol);
parameters = JSONParser.addKeyValue(parameters, "side", side);
parameters = JSONParser.addKeyValue(parameters, "type", type);
this.handler.signedRequest("order.test", parameters);
}
/**
* Check execution status of an order.
*
* If both orderId and origClientOrderId parameters are specified, only orderId is used and origClientOrderId is ignored.
*
* @param symbol String
* @param parameters JSONObject composed by key-value pairs:
*
* orderId -- optional/int -- Lookup order by orderId
* origClientOrderId -- optional/String -- Lookup order by clientOrderId
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#query-order-user_data
*/
public void getOrder(String symbol, JSONObject parameters) {
ParameterChecker.checkOneOfParametersRequired(parameters, "orderId", "origClientOrderId");
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
parameters = JSONParser.addKeyValue(parameters, "symbol", symbol);
this.handler.signedRequest("order.status", parameters);
}
/**
* Cancel an active order.
*
* If both orderId and origClientOrderId parameters are specified, only orderId is used and origClientOrderId is ignored.
*
* @param symbol String
* @param parameters JSONObject composed by key-value pairs:
*
* orderId -- optional/int -- Cancel order by orderId
* origClientOrderId -- optional/String -- Cancel order by clientOrderId
* newClientOrderId -- optional/String -- New ID for the canceled order. Automatically generated if not sent
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#cancel-order-trade
*/
public void cancelOrder(String symbol, JSONObject parameters) {
ParameterChecker.checkOneOfParametersRequired(parameters, "orderId", "origClientOrderId");
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
parameters = JSONParser.addKeyValue(parameters, "symbol", symbol);
this.handler.signedRequest("order.cancel", parameters);
}
/**
* Cancel an existing order and immediately place a new order instead of the canceled one.
*
* Similar to the order.place request, additional mandatory parameters (*) are determined by the new order type.
* If both cancelOrderId and cancelOrigClientOrderId parameters are specified, only cancelOrderId is used and cancelOrigClientOrderId is ignored.
*
* @param symbol String
* @param cancelReplaceMode String -- STOP_ON_FAILURE or ALLOW_FAILURE
* @param side String -- BUY or SELL
* @param type String
* @param parameters JSONObject composed by key-value pairs:
*
* cancelOrderId -- optional/int -- Cancel order by orderId
* cancelOrigClientOrderId -- optional/String -- Cancel order by clientOrderId
* cancelNewClientOrderId -- optional/String -- New ID for the canceled order. Automatically generated if not sent
* timeInForce -- optional/String
* price -- optional/double
* quantity -- optional/double
* quoteOrderQty -- optional/double
* newClientOrderId -- optional/String -- Arbitrary unique ID among open orders. Automatically generated if not sent
* newOrderRespType -- optional/String -- Select response format: ACK, RESULT, FULL. MARKET and LIMIT orders use FULL by default, other order types default to ACK.
* stopPrice -- optional/double
* trailingDelta -- optional/int -- see https://github.com/binance/binance-spot-api-docs/blob/master/faqs/trailing-stop-faq.md
* icebergQty -- optional/double
* strategyId -- optional/int -- Arbitrary numeric value identifying the order within an order strategy.
* strategyType -- optional/int -- Arbitrary numeric value identifying the order strategy. Values smaller than 1000000 are reserved and cannot be used.
* selfTradePreventionMode -- optional/String -- The allowed enums is dependent on what is configured on the symbol. The possible supported values are EXPIRE_TAKER, EXPIRE_MAKER, EXPIRE_BOTH, NONE.
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#cancel-and-replace-order-trade
*/
public void cancelReplaceOrder(String symbol, String cancelReplaceMode, String side, String type, JSONObject parameters) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
ParameterChecker.checkParameterType(cancelReplaceMode, String.class, "cancelReplaceMode");
ParameterChecker.checkParameterType(side, String.class, "side");
ParameterChecker.checkParameterType(type, String.class, "type");
ParameterChecker.checkOneOfParametersRequired(parameters, "cancelOrderId", "cancelOrigClientOrderId");
parameters = JSONParser.addKeyValue(parameters, "symbol", symbol);
parameters = JSONParser.addKeyValue(parameters, "cancelReplaceMode", cancelReplaceMode);
parameters = JSONParser.addKeyValue(parameters, "side", side);
parameters = JSONParser.addKeyValue(parameters, "type", type);
this.handler.signedRequest("order.cancelReplace", parameters);
}
/**
* Query execution status of all open orders.
*
* @param parameters JSONObject composed by key-value pairs:
*
* symbol -- optional/String -- If omitted, open orders for all symbols are returned
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#current-open-orders-user_data
*/
public void getOpenOrders(JSONObject parameters) {
this.handler.signedRequest("openOrders.status", parameters);
}
/**
* Cancel all open orders on a symbol, including OCO orders.
* Cancellation reports for orders and OCOs have the same format as in order.cancel.
*
* @param symbol String
* @param parameters JSONObject composed by key-value pairs:
*
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#cancel-open-orders-trade
*/
public void cancelAllOpenOrders(String symbol, JSONObject parameters) {
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
parameters = JSONParser.addKeyValue(parameters, "symbol", symbol);
this.handler.signedRequest("openOrders.cancelAll", parameters);
}
/**
* Send in a new one-cancels-the-other (OCO) pair: LIMIT_MAKER + STOP_LOSS/STOP_LOSS_LIMIT orders (called legs), where activation of one order immediately cancels the other.
*
* @param symbol String
* @param side String -- BUY or SELL
* @param price double -- Price for the limit order
* @param quantity double
* @param parameters JSONObject composed by key-value pairs:
*
* listClientOrderId -- optional/String -- Arbitrary unique ID among open OCOs. Automatically generated if not sent.
* limitClientOrderId -- optional/String -- Arbitrary unique ID among open orders for the limit order. Automatically generated if not sent.
* limitIcebergQty -- optional/double
* limitStrategyId -- optional/int -- Arbitrary numeric value identifying the limit order within an order strategy.
* limitStrategyType -- optional/int -- Arbitrary numeric value identifying the limit order strategy. Values smaller than 1000000 are reserved and cannot be used.
* stopPrice -- optional/double -- Either stopPrice or trailingDelta, or both must be specified
* trailingDelta -- optional/int -- see https://github.com/binance/binance-spot-api-docs/blob/master/faqs/trailing-stop-faq.md
* stopClientOrderId -- optional/String -- Arbitrary unique ID among open orders for the stop order. Automatically generated if not sent.
* stopLimitPrice -- optional/double
* stopLimitTimeInForce -- optional/String -- See order.place for available options
* stopIcebergQty -- optional/double
* stopStrategyId -- optional/int -- Arbitrary numeric value identifying the stop order within an order strategy.
* stopStrategyType -- optional/int -- Arbitrary numeric value identifying the stop order strategy. Values smaller than 1000000 are reserved and cannot be used.
* newOrderRespType -- optional/String -- Select response format: ACK, RESULT, FULL (default).
* selfTradePreventionMode -- optional/String -- The allowed enums is dependent on what is configured on the symbol. The possible supported values are EXPIRE_TAKER, EXPIRE_MAKER, EXPIRE_BOTH, NONE.
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#place-new-oco-trade
*/
public void newOcoOrder(String symbol, String side, double price, double quantity, JSONObject parameters) {
ParameterChecker.checkOneOfParametersRequired(parameters, "stopPrice", "trailingDelta");
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
ParameterChecker.checkParameterType(side, String.class, "side");
ParameterChecker.checkParameterType(price, Double.class, "price");
ParameterChecker.checkParameterType(quantity, Double.class, "quantity");
parameters = JSONParser.addKeyValue(parameters, "symbol", symbol);
parameters = JSONParser.addKeyValue(parameters, "side", side);
parameters = JSONParser.addKeyValue(parameters, "price", price);
parameters = JSONParser.addKeyValue(parameters, "quantity", quantity);
this.handler.signedRequest("orderList.place", parameters);
}
/**
* Check execution status of an OCO.
*
* If both origClientOrderId and orderListId parameters are specified, only origClientOrderId is used and orderListId is ignored.
*
* @param parameters JSONObject composed by key-value pairs:
*
* origClientOrderId -- optional/String -- Query OCO by listClientOrderId
* orderListId -- required/int -- Query OCO by orderListId
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#query-oco-user_data
*/
public void getOcoOrder(JSONObject parameters) {
ParameterChecker.checkOneOfParametersRequired(parameters, "origClientOrderId", "orderListId");
this.handler.signedRequest("orderList.status", parameters);
}
/**
* Cancel an active OCO.
*
* If both orderListId and listClientOrderId parameters are specified, only orderListId is used and listClientOrderId is ignored.
* Canceling an individual leg with order.cancel will cancel the entire OCO as well.
*
* @param symbol String
* @param parameters JSONObject composed by key-value pairs:
*
* listClientOrderId -- optional/String -- Cancel OCO by listClientOrderId
* orderListId -- required/int -- Cancel OCO by orderListId
* newClientOrderId -- optional/String -- New ID for the canceled OCO. Automatically generated if not sent.
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#cancel-oco-trade
*/
public void cancelOcoOrder(String symbol, JSONObject parameters) {
ParameterChecker.checkOneOfParametersRequired(parameters, "listClientOrderId", "orderListId");
ParameterChecker.checkParameterType(symbol, String.class, "symbol");
parameters = JSONParser.addKeyValue(parameters, "symbol", symbol);
this.handler.signedRequest("orderList.cancel", parameters);
}
/**
* Query execution status of all open OCOs.
*
* If both orderListId and listClientOrderId parameters are specified, only orderListId is used and listClientOrderId is ignored.
* Canceling an individual leg with order.cancel will cancel the entire OCO as well.
*
* @param parameters JSONObject composed by key-value pairs:
*
* recvWindow -- optional/int -- The value cannot be greater than 60000
* requestId -- optional/String or int
*
* @see
* https://binance-docs.github.io/apidocs/websocket_api/en/#current-open-ocos-user_data
*/
public void getOpenOcoOrders(JSONObject parameters) {
this.handler.signedRequest("openOrderLists.status", parameters);
}
}