# How to get the most and least Volatile Cryptocurrencies

**Python – Predictive Hacks**, and kindly contributed to python-bloggers]. (You can report issue about the content on this page here)

Want to share your content on python-bloggers? click here.

In this tutorial, we will show you how to get the volatility of each cryptocurrency for a specific time period. Although there are different approaches, for this example we will define cryptocurrency volatility as the standard deviation of the returns. I.e.

\(\sigma =\sqrt{\frac{\sum_{i=1}^{N}(r_i-\bar{r})}{N-1}}\)

Where:

- \(N\): Number of observations
- \(r_i\): Return at period \(i\)
- \(\bar{r}\): Mean return

For this tutorial, we will use the Kraken API. First, we will get the daily tickers’ prices in USD from the first of the year (i.e. ~ from **2022-01-01** to **2022-03-20**) and we will calculate the average daily return and the standard deviation of the daily returns which is the “volatility”.

## Get the Tickers

We will get the available cryptocurrencies in USD. From the API we will get all the asset pairs and then we will keep only those that are over USD.

import pandas as pd import requests import time resp = requests.get('https://api.kraken.com/0/public/AssetPairs') resp = resp.json() # Keep all the cryptos over USD dollar_pairs = [] for pair in resp['result']: if pair.endswith('USD'): dollar_pairs.append(pair)

We got 128 pairs in total.

## Get the Daily Close Prices

We will get the OHLC prices of all pairs considering an interval of **1440 minutes**, i.e. one day, and the prices since **2022-01-01**. Note that we need to convert the 2022-01-01 to epoch as follows.

date_time = '2022-01-01 00:00:00' pattern = '%Y-%m-%d %H:%M:%S' epoch = int(time.mktime(time.strptime(date_time, pattern))) print(epoch)

Output:

1640988000

The next step is to get the OHLC prices of all assets and store them in a pandas data frame. We can easily do it with the following “for loop”.

df = pd.DataFrame() for pair in dollar_pairs: tmp_url = f'https://api.kraken.com/0/public/OHLC?pair={pair}&since={epoch}&interval=1440'.format(pair,epoch) resp = requests.get(tmp_url) tmp_df = pd.DataFrame(resp.json()['result'][pair]) tmp_df['asset'] = pair df = pd.concat([df,tmp_df]) df.columns = ['unixtimestap', 'open', 'high', 'low', 'close', 'vwap', 'volume', 'count', 'asset'] df

## Get the Daily Returns

The next step is to get the daily returns of each cryptocurrency.

# keep only the close price df = df[['unixtimestap','asset','close']].copy() # drop the NAs df.dropna(inplace=True) # convert the close to float df['close'] = df['close'].astype('float') df['returns'] = df.groupby('asset')['close'].pct_change() df.dropna(inplace=True) df

## Get the Average and the StDev of the Returns

Finally, we can get the average and the Standard Deviation of each asset.

# get the mean returns summary_df = pd.DataFrame(df.groupby('asset')['returns'].mean()) # get the standard deviation summary_df['volatility'] = df.groupby('asset')['returns'].std() # get the number of observations summary_df['observations'] = df.groupby('asset')['returns'].size() # remove the cryptos that have less observations summary_df = summary_df.loc[summary_df.observations==78] summary_df

## Get the Most and Least Volatile Assets

Note that the Kraken APIs returns all the asset pairs over USD including the EURO, GBP, AUD and other stable cryptocurrencies such as the Tether (USDT), the USDC, and other special cryptos like the PAX and so on. Of course, we can ignore these assets but is also good to have them for comparison.

**The 20 least volatile assets**

summary_df.sort_values('volatility', ascending=True).head(20)

From the typical cryptocurrencies, the least volatile are the:

and so on

**The 20 most volatile assets**

Similarly, we can get the 20 most volatile assets as follows:

summary_df.sort_values('volatility', ascending=False).head(20)

Some of the most volatile cryptos are:

and so on.

## Final Thoughts

When we build a portfolio of cryptocurrencies, it is good to look at the volatility of each crypto. Higher volatility implies higher risk. Of course, if we want to build a diversified portfolio, we will need to look at the average returns and the correlation of the cryptos. Stay tuned for the next tutorials.

**leave a comment**for the author, please follow the link and comment on their blog:

**Python – Predictive Hacks**.

Want to share your content on python-bloggers? click here.