Using Binance API to get the user’s trading history

  • Summary: Developers are interested in interacting with exchanges to get data about their past trades. Such information may be important for accounting and taxes planning.

  • Audience: Systems architects, JavaScript developers, Front-end developers, Back-end developers, Technical traders.

  • This meeting:
  • Participants: Marcio S Galli
  • Text Language: en-US
  • Tags: Binance, API, trading, trade, JavaScript.

Introduction

In this project, we are interested in the user’s private history of trades. Our goal will be to fetch the user’s record of trades to help with accounting. Keep in mind that this is not a complete example of what one would want to do with the data — it’s the starting point.

Introduction to Binance API and the “myTrades” rest endpoint

Our goal is to get the user’s private data, the history of trades. Since this is private information, it will depend on an initial authentication step. Remember that these steps depend on interaction with Binance API (check Binance API docs).

Momentarily, take aside the necessary authentication procedures. Assuming that the authentication went through, the most direct form of accessing this endpoint depends on an HTTP call using GET method:

GET /api/v3/myTrades

Developers usually create abstractions on top of these basic methods. One example is the following method, binance.trades, available from Node-Binance-API NPM module:

binance.trades("SNMBTC", (error, trades, symbol) => {
  console.info(symbol+" trade history", trades);
});

You may check the source code for this ".trades" method — see that it uses “api/v3/myTrades” endpoint:

.trades method from Node-binance-api uses "myTrades"

Obtaining “myTrades” using Node-Binance-API

You might want to fetch the following source code: a very simple NodeJS fetch example that pulls two trade events using Node-Binance-API. The following source code was tested with a user account known as “spot account” at Binance.

Check binance-mytrades-accounting sample project

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

const Binance = require('node-binance-api');
const Keys    = require('./config.js');
const binance = new Binance().options(Keys);
// This example shows how to pass options
let options = { 
  limit: 2 // Limit results to two entries
};
binance.trades("BTCBRL", (error, trades, symbol) => {
  console.info(symbol+" trade history", trades);
}, options);

Other options are also available according to Binance API myTrades documentation. Let’s see what this example does:

  • It uses “node-binance-api” which creates an abstraction in order to help with authentication and REST protocol details.

  • 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 “BTCBRL”. Therefore, if you expect to get data from the above example, you will need to have done 2 trades using the pair BTCBRL, at least, using your Binance Spot account.

  • It passes limit: 2 — meaning that it will only return 2 elements, if there are 2 trades.

Example of the API’s response

I suggest you to check additional information about ths response data at Binance API documentation for myTrades endpoint.

Fragment from the output from the example

The following is entire response for the provided example(Check binance-mytrades-accounting sample project):

Example response

Improving the readability of the API response

According to Binance API documentation, “All time and timestamp related fields are in milliseconds.” Therefore, the main challenge here is to translate the time attribute to something more readable. In this case, it’s pretty much straightforward use of Date function in JavaScript. The other attributes are not modified:

binance.trades("BTCBRL", (error, trades, symbol) => {  
  for(let k in trades) { 
    let trade = trades[k];
    let tradeDateTime = new Date(trade.time);
    console.log(`Trade event --------------------------------`);
    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(trade);
  }
}, options);

An example of the output with better date-time formatting:

Date and time information is more readable

References

Author
Marcio S Galli
Other writings

Made with ❤ by mGalli & MePlex