Using Binance API to get the user’s trading history — for a margin account

Marcio S Galli
4 min readApr 9, 2021

--

This article is based in a prior article entitled Using Binance API to get the user’s trades data. Here, however, our goal will be to fetch the data for the user’s margin account. It turns out that Binance uses different API endpoints depending on the kind of account (spot account, margin account for example).

Introduction to Binance API and the “myTrades” rest endpoint

Check Using Binance API to get the user’s trades data.

Accessing the user’s margin account “myTrades”

The following explanation is based in the following sample:

Let’s go through the code which is very simple and straightforward:

/* Notice that node-binance-api was replaced 
with a local version *
*/
//const Binance = require('node-binance-api');
const Binance = require('./node-binance-api');
// Your local key info
const Keys = require('./config-REMOVETHIS.js');
const binance = new Binance().options(Keys);// Trying to set date randelet initialDate = new Date("Mon Apr 9 2021 13:00:00 GMT-0300");
let lastDate = new Date("Mon Apr 9 2021 14:15:00 GMT-0300");
// This example shows how to pass optionslet options = {
limit : 10,
isIsolated : true,
startTime : initialDate.getTime(),
endTime : lastDate.getTime(),
};
binance.trades("ETHUSDT", (error, trades, symbol) => {
for(let k in trades) {
let trade = trades[k];
let tradeDateTime = new Date(trade.time);
console.log(`Trade event ${k} ------------------------------`);
console.log(`time : ${tradeDateTime}`);
console.log(`symbol : ${trade.symbol}`);
console.log(`price : ${trade.price}`);
console.log(`quantity : ${trade.qty}`);
console.log(`commission fee : ${trade.commission}`);
console.log(`commission asset : ${trade.commissionAsset}`);
console.log(`Is buyer : ${trade.isBuyer}`);
console.log(`Is Maker : ${trade.isMaker}`);
// console.log(trade);
}
}, options);

For additional documentation related to the myTrades call (the underlying API from Binance) for the margin account please check the following resource:

Studying what the above code does

  • It uses a local version of the “node-binance-api” module. I will go through the modification that I did later on.
  • It passes Keys object with the user’s APIKEY and the user’s APISECRET. You will want to set these elements using your Binance account. See this support post on How to Create an API key for more.
  • It passes the symbol, in this case using “ETHUSDT”. Therefore, if you expect to get data from the above example, you will need to have a few trades done in your margin account with ETHUSDT pair.
  • It passes limit: 10— meaning that it will only return a maximum of 10 elements. Yes, it may be a bit confusing the fact that I have used limit:10 and at the same a date range. Apologies.

Example of the sample’s response

The modification to the node-binance-api for working with margin account

It turns out that Node-Binance-API binance.trades method was implemented to be compatible with the Binance spot account endpoint which uses (at the time of this writing) “/base/v3/myTrades” as the endpoint. The margin account uses a different endpoint which is “/sapi/v1/margin/myTrades”.

The file patched is the “node-binance-api.js” from the node-binance-api module project.

New local node-binance-api.js (/sapi/v1/margin/myTrades)

/**
* Get trades for a given symbol
* @param {string} symbol - the symbol
* @param {function} callback - the callback function
* @param {object} options - additional options
* @return {promise or undefined} - omitting the callback returns a promise
*/
trades: ( symbol, callback, options = {} ) => {
let parameters = Object.assign( { symbol: symbol }, options );
if ( !callback ) {
return new Promise( ( resolve, reject ) => {
callback = ( error, response ) => {
if ( error ) {
reject( error );
} else {
resolve( response );
}
}
signedRequest( sapi + 'v1/margin/myTrades', parameters, function ( error, data ) {
return callback.call( this, error, data, symbol );
} );
} )
} else {
signedRequest( sapi + 'v1/margin/myTrades', parameters, function ( error, data ) {
return callback.call( this, error, data, symbol );
} );
}
},

For reference, the prior version (/base/v3/myTrades)

/**
* Get trades for a given symbol
* @param {string} symbol - the symbol
* @param {function} callback - the callback function
* @param {object} options - additional options
* @return {promise or undefined} - omitting the callback returns a promise
*/
trades: ( symbol, callback, options = {} ) => {
let parameters = Object.assign( { symbol: symbol }, options );
if ( !callback ) {
return new Promise( ( resolve, reject ) => {
callback = ( error, response ) => {
if ( error ) {
reject( error );
} else {
resolve( response );
}
}
signedRequest( base + 'v3/myTrades', parameters, function ( error, data ) {
return callback.call( this, error, data, symbol );
} );
} )
} else {
signedRequest( base + 'v3/myTrades', parameters, function ( error, data ) {
return callback.call( this, error, data, symbol );
} );
}
},

References

Disclaimer — this is a public documentation taken from my own meeting id 0421981a-d179–4a27-a0d1-fe1280b5e1e5. I use this method to improve my own documentation as I am learning to use APIs related to trading systems. Please understand that this article is an educational article — it’s provided under the so called “as-is” form, no guarantees of any kind, whatsoever. And of course this content won’t contain any financial advice.

--

--

Marcio S Galli
Marcio S Galli

No responses yet