If you’ve ever dabbled in a bit of crypto trading, you may have wondered if there was a way to get the stats that you find most useful in one handy web page. Well, as it turns out, there is. With a few simple lines of code, you can create your own Crypto Stats webpage.
You will need:
- Access to a web host that has PHP installed (Note: if your web host runs WordPress or cPanel, it should have PHP installed by default. If not, you should be able to install it via your web-hosting control panel, or via the command line)
- An idea of which cryptocurrency (or cryptocurrencies) you’d like to get stats for
Step 1: Determine your asset pair(s)
- Decide which cryptocurrency to get stats for. For this example, I’m going to use ADA (Cardano), because I love that it’s named after Ada Lovelace.
- Decide which fiat currency to display the prices in. A fiat currency is a ‘normal’ currency, such as USD ($) or GBP (£). For this example, I’m going to use GBP (£).
- The cryptocurrency name combined with the fiat currency is the asset pair. To get the stats for the asset pair, we are going to use the Kraken Public API.
- In your browser, go to https://api.kraken.com/0/public/AssetPairs. You will be presented with a large JSON array of information about the available asset pairs on Kraken. If you search the page for your crypto code combined with your chosen currency code, you should be able to find it. For this example, I find that
ADAGBP
is a valid asset pair in the Kraken API, so I will use that. You could also chooseETHUSD
(Etherium in USD($)) or any other pair.
Step 2: Get the ticker information for the asset pair
Using your chosen asset pair, go to https://api.kraken.com/0/public/Ticker?pair=ADAGBP in your browser. You’ll see a JSON array of ticker data. This table explains what each data array is:
Array Key | Array Name | Item One | Item Two | Item Three |
a | Ask array | price | whole lot volume | lot volume |
b | Bid array | price | whole lot volume | lot volume |
c | Last trade closed array | price | lot volume | N/A |
v | Volume array | today | last 24 hours | N/A |
p | Volume weighted average price array | today | last 24 hours | N/A |
t | Number of trades array | today | last 24 hours | N/A |
l | Low array | today | last 24 hours | N/A |
h | High array | today | last 24 hours | N/A |
o | Today’s opening price | price | N/A | N/A |
We are going to use this endpoint in our PHP script to get the current price for ADA in GBP. Create a new PHP script called tracker.php
, and enter the following code:
<?php function get_current_price($pair) { $url = 'https://api.kraken.com/0/public/Ticker?pair=' . $pair; $data = file_get_contents($url); $json_array = json_decode($data, true); $current_price = $json_array['result'][$pair]['c'][0]; return $current_price; }
This function will call the API endpoint for the ticker information, and extract the price from the ‘c’ array, which, as we can see from the table, is the ‘Last trade closed’ price. This is effectively the current price.
Step 3: Use historic data for the asset pair to generate stats
Next we need to get some historic data, in order to produce some stats. We will use the Kraken OHLC endpoint for this. If you go to https://api.kraken.com/0/public/OHLC?pair=ADAGBP&interval=1440 in your browser, you will see a JSON array of historic data for ADA in GPB(£). Some example data is below:
{"error": [], "result": {"ADAGPB": [ [1624147200, "1.00520", "1.05875", "0.95188", "1.03663", "0.99311", "865404.81347312", 1285], [1624233600, "1.04041", "1.04212", "0.84270", "0.84904", "0.90723", "1829249.14853250", 1702], ], "last": 1624579200}}
This table shows what each piece of data in an OHLC data array is:
timestamp | open | high | low | close | vwrap | volume | count |
[1624147200, | “1.00520”, | “1.05875”, | “0.95188”, | “1.03663”, | “0.99311”, | “865404.81347312”, | 1285] |
We’re going to use this data to generate some stats, more specifically some rolling mean values from the last 7 days. In the tracker.php
script, add the following function:
function get_rolling_mean_data($pair) { $interval = 1440; // daily $now = time(); $one_week_ago_ts = $now - ($interval * 60 * 7); $since = $one_week_ago_ts ; // 7 days ago timestamp $url = 'https://api.kraken.com/0/public/OHLC?pair=' . $pair . '&interval=' . $interval . '&since=' . $since; $data = file_get_contents($url); $json_array = json_decode($data, true); $total = 0; $total_high = 0; $total_low = 0; $historic_data = $json_array['result'][$pair]; $num_days = count($historic_data); foreach($historic_data as $x => $daily_data) { $closing_price = $daily_data[4]; $high = $daily_data[2]; $low = $daily_data[3]; $total += $closing_price; $total_high += $high; $total_low += $low; } $mean = round($total / $num_days, 6); $mean_high = round($total_high / $num_days, 6); $mean_low = round($total_low / $num_days, 6); return array('mean' => $mean, 'mean_high' => $mean_high, 'mean_low' => $mean_low);
This function gets the historic data for the last 7 days, at an interval of one day (so one data array per day), and calculates the mean price, the mean high price and the mean low price. Note that we used an additional URL param ‘since’, with a timestamp, to indicate we only want data since that timestamp (which is calculated as the timestamp from 7 days ago).
Now our code just needs to call these functions to get the data. Add these lines to the tracker.php script:
const ADA_PAIR = 'ADAGBP'; $ada_mean_array = get_rolling_mean_data(ADA_PAIR); $ada_mean = $ada_mean_array['mean']; $ada_mean_high = $ada_mean_array['mean_high']; $ada_mean_low = $ada_mean_array['mean_low']; $current_ada_price = get_current_price(ADA_PAIR); ?>
Step 4: Make the Script a Webpage
Right now our script is just a script. But the magic of PHP is that it can be turned into a webpage, just by adding a few lines of html. After the closing ?>
of your PHP script, add the following HTML/PHP:
<html> <head> <title>Crypto Stats</title> <meta name="robots" content="noindex"> <style type="text/css"> p { font-size: 1.25em; font-family: Verdana, sans-serif; } </style> </head> <body> <p>7-Day ADA Stats:</p> <p>Current ADA Price: <?=$current_ada_price;?> GBP</p> <p>Mean ADA Price: <?=$ada_mean;?> GBP</p> <p>Mean High ADA Price: <?=$ada_mean_high;?> GBP</p> <p>Mean Low ADA Price: <?=$ada_mean_low;?> GBP</p> </body> </html>
This has now turned our tracker.php
script into a simple webpage, which displays our crypto stats. Note that the <meta>
tag is optional here. Keep it in if you want to keep your stats private and not visible in search engines.
Step 5: Upload the Webpage to your hosting
The script in full should now look like:
<?php function get_current_price($pair) { $url = 'https://api.kraken.com/0/public/Ticker?pair=' . $pair; $data = file_get_contents($url); $json_array = json_decode($data, true); $current_price = $json_array['result'][$pair]['c'][0]; return $current_price; } function get_rolling_mean_data($pair) { $interval = 1440; // daily $now = time(); $one_week_ago_ts = $now - ($interval * 60 * 7); $since = $one_week_ago_ts ; // 7 days ago timestamp $url = 'https://api.kraken.com/0/public/OHLC?pair=' . $pair . '&interval=' . $interval . '&since=' . $since; $data = file_get_contents($url); $json_array = json_decode($data, true); $total = 0; $total_high = 0; $total_low = 0; $historic_data = $json_array['result'][$pair]; $num_days = count($historic_data); foreach($historic_data as $x =--> $daily_data) { $closing_price = $daily_data[4]; $high = $daily_data[2]; $low = $daily_data[3]; $total += $closing_price; $total_high += $high; $total_low += $low; } $mean = round($total / $num_days, 6); $mean_high = round($total_high / $num_days, 6); $mean_low = round($total_low / $num_days, 6); return array('mean' => $mean, 'mean_high' => $mean_high, 'mean_low' => $mean_low); } const ADA_PAIR = 'ADAGBP'; $ada_mean_array = get_rolling_mean_data(ADA_PAIR); $ada_mean = $ada_mean_array['mean']; $ada_mean_high = $ada_mean_array['mean_high']; $ada_mean_low = $ada_mean_array['mean_low']; $current_ada_price = get_current_price(ADA_PAIR); ?> <html> <head> <title>Crypto Stats</title> <meta name="robots" content="noindex"> <style type="text/css"> p { font-size: 1.25em; font-family: Verdana, sans-serif; } </style> </head> <body> <p>7-Day ADA Stats:</p> <p>Current ADA Price: <?=$current_ada_price;?> GBP</p> <p>Mean ADA Price: <?=$ada_mean;?> GBP</p> <p>Mean High ADA Price: <?=$ada_mean_high;?> GBP</p> <p>Mean Low ADA Price: <?=$ada_mean_low;?> GBP</p> </body> </html>
You will need to upload the tracker.php
file to your public_html
directory on your web host. Then you should be able to navigate to it in your browser by going to http://yourwebsite.com/tracker.php. If successful, the page should display something like this:

Conclusion
Now you should have your very own crypto stats page. There are, of course, other stats you could pull out of the same data, and indeed you could track more than one cryptocurrency if you wished. For more information on the Kraken Public API, and for other crypto data endpoints, see https://docs.kraken.com/rest/#tag/Market-Data. If you want to add an asset pair dropdown to your stats page, see my follow-on post Creating a Crypto Stats Page: Part 2: Asset Pair Dropdown. Otherwise, see you next time!
1 Response
[…] my post Creating a Crypto Stats Page, I created a simple stats page in PHP for one cryptocurrency against one fiat currency. Since then, […]