# Trump2Cash
This bot watches [Donald Trump's tweets](https://twitter.com/realDonaldTrump)
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
[@Trump2Cash](https://twitter.com/Trump2Cash).
*You can read more about the background story [here](http://trump2cash.biz).*
[![Trump2Cash](https://cdn-images-1.medium.com/max/1400/1*VbnhlLnZz0KvWO0QsM5Ihw.png)](http://trump2cash.biz)
The code is written in Python and is meant to run on a
[Google Compute Engine](https://cloud.google.com/compute/) instance. It uses the
[Twitter Streaming APIs](https://dev.twitter.com/streaming/overview) to get
notified whenever Trump tweets. The entity detection and sentiment analysis is
done using Google's
[Cloud Natural Language API](https://cloud.google.com/natural-language/) and the
[Wikidata Query Service](https://query.wikidata.org/) provides the company data.
The [TradeKing API](https://developers.tradeking.com/) does the stock trading.
The [`main`](main.py) module defines a callback where incoming tweets are
handled and starts streaming Trump's feed:
```python
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)
```
The core algorithms are implemented in the [`analysis`](analysis.py) and
[`trading`](trading.py) modules. The former finds mentions of companies in the
text of the tweet, figures out what their ticker symbol is, and assigns a
sentiment score to them. The latter chooses a trading strategy, which is either
buy now and sell at close or sell short now and buy to cover at close. The
[`twitter`](twitter.py) module deals with streaming and tweeting out the
summary.
Follow these steps to run the code yourself:
### 1. Create VM instance
Check out the [quickstart](https://cloud.google.com/compute/docs/quickstart-linux)
to create a Cloud Platform project and a Linux VM instance with Compute Engine,
then SSH into it for the steps below. The predefined
[machine type](https://cloud.google.com/compute/docs/machine-types) `g1-small`
(1 vCPU, 1.7 GB memory) seems to work well.
### 2. Set up auth
The authentication keys for the different APIs are read from shell environment
variables. Each service has different steps to obtain them.
#### Twitter
Log in to your [Twitter](https://twitter.com/) account and
[create a new application](https://apps.twitter.com/app/new). Under the *Keys
and Access Tokens* tab for [your app](https://apps.twitter.com/) you'll find
the *Consumer Key* and *Consumer Secret*. Export both to environment variables:
```shell
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](https://dev.twitter.com/oauth/overview). Then
export both to environment variables:
```shell
export TWITTER_ACCESS_TOKEN="<YOUR_ACCESS_TOKEN>"
export TWITTER_ACCESS_TOKEN_SECRET="<YOUR_ACCESS_TOKEN_SECRET>"
```
#### Google
Follow the
[Google Application Default Credentials instructions](https://developers.google.com/identity/protocols/application-default-credentials#howtheywork)
to create, download, and export a service account key.
```shell
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/credentials-file.json"
```
You also need to [enable the Cloud Natural Language API](https://cloud.google.com/natural-language/docs/getting-started#set_up_your_project)
for your Google Cloud Platform project.
#### TradeKing
Log in to your [TradeKing](https://www.tradeking.com/) account and
[create a new application](https://developers.tradeking.com/applications/CreateApplication).
Behind the *Details* button for
[your application](https://developers.tradeking.com/Applications) you'll find
the *Consumer Key*, *Consumer Secret*, *OAuth (Access) Token*, and *Oauth (Access)
Token Secret*. Export them all to environment variables:
```shell
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](https://investor.tradeking.com/Modules/Dashboard/dashboard.php)*:
```shell
export TRADEKING_ACCOUNT_NUMBER="<YOUR_ACCOUNT_NUMBER>"
```
### 3. Install dependencies
There are a few library dependencies, which you can install using
[pip](https://pip.pypa.io/en/stable/quickstart/):
```shell
$ pip install -r requirements.txt
```
### 4. Run the tests
Verify that everything is working as intended by running the tests with
[pytest](http://doc.pytest.org/en/latest/getting-started.html) using this
command:
```shell
$ export USE_REAL_MONEY=NO && pytest *.py --verbose
```
### 5. Run the benchmark
The [benchmark report](benchmark.md) 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:
```shell
$ ./benchmark.py > benchmark.md
```
### 6. Start the bot
Enable real orders that use your money:
```shell
$ export USE_REAL_MONEY=YES
```
Have the code start running in the background with this command:
```shell
$ 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.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
trump2cash-master.zip (69个子文件)
trump2cash-master
.gitignore 1KB
README.md 6KB
benchmark.md 27KB
LICENSE 11KB
analysis_tests.py 26KB
trading.py 22KB
main.py 3KB
requirements.txt 174B
twitter_tests.py 6KB
benchmark.py 10KB
logs_tests.py 1KB
twitter.py 12KB
trading_tests.py 15KB
market_data
F_20170124.txt 19KB
GM_20170124.txt 18KB
STT_20161213.txt 18KB
XOM_20161213.txt 20KB
WMT_20170117.txt 19KB
LMT_20170118.txt 18KB
BRK.B_20170324.txt 21KB
BRK.A_20170324.txt 6KB
STT_20170117.txt 18KB
PNC_20170118.txt 19KB
DAL_20170130.txt 19KB
M_20150716.txt 18KB
LMT_20161222.txt 18KB
ROK_20170130.txt 19KB
F_20170117.txt 19KB
BA_20161223.txt 19KB
BLK_20161213.txt 19KB
INTC_20170208.txt 19KB
F_20170118.txt 19KB
ROK_20170127.txt 18KB
BA_20161222.txt 19KB
GM_20170125.txt 19KB
BLK_20161212.txt 19KB
PNC_20161213.txt 19KB
LMT_20161212.txt 21KB
PNC_20161212.txt 19KB
CHTR_20170324.txt 18KB
STT_20161212.txt 18KB
JWN_20170208.txt 19KB
GM_20161230.txt 18KB
F_20170109.txt 19KB
M_20151204.txt 18KB
BA_20161206.txt 21KB
LMT_20161223.txt 20KB
F_20170125.txt 20KB
GM_20170117.txt 18KB
LMT_20170117.txt 19KB
UTX_20161130.txt 18KB
XOM_20161212.txt 20KB
BLK_20170118.txt 17KB
M_20151207.txt 18KB
DAL_20170127.txt 18KB
GM_20170118.txt 18KB
LMT_20161209.txt 19KB
BLK_20170117.txt 17KB
TM_20170105.txt 12KB
M_20151112.txt 20KB
PNC_20170117.txt 19KB
GM_20170103.txt 19KB
FCAU_20170109.txt 20KB
F_20170103.txt 19KB
F_20170104.txt 20KB
UTX_20161129.txt 19KB
BA_20161205.txt 20KB
analysis.py 10KB
logs.py 5KB
共 69 条
- 1
资源评论
Jonathanzc
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功