Predictive Hacks

# How to Detect Trends in Cryptocurrencies with ADX using Kraken API

You may have heard the terms “bull” and “bear” markets, which are terms to describe a trend in the market. There are different ways to detect trends, but the most popular one is the Average Directional Movement Index (ADX).

In order to derive the ADX, first, we will need to calculate the positive and negative directional indicators, +DI and -DI respectively. Before we get the directional indicators, we need to get the direction movements, +DM and -DM respectively.

• UpMove = today’s high − yesterday’s high
• DownMove = yesterday’s low − today’s low
• if UpMove > DownMove and UpMove > 0, then +DM = UpMove, else +DM = 0
• if DownMove > UpMove and DownMove > 0, then -DM = DownMove, else -DM = 0

Then we usually consider a 14-day period and the +DI and -DI are calculated as follows:

Finally, we are in a position to calculate the ADX as follows:

## Interpretation

The ADX is a tool that measures the strength of a trend. It is a lagging indicator, which means that it takes a while for it to give a signal that a trend is happening. The ADX ranges from 0 to 100. A low number means that the trend is weak, and a high number means that the trend is strong. A really strong trend is indicated by a number over 50.

## Trends in Bitcoin

Let’s get the trends in Bitcoin since 2022. We will get the data using the Kraken API. Let’s get the OHLC data.

```import pandas as pd
import requests
import time
import datetime
import talib
import numpy as np

import matplotlib.pyplot as plt
%matplotlib inline

epoch = datetime.datetime(2022, 1, 1, 0, 0, 0).timestamp()

# BITCOIN over USD
pair = 'XXBTZUSD'

url = f'https://api.kraken.com/0/public/OHLC?pair={pair}&amp;since={epoch}&amp;interval=1440'.format(pair,epoch)
resp = requests.get(url)
df = pd.DataFrame(resp.json()['result'][pair])
df.columns = ['unixtimestap', 'Open', 'High', 'Low', 'Close', 'vwap', 'volume', 'count']
df['unixtimestap'] = pd.to_datetime(df['unixtimestap'], unit='s')
df.set_index('unixtimestap', inplace=True)
df = df[['Open', 'High', 'Low', 'Close']]
df['Open'] = df['Open'].astype('float')
df['High'] = df['High'].astype('float')
df['Low'] = df['Low'].astype('float')
df['Close'] = df['Close'].astype('float')
df

```

Let’s get the ADX with a rolling window of a 14-day period using the talib Python library.

```# Calculate the ADX with the default time period

```

Let’s build a function that returns:

```def adx_trend(x):
if x<=25:
return "No Trend"
if x>50:
return "Strong Trend"
if x>25:
return "Mild Trend"

df.tail(20)

```

As we can see, during this time period (i.e. the last 20 days) there is a moderate trend in the market. Let’s have a look at the graph of the closing prices of Bitcoin, annotating the labels of the trend when there is a mild or strong trend.

```ax = df.Close.plot(figsize=(30,15), title= "Trends")
for i in range(df.shape[0]):
if label=='Strong Trend' or label=='Mild Trend':
ax.annotate(label,
(df.index[i], df['Close'][i]),
xytext=(0, 15),
textcoords='offset points',
arrowprops=dict(arrowstyle='-|>'), color='r', ha='center')

```

## Related Articles

### Get updates and learn from the best

Python

#### Creating Dynamic Forms with Streamlit: A Step-by-Step Guide

In this blog post, we’ll teach you how to create dynamic forms based on user input using Streamlit’s session state

Python

#### How to Connect Wikipedia with ChatGPT and LangChain

ChatGPT’s knowledge is limited to its training data, which has the cutoff year of 2021. This implies that we cannot