Is it profitable to trade bitcoin from the Fear and Greed index?

Analysis of a basic case

If there is one indicator that bitcoin price doomsayers like, it is without a doubt the Fear and Greed Index, which is published daily on the Crypto Fear & Greed Index.

It is a metric that tries to guess the market sentiment, if it is positive (>50) it presupposes that there will be a greater demand for BTC so the price will rise and if it is negative (<50) it will result in sales of the asset with the consequent price drop.indicator Alternative for bitcoin has been published since 2/5/2018.

We are going to carry out a study to find out if it is profitable to buy and sell BTC following this indicator, which is known as backtesting, we choose a basic case where bitcoin is bought when the index is greater than 50, which is the value where it starts greed, and are sold when it drops below that limit. We will use the Python programming language together with the libraries that it has prepared for this type of analysis.

This metric is built daily from 5 data sources:

  • Volatility (25%). The volatility of the previous day is compared with the averages of the last 30 and 90 days. The idea behind it would be that high volatility is a sign of a fearful market.
  • Market volume/momentum (25%). This indicator compares yesterday’s volume with the average values ​​of the last 30 and 90 days. Understanding that high buying volumes indicate that the market is greedy and bullish.
  • Social Networks (15%). Analysis of messages on Twitter related to Bitcoin. High engagement rates mean more interest which translates into a greedy market.
  • Dominance (10%). The dominance of a coin is similar to the market capitalization share of the entire crypto market. If that bitcoin dominance increases against the other altcoins it is a bullish sign and if it decreases it is a bearish indicator.
  • Trends (10%). This indicator is extracted from the Google Trends data for various search queries related to Bitcoin, these numbers are analyzed and if the number of searches goes up it is assumed that the interest in Bitcoin increases and therefore the euphoria increases and the fear decreases.

First, the behavior of the Fear and Greed (F&G) indicator will be analyzed without taking into account the evolution of bitcoin prices.

For this we import the python libraries that will be used.

import pandas as pd
import numpy as np 
import requests
import matplotlib.pyplot as plt
import seaborn as sns
import yfinance as yf
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
from scipy.stats import norm
from pylab import rcParams
import warnings

warnings.filterwarnings( 'ignore')
rcParams['figure.figsize'] = 10, 5

Data is downloaded Fear and Greed index data history

# Data download of https://alternative.me/crypto/fear-and-greed-index/
# The index fear_greed that shows today is the value calculated with yesterday's data
r = requests.get('https://api.alternative.me/fng/?limit=0')

A graph is shown with the time evolution of the

# Show fear and greed index since the beginning of the year 2018
# The values range between 0 and 100, with low values indicating fear and high values indicating euphoria.
df = pd.DataFrame(r.json()['data'])
df.value = df.value.astype(int)
df.timestamp = pd.to_datetime(df.timestamp, unit='s')
df.set_index (df.timestamp, inplace=True)
df.rename(columns = {'value':'fear_greed'}, inplace=True)
df.drop(['timestamp', 'time_until_update'], axis=1, inplace=True )
df.fear_greed.plot(figsize=(20,10))

The values ​​of the F&G index range from 0 to 100, where low data indicates fear in the market and high data indicates euphoria.

A priori, seeing the graph, no clear conclusions can be drawn, so I will choose to “interrogate” the data.

Initially, I look at the distribution of the values ​​and it can be seen that most of the data are around the value 26, which indicates that fear has predominated in this period and that the high values ​​associated with euphoria are less frequent.

# Show in a graph the distribution of the values ​​of the fear and euphoria index
# Where it can be seen that in this period the most recurrent values ​​are around 20 and 40, which are indicators of fear
sns.distplot(df['fear_greed' ], color='g', bins=30)

The mean is 42, the median is 39, which indicates that in this period, 2018 to 2022, in general terms there has been more fear than euphoria and in the box plot it can be seen Furthermore, most of the values ​​are between 24 and 59 and the rest of the values ​​are less frequent.

# The mean and median values ​​are around 40, which indicates that fear prevails over euphoria
print("Mean: " + str(df.fear_greed.mean()))
print("Median: " + str(df.fear_greed.median( )))
sns.boxplot(x='fear_greed', data=data)

If we analyze the autocorrelation of the sample data we will see that it exists and is greater the closer the data are. Which seems logical since fear and euphoria follow trends, they do not change arbitrarily from one day to the next and they will always have more influence on today’s value, yesterday’s data, than the day before yesterday, than two days ago. … and as the days go by this influence decreases.

# There is a high autocorrelation in the fear and greed index that decreases little by little as the days go by
# It is normal since fear and greed work for periods it is not usual to go from extreme fear one day to high greed the next day
# Today's index is more likely to be more like yesterday's than 2 weeks ago as trend changes take time to occur
plot_acf(df["fear_greed"], lags=30)
plt.show()

Regarding partial autocorrelation we don’t see this dependency so staggered, the data from yesterday and the day before yesterday have an influence, but the rest is already irrelevance.

# The partial autocorrelation is strong with respect to yesterday, a little more with the day before yesterday, but after those two days
# Does not exist since they are statistically insignificant since they are within the confidence interval (shaded part)
plot_pacf(df[" fear_greed"], lags=30)
plt.show()

This correlation that occurs in the value of F&G with previous days is not maintained if we focus on the increase or decrease in value from one day to the next where if we have a greater randomness.

# But if we focus on the variations of the index from one day to the next, we do find a great randomness
df.fear_greed.diff().plot(figsize=(15,6))
# If the distribution of values ​​of the difference is shown, we see that it is an almost normal distribution centered on a value close to 0
sns.distplot(df.fear_greed.diff(), color='c', bins=30, fit=norm)

Now it’s time to analyze the price of bitcoin in relation to the F&G index and for this reason I start by downloading the data of the closing price of bitcoin from the Yahoo finance website.

# Data Bitcoin price download of yahoo finance
df1 = yf.download('BTC-USD', interval = '1d')[['Close']]
df1.rename(columns = {'Close':'close'}, inplace =True)
df1.index.name = 'timestamp'
df1['timestamp'] = df1.index
df1.reset_index(drop=True, inplace=True)
df1.timestamp = pd.to_datetime(df1.timestamp, unit='s ').dt.tz_localize(None)
df1.set_index(df1.timestamp, inplace=True)
df1.drop(['timestamp'], axis=1, inplace=True)

We join in the same dataframe the index data plus the of the BTC price.

# The fear and greed index data is merged with the bitcoin price data in dollars
data = df.merge(df1, on='timestamp')
data = data.sort_index()
data.tail()

I show the price history in a graph of bitcoin from 5/2/2018 to the end of 2022 which is the data we have from the F&G index.

Making a very brief analysis, several differentiated periods can be seen:

  • From 2018 to the end of 2020, where there is relative stability in the price
  • From the end of 2020 to the first quarter of 2021, where there is an upward period, then we have a downward period, and then we get in November 2021 the highest price of BTC in history
  • And from then until the end of 2022 we live a bearish period
# The graph of the price of bitcoin during that same period
# Briefly analyzed we see a relative stability between 2018 and the end of 2020
# Subsequently a very pronounced rise until the end of the first quarter of 2021
# Then a descent followed by a big rise ending at the end of 2022
# And since then a bearish period
data.close.plot()

In order to compare prices and to know how good this indicator is to predict them we have to take into account that today’s Fear and Greed index data is published at 00:00 UTC from the information of the previous day and the pr One-day bitcoin closing price is its market value at 00:00 UTC. This means that at 00:00 UTC of the day we have the F&G index with today’s date and the closing price of BTC also with today’s date and with that information we want to execute a buy or sell strategy and that we will have to compare it to tomorrow’s closing price at 00:00 UTC of bitcoin.

# The fear and greed index is published at 00:00 UTC taking into account the data of the previous day
# The closing data of the bitcoin price is the value at 00:00 UTC
# Therefore, in order to make money, the value to be compared with is the closing price of the next day

# A column is included with the data from tomorrow's close and the difference between the two values
​​# Another columns with returns in value today and tomorrow and change ntc price

data['close_tomorrow'] = data['close'].shift(-1)
data['returns'] = data['close_tomorrow'] / data['close'] - 1
data['change_btc'] = (data['returns'] + 1 ).cumprod()
data = data.dropna()

Before carrying out the strategy, we see that the correlation between the F&G index and the BTC price of the following day is 0.24, a value above 0 indicates that there is a relationship, at least in the period studied, between the two values.

# We perform a correlation between the F&G index and the closing price of BTC the next day
# A total correlation would be 1 and 0 indicates that there is no correlation, so 0.24 indicates that there is a slight correlation
data['fear_greed']. corr(data['close_tomorrow'])

0.2400391649234413

We define the basic strategy where we proceed to buy when the F&G indicator is greater than 50 and sell when it is less than that value.

# A basic BTC purchase strategy is analyzed when the fear and greed index is greater than 50 
# and when it is less than that value, nothing is done
data['signal'] = np.where(data.fear_greed > 50, 1, 0)
data['strategy'] = (data['returns'] +1) ** data['signal']
data['cumulative'] = data[['strategy']].dropna().cumprod()
data.tail()

We show the backtesting in a graph where the performance of the chosen strategy is observed together with the evolution of the bitcoin price for the same period.

# Graph of the performance of the strategy compared to the evolution of the price
plt.figure(figsize=(20,10))
plt.plot(data['cumulative'], label ='Cumulative')
plt.plot(data[ 'change_btc'], label ='BTC')
plt.legend()

The result of this strategy is clearly positive with a profit of about 600%. In the final part of the graph it can be seen that the benefit remains horizontal because the Fear and Greed index has had values ​​below 50 since April 5, 2022.

# It is clearly observed that it is a good strategy for the period studied.
print('Initial value: \t' + str(data['cumulative'].iloc[1]))
print('End value: \t' + str(data['cumulative'].iloc[-2]) )
print('End value BTC: \t' + str(data['change_btc'].iloc[-2]))

Initial value: 1.0
End value: 6.430155875523231
End value BTC: 2.264909298446648

At first it seems like a very good strategy but now We know that the devil is in the details:

  • It is only 100% valid for the period, 02/05/2018 to 10/27/2022, but we do not know the performance it would have if we had a larger history of Fear and Greed index data.
  • The percentage of final profit is dependent on the initial point and the final point chosen with other references, the benefit can fluctuate considerably, but it is true that the graph shows that most of the time it has a better performance than the price of BTC.
  • The calculation of the profit follows the typical analysis of trading strategies where percentages are calculated, which is equivalent to the fact that every time a buy signal is given, all the capital is used, which is not very realistic.
  • And most importantly, the commissions that are going to be very important in this strategy have not been taken into account because all the available capital is being used in the purchase and sale orders.

I create a function to calculate the real profit of this strategy assuming that only an initial capital is used, that when there is a feeling of euphoria above 50 all the capital is used to buy BTC and that when the index falls below 50 all are sold bitcoin and including commissions.

# I carry out the most realistic practical exercise starting with an initial investment and adding a commission on purchases and sales
# Everything is sold when the buy signal runs out since in this strategy all the capital is used for buying and selling and the money is needed to buy again

def profit_calculation (_data, _initial_capital, _feeds):
  n_buy_sell = 0
  sell = 0
  profit = _initial_capital
  i = 0
  _buy_where = []
  _sell_where = []

  for date,row in data.iterrows(): 
      i+=1 
      if row['signal'] == 1:
          n_buy_sell += 1                
          buy = (profit / row['close'])*(1-_feeds) 
          _data.at[date, 'buy'] = buy
          profit = buy * row['close_tomorrow']
          _data.at[date, 'profit'] = profit
          sell = profit
          _buy_where.append(i)

      elif sell != 0:
          n_buy_sell += 1
          sell_BTC = (sell/ row['close'])*(1-_feeds) 
          _data.at[date, 'profit'] = profit
          _data.at[date, 'sell'] = sell_BTC
          profit = sell
          sell = 0
          _sell_where.append(i)

  print("Nº buy and sell: \t" + str(n_buy_sell))
  print("Feeds: \t\t\t" + str(_feeds) + "%")
  print("Initial capital: \t$" + str(_initial_capital))
  print("End capital: \t\t$" + str(round(profit,2)))
  print("Profit: \t\t$" + str(round(profit - _initial_capital, 2)))

  return(_buy_where, _sell_where)

The practical exercise is carried out with an initial capital of 10,000 dollars and we assume that the broker charges a commission of 0.3% for each purchase or sale.

# Initial investment of $10,000 and a commission of 0.3%
feeds = 0.003
initial_portfolio = 10000
portfolio_calculation(data, initial_portfolio, feeds)

The number of times buy or sell orders have been launched is 619 when about 1700 days have passed.

The capital that would be available today (October 27, 2022) is $11,846, $10,000 has been initially invested, so the total profit is $1,846, far from the 600% that the backtesting of the strategy indicated.

This is due to the commissions that, although they do not seem very high, having made so many purchases and sales and always with the total capital, makes the profits drop considerably.

No. buy and sell: 	619
Feeds: 			    0.003%
Initial portfolio: 	$10,000
End Portfolio: 		$11,846.65
Profit: 		    $1,846.65
plt.figure(figsize=(20,10))
plt.plot(data.close, label='BTC', c='y')
plt.scatter(data.iloc[buy_where].index,data.iloc[buy_where]['close'],marker='^',color='g',s=100)
plt.scatter(data.iloc[sell_where].index,data.iloc[sell_where]['close'],marker='v',color='r',s=100)
plt.legend()
plt.show()

We stop the exercise here, leaving the reader to try other strategies on this F&G index for himself.

Notebook in Python with the script to be able to copy and execute it Bitcoin Price Prediction with the Fear and Greed

Notebook in Github Bitcoin price prediction with the fear and greed



Categories: Bitcoin, BTC, Code, Notebook, Price, Python, Script, Trader, Trading

1 reply

Trackbacks

  1. ¿Es rentable realizar trading en bitcoin a partir del índice Fear and Greed? – BGeometrics

Leave a Reply

Discover more from BGeometrics

Subscribe now to keep reading and get access to the full archive.

Continue reading