Predictive Hacks

## Introduction

First of all, I need to clarify there is no trading strategy without risk and no trading strategy can guarantee a profit. However, it makes sense to apply strategies where the expected return is positive, in other words, to be more likely to make money than to lose. In this post, we will provide an example of a trading strategy based on pairs trading concepts.

## The Assumption

We believe that in the market, there exist some cryptocurrencies that follow others with a lag equal to k. For this analysis, we will examine the k equal to 1-hour, but feel free to try different values of k. So, the idea is that if there is a “Leader and Follower” pair of cryptos, then we may predict the movement of the “Follower” based on the movement of the “Leader”, if the leader goes up at time t=0 then we expect the “Follower” to go up in the next k interval and vice versa.

## Analysis

We will get the 1-hour data of all cryptocurrencies’ close prices for the last 3.5 months. For all pairs (X,Y), we will test if (Xt,Yt-1) or (Xt-1,Yt) is co-integrated. For this analysis, we work with the log prices. Thus, if we identify the leaders and the followers, we expect that when there is an increase in X then the Y will follow and the strategy is to buy Y when X diverges significantly from its equilibrium. Of course, there are many risks like the chance that the X will never converge back to its equilibrium. Maybe there is an equilibrium again but because the correction occurred only to X and the Y never followed etc. You can find the cleaned data here and you can follow along with the tutorial. For this analysis, we will work in Python.

## Coding

Let’s get our hands dirty. Because there are many cryptos with a value less than 1 dollar, and the log price becomes negative, we will multiply all the prices by 100 like representing the prices to cents instead of dollars. Notice that the time is in UNIX Timestamp.

# http://web.pdx.edu/~crkl/ceR/Python/example14_3.py
df.set_index('open_time', inplace=True)

df = df*100
# get the log prices and remove the first not NA row
# in order to have the same period of data with the leader (lag(1))
log_prices = np.log(df).dropna(how='any')
log_prices = log_prices.iloc[1:]

# get the log prices of the leader, where by leaders we refer to the lag 1

# remove the USDT suffix from the column names
log_prices.columns =  for c in log_prices.columns]

log_prices


Try to find co-integrated pairs by taking into account only the pairs which are highly correlated (>0.95) Apply the Co-Integration Test and run the linear regression without intercept i.e log(leader) = beta x log(follower).

my_list = []
Follower = []
Cointegration = []
Beta = []
for i in log_prices.columns:
for j in log_prices.columns:
Follower.append(i)

'Value':my_list, 'Cointegration':Cointegration,
'Beta':Beta})

# keep only the cointegrated parts p-value 0.01
output = output.loc[output.Cointegration<0.01]

# remove the negative Betas
output = output.loc[output.Beta>0]



We come up with 277 co-integrated pairs which satisfy the following conditions:

• Co-integration test p-value<0.01
• Correlation>0.95

We can plot all the spreads which are of the form Spread = Leader – Beta x Follower

for i in range(sample.shape[0]):
plt.figure(figsize=(12,6))
plt.axhline(y=0.0, color='r', linestyle='-')
plt.title(sample.iloc[i]['Leader'] + "-" + str(sample.iloc[i]['Beta']) + " X " + sample.iloc[i]['Follower'])
plt.show()


Example of some co-integrated pairs

## The Takeaway

Now, you can apply a strategy based on statistical arbitrage. You can even go in two directions, by goings long the one asset and going short for the other. Generally speaking, you can try many strategies based on the assumption of the Leaders and Followers. In the real world, when you apply high-frequency trading you have to pay transaction fees that most of the time are higher than the expected returns. So, this strategy may work BUT assuming that there are no fees 🙂

## Get 25$in BTC for FREE You can get$25 in Bitcoin by investing \$100 in Nexo

### Get updates and learn from the best

Miscellaneous

#### How to Redirect and Save Errors in Unix

In Unix, there are three types of redirection such as: Standard Input (stdin) that is denoted by 0. Usually, it’s

Python

#### Content-Based Recommender Systems with TensorFlow Recommenders

In this post, we will consider as a reference point the “Building deep retrieval models” tutorial from TensorFlow and we