Geek Culture
Published in

Geek Culture

This bot can trade Bitcoin whenever Elon Musk tweets about it

Sentiment based trading-bot on Binance using NodeJS

Source: Blockchain Research Lab
Source: Blockchain Research Lab

📝 Prerequisites :

  • NodeJS : To run this bot, you need to have NodeJS installed on your machine. If you haven’t done it yet, use this link.
  • Twitter Developer Account: For getting the Bearer token to make valid requests to Twitter API.
  • Binance Testnet Keys: The keys are required to place orders programatically on the Binance Testnet exchange. It can be obtained by signing in with your GitHub account.

🧘 Preparation :

Let’s create a new directory for this bot and switch our current directory to it. I’m gonna call it the “musk-bot”.

mkdir musk-bot && cd musk-bot
  • dotenv: To read values of the environment variables
  • node-binance-api: To place orders on the Binance Exchange (testnet)
  • sentiment: To retrieve the sentiment of the tweet.
npm install axios dotenv node-binance-api sentiment --save 
touch trade.logs tweetId.txt  

💡 Algorithm:

The way our bot works is pretty simple and straightforward.

  • We will maintain an array of words which is required to check which tweets are needed for processing.The tweets without that words are neglected. In our case the word array will be something like ['btc', 'bitcoin'] . There can be some hyperlinks in the tweets that contains the words present in the array, so we will remove the hyper links from the tweet using Regex before checking it against the array of words.
  • Extract the sentiment score from the tweet. If neutral or it doesn’t break the threshold, we ignore it.
  • If the tweet breaks the positive/negative threshold score (can be defined in a file, check implementation for more details), we will place a buy/sell order based on the sentiment.
  • Then we will record the trade log and the lastProcessedTweetId in a file.
  • If all the 10 tweets doesn't contain any tweets related to the words in word array, then we simply update the lastProcessedTweetId to the most recent tweet present in the tweet array returned by the Twitter API (ie., lastProcessedTweetId = tweets[0].id)and wait for the next iteration (sliding window technique).


Create a new file called app.js in the project root directory and add the following contents.

module.exports = {BASE: "BTC",QUOTE: "USDT",//buys BTC using 30% of USDT balance
//sells 35% of BTC
//bot checks for these words in the tweets
shouldInclude: ["btc", "bitcoin"],
//UserId of the twitter user. It can be obtained from this API
twitterUserId: "elon_musk_id_goes_here",
//Frequency to check for new tweets (in minutes)
interval : 2
  • If the action is SELL due to negative sentiment, then the script sells around SELL PERCENT (35%) of the total holdings for the market price.

😋 It’s time for Live Action:

For the testing purpose I added my Twitter userId in the config.js file and started the bot by running the node app command.

Trading Bot Output 1
25/06/2021, 00:06:02 - Bought 0.04739 BTC for 1656.069314602 USDT. OrderID : 5005343
Trading Bot Output 2
Results [
"text": "#Bitcoin 💔 ",
"sentiment": "NEGATIVE"
...Results [
text: '@Cointelegraph This is inaccurate. Tesla only sold ~10% of holdings to confirm BTC could be liquidated easily without moving market.\n' +
'\n' +
'When there’s confirmation of reasonable (~50%) clean energy usage by miners with positive future trend, Tesla will resume allowing Bitcoin transactions.',
sentiment: 'POSITIVE'

👋 End Notes:

Yes I agree that this bot is very simple and as always, there is a plenty of room for improvement. But the main goal is to showcase how we can leverage Twitter API and place orders using Binance API. This bot is absolutely fun, and it helps us to learn something new and interesting. You can try to change the config file and play around with this bot. If you find any issues or wish to share your thoughts please feel to post them in the comments.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pranesh A S

Backend Engineer and Blockchain Developer. Keep learning | Spread Knowledge