Skip to content

Commit 729a8ef

Browse files
authored
Merge pull request #946 from ccxt/trailing-delta-binance
fix(client): add trailingDelta check
2 parents fa0413f + 3042e1e commit 729a8ef

2 files changed

Lines changed: 50 additions & 12 deletions

File tree

src/node-binance-api.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,9 @@ export default class Binance {
735735
* @param {string} quantity - The quantity to buy or sell
736736
* @param {string} price - The price per unit to transact each unit at
737737
* @param {object} params - additional order settings
738+
* @param {number} [params.quoteOrderQty] - The quote order quantity, used for MARKET orders
739+
* @param {number} [params.stopPrice] - The stop price, used for STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT orders
740+
* @param {number} [params.trailingDelta] - Delta price
738741
* @return {undefined}
739742
*/
740743
async order(type: OrderType, side: OrderSide, symbol: string, quantity: number, price?: number, params: Dict = {}): Promise<Order> {
@@ -778,6 +781,15 @@ export default class Binance {
778781
request.newClientOrderId = this.SPOT_PREFIX + this.uuid22();
779782
}
780783

784+
const allowedTypesForStopAndTrailing = ['STOP_LOSS', 'STOP_LOSS_LIMIT', 'TAKE_PROFIT', 'TAKE_PROFIT_LIMIT'];
785+
if (params.trailingDelta) {
786+
request.trailingDelta = params.trailingDelta;
787+
788+
if (!allowedTypesForStopAndTrailing.includes(request.type)) {
789+
throw Error('trailingDelta: Must set "type" to one of the following: STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT');
790+
}
791+
}
792+
781793
/*
782794
* STOP_LOSS
783795
* STOP_LOSS_LIMIT
@@ -788,7 +800,9 @@ export default class Binance {
788800
// if (typeof params.icebergQty !== 'undefined') request.icebergQty = params.icebergQty;
789801
if (params.stopPrice) {
790802
request.stopPrice = params.stopPrice;
791-
if (request.type === 'LIMIT') throw Error('stopPrice: Must set "type" to one of the following: STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT');
803+
if (!allowedTypesForStopAndTrailing.includes(request.type)) {
804+
throw Error('stopPrice: Must set "type" to one of the following: STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT');
805+
}
792806
}
793807
const response = await this.privateSpotRequest(endpoint, this.extend(request, params), 'POST');
794808
// to do error handling

tests/binance-class-live.test.ts

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,10 @@ describe('Limit buy Order', function () {
272272
assert(res['orderId'] !== undefined)
273273
spotOrderId = res['orderId'];
274274
} catch (e) {
275-
assert(e.toString().includes('{"code":-2010,"msg":"Account has insufficient balance for requested action."}'));
275+
const exceptionA = '{"code":-2010,"msg":"Account has insufficient balance for requested action."}';
276+
const exceptionB = '{"code":-2019,"msg":"Margin is insufficient."}'
277+
const eStr = e.toString();
278+
assert(eStr.includes(exceptionA) || eStr.includes(exceptionB));
276279
}
277280
}
278281

@@ -287,7 +290,10 @@ describe('MarketSell', function () {
287290
const res = await binance.marketSell('LTCUSDT', quantity)
288291
assert(res['orderId'] !== undefined)
289292
} catch (e) {
290-
assert(e.toString().includes('{"code":-2010,"msg":"Account has insufficient balance for requested action."}'));
293+
const exceptionA = '{"code":-2010,"msg":"Account has insufficient balance for requested action."}';
294+
const exceptionB = '{"code":-2019,"msg":"Margin is insufficient."}'
295+
const eStr = e.toString();
296+
assert(eStr.includes(exceptionA) || eStr.includes(exceptionB));
291297
}
292298

293299
}).timeout(TIMEOUT);
@@ -301,28 +307,46 @@ describe('Futures MarketBuy', function () {
301307
assert(res['orderId'] !== undefined)
302308
futuresOrderId = res['orderId'];
303309
} catch (e) {
304-
assert(e.toString().includes('{"code":-2010,"msg":"Account has insufficient balance for requested action."}'));
310+
const exceptionA = '{"code":-2010,"msg":"Account has insufficient balance for requested action."}';
311+
const exceptionB = '{"code":-2019,"msg":"Margin is insufficient."}'
312+
const eStr = e.toString();
313+
assert(eStr.includes(exceptionA) || eStr.includes(exceptionB));
305314
}
306315

307316
}).timeout(TIMEOUT);
308317
});
309318

310319
describe('Futures Limit buy Order', function () {
311320
it('Attempt to buy ETH', async function () {
312-
if (ethusdtPrice !== 0) {
313-
let quantity = 0.1;
314-
const res = await futuresBinance.futuresOrder('LIMIT', 'BUY', 'ETHUSDT', quantity, Math.round(ethusdtPrice * 0.8))
315-
assert(res['orderId'] !== undefined)
316-
futuresOrderId = res['orderId'];
321+
try {
322+
if (ethusdtPrice !== 0) {
323+
let quantity = 0.1;
324+
const res = await futuresBinance.futuresOrder('LIMIT', 'BUY', 'ETHUSDT', quantity, Math.round(ethusdtPrice * 0.8))
325+
assert(res['orderId'] !== undefined)
326+
futuresOrderId = res['orderId'];
327+
}
328+
} catch (e) {
329+
const exceptionA = '{"code":-2010,"msg":"Account has insufficient balance for requested action."}';
330+
const exceptionB = '{"code":-2019,"msg":"Margin is insufficient."}'
331+
const eStr = e.toString();
332+
assert(eStr.includes(exceptionA) || eStr.includes(exceptionB));
317333
}
334+
318335
}).timeout(TIMEOUT);
319336
});
320337

321338
describe('Futures MarketSell', function () {
322339
it('futures Attempt to buy ETH at market price', async function () {
323-
let quantity = 0.1;
324-
const res = await futuresBinance.futuresMarketSell('ETHUSDT', quantity)
325-
assert(res['orderId'] !== undefined)
340+
try {
341+
let quantity = 0.1;
342+
const res = await futuresBinance.futuresMarketSell('ETHUSDT', quantity)
343+
assert(res['orderId'] !== undefined)
344+
} catch (e) {
345+
const exceptionA = '{"code":-2010,"msg":"Account has insufficient balance for requested action."}';
346+
const exceptionB = '{"code":-2019,"msg":"Margin is insufficient."}'
347+
const eStr = e.toString();
348+
assert(eStr.includes(exceptionA) || eStr.includes(exceptionB));
349+
}
326350

327351
}).timeout(TIMEOUT);
328352
});

0 commit comments

Comments
 (0)