From 3046443790222748c8310b5d547c07f2c483dace Mon Sep 17 00:00:00 2001 From: Nikolay Date: Thu, 4 Aug 2022 06:48:21 +0300 Subject: [PATCH] Adding the ability to create stop_loss_limit order with trailing delta (a.k.a trailing stop loss) --- examples/trailing-stop-loss.js | 12 ++++++++++++ node-binance-api.js | 1 + test.js | 13 ++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 examples/trailing-stop-loss.js diff --git a/examples/trailing-stop-loss.js b/examples/trailing-stop-loss.js new file mode 100644 index 00000000..e1f56137 --- /dev/null +++ b/examples/trailing-stop-loss.js @@ -0,0 +1,12 @@ +const Binance = require('node-binance-api'); +const binance = new Binance().options('options.json'); + +const quantity = 1; +const price = 2000; +const trailingDelta = 500; + +await binance.sell("ETHUSDT", quantity, price, { + timeInForce: 'GTC', + trailingDelta: trailingDelta, + type: 'STOP_LOSS_LIMIT' +}) \ No newline at end of file diff --git a/node-binance-api.js b/node-binance-api.js index 615485fc..f17b7cca 100644 --- a/node-binance-api.js +++ b/node-binance-api.js @@ -400,6 +400,7 @@ let api = function Binance( options = {} ) { * LIMIT_MAKER */ if ( typeof flags.icebergQty !== 'undefined' ) opt.icebergQty = flags.icebergQty; + if ( typeof flags.trailingDelta !== 'undefined' ) opt.trailingDelta = flags.trailingDelta; if ( typeof flags.stopPrice !== 'undefined' ) { opt.stopPrice = flags.stopPrice; if ( opt.type === 'LIMIT' ) throw Error( 'stopPrice: Must set "type" to one of the following: STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT' ); diff --git a/test.js b/test.js index 2c62e358..4b4eddc1 100644 --- a/test.js +++ b/test.js @@ -297,7 +297,7 @@ describe('Buy order advanced', function () { }); describe('Sell Stop loess', function () { - it('Attempt to create a stop loss order', function (done) { + it('Attempt to create a stop loss order with a trailing delta', function (done) { let type = 'STOP_LOSS'; let quantity = 1; let price = 0.069; @@ -307,6 +307,17 @@ describe('Sell Stop loess', function () { }).timeout(TIMEOUT); }); +describe('Sell with trailing Stop loss', function () { + it('Attempt to create a stop loss order', function (done) { + let type = 'STOP_LOSS_LIMIT'; + let quantity = 1; + let price = 0.069; + let delta = 100; + assert(typeof (binance.sell('ETHBTC', quantity, price, { trailingDelta: delta, type: type })) === 'undefined', WARN_SHOULD_BE_UNDEFINED); + done(); + }).timeout(TIMEOUT); +}); + describe('Iceberg sell order', function () { it('Attempt to create a sell order', function (done) { let quantity = 1;