24x7 Live crypto trading | Buy Doge or any crypto using sentiment

Shyam BV
Code Sprout
Published in
7 min readMay 16, 2021

Introduction

I have been an investor in the stock and crypto market for a while. However, the current landscape looks completely different. Currently, valuation does not make any sense! Too much sentiment from investors is changing the prices of crypto. So I have created a system to trade with social sentiments for a while. In the article, I will explain how to read market sentiments and buy cryptocurrencies autonomously.

Architecture

Let me be frank! It is tough to track Elon Musk(Doge-father). He often tweets about different things, and he also mentions Dogecoin, Defi, BTC, etc. Once he tweets about it, the prices pump up!

Crypto prices are purely connected to investor sentiment. So why not build a system to get all the community knowledge and trade in production? You may see many articles about sentiment analysis. However, many of them use CSV and cant be used in the real-time environment.

So I decided to build a system that works frequently and does not miss the opportunity. Below are the tools I am going to use.

  1. Off-course Python!
  2. Twitter developer API and keys
  3. Elon Musk tweets
  4. Robin-stocks a python package
  5. Gemini a crypto exchange to trade with API
  6. Apache Airflow for the scheduler
  7. AWS or Any instance which runs continuously

Below is the flow diagram which we are going to create.

As Doge is famous right now, which has risen more than 8000%, we will get the sentiment of Dogecoin. But the architecture is the same for different altcoins.

Twitter API

To get the tweets, you needed a developer account and an app in the Twitter developer console. You require four different keys.

TWITTER_CONSUMER_KEY
TWITTER_CONSUMER_SECRET
TWITTER_APP_TOKEN
TWITTER_APP_SECRET

With these four keys, you can connect to Twitter via API using the package tweepy.

auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY,TWITTER_CONSUMER_SECRET)auth.set_access_token(TWITTER_APP_TOKEN, TWITTER_APP_SECRET)api = tweepy.API(auth)

Search for tweets

We need to search for the tweets across Twitter with the text. For example, Doge is a keyword we need to search for on Twitter. We are searching for the symbol and name.

Symbol: Doge
Name: Dogecoin

Because some may tweet with the symbol or with a name, so we don’t miss the Doge tweets! Finally, we can get the sentiment of all the tweets. The sentiment of the tweets itself is a separate topic. On a high level, I have used the transformer model to get the sentiment wrapped under the function obtain_sentiments.

Twitter Trends

We also wanted to captivate the trends of Twitter. If Dogecoin is a trending topic, we will gather that as well.

#Trends in US
us_trends = api.trends_place(2352824)
us_trends = [trend['name'] for trend in us_trends[0]['trends'] ]

We can go across different regions of the world. When you go across the world, we might get data in different languages which need to be parsed.

Doge-Father tweets

Now it is time to get our Doge-father Elon Musk tweets. Depending on the frequency, you can set a time frame. If you are going to run daily(we will talk about streaming shortly), you need the set the time accordingly, so you don't get older tweets of Dogecoin.

Once we get the tweets, we need to search for doge word and names from all his tweets and get the volume and sentiments.

Off-course I can search for more words and names. It can be bitcoin or any words. Perform fuzzy search and do a ton of different matching here.

Get sentiments of all the tweets.

After collecting all the tweets, now it is time to find the sentiments of the tweets. To find the sentiment, we need to use any of these models.

  1. Transformer models
  2. Trained supervised machine learning models
  3. Use external services such as AWS comprehend or GCP Sentiment analysis.

I will go over these options in detail in another article(Leave a comment if you needed this), as it is not the focus of this article. This article focuses on the code and architecture for buying and selling in real-time frequently. For this article, I have used AWS comprehend for getting the sentiments of tweets. However, performance can be greatly improved by having a trained model.

Creating buy signal

In this section, we need to collect all the sentiment and tweets and decide if we wanted to buy or sell them. Some of the options here are

  1. Train an AI model to get the tweets and sentiments and buy
  2. Create a rule according to our needs for buy and sell

In the first approach, there is always some risk of buy and sell at the wrong time. I will walk through another article. I am creating a simple score for me to buy. Doge-father(Elon) carries higher weight. Then comes the trending part, and finally, the community sentiment. Please feel free to change it. To get this code, please see it at the end of the article.

Buying Doge in Gemini

After final scoring, we have to buy using any exchanges if we decide to buy according to our rules. You can use Binance, Coinbase Pro, or Gemini.

At the time of writing this article, Doge is not supported in Coinbase. And for some reason, binance API is not so great. Gemini exchange provides an API key, and the robin-stocks package supports it, so that I will use Gemini. Please feel free to use any other exchanges.

g.authentication.login('api_key1', 'secret_key1')my_order, error = g.orders.order_market('doge',500, 'buy', jsonify=True)if error:   return("Error while placing the order")else:   return("Order placed successfully")

We can also add additional functions for retrieving the trade order confirmations and email. There are different functions, which we can use here. Such as trade confirmations, limit order, or change from one coin to another.

Airflow

Apache Airflow is an open-source workflow management platform. Airflow provides many plug-and-play operators ready to execute your tasks on Google Cloud Platform, Amazon Web Services, Microsoft Azure. Finally, the code is just python. You can write workflows with python code, more on airflow in upcoming articles. Please leave your comment if you need to know more about airflow.

Installation can be done using the below command. Check out more about airflow installation here.

pip install apache-airflow

Airflow dags and folder structure

Airflow uses a Directed Acyclic Graph(dags). As this is not an airflow tutorial, I will directly go into the folder structure and show how it works.

Folder structure

Currently, we needed a simple DAG to schedule it. However, we can write additional code for more features.

Airflow code

Here we will use a simple dag to make to schedule it. Python Operator will be used to call a python function with additional parameters.

Simple Dag code

You can also see the execution of airflow in the airflow portal. Below is my localhost portal, which shows the execution of the DAG. Red indicates the failed executions. I was trying to make some changes, and there was some typo in the code. That gave me some red dots.

Dag execution

Scheduling Frequently

As a next step, we need to perform this action frequently. Often one-time setup is to test or develop. Real-time setup and automated trading needs do not touch daily setup, and live systems come into the picture. Ideally, I like everything to be automated, so it does not require my intervention. In the below section, you can perform a bunch of scheduling operations.

You can even schedule for a minute by minute. However, I think it is a bit of overkill. Airflow can also be installed in the cloud for continuous execution.

Final thoughts

  1. We can create a real-time system for buying and selling cryptocurrencies
  2. Use sentiment analysis and make trading decisions
  3. Use the system in a real-time environment. 24x7 trading on crypto
  4. Create different sentiment models and use the best model.
  5. Create your own rules or logic for buy and sell

I am planning to write additional articles based on the comments and feedback on the article. Please check related articles

  1. 24x5 AI Stock Trading agent to predict stock prices | Live trading

2. Make money using NFT + AI | GAN image generation

Get Code

Please subscribe to my newsletter to get the free working code for my articles and other updates.

--

--