What is Crypto Sentiment Analysis? (Opinion Mining)
Crypto sentiment analysis involves evaluating public perceptions of cryptocurrencies by monitoring social media and news sentiment. Also called ‘opinion mining’, it helps traders gauge market sentiment and understand how other investors feel about a particular coin or category. In the case of crypto, Twitter/X is the optimal platform to measure social sentiment.
While the process typically involves machine learning and artificial intelligence to mine and process the data, we will demonstrate how to perform a crypto sentiment analysis using Twitter/X and CoinGecko API.
How to Develop a Sentiment Based Crypto Trading Strategy
To execute a market sentiment based crypto trading strategy, follow the 5 steps outlined below:
Generate Twitter/X and CoinGecko API keys. Open an IDE/code editor (like a Jupyter notebook), download Python and pip. Install Python packages that process market sentiment data. Extract sentiment, synthesize the dataset and generate polarity scores (sentiment scores). Develop crypto trading signals based on sentiment scores.
Essentially by synthesizing crypto market sentiment data, you can use insights to inform potential entry and exit positions. Let’s dive in!
Generate Twitter and CoinGecko API keys
Twitter/X API
The crypto community has ingrained itself into Twitter/X, also known as ‘crypto Twitter’, where news often breaks faster than traditional media. Given the volatile nature of crypto linked to perception shifts, monitoring market sentiment on Twitter/X can provide traders with a valuable edge. On this basis, we will develop a crypto trading strategy based on Twitter/X sentiment analysis, using the Twitter/X API.
To access the Twitter/X API, you will first need to create a developer account. After that, you will receive an API Key, an API Secret Key, a Bearer Token, an Access Token, and an Access Token Secret.
As of February 9, 2023, Twitter/X introduced their Basic and Pro plans. In this guide, we’ll be using the ‘Search Tweets’ endpoint found under their basic plan. The basic plan also allows you to pull data for tweet counts and retweets, which can come in handy when analyzing sentiment.
CoinGecko API
Each CoinGecko user can generate 1 API key by signing up for the Demo Plan, and will enjoy a stable rate limit of 30 calls per minute and a monthly limit of 10,000 calls. We will use the Open High Low Close (OHLC) /coins/{id}/ohlc endpoint for this directory and retrieve coins’ OHLC data where data granularity is automatic for Demo API users:
30-minute data granularity for 1-2 days away from now 4-hourly data for 3-30 days away from now 4-day data for >31 days from now
Download Python and Pip
Also, before you start, make sure you’ve downloaded Python and pip. Python can be downloaded here, and you can follow these steps for pipe installation:
Press command + spacebar and type ‘Terminal’ Check your Python version by typing python3 –version Download pip by typing: curl https://bootstrap.pypa.io/get-pip.py -o get- pip.py Then type: python3 get-pip.py
Install Python packages to process sentiment data
We recommend using the following 3 Python packages to effectively process the market sentiment data:
ReGex Python package: To clean the tweets we’re looking for, we’ll need the regular expression (regex) Python package. This will help wash the tweets of the extended characters attached to each call.
Tweepy Package: The easiest and most convenient way to access the Twitter/X API through Python is through the open source Tweepy package. The Tweepy package will give us a convenient way to access the Twitter API through Python. This includes a variety of Twitter/X’s or X’s-like methods, API endpoints, which provide a variety of implementation help and detail.
NLTK Vader Lexicon Package: Text analysis is an integral part of a number of industries, and one of its most important subdivisions is sentiment analysis. The Natural Language Toolkit (NLTK) is a ubiquitous library in Python programming for natural language processing (NLP). Advances in NLP have made it possible to perform sentiment analysis on large volumes of data, which together with our construction of a sentiment-based strategy. There are several ways to perform NLP, such as lexicon-based analysis, machine learning, or pre-trained transformer-based deep learning. For this strategy we will use a lexicon-based analysis.
The NLTK Vader Sentiment Analyzer uses a set of predefined rules to determine the sentiment of a text. Therefore, we can specify the degree of positive or negative polarity required in our composite score to execute a trade.
Use pip to install the above packages, still in the terminal:
pip install tweepy pip install nltk nltk.download(‘vader_lexicon’)
Then go to Jupyter notebook and import the required packages:
import pandas as pd import twopy import re from nltk.sentiment.vader import SentimentIntensityAnalyzer
Perform a Crypto Sentiment Analysis with Python
Now that we’ve installed Python and Pip and imported the required packages, it’s time to finally collect Twitter/X sentiment data.
Apply our newly acquired Twitter/X API credentials in Jupyter Notebook:
API_key = ‘********************’ API_secret = ‘************************ ‘ Access_token = ‘********************’ Access_secret = ‘******************** **** **’
Simply replace the stars with your personal developer details. We’ll then use Tweepy to authenticate, using our credentials, and access the API:
# From Tweepy docs auth = tweepy.OAuthHandler(API_key, API_secret) auth.set_access_token(Access_token, Access_secret) api = tweepy.API(auth)
We can then define our parameters and create a function using regex to clean up our pulled tweets. We will pull tweets containing $ETHUSD as an example:
# Define Variables count = 10 coinid = ‘$ETHUSD’
#Clear collected tweets with regex def nice_tweet(tweet): return’ ‘.join(re.sub(“(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+), ” “, tweet).split())
To apply the function to actual tweets, let’s next create a ‘get_tweets’ method. Starting with an empty set to contain our 10 tweets, we’ll then sift through it to determine if the tweet is already included, before leaving a slim list of recent tweets involving ‘$ETHUSD’:
def get_tweets(coinid, count): tweets = set() collected_tweets = api.search(q = coinid, count = count) for tweet in collected_tweets: cleaned_tweet = cleaned_tweet(tweet.text) if cleaned_tweet not in tweets: tweets.add cleaned_tweet) returns tweets
After getting data on the $ETHUSD tweets, we run them through a sentiment analyzer to determine their polarity (ie how positive/negative/neutral they are).
def polarity(tweets): counts = [] for tweet in tweets: score = SentimentIntensityAnalyzer().polarity_scores(tweet) score[‘tweet’] = tweet counts.append(count) returns counts
With that, we pulled tweets from the Twitter/X API, cleaned the data, and determined their sentiment score using the NLTK Vader Sentiment Intensity Analyzer.
Develop Crypto Trading Signals based on sentiment scores
Now let’s pull in crypto price data using the CoinGecko API, combine it with our previous sentiment analysis and create trades. Use the following code to import Ethereum (ETH) Open High Low Close (OHLC) data for the last 30 days:
from pycoingecko import CoinGeckoAPI cg = CoinGeckoAPI()
ohlc = cg.get_coin_ohlc_by_id( id=”ethereum”, vs_currency=”usd”, days=”30″)
df = pd.DataFrame(ohlc) df.columns = [“date”, “open”, “high”, “low”, “close”] df[“date”] = pd.to_datetime(df[“date”]unit=”ms”) df.set_index(‘date’, inplace=True)
With this data and our polarity scores we can build a simple signal to enter long and short positions with ETH prices. The composite score produced from our polarity function combines negative, positive, and neutral scores, giving us a single consolidated value. Rules can be varied, and for a simple trading signal we can follow this logic:
If the composite score is greater than 0.06, buy 1 ETH at the spot price If the composite score is below 0.04, we sell 1 ETH position. Otherwise, no action
This translates to the following python script:
def Signal(counts): LongPrice = [] Short price = [] Signal = 0 Signal List = [] tweets = get_tweets(coinid, count) counts = polarity(tweets)
#Get composite score df = pd.DataFrame.from_records(scores) mean = df.mean() compound_score = mean[‘compound’]
for i in range(len(df)): if (composition_count > 0.06 and signal != 1 ): LongPrice.append(self.df[‘open’].iloc[i+1]) ShortPrice.append(np.nan) Signal = 1 elif (composition_count <= 0.04 and Signal == 1 ): LongPrice.append(np.nan) ShortPrice.append(self.df['open'].iloc[i+1]) Signal = -1 else: LongPrice.append(np.nan) ShortPrice.append(np.nan) if Signal == -1: Signal = 0
SignalList.append(Signal)
return SignalList, LongPrice, ShortPrice
With that, we created a signal using our pulled CoinGecko $ETHUSD price data and polarity counts.
💡 Pro tip: Check out Shashank Vemuri’s Medium blog, which covers articles on the Tweepy and NLTK libraries.
For professional traders: unlock daily OHLC data
Incorporating sentiment analysis into crypto trading strategies provides valuable market insights for informed decision making. Using the Tweepy and NLTK Vader Sentiment Analyzer packages, traders can extract sentiment data from platforms like crypto Twitter/X, generating polarity or sentiment scores. Trading signals can then be developed based on sentiment scores and coin OHLC data from CoinGecko API. However, it is important to note that results may vary based on individual parameters and market conditions.
Professional traders looking to maximize the OHLC endpoint with a daily candle interval data granularity can consider subscribing to our Analyst API plan. It is exclusively available to paid subscribers, along with endpoints such as:
/coins/list/new – get the latest 200 coins as listed on CoinGecko /coins/top_gainers_losers – get the top 30 coins with the biggest price gain and loss, within a specific time period /exchange/{exchange_id}/volume_chart/range – get the historical volume data of an exchange for a specified date range
If you need a custom solution, fill out the form below to get in touch with our API sales team:
Disclaimer: This guide is for illustrative and informational purposes only, and does not constitute professional or financial advice. Always do your own research and be careful when putting your money into any crypto or financial asset.
Tell us how much you like this article!
Jackson Henning
Jackson Henning has a background in economics and spent 3 years in crypto, mainly entrenched in NFTs and DeFi. He is particularly intrigued by the perpetual ingenuity common to crypto trading, along with the use of technical analysis and the ability of DeFi to push the boundaries of traditional finance. Follow the author on Twitter @Henninng
Read more about Jackson Henning
Disclaimer for Uncirculars, with a Touch of Personality:
While we love diving into the exciting world of crypto here at Uncirculars, remember that this post, and all our content, is purely for your information and exploration. Think of it as your crypto compass, pointing you in the right direction to do your own research and make informed decisions.
No legal, tax, investment, or financial advice should be inferred from these pixels. We’re not fortune tellers or stockbrokers, just passionate crypto enthusiasts sharing our knowledge.
And just like that rollercoaster ride in your favorite DeFi protocol, past performance isn’t a guarantee of future thrills. The value of crypto assets can be as unpredictable as a moon landing, so buckle up and do your due diligence before taking the plunge.
Ultimately, any crypto adventure you embark on is yours alone. We’re just happy to be your crypto companion, cheering you on from the sidelines (and maybe sharing some snacks along the way). So research, explore, and remember, with a little knowledge and a lot of curiosity, you can navigate the crypto cosmos like a pro!
UnCirculars – Cutting through the noise, delivering unbiased crypto news