This bot watches Donald Trump's tweets and waits for him to mention any publicly traded companies. When he does, it uses sentiment analysis to determine whether his opinions are positive or negative toward those companies. The bot then automatically executes trades on the relevant stocks according to the expected market reaction. It also tweets out a summary of its findings in real time at @TrumpCorrection.
TODO: embed sample tweet
The code is written in Python and uses APIs from Twitter, Google, and TradeKing to do it's thing. It is built to run in a Compute Engine instance. Follow these steps to run the code yourself:
The authentication keys for the different APIs are read from shell environment variables. Each service has different steps to obtain them.
Log in to your Twitter account and create a new application. Under the Keys and Access Tokens tab for your app you'll find the Consumer Key and Consumer Secret. Export both to environment variables:
export TWITTER_CONSUMER_KEY="<YOUR_CONSUMER_KEY>"
export TWITTER_CONSUMER_SECRET="<YOUR_CONSUMER_SECRET>"
If you want the tweets to come from the same account that owns the application, simply use the Access Token and Access Token Secret on the same page. If you want to tweet from a different account, follow the steps to obtain an access token. Then export both to environment variables:
export TWITTER_ACCESS_TOKEN="<YOUR_ACCESS_TOKEN>"
export TWITTER_ACCESS_TOKEN_SECRET="<YOUR_ACCESS_TOKEN_SECRET>"
Follow the Google Application Default Credentials instructions to create, download, and export a service account key.
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials-file.json"
You also need to enable the Cloud Natural Language API for your Google Cloud Platform project.
Log in to your TradeKing account and create a new application. Behind the Details button for your application you'll find the Consumer Key, Consumer Secret, OAuth (Access) Token, and Oauth (Access) Token Secret. Export them all to environment variables:
export TRADEKING_CONSUMER_KEY="<YOUR_CONSUMER_KEY>"
export TRADEKING_CONSUMER_SECRET="<YOUR_CONSUMER_SECRET>"
export TRADEKING_ACCESS_TOKEN="<YOUR_ACCESS_TOKEN>"
export TRADEKING_ACCESS_TOKEN_SECRET="<YOUR_ACCESS_TOKEN_SECRET>"
Also export your TradeKing account number, which you'll find under My Accounts:
export TRADEKING_ACCOUNT_NUMBER="<YOUR_ACCOUNT_NUMBER>"
There are a few library dependencies, which you can install using pip:
$ pip install -r requirements.txt
Verify that everything is working as intended by running the tests with pytest using this command:
$ export USE_REAL_MONEY=NO && pytest *.py --verbose
The benchmark report shows how the current implementation of the analysis and trading algorithms would have performed against historical data. You can run it again to benchmark any changes you may have made:
$ ./benchmark.py > benchmark.md
Enable real orders that use your money:
$ export USE_REAL_MONEY=YES
Have the code start running in the background with this command:
$ nohup ./main.py &
##License
Copyright 2017 Max Braun
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.