Skip to content

Commit ddf7eb3

Browse files
committed
feat: add new algo service
1 parent 5fca9eb commit ddf7eb3

File tree

2 files changed

+172
-17
lines changed

2 files changed

+172
-17
lines changed

src/node-binance-api.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,9 +1187,70 @@ export default class Binance {
11871187
if (!params.newClientOrderId) {
11881188
params.newClientOrderId = this.CONTRACT_PREFIX + this.uuid22();
11891189
}
1190+
// check if it is algoOrder
1191+
const conditionalTypes = [
1192+
'STOP',
1193+
'STOP_MARKET',
1194+
'TAKE_PROFIT',
1195+
'TAKE_PROFIT_MARKET',
1196+
'TRAILING_STOP_MARKET',
1197+
];
1198+
const typeUpperCase = type.toUpperCase();
1199+
if (typeUpperCase && conditionalTypes.includes(typeUpperCase)) {
1200+
const algoPayload = { ...params };
1201+
if (!algoPayload.clientAlgoId) {
1202+
algoPayload.clientAlgoId = this.CONTRACT_PREFIX + this.uuid22();
1203+
}
1204+
delete algoPayload.newClientOrderId;
1205+
algoPayload.algoType = 'CONDITIONAL';
1206+
if (algoPayload.stopPrice && !algoPayload.triggerPrice) {
1207+
algoPayload.triggerPrice = algoPayload.stopPrice;
1208+
delete algoPayload.stopPrice;
1209+
}
1210+
return await this.privateFuturesRequest('v1/algoOrder', algoPayload, 'POST');
1211+
}
11901212
return await this.privateFuturesRequest('v1/order', params, 'POST');
11911213
}
11921214

1215+
// Futures internal functions
1216+
/**
1217+
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/New-Algo-Order
1218+
* @param type
1219+
* @param side
1220+
* @param symbol symbol if the market
1221+
* @param quantity
1222+
* @param price
1223+
* @param params extra parameters to be sent in the request
1224+
* @returns
1225+
*/
1226+
async futuresAlgoOrder(type: OrderType, side: string, symbol: string, quantity: number, price?: number, params: Dict = {}): Promise<FuturesOrder> {
1227+
params.symbol = symbol;
1228+
params.side = side;
1229+
params.type = type;
1230+
if (quantity) params.quantity = quantity;
1231+
// if in the binance futures setting Hedged mode is active, positionSide parameter is mandatory
1232+
if (!params.positionSide && this.Options.hedgeMode) {
1233+
params.positionSide = side === 'BUY' ? 'LONG' : 'SHORT';
1234+
}
1235+
// LIMIT STOP MARKET STOP_MARKET TAKE_PROFIT TAKE_PROFIT_MARKET
1236+
// reduceOnly stopPrice
1237+
if (price) {
1238+
params.price = price;
1239+
}
1240+
if (!params.timeInForce && (params.type.includes('LIMIT') || params.type === 'STOP' || params.type === 'TAKE_PROFIT')) {
1241+
params.timeInForce = 'GTX'; // Post only by default. Use GTC for limit orders.
1242+
}
1243+
1244+
if (!params.clientAlgoId) {
1245+
params.clientAlgoId = this.CONTRACT_PREFIX + this.uuid22();
1246+
}
1247+
1248+
if (!params.algoType) {
1249+
params.algoType = 'CONDITIONAL';
1250+
}
1251+
return await this.privateFuturesRequest('v1/algoOrder', params, 'POST');
1252+
}
1253+
11931254
/**
11941255
* @see https://developers.binance.com/docs/derivatives/option/trade/New-Order
11951256
* @param type type of order to create
@@ -4502,39 +4563,92 @@ export default class Binance {
45024563

45034564
/**
45044565
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Query-Order
4566+
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Query-Algo-Order
45054567
* @param symbol symbol if the market
45064568
* @param params extra parameters to be sent in the request
4569+
* @param params.conditional set to true to query algo order
45074570
* @returns
45084571
*/
45094572
async futuresOrderStatus(symbol: string, params: Dict = {}): Promise<FuturesOrder> { // Either orderId or origClientOrderId must be sent
45104573
params.symbol = symbol;
4574+
if ('conditional' in params) {
4575+
delete params.conditional;
4576+
return await this.privateFuturesRequest('v1/algoOrder', params);
4577+
}
45114578
return await this.privateFuturesRequest('v1/order', params);
45124579
}
45134580

4581+
/**
4582+
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Query-Algo-Order
4583+
* @param symbol symbol if the market
4584+
* @param params extra parameters to be sent in the request
4585+
* @returns
4586+
*/
4587+
async futuresAlgoOrderStatus(symbol: string, params: Dict = {}): Promise<FuturesOrder> { // Either orderId or origClientOrderId must be sent
4588+
params.symbol = symbol;
4589+
return await this.privateFuturesRequest('v1/algoOrder', params);
4590+
}
4591+
45144592
/**
45154593
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-Order
4594+
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-Algo-Order
45164595
* @param symbol symbol if the market
45174596
* @param orderId
4597+
* @param params.conditional set to true to cancel algo order
45184598
* @param params extra parameters to be sent in the request
45194599
* @returns
45204600
*/
45214601
async futuresCancel(symbol: string, orderId?: number | string, params: Dict = {}): Promise<CancelOrder> { // Either orderId or origClientOrderId must be sent
45224602
params.symbol = symbol;
4603+
if ('conditional' in params) {
4604+
delete params.conditional;
4605+
if (orderId) params.algoid = orderId;
4606+
return await this.privateFuturesRequest('v1/algoOrder', params, 'DELETE');
4607+
}
45234608
if (orderId) params.orderId = orderId;
45244609
return await this.privateFuturesRequest('v1/order', params, 'DELETE');
45254610
}
45264611

4612+
/**
4613+
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-Algo-Order
4614+
* @param symbol symbol if the market
4615+
* @param orderId
4616+
* @param params extra parameters to be sent in the request
4617+
* @returns
4618+
*/
4619+
async futuresCancelAlgoOrder(symbol: string, orderId?: number | string, params: Dict = {}): Promise<CancelOrder> { // Either orderId or origClientOrderId must be sent
4620+
params.symbol = symbol;
4621+
if (orderId) params.algoid = orderId;
4622+
return await this.privateFuturesRequest('v1/algoOrder', params, 'DELETE');
4623+
}
4624+
45274625
/**
45284626
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-All-Open-Orders
45294627
* @param symbol symbol if the market
45304628
* @param params extra parameters to be sent in the request
4629+
* @param params.conditional set to true to cancel algo order
45314630
* @returns
45324631
*/
45334632
async futuresCancelAll(symbol: string, params: Dict = {}): Promise<Response> {
45344633
params.symbol = symbol;
4634+
if ('conditional' in params) {
4635+
delete params.conditional;
4636+
return await this.privateFuturesRequest('v1/algoOpenOrders', params, 'DELETE');
4637+
}
45354638
return await this.privateFuturesRequest('v1/allOpenOrders', params, 'DELETE');
45364639
}
45374640

4641+
/**
4642+
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-All-Algo-Open-Orders
4643+
* @param symbol symbol if the market
4644+
* @param params extra parameters to be sent in the request
4645+
* @returns
4646+
*/
4647+
async futuresCancelAllAlgo(symbol: string, params: Dict = {}): Promise<Response> {
4648+
params.symbol = symbol;
4649+
return await this.privateFuturesRequest('v1/algoOpenOrders', params, 'DELETE');
4650+
}
4651+
45384652
/**
45394653
*
45404654
* @param symbol symbol if the market
@@ -4552,13 +4666,29 @@ export default class Binance {
45524666
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Current-All-Open-Orders
45534667
* @param symbol symbol if the market
45544668
* @param params extra parameters to be sent in the request
4669+
* @param params.conditional set to true to query algo open orders
45554670
* @returns
45564671
*/
45574672
async futuresOpenOrders(symbol?: string, params: Dict = {}): Promise<FuturesOrder[]> {
45584673
if (symbol) params.symbol = symbol;
4674+
if ('conditional' in params) {
4675+
delete params.conditional;
4676+
return await this.privateFuturesRequest('v1/algoOpenOrders', params);
4677+
}
45594678
return await this.privateFuturesRequest('v1/openOrders', params);
45604679
}
45614680

4681+
/**
4682+
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Current-All-Algo-Open-Orders
4683+
* @param symbol symbol if the market
4684+
* @param params extra parameters to be sent in the request
4685+
* @returns
4686+
*/
4687+
async futuresOpenAlgoOrders(symbol?: string, params: Dict = {}): Promise<FuturesOrder[]> {
4688+
if (symbol) params.symbol = symbol;
4689+
return await this.privateFuturesRequest('v1/openAlgoOrders', params);
4690+
}
4691+
45624692
/**
45634693
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/All-Orders
45644694
* @param symbol symbol if the market
@@ -4570,6 +4700,17 @@ export default class Binance {
45704700
return await this.privateFuturesRequest('v1/allOrders', params);
45714701
}
45724702

4703+
/**
4704+
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Query-All-Algo-Orders
4705+
* @param symbol symbol if the market
4706+
* @param params extra parameters to be sent in the request
4707+
* @returns
4708+
*/
4709+
async futuresAllAlgoOrders(symbol?: string, params: Dict = {}): Promise<FuturesOrder[]> { // Get all account orders; active, canceled, or filled.
4710+
if (symbol) params.symbol = symbol;
4711+
return await this.privateFuturesRequest('v1/allAlgoOrders', params);
4712+
}
4713+
45734714
/**
45744715
*
45754716
* @param params extra parameters to be sent in the request

src/types.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,28 +96,42 @@ export interface Order {
9696
}
9797

9898
export interface FuturesOrder {
99-
clientOrderId: string
100-
cumQty: string
101-
cumQuote: string
102-
executedQty: string
103-
orderId: number
104-
avgPrice: string
105-
origQty: string
99+
clientOrderId?: string
100+
cumQty?: string
101+
cumQuote?: string
102+
executedQty?: string
103+
orderId?: number
104+
avgPrice?: string
105+
origQty?: string
106106
price: string
107107
reduceOnly: boolean
108108
side: OrderSide
109109
positionSide: PositionSide
110-
status: OrderStatus
111-
stopPrice: string
112-
closePosition: boolean
110+
status?: OrderStatus
111+
stopPrice?: string
112+
closePosition?: boolean
113113
symbol: string
114-
timeInForce: TimeInForce
115-
type: OrderType
116-
origType: OrderType
117-
activatePrice: string
118-
priceRate: string
119-
updateTime: number
120-
workingType: WorkingType
114+
timeInForce?: TimeInForce
115+
type?: OrderType
116+
origType?: OrderType
117+
activatePrice?: string
118+
priceRate?: string
119+
updateTime?: number
120+
workingType?: WorkingType
121+
// algo orders fields
122+
algoId?: number;
123+
triggerPrice?: string;
124+
orderStatus?: string;
125+
clientAlgoId?: string;
126+
algoStatus?: string;
127+
actualOrderId?: string;
128+
actualPrice?: string;
129+
tpTriggerPrice?: string;
130+
tpPrice?: string;
131+
slTriggerPrice?: string;
132+
slPrice?: string;
133+
tpOrderType?: string;
134+
slOrderType?: string;
121135
}
122136

123137
export type PositionSide = 'BOTH' | 'SHORT' | 'LONG'

0 commit comments

Comments
 (0)