Throughout this part, I will be showing you how to develop the strategy in python. Nevertheless, due to the size of the actual model, I have only attached snippets of the program on which you can expand and develop the strategy yourself. If you want to see the complete code, I have created a notebook, with step-by-step comments in every line that can be run and trade for you without having to code anything yourself! The notebook can be found here.
Before establishing a connection between the broker and twitter, it is important to import all libraries used throughout this project. For your convenience, here are the libraries and dependencies you need to import:
import re
import nltk
from textblob import TextBlob
import sys
from nltk import wordpunct_tokenize
from nltk.corpus import stopwords
import robin_stocks as r
import pyotp
import pprint
import tweepy
Connecting with a broker and twitter
Obviously, in order for the model to work efficiently, it must automatically buy X USD of the cryptocurrency before the retail investors manage to establish their positions and drive up the price. There are many platforms and APIs one may use to do this, (I would recommend the Binance API), but for this project I will be using the unofficial “robin_stocks” Robinhood API.
Setting it up is extremely easy, as the only thing left to do after importing the library, is to input your credentials in the designated fields, as illustrated below:
Next, we need a twitter developer account, so we can fetch the tweets in real-time. Although the process is quite short and straight-forward, the review of your request may take some days (here is a step-by-step tutorial on how to become a twitter developer).
After your account has been granted access, you will be provided with a number of keys that we will need to establish a connection between our application and the twitter platform.
Creating the Core Logic
After having connected to twitter, we create a listener and some basic functions, so as to clean the tweet from any special symbols, URLs, etc. that may confuse the sentiment analysis model.
With that being done, we will now create two variables, one for the subjectivity and one for the polarity of the tweet.
What we will be using next is the ‘sentiment_polarity’ variable, as:
- sentiment_polarity < 0: Negative Sentiment
- sentiment_polarity = 0: Neutral Sentiment
- sentiment_polarity > 0: Positive Sentiment
Using the cryptocurrencies noted above, as well as the aforementioned categorization of sentiment values, we can create the core logic of our strategy:
Defining what users’ feeds to monitor
With the core logic having been clearly set forward, it is time to enter the IDs of the twitter accounts we want to monitor.
This can easily be done by typing:
It is important to use the User ID rather the twitter account name. Although, these can be found online by using specialized tools, I have created a list with the IDs of the most influential, in my opinion, pornstars, who have the ability to move the markets (You can access it here).