In the first article we covered how to program a trading bot in python to buy Bitcoin every time memelord Musk tweets about it. In this article we will cover some of the limitations of the existing build as well as improving the existing code, so if you haven’t read the previous article and want to follow along with your own coding I recommend you start with the link above.
Limitations of this crypto bot
I know what you’re thinking – “Wow how can there be any limitations to a bot that buys Bitcoin based on what Elon Musk Tweets?” or “The only limitation is that it doesn’t buy DOGE!“
But it does have its limitations, here are the two most important ones:
- It does not take the Tweet sentiment into account, it will buy Bitcoin even if Elon Tweets – “I will never buy a single bitcoin again”.
- It will only trade when Elon tweets, missing out on many potential opportunities.
Improving the formula
There’s nothing we can do about the second limitation, since that’s what we set out to do, but we can address the first point – the tweet sentiment. Elon Musk can be hard to predict on Twitter, and while Tesla is now accepting Bitcoin payments for its vehicles, you never know when Elon might tweet – “bitcoin price is too high imo”.
At this stage, if we assume that his twitter account has enough influence to move the bitcoin market we can see that our bot will not perform very well in the current set up as it will just buy more.
Adding Sentiment analysis
While scouring the web for a sentiment analysis API, I came across one that suited the needs of this bitcoin bot quite well. I have tested it and it appears to be fairly consistent, yet relatively simple in its approach, but it will do nicely in the beginning.
Here is the link to the API that I ended up using, you will need to create a rapidAPI account if you want to be able to use it yourself. The API works by taking your sentence input, in this case a tweet – and scoring it by giving it a value for one of three sentiments: positive, neutral and negative.
In addition to the imports from the first build you will also need to import http.client and json
Will also be required to store an additional key in your code for the text sentiment API
Now it’s time to dive to the sentiment analysis function that we created to analyse the Tweets coming from Tweepy API.
In a previous function, we are returning Elon’s last tweet and this is being used in the analyze_sentence. After the result is received from the sentiment API, we are returning the value inside the positive (pos) key. If this is 0, it means the tool hasn’t found anything positive about this sentence, and therefore will have scored as neutral or negative. Since our crypto trading bot only buys, we just need to know if Elon’s Tweet is positive, we don’t need the other keys.
Finally we need to add the sentiment into the logic check, that indicates whether we should be opening a trade or not
These are only snippets of code that I added to the original one, and to do not contain the whole block of code. A GitHub repo is available below so you can see the whole program.
If you find this content useful, subscribe for more amazing crypto bot code and guides!