Generating cryptocurrencies trading pairs to day trade using python.

In this tutorial we would be writing a script to help us generate a list of cryptocurrencies trading pairs to trade for the day using volume and sentiment.

simi
7 min readMay 19, 2021
Photo by Nick Chong on Unsplash

Background

I have always wanted to get into finance, algorithmic trading. Saw a couple of youtube videos but really loved Quantitative Trading: How to Build your own Algorithmic Trading Business by Ernest Chan as I was feeling like a superman. I was about to pick another course when I decided to actually write the code out. Tutorials are good, but writing the actual code is way cooler.

So I itemised the steps I needed in writing a trading bot or an quant system. The first two steps where selecting trading strategies and portfolio. I do not really know which goes before the other. But I was quite interested in how to select a portfolio for day trading. Yeah trading strategies might be straight forward, but how do you know which assets to invest in?

Thus that’s is what this post is about writing a function that can generate the assets to invest in based on some criteria.

Based on my research, I have chosen to select assets based on volume change and sentiment.

Assets in this post refers to cryptocurrency trading pairs and it is based on trading pairs for day trading.

Please note, I am not a financial expert.

Code Implementation

Step I: Setting up the environment.

Binance API: Binance is a cryptocurrency exchange platform and we could get the complete data for several trading pairs. We use it to get tradable cryptocurrency so we are not selecting cryptocurrencies that are not tradable. Here is a link that takes you to the page that direct you through the steps needed to get your API Key and Secret Key

Coin market Cap: This is an informational (though it has become more than that) web application for cryptocurrencies. Like the dictionaries of cryptocurrencies. Click here to sign up for basic plan and get assess to the coin market cap API Key.

Twitter API: We will be using tweets for sentiment analysis so we need to set up twitter api also. Click on Twitter Developers to set it up. Log in with your twitter account. Create an app and then you get the Access Token, Access Token Secret, Api Key and Secret Key

Please note: You could replace this library with other options e.g coinbase api over binance api or coin gecko over coin market cap. I have chosen these ones based on my familiarity and availability in my region.

Then save all these keys in your .env file. Be security conscious, never put your secret in your code even in tutorials. Your .env should be like this.

secret_key = xxxxx
api_key = xxx
tw_accessToken =xxxx
tw_accessTokenSecret =xxx
tw_ApiKey = xxxx
tw_SecretKey = xxxx
coin_mkcap_api = xxx

Recap: Binance to help us get tradable pairs, coin market cap to get the information for us, twitter to help us get the tweets needed for sentiment analysis

Finally some

Python packages: python-binance, a python wrapper around binance API, tweepy, a wrapper around twitter API, requests for processing http requests, pandas for data processing, textblob for textual processing and python-dotenv for loading .env files

Step II. Initialise class, get 24 hour price statistics and filter by volume.

We have successfully setup the environment, we will begin writing the code to initialise the class and end up getting crypto-trading pairs filtered by a volume criteria.

Initialising the class

Basically we are just setting up the client from the python-binance library and setting some variables here self.percent_change_lower and self.percent_change_higher acting like a lower and higher limit. So we are selecting assets whose percentage change is between this limits.

The next step.

Getting the 24 hour price statistics and filtering by volume

We are getting the 24 hour price statistics because we are interested in the day trading, thus price change in the last 24 hour is more important to us

line 2: we get the tickers which returns the 24 hour statistics of trading pairs of assets on binance.

line 6: we select the median volume

line 9: further selecting volume greater than the median volume.

line 12: final filtering based on volume between the specified lower and higher limits specified in the class initiation.

line 14, 16, 18: selecting first 50 rows, then selecting the symbol column and returning it.

If you did run this you should get an impressive list of trading pairs that we would be using for the next steps.

Step III: Getting the base assets based on the trading pairs

The symbols that we have gotten from Step II is actually the trading pairs, but to get the information we need to get the base asset due to how the coin market cap API is built. For example, BTCUSDT is what we would get from step II, but to get the information from coin market cap API we need get the base asset which in this case is the BTC. So we are writing the method to get the base assets for the trading pairs we have gotten.

line 1–5: we create a query based on the symbols passed to follow the pattern needed to make the necessary query. The pattern is something like this; [“BTC”, “BNB”].

line 7-12: we make a query using the requests package and get the response.text.

this is how the response looks like

{
"timezone": "UTC",
"serverTime": 1621376811527,
"rateLimits": [
{
"rateLimitType": "REQUEST_WEIGHT",
"interval": "MINUTE",
"intervalNum": 1,
...
],"exchangeFilters": [],
"symbols": [
{
"symbol": "ZRXBTC",
"status": "TRADING",
"baseAsset": "ZRX",
...

and we are interested in the symbols object hence line 14

line 16: we normalize the symbol object into a pandas dataframe

line 18: selection of baseAssets

line 20–21: we are just writing some values to csv — symbol, status and baseAsset. The status help us to know if that trading pair is being traded.

Step IV: Getting the Coin market cap information.

Next, we get the coin market cap information for the baseAssets from the above. We are actually interested in the name of the cryptocurrency and not the symbol to be able to get the related tweets to that project.

line 2–5: we create a query based on the list of base assets supplied to us.

line 9–24: we make a request to the coin market cap api to get the info. This particular endpoint accepts multiples assets that’s why we are passing the concatenated query from line 5 to it.

inspecting the query response we see that we need only the data property to go forward hence line 29.

line 29: We convert the data from the coinmarketcap api to a pandas dataframe selecting only the data property.

Step V: Getting Cryptocurrency names

We have gotten the coin market cap api information for the list of base assets. But we only need name that represents the cryptocurrency. So we start with that.

It turns out that from the slug we can actually get the name of the cryptocurrencies. So in line 7 we create a dictionary iterating over the coin market cap pandas data we previously created.

Since the list is quite long and we are trying to get about 200 tweets we cannot make all the request at once. So we have a simple method to do that for us. Here:

we pass the size and it splits it for us accordingly.

Step VI: Setting up twitter sentiment class and generating report

We would be setting up a twitter sentiment class based on the tutorial from Geeks for geeks. For in-depth study here is the link Twitter Sentiment Analysis using Python — GeeksforGeeks

But for our sake, we only need to install some NLTK corpora using following command:

python3 -m textblob.download_corpora

then we create a new class called social_sentiment.py where we copy and paste the following code.

Then back to our code.

We import the above social_sentiment.py into our code from step V.

import social_sentiment as social_snt

We would be continue with the get_twitter_report method to look something like this.

line 1: we split the names into five groups

line 4–7: for each group we iterate over the items in the group and get the tweet object for each item. The tweets in line 7 returns a dictionary based on the tweets with sentiment as key and either positive, negative or neutral as a value.

line 10–23: we get the sentiment of the tweets based. We filter based on whether it was positive or negative and then calculate the necessary values for the positive, negative and neutral values. Then on line 23 we append the SentimentReport to the list.

line 27: we sleep for 20

line 29–31: we read the values that have been generated based on the sentiment report and write it to a csv file.

To recap, in this section, we import a sentiment analysis class that returns a dictionary of positive and negative tweets ratings. We then use it to calculate the respective values then write it to the csv file.

At the end of the day we have something like this:

symbol,positive,negative,neutral1INCH,0.26315789473684204,0.09210526315789401,0.644736842105263
ADADOWN,0.185185185185185,0.037037037037037,0.777777777777777
AERGO,0.48101265822784806,0.063291139240506,0.45569620253164506
AST,0.223684210526315,0.09210526315789401,0.684210526315789
CKB,0.4,0.12,0.48
COS,0.044776119402985,0.0,0.955223880597014
CTXC,0.24637681159420202,0.11594202898550701,0.637681159420289
DGB,0.461538461538461,0.06153846153846101,0.47692307692307606
DNT,0.3125,0.15625,0.53125
ENJ,0.403846153846153,0.019230769230769003,0.5769230769230761
FTM,0.341176470588235,0.129411764705882,0.529411764705882
GTO,0.170212765957446,0.063829787234042,0.7659574468085101
IQ,0.339285714285714,0.053571428571428006,0.6071428571428571
...

And Remember from step III we have the assets.csv, something like this:

symbol,status,baseAssetLRCBTC,TRADING,LRC
DNTBTC,TRADING,DNT
ASTBTC,TRADING,AST
REQBTC,TRADING,REQ
XRPBTC,TRADING,XRP
ENJBTC,TRADING,ENJ
POWRBNB,BREAK,POWR
MANABTC,TRADING,MANA
GTOETH,BREAK,GTO
...

Thus if an asset has greater positive sentiment compared to negative and its trading, we add it to our trading portfolio for the day. Also we can know which trading pair to actually currently trading.

In this case as at the time of this writing, the sentiment for AERGO is as follows

AERGO,0.48101265822784806,0.063291139240506,0.45569620253164506

and it is also trading against USD on binance:

AERGOBUSD,TRADING,AERGO

Hence we can add this as part of our day trading portfolio.

Conclusion

In this tutorial we have being able to generate a list of trading pairs for the day. It was based on volume change in the last 24 hours (the previous day) and the twitter sentiment. We made a couple of trips to several API s — binance, coin market cap then twitter, which might not be the best approach, but since I couldn’t find a single API to do all that, it still is my best approach.

This could be a good starting point for several of us from manual traders to algorithmic traders.

Feel free to ask the questions or drop comments or reach out.

Happy coding!

--

--

simi

I write about software and at other times about not-software