fromstocktalkimportstreaming# Credentials to access Twitter API API_KEY='XXXXXXXXXX'API_SECRET='XXXXXXXXXX'ACCESS_TOKEN='XXXXXXXXXX'ACCESS_TOKEN_SECRET='XXXXXXXXXX'credentials= [API_KEY, API_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET]
# First element must be ticker/name, proceeding elements are extra queriesTSLA= ['TSLA', 'Tesla']
SNAP= ['SNAP', 'Snapchat']
AAPL= ['AAPL', 'Apple']
AMZN= ['AMZN', 'Amazon']
# Variablestickers= [TSLA,SNAP,AAPL,AMZN] # Used for identification purposesqueries=TSLA+SNAP+AAPL+AMZN# Filters tweets containing one or more query refresh=30# Process and log data every 30 seconds# Create a folder to collect logs and temporary filespath="/Users/Anthony/Desktop/Data/"streaming(credentials, tickers, queries, refresh, path, \
realtime=True, logTracker=True, logTweets=True, logSentiment=True, debug=True)
Realtime Visualization
fromstocktalkimportvisualize# Make sure these variables are consistent with streaming.pytickers= ['TSLA','SNAP','AAPL','AMZN']
refresh=30path="/Users/Anthony/Desktop/Data/"visualize(tickers, refresh, path)
'''Steps to run local bokeh server1. Make sure streaming.py is running...2. Traverse in console to the directory containing visualize.py3. python -m bokeh serve --show visualize.py'''# Note: Volume is the thick blue line while sentiment is the thin white line
TSLA_Tweets.txt
datetime,tweet,sentiment
03/01/2017 10:30:02,#Tesla zeroing in market with strong relations,0.54
03/01/2017 10:30:03,$TSLA needs 8 Billion for Supercharger network,0.0
03/01/2017 10:30:03,#Tesla grossing high yet still losing money,-0.32
03/01/2017 10:30:03,Tesla's soon to be as affordable as gas-powered cars,0.11
03/01/2017 10:30:05,The technical reason why Tesla shares could soon rise,0.42
Underlying Features
Text Processing
textOne="@TeslaMotors shares jump as shipments more than double! #winning"print(process(textOne))
textTwo="Tesla announces its best sales quarter: http://trib.al/RbTxvSu $TSLA"print(process(textTwo))
textThree="Tesla $TSLA reports deliveries of 24500, above most views."print(process(textThree))
shares jump as shipments more than double winning
tesla announces its best sales quarter
tesla reports deliveries of number above most views
Sentiment Analysis
textOne="shares jump as shipments more than double winning"print(sentiment(textOne))
textTwo="tesla reports deliveries of number above most views"print(sentiment(textTwo))
textThree="not looking good for tesla competition on the rise"print(sentiment(textThree))