Skip to content

Commit

Permalink
Add isolated option for margin trading by hson011011
Browse files Browse the repository at this point in the history
  • Loading branch information
jaggedsoft authored Mar 7, 2021
2 parents b161cc6 + 4df545e commit 59de13c
Showing 1 changed file with 41 additions and 19 deletions.
60 changes: 41 additions & 19 deletions node-binance-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ let api = function Binance( options = {} ) {
quantity: quantity
};
if ( typeof flags.type !== 'undefined' ) opt.type = flags.type;
if (typeof flags.isIsolated !== 'undefined') opt.isIsolated = flags.isIsolated;
if ( opt.type.includes( 'LIMIT' ) ) {
opt.price = price;
if ( opt.type !== 'LIMIT_MAKER' ) {
Expand Down Expand Up @@ -2919,7 +2920,7 @@ let api = function Binance( options = {} ) {
* @param {function} callback - the callback function
* @return {promise or undefined} - omitting the callback returns a promise
*/
marketSell: function ( symbol, quantity, flags = { type: 'MARKET' }, callback = false ) {
marketSell: function ( symbol, quantity, flags = { type: 'MARKET' }, callback = false ) {
if ( typeof flags === 'function' ) { // Accept callback as third parameter
callback = flags;
flags = { type: 'MARKET' };
Expand Down Expand Up @@ -4389,10 +4390,11 @@ let api = function Binance( options = {} ) {
* @param {numeric} price - the price to pay for each unit
* @param {object} flags - additional buy order flags
* @param {function} callback - the callback function
* @param {string} isIsolated - the isolate margin option
* @return {undefined}
*/
mgOrder: function ( side, symbol, quantity, price, flags = {}, callback = false ) {
marginOrder( side, symbol, quantity, price, flags, callback );
mgOrder: function ( side, symbol, quantity, price, flags = {}, callback = false,isIsolated='FALSE' ) {
marginOrder( side, symbol, quantity, price, {...flags,isIsolated}, callback );
},

/**
Expand All @@ -4402,10 +4404,11 @@ let api = function Binance( options = {} ) {
* @param {numeric} price - the price to pay for each unit
* @param {object} flags - additional buy order flags
* @param {function} callback - the callback function
* @param {string} isIsolated - the isolate margin option
* @return {undefined}
*/
mgBuy: function ( symbol, quantity, price, flags = {}, callback = false ) {
marginOrder( 'BUY', symbol, quantity, price, flags, callback );
mgBuy: function ( symbol, quantity, price, flags = {}, callback = false,isIsolated='FALSE' ) {
marginOrder( 'BUY', symbol, quantity, price, {...flags,isIsolated}, callback );
},

/**
Expand All @@ -4415,10 +4418,11 @@ let api = function Binance( options = {} ) {
* @param {numeric} price - the price to sell each unit for
* @param {object} flags - additional order flags
* @param {function} callback - the callback function
* @param {string} isIsolated - the isolate margin option
* @return {undefined}
*/
mgSell: function ( symbol, quantity, price, flags = {}, callback = false ) {
marginOrder( 'SELL', symbol, quantity, price, flags, callback );
mgSell: function ( symbol, quantity, price, flags = {}, callback = false,isIsolated='FALSE' ) {
marginOrder( 'SELL', symbol, quantity, price, {...flags,isIsolated}, callback );
},

/**
Expand All @@ -4427,15 +4431,16 @@ let api = function Binance( options = {} ) {
* @param {numeric} quantity - the quantity required
* @param {object} flags - additional buy order flags
* @param {function} callback - the callback function
* @param {string} isIsolated - the isolate margin option
* @return {undefined}
*/
mgMarketBuy: function ( symbol, quantity, flags = { type: 'MARKET' }, callback = false ) {
mgMarketBuy: function ( symbol, quantity, flags = { type: 'MARKET' }, callback = false,isIsolated='FALSE' ) {
if ( typeof flags === 'function' ) { // Accept callback as third parameter
callback = flags;
flags = { type: 'MARKET' };
}
if ( typeof flags.type === 'undefined' ) flags.type = 'MARKET';
marginOrder( 'BUY', symbol, quantity, 0, flags, callback );
marginOrder( 'BUY', symbol, quantity, 0, {...flags,isIsolated}, callback );
},

/**
Expand All @@ -4444,15 +4449,16 @@ let api = function Binance( options = {} ) {
* @param {numeric} quantity - the quantity required
* @param {object} flags - additional sell order flags
* @param {function} callback - the callback function
* @param {string} isIsolated - the isolate margin option
* @return {undefined}
*/
mgMarketSell: function ( symbol, quantity, flags = { type: 'MARKET' }, callback = false ) {
mgMarketSell: function ( symbol, quantity, flags = { type: 'MARKET' }, callback = false, isIsolated='FALSE' ) {
if ( typeof flags === 'function' ) { // Accept callback as third parameter
callback = flags;
flags = { type: 'MARKET' };
}
if ( typeof flags.type === 'undefined' ) flags.type = 'MARKET';
marginOrder( 'SELL', symbol, quantity, 0, flags, callback );
marginOrder( 'SELL', symbol, quantity, 0, {...flags,isIsolated}, callback );
},

/**
Expand All @@ -4462,8 +4468,8 @@ let api = function Binance( options = {} ) {
* @param {function} callback - the callback function
* @return {undefined}
*/
mgCancel: function ( symbol, orderid, callback = false ) {
signedRequest( sapi + 'v1/margin/order', { symbol: symbol, orderId: orderid }, function ( error, data ) {
mgCancel: function ( symbol, orderid, callback = false,isIsolated='FALSE') {
signedRequest( sapi + 'v1/margin/order', { symbol: symbol, orderId: orderid,isIsolated }, function ( error, data ) {
if ( callback ) return callback.call( this, error, data, symbol );
}, 'DELETE' );
},
Expand Down Expand Up @@ -4634,11 +4640,18 @@ let api = function Binance( options = {} ) {
* @param {string} asset - the asset
* @param {number} amount - the asset
* @param {function} callback - the callback function
* @param {string} isIsolated - the isolated option
* @param {string} symbol - symbol for isolated margin
* @return {undefined}
*/
mgBorrow: function ( asset, amount, callback ) {
mgBorrow: function ( asset, amount, callback, isIsolated='FALSE',symbol=null ) {
let parameters = Object.assign( { asset: asset, amount: amount } );
signedRequest( sapi + 'v1/margin/loan', parameters, function ( error, data ) {
if (isIsolated ==='TRUE' && !symbol) throw new Error('If "isIsolated" = "TRUE", "symbol" must be sent')
const isolatedObj = isIsolated === 'TRUE'?{
isIsolated,
symbol
}:{}
signedRequest( sapi + 'v1/margin/loan', {...parameters,...isolatedObj}, function ( error, data ) {
if ( callback ) return callback( error, data );
}, 'POST' );
},
Expand All @@ -4648,21 +4661,30 @@ let api = function Binance( options = {} ) {
* @param {string} asset - the asset
* @param {number} amount - the asset
* @param {function} callback - the callback function
* @param {string} isIsolated - the isolated option
* @param {string} symbol - symbol for isolated margin
* @return {undefined}
*/
mgRepay: function ( asset, amount, callback ) {
mgRepay: function ( asset, amount, callback ,isIsolated='FALSE',symbol=null ) {
let parameters = Object.assign( { asset: asset, amount: amount } );
signedRequest( sapi + 'v1/margin/repay', parameters, function ( error, data ) {
if (isIsolated ==='TRUE' && !symbol) throw new Error('If "isIsolated" = "TRUE", "symbol" must be sent')
const isolatedObj = isIsolated === 'TRUE'?{
isIsolated,
symbol
}:{}
signedRequest( sapi + 'v1/margin/repay', {...parameters,...isolatedObj}, function ( error, data ) {
if ( callback ) return callback( error, data );
}, 'POST' );
},
/**
* Margin account details
* @param {function} callback - the callback function
* @param {boolean} isIsolated - the callback function
* @return {undefined}
*/
mgAccount: function( callback ) {
signedRequest( sapi + 'v1/margin/account', {}, function( error, data ) {
mgAccount: function( callback ,isIsolated = false) {
const endpoint = 'v1/margin' + (isIsolated)?'/isolated':'' + '/account'
signedRequest( sapi + endpoint, {}, function( error, data ) {
if( callback ) return callback( error, data );
} );
},
Expand Down

0 comments on commit 59de13c

Please sign in to comment.