by Tosch

In the last topic I showed you how to retrieve basic data about Assets from the Nxt Asset Exchange.

In this article I want to cover how to receive historical trades.

The API call we are using here is “getTrades”.

Historical Trades

Historical trades can either be from any Account or from any Assets, we are continuing as we did before and get latest trades from the Asset SuperNET.

Again we will be using the parameters firstIndex and lastIndex to retreive only the last 10 entries. If you want to get more trades, just change lastIndex to a higher number or erase both firstIndex and lastIndex completely.

If you erase firstIndex and lastIndex it is possible that you get a limited number of entries — this can be setup in your Nxt configuration file. Have a look at the property: nxt.maxAPIRecords=100 and configure it to your needs (just when you are working without an admin password).

Now, let’s jump back to the code.

<!DOCTYPE html>

<html>

<head>

<!-- Latest compiled and minified CSS -->

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> </head>

<body>

<div class="col-md-12">

<h2>Trade History</h2>

<table class="table table-striped">

<thead>

<tr>

<th>Date</th>

<th>Type</th>

<th>Quantity</th>

<th>Price</th>

<th>Total</th>

<th>Buyer</th>

<th>Seller</th>

</tr>

</thead>

<tbody id="tradeHistory">



</tbody>

</table>

</div> <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>

<!-- Latest compiled and minified JavaScript -->

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

<script>



$.getJSON('http://localhost:7876/nxt', {"requestType": "getTrades", "asset": "12071612744977229797", "firstIndex": 0, "lastIndex": 9, "includeAssetInfo": "true"}, function(request) {



var trades = request.trades;

var rows;

var nxtTime = 1385294400; $.each(trades, function(key, data) { var decimals = data.decimals;

var NQT_QNT = 8 - decimals;

var quantityQNT = data.quantityQNT / Math.pow(10, decimals);

var priceNQT = data.priceNQT / Math.pow(10, NQT_QNT);

var total = data.quantityQNT * data.priceNQT / Math.pow(10,8);

var NxtTimestamp = (+data.timestamp + +nxtTime) * 1000;

var d = new Date(NxtTimestamp); rows += '<tr>';

rows += '<td>'+d.toDateString()+'</td>';

rows += '<td>'+data.tradeType+'</td>';

rows += '<td>'+quantityQNT+'</td>';

rows += '<td>'+priceNQT+'</td>';

rows += '<td>'+total+'</td>';

rows += '<td>'+data.buyerRS+'</td>';

rows += '<td>'+data.sellerRS+'</td>';

rows += '</tr>'; }); $("#tradeHistory").html(rows);



});



</script>

</body> </html>

For the API request, as stated above, we limited the response by setting firstIndex and lastIndex to 10 entries. Furthermore, the includeAssetInfo set as true will show the decimal for the Asset and the Name of the Asset. This will make the call a bit slower but especially when we are looking at more than one Asset a very helpful addition.

Instead of including includeAssetInfo we could make a getAsset call to receive those parameters (when just looking at one Asset this might be recommended). But when you are looking at an Account last trades instead of an Asset, this can save a lot of work and time.

The most important calculations to mention for this script are the Nxt timestamp, quantity and price. They are calculated as already explained above.

Nxt Timestamp Nxt has its own timestamp. Nxt was started November on Nov 24, 2013 12:00 am. In Nxt this would be timestamp 0. In Javascript this is 1385294400 (as Javascript started January 1, 1970, 00:00:00), but JavaScript uses Miliseconds, therefore we have to multiply 1000 to get there. As a result we need to add the given timestamp of Nxt to 1385294400 and add the miliseconds to retreive the timestamp in JavaScript.

Analysing the historical data will get you a lot of information about the frequency of trading and how many accounts have been trading an Asset. The more accounts are involved in trading, the better an Asset is spread among traders, which is usually a good sign. Of course, you have to be aware that you do not know the origin of Accounts. It is possible that one person creates multiple accounts and trades from all of these accounts. The Asset appears to be traded by more parties, which seems beneficial for the Asset owner, on the other hand people might create several accounts to track different types of holdings. In a lot of cases it makes sense for a user to have more than just one Account.

The above script will show you a table like this one:

Account Information

As you already know, the Blockchain is fully transparent; you can see any transaction in the network and all the holdings of one account. It still enables you to write encrypted messages, which cannot be read by anybody but the person who holds the passphrase of either the sender or the recipient.

The next step for you is to find out how to create a nice interface to view Accounts. All the necessary steps to get there are explained here.

The API call needed to get Account information is getAccount. You could also use getAccountAssets to monitor Asset holdings but getAccount will show you all the holdings of one account.

A hint: as in the getTrades API, you can decide to include furter informations with includeLessors, includeAssets, includeCurrencies or includeEffectiveBalance

Enjoy developing with Nxt! In the next article we will explore creating transactions and more details about the Blockchain. If you have any questions, feel free to contact me via email or my Nxt Forum account.

More in this series:

Coding for NXT Crypto Platform #1: Configuration

Coding for NXT Crypto Platform #2: First Steps

Coding for NXT Crypto Platform #3: Output from the Asset Exchange

For more information, please visit www.nxt.org or www.nxtwiki.org