Live order book / depth chart

A demo of amCharts 4, the previous version. See it in amCharts 5.

This demo shows how to build a simple order book (market depth) chart with an amCharts 4 XYChart. It shows the real distribution of buy and sell orders, and their volume, for the ETH/BTC pair (Ether priced in bitcoin).

Live data loading and processing

This chart uses amCharts 4 built-in external data loading to fetch the top 50 bids and asks from the public Binance market data API. The endpoint allows cross-origin requests and needs no API key, so the data loads directly in the browser.

Since the API returns data in a format that is not directly compatible with the chart, we pre-process it using the parsedData adapter.

The chart is set to reload the data every 30 seconds.

More about loading external data

Adapters

Adapters in amCharts 4 is a very powerful concept. It allows defining custom functions to dynamically modify just about any value or property on the chart, including dynamically loaded data.

More about adapters

This is the amCharts 4 version. See this demo in amCharts 5

Open in:

Code

JavaScript

// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end

// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);

// Add data
chart.dataSource.url = "https://data-api.binance.vision/api/v3/depth?symbol=ETHBTC&limit=50";
chart.dataSource.reloadFrequency = 30000;
chart.dataSource.adapter.add("parsedData", function(data) {
  
  // Function to process (sort and calculate cummulative volume)
  function processData(list, type, desc) {

    // Convert to data points
    for(var i = 0; i < list.length; i++) {
      list[i] = {
        value: Number(list[i][0]),
        volume: Number(list[i][1]),
      }
    }

    // Sort list just in case
    list.sort(function(a, b) {
      if (a.value > b.value) {
        return 1;
      }
      else if (a.value < b.value) {
        return -1;
      }
      else {
        return 0;
      }
    });

    // Calculate cummulative volume
    if (desc) {
      for(var i = list.length - 1; i >= 0; i--) {
        if (i < (list.length - 1)) {
          list[i].totalvolume = list[i+1].totalvolume + list[i].volume;
        }
        else {
          list[i].totalvolume = list[i].volume;
        }
        var dp = {};
        dp["value"] = list[i].value;
        dp[type + "volume"] = list[i].volume;
        dp[type + "totalvolume"] = list[i].totalvolume;
        res.unshift(dp);
      }
    }
    else {
      for(var i = 0; i < list.length; i++) {
        if (i > 0) {
          list[i].totalvolume = list[i-1].totalvolume + list[i].volume;
        }
        else {
          list[i].totalvolume = list[i].volume;
        }
        var dp = {};
        dp["value"] = list[i].value;
        dp[type + "volume"] = list[i].volume;
        dp[type + "totalvolume"] = list[i].totalvolume;
        res.push(dp);
      }
    }

  }

  // Init
  var res = [];
  processData(data.bids, "bids", true);
  processData(data.asks, "asks", false);

  return res;
});

// Set up precision for numbers
chart.numberFormatter.numberFormat = "#,###.####";

// Create axes
var xAxis = chart.xAxes.push(new am4charts.CategoryAxis());
xAxis.dataFields.category = "value";
//xAxis.renderer.grid.template.location = 0;
xAxis.renderer.minGridDistance = 50;
xAxis.title.text = "Price (BTC/ETH)";

var yAxis = chart.yAxes.push(new am4charts.ValueAxis());
yAxis.title.text = "Volume";

// Create series
var series = chart.series.push(new am4charts.StepLineSeries());
series.dataFields.categoryX = "value";
series.dataFields.valueY = "bidstotalvolume";
series.strokeWidth = 2;
series.stroke = am4core.color("#0f0");
series.fill = series.stroke;
series.fillOpacity = 0.1;
series.tooltipText = "Ask: [bold]{categoryX}[/]\nTotal volume: [bold]{valueY}[/]\nVolume: [bold]{bidsvolume}[/]"

var series2 = chart.series.push(new am4charts.StepLineSeries());
series2.dataFields.categoryX = "value";
series2.dataFields.valueY = "askstotalvolume";
series2.strokeWidth = 2;
series2.stroke = am4core.color("#f00");
series2.fill = series2.stroke;
series2.fillOpacity = 0.1;
series2.tooltipText = "Ask: [bold]{categoryX}[/]\nTotal volume: [bold]{valueY}[/]\nVolume: [bold]{asksvolume}[/]"

var series3 = chart.series.push(new am4charts.ColumnSeries());
series3.dataFields.categoryX = "value";
series3.dataFields.valueY = "bidsvolume";
series3.strokeWidth = 0;
series3.fill = am4core.color("#000");
series3.fillOpacity = 0.2;

var series4 = chart.series.push(new am4charts.ColumnSeries());
series4.dataFields.categoryX = "value";
series4.dataFields.valueY = "asksvolume";
series4.strokeWidth = 0;
series4.fill = am4core.color("#000");
series4.fillOpacity = 0.2;

// Add cursor
chart.cursor = new am4charts.XYCursor();

HTML

<div id="chartdiv"></div>

CSS

#chartdiv {
  width: 100%;
  height: 500px;
}

Resources

https://cdn.amcharts.com/lib/4/core.js
https://cdn.amcharts.com/lib/4/charts.js
https://cdn.amcharts.com/lib/4/themes/animated.js

All in one page

<!-- Resources -->
<script src="https://cdn.amcharts.com/lib/4/core.js"></script>
<script src="https://cdn.amcharts.com/lib/4/charts.js"></script>
<script src="https://cdn.amcharts.com/lib/4/themes/animated.js"></script>

<!-- Chart code -->
<script>
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end

// Create chart instance
var chart = am4core.create("chartdiv", am4charts.XYChart);

// Add data
chart.dataSource.url = "https://data-api.binance.vision/api/v3/depth?symbol=ETHBTC&limit=50";
chart.dataSource.reloadFrequency = 30000;
chart.dataSource.adapter.add("parsedData", function(data) {
  
  // Function to process (sort and calculate cummulative volume)
  function processData(list, type, desc) {

    // Convert to data points
    for(var i = 0; i < list.length; i++) {
      list[i] = {
        value: Number(list[i][0]),
        volume: Number(list[i][1]),
      }
    }

    // Sort list just in case
    list.sort(function(a, b) {
      if (a.value > b.value) {
        return 1;
      }
      else if (a.value < b.value) {
        return -1;
      }
      else {
        return 0;
      }
    });

    // Calculate cummulative volume
    if (desc) {
      for(var i = list.length - 1; i >= 0; i--) {
        if (i < (list.length - 1)) {
          list[i].totalvolume = list[i+1].totalvolume + list[i].volume;
        }
        else {
          list[i].totalvolume = list[i].volume;
        }
        var dp = {};
        dp["value"] = list[i].value;
        dp[type + "volume"] = list[i].volume;
        dp[type + "totalvolume"] = list[i].totalvolume;
        res.unshift(dp);
      }
    }
    else {
      for(var i = 0; i < list.length; i++) {
        if (i > 0) {
          list[i].totalvolume = list[i-1].totalvolume + list[i].volume;
        }
        else {
          list[i].totalvolume = list[i].volume;
        }
        var dp = {};
        dp["value"] = list[i].value;
        dp[type + "volume"] = list[i].volume;
        dp[type + "totalvolume"] = list[i].totalvolume;
        res.push(dp);
      }
    }

  }

  // Init
  var res = [];
  processData(data.bids, "bids", true);
  processData(data.asks, "asks", false);

  return res;
});

// Set up precision for numbers
chart.numberFormatter.numberFormat = "#,###.####";

// Create axes
var xAxis = chart.xAxes.push(new am4charts.CategoryAxis());
xAxis.dataFields.category = "value";
//xAxis.renderer.grid.template.location = 0;
xAxis.renderer.minGridDistance = 50;
xAxis.title.text = "Price (BTC/ETH)";

var yAxis = chart.yAxes.push(new am4charts.ValueAxis());
yAxis.title.text = "Volume";

// Create series
var series = chart.series.push(new am4charts.StepLineSeries());
series.dataFields.categoryX = "value";
series.dataFields.valueY = "bidstotalvolume";
series.strokeWidth = 2;
series.stroke = am4core.color("#0f0");
series.fill = series.stroke;
series.fillOpacity = 0.1;
series.tooltipText = "Ask: [bold]{categoryX}[/]\nTotal volume: [bold]{valueY}[/]\nVolume: [bold]{bidsvolume}[/]"

var series2 = chart.series.push(new am4charts.StepLineSeries());
series2.dataFields.categoryX = "value";
series2.dataFields.valueY = "askstotalvolume";
series2.strokeWidth = 2;
series2.stroke = am4core.color("#f00");
series2.fill = series2.stroke;
series2.fillOpacity = 0.1;
series2.tooltipText = "Ask: [bold]{categoryX}[/]\nTotal volume: [bold]{valueY}[/]\nVolume: [bold]{asksvolume}[/]"

var series3 = chart.series.push(new am4charts.ColumnSeries());
series3.dataFields.categoryX = "value";
series3.dataFields.valueY = "bidsvolume";
series3.strokeWidth = 0;
series3.fill = am4core.color("#000");
series3.fillOpacity = 0.2;

var series4 = chart.series.push(new am4charts.ColumnSeries());
series4.dataFields.categoryX = "value";
series4.dataFields.valueY = "asksvolume";
series4.strokeWidth = 0;
series4.fill = am4core.color("#000");
series4.fillOpacity = 0.2;

// Add cursor
chart.cursor = new am4charts.XYCursor();
</script>

<!-- HTML -->
<div id="chartdiv"></div>