Automated Stock Trading with Twitter Sentiment Analysis

Automated Stock Trading with Twitter Sentiment Analysis

Imagine a robot that can predict the stock market based on the tweets of a world leader. Sounds like science fiction, but it’s a reality with the Trump2Cash project. This article will guide you through the process of setting up and running this robot, which uses natural language processing and sentiment analysis to make trades based on Donald Trump’s tweets.

Background

The Trump2Cash project was created in response to the volatility of the domestic stock market in 2019. The robot tracks Donald Trump’s tweets and uses sentiment analysis to determine his opinion on various companies. When a company is mentioned in a tweet, the robot analyzes the sentiment and executes trades based on the expected market reaction.

Project Overview

The Trump2Cash project is built using Python and can be run on Google Compute Engine instances. The core algorithm involves entity detection and sentiment analysis using Google’s Cloud Natural Language API, as well as Wikidata Query Service for company data and TradeKing API for stock trading.

Setting Up the Project

To run the Trump2Cash project, follow these steps:

  1. Create a Virtual Machine Instance: Quick start by using Google Compute Engine to create a cloud platform project and a Linux virtual machine instance. Perform the following steps via SSH:
    • Create a new virtual machine instance with a preferred price and performance to match the default machine type.
    • Alternatively, use a Dockerfile to build and run the project on Compute Engine or other platforms.
    • Build the Docker image: docker build -t trump2cash .
    • Tag the image: docker tag trump2cash gcr.io/<YOUR_GCP_PROJECT_NAME>/trump2cash
    • Push the image: docker push gcr.io/<YOUR_GCP_PROJECT_NAME>/trump2cash:latest
  2. Set Authentication: Obtain API keys for Twitter, Google, and TradeKing by following these steps:
    • Twitter: Log in to your Twitter account and create a new application. Export the Consumer Key, Consumer Secret, Access Token, and Access Token Secret as environment variables.
    • Google: Follow the instructions to create, download, and export a service account key. Export the GOOGLE_APPLICATION_CREDENTIALS environment variable.
    • TradeKing: Log in to your TradeKing account and create a new application. Export the Consumer Key, Consumer Secret, Access Token, and Access Token Secret as environment variables.
  3. Install Dependencies: Install the required libraries using pip: $ pip install -r requirements.txt
  4. Run Tests: Verify the normal operation of the project using pytest: $ export USE_REAL_MONEY=NO && pytest * .py -vv
  5. Run Benchmark: Run the benchmark to analyze the performance of the analysis and trading algorithms on historical data: $ python benchmark.py > benchmark.md
  6. Start Robot: Enable the use of real money orders: $ export USE_REAL_MONEY=YES and run the project in the background: $ nohup python main.py &

License

The Trump2Cash project is licensed under the Apache License, Version 2.0. You can obtain a copy of the license at http://www.apache.org/licenses/LICENSE-2.0.

Code Snippets

Here are some key code snippets from the project:

def twitter_callback(tweet):
    companies = analysis.find_companies(tweet)
    if companies:
        trading.make_trades(companies)
        twitter.tweet(companies, tweet)

if __name__ == "__main__":
    twitter.start_streaming(twitter_callback)

This code defines a callback function to process incoming tweets and execute trades based on the sentiment analysis.

Conclusion

The Trump2Cash project demonstrates the power of natural language processing and sentiment analysis in predicting stock market trends. By setting up and running this project, you can gain insights into the market and make informed investment decisions.