Why would someone do this?
The answer is a simple one – the Techno King of Tesla has a history of influencing crypto markets whenever he tweets about them, to the point where a movement in the market is almost expected when he picks up is phone and starts expressing his opinions on the blockchain technology over twitter.
By creating a crypto trading bot that buys bitcoin every time the Tesla boss tweets about it you can rest assured that you are going to catch a VIP seat on the rocket that will slingshot right past the moon and make its way directly to Mars, where Elon spends most of the summer months due to its cold weather and dry climate.
Will this actually work?
The quick answer is “not sure” – as no one tested this strategy before. The longer answer is probably – as long as we’re talking about Bitcoin. Statistically speaking, regardless of the time you bought your bitcoin, you are most likely in profit (excluding the recent all time high around the time of writing).
So if nothing else, you will at least spice up your BTC HODLing strategy with a bit of help from Elon. This article won’t go into a detailed analysis to show whether this strategy actually works or not. This article is about building it for fun, but it does have serve as a powerful reminder of just how many resources we have at our disposal and that you can build just about any crypto trading bot you can think of.
You will also be able to see and use the code so you can test it or improve it.
How to set your bitcoin bot up
UPDATE: Video guide for this is now available. Simply continue reading if you prefer the written guide.
What this article is focused on is the actual technical building of the bitcoin trading bot, and how to set it up in a safe test environment, so let’s get to it.
You will need the following resources:
- A MetaTrader5 account
- A demo account with XBTFX so you can safely test your strategy
- A Twitter Dev Account
- A Tweepy API account
Setting up MetaTrader5 and XBTFX
As the name suggests, MT5 is a platform which supports multiple brokers along with detailed technical analysis – the main reason to start your crypto bot building journey with MT5 is due to it’s easy integration with Python and out-of-the-box support for a demo or virtual account so that you can test in a safe demo environment.
There are detailed instructions on how to install and configure MetaTrader5 as well as the XBTFX crypto broker in the previous post that covers how to build a crypto trading bot in python, so we’ll only briefly going over these steps in this article. If you need more information on how to do it, as well as why those two platforms were chosen, please refer back to the linked article above.
Start by downloading and installing MetaTrader5 and create an account on their platform. The next thing you need is a broker that you can place your trades with – I recommend XBTFX as they offer the most crypto-pairs of all brokers that work with the MT5 terminal. Register with XBTFX and create a demo account.
You can now connect to your demo account via MT5 by navigating over to File > Open an Account and searching for XBTFX. If you have registered using the referral link above you will need to select “Connect to Existing Account”, otherwise proceed to create a new account.
Apply for a Dev Account with Twitter
Before you can use Twitter’s API or the Tweepy Python module, you need a developer account with Twitter. Luckily the application process is quick and easy, and you will probably be accepted as long as you describe why you need the access to the Twitter API.
Nativate over to twitter’s dev platform and click Apply in the top right corner of the navigation menu.
On the next page click Apply for a Developer account and you will be prompted to sign in with your twitter account.
Follow the registration process and explain your intentions with the API
After you have completed all the necessary information, it may take anywhere between a couple of hours to a couple of days before you can get access to the platform. In my experience it was only a few hours.
Once your dev account is ready navigate over to the Projects & Apps tab open Project 1, if this is not available go ahead and create one. Under your project go to Keys and Tokens and generate the following (make sure to save them or you will need to regenerate the keys!):
Defining bot parameters
- The bot will open a buy position on bitcoin every time Elon mentions bitcoin in his tweet
- Take profit is set to 10% and stop loss to 5%
- The bitcoin bot will not place another trade if there is already an active trade (can be adjusted)
Coding for your bitcoin trading bot
Preliminary set-up
First off you need to import the MetaTrader5 and Tweepy modules using PyPi.
The next step is to import these modules along with a few others into your Python interpreter.
We now need to store the secret keys and tokens that you generated using the Twitter Dev platform in order to use them with Tweepy.
In the last part of the preliminary set up you need to connect to the MT5 terminal, store your account’s equity and define the trading instrument that we will be working with – in this case it’s Bitcoin. We will also create a short list of keywords to query Elon’s last tweet against.
Getting Elon’s latest tweet
With all the preliminary stuff out of the way, it’s time to focus on the cool parts of this bot. Let’s start by getting Elon’s last tweet with Tweepy as shown below in the get_elons_tweet() function.
During testing, emojis and other invalid characters would break the script, so each tweet is re formatted to only contain alpha-numeric characters.
Logic check and preparing the trading request
Now that we have Elon’s last tweet we can start preparing the logic and the trading request in function trade(). For more information regarding the format of the trade request, have a look at the MT 5 documentation.
what_musk_said contains the last tweet and the logic will check whether any of the keywords defined in our keywords variable above are present in Elon’s tweet. If that is true, the bitcoin trading bot will place a buy order on bitcoin with instant execution. In case it’s false it will simply return to us the tweet.
Putting it all together
We now need to decide how often we should be iterating through the code below. By default, it pull and analyse Elon’s last tweet once every 5 seconds, but this can be adjusted in the time.sleep function below.
Additional Resources:
It was a fun project work on and I hope that you enjoyed this article. Please subscribe to the newsletter for more awesome content delivered straight to your inbox!
2 thoughts on “Program a trading bot to buy Bitcoin when Musk Tweets about it (Part 1)”