Skip to content

Instantly share code, notes, and snippets.

@slaght
Created September 8, 2016 02:45
Show Gist options
  • Save slaght/70c99170cb4886b9e3b08348238c9a9a to your computer and use it in GitHub Desktop.
Save slaght/70c99170cb4886b9e3b08348238c9a9a to your computer and use it in GitHub Desktop.
Tweet Stocks
import json
from textblob import TextBlob
linenum = 0
sentiment = []
Apple= []
AmericanExpress= []
BankofAmerica = []
Caterpillar = []
Cisco = []
Chevron= []
DuPont= []
Disney= []
GeneralElectric= []
GoldmanSachs = []
HomeDepot= []
IBM= []
Intel= []
JohnsonJohnson= []
JPMorgan= []
CocaCola = []
McDonald= []
M3= []
Merck= []
Microsoft= []
Nike= []
Pfizer= []
ProcterGamble= []
TravelersCompanies= []
UnitedHealth= []
UnitedTechnologies= []
Visa= []
Verizon= []
Walmart= []
ExxonMobil= []
with open("C:/Users/user/documents/data.txt") as f:
for line in f:
if line.strip():
tweet = json.loads(line)
try:
if (tweet["lang"]=="en"):
text = TextBlob(tweet["text"])
print (linenum)
linenum+=1
if ("Apple" in tweet["text"] or "$AAPL" in tweet["text"]):
Apple.append(text.sentiment)
if ("American Express" in tweet["text"] or "$AXP" in tweet["text"]):
AmericanExpress.append(text.sentiment)
if ("Bank of America" in tweet["text"] or "$BA" in tweet["text"]):
BankofAmerica.append(text.sentiment)
if ("Caterpillar" in tweet["text"] or "$CAT" in tweet["text"]):
Caterpillar.append(text.sentiment)
if ("Cisco" in tweet["text"] or "$CSCO" in tweet["text"]):
Cisco.append(text.sentiment)
if ("Chevron" in tweet["text"] or "$CVX" in tweet["text"]):
Chevron.append(text.sentiment)
if ("DuPont" in tweet["text"] or "$DD" in tweet["text"]):
DuPont.append(text.sentiment)
if ("Disney" in tweet["text"] or "$DIS" in tweet["text"]):
Disney.append(text.sentiment)
if ("General Electric" in tweet["text"] or "$GE" in tweet["text"]):
GeneralElectric.append(text.sentiment)
if ("Goldman Sachs" in tweet["text"] or "$GS" in tweet["text"]):
GoldmanSachs.append(text.sentiment)
if ("Home Depot" in tweet["text"] or "$HD" in tweet["text"]):
HomeDepot.append(text.sentiment)
if ("IBM" in tweet["text"] or "$IBM" in tweet["text"]):
IBM.append(text.sentiment)
if ("Intel" in tweet["text"] or "$INTC" in tweet["text"]):
Intel.append(text.sentiment)
if ("Johnson & Johnson" in tweet["text"] or "$JNJ" in tweet["text"]):
JohnsonJohnson.append(text.sentiment)
if ("JPMorgan" in tweet["text"] or "$JPM" in tweet["text"]):
JPMorgan.append(text.sentiment)
if ("Coca Cola" in tweet["text"] or "$KO" in tweet["text"]):
CocaCola.append(text.sentiment)
if ("McDonald's" in tweet["text"] or "$MCD" in tweet["text"]):
McDonald.append(text.sentiment)
if ("3M" in tweet["text"] or "$MMM" in tweet["text"]):
M3.append(text.sentiment)
if ("Merck" in tweet["text"] or "$MRK" in tweet["text"]):
Merck.append(text.sentiment)
if ("Microsoft" in tweet["text"] or "$MSFT" in tweet["text"]):
Microsoft.append(text.sentiment)
if ("Nike" in tweet["text"] or "$NKE" in tweet["text"]):
Nike.append(text.sentiment)
if ("Pfizer" in tweet["text"] or "$PFE" in tweet["text"]):
Pfizer.append(text.sentiment)
if ("Procter & Gamble" in tweet["text"] or "$PG" in tweet["text"]):
ProcterGamble.append(text.sentiment)
if ("Travelers Companies" in tweet["text"] or "$TRV" in tweet["text"]):
TravelersCompanies.append(text.sentiment)
if ("United Health" in tweet["text"] or "$UNH" in tweet["text"]):
UnitedHealth.append(text.sentiment)
if ("United Technologies" in tweet["text"] or "$UTX" in tweet["text"]):
UnitedTechnologies.append(text.sentiment)
if ("Visa" in tweet["text"] or "$V" in tweet["text"]):
Visa.append(text.sentiment)
if ("Verizon" in tweet["text"] or "$VZ" in tweet["text"]):
Verizon.append(text.sentiment)
if ("Walmart" in tweet["text"] or "$WMT" in tweet["text"]):
Walmart.append(text.sentiment)
if ("Exxon Mobil" in tweet["text"] or "$XOM" in tweet["text"]):
ExxonMobil.append(text.sentiment)
sentiment.append(text.sentiment)
except KeyError:
pass
print ("Apple tweets : "+str(len(Apple)))
print ("AmericanExpres tweets : "+str(len(AmericanExpress)))
print ("BankofAmerica tweets : "+str(len(BankofAmerica )))
print ("Caterpillar tweets : "+str(len(Caterpillar )))
print ("Cisco tweets : "+str(len(Cisco )))
print ("Chevron tweets : "+str(len(Chevron)))
print ("DuPont tweets : "+str(len(DuPont)))
print ("Disney tweets : "+str(len(Disney)))
print ("GeneralElectric tweets : "+str(len(GeneralElectric)))
print ("GoldmanSachs tweets : "+str(len(GoldmanSachs )))
print ("HomeDepot tweets : "+str(len(HomeDepot)))
print ("IBM tweets : "+str(len(IBM)))
print ("Intel tweets : "+str(len(Intel)))
print ("JohnsonJohnson tweets : "+str(len(JohnsonJohnson)))
print ("JPMorgan tweets : "+str(len(JPMorgan)))
print ("CocaCola tweets : "+str(len(CocaCola )))
print ("McDonald tweets : "+str(len(McDonald)))
print ("M3 tweets : "+str(len(M3)))
print ("Merck tweets : "+str(len(Merck)))
print ("Microsoft tweets : "+str(len(Microsoft)))
print ("Nike tweets : "+str(len(Nike)))
print ("Pfizer tweets : "+str(len(Pfizer)))
print ("ProcterGamble tweets : "+str(len(ProcterGamble)))
print ("TravelersCompanies tweets : "+str(len(TravelersCompanies)))
print ("UnitedHealth tweets : "+str(len(UnitedHealth)))
print ("UnitedTechnologies tweets : "+str(len(UnitedTechnologies)))
print ("Visa tweets : "+str(len(Visa)))
print ("Verizon tweets : "+str(len(Verizon)))
print ("Walmart tweets : "+str(len(Walmart)))
print ("ExxonMobil tweets : "+str(len(ExxonMobil)))
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from pylab import *
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in sentiment], [y.subjectivity for y in sentiment],alpha=0.1,s=1)
plt.xlabel("Polarity")
plt.ylabel("Subjectivity")
savefig('all.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Apple], [y.subjectivity for y in Apple],color='darkred',alpha=0.2,s=2)
plt.xlabel("Apple Polarity")
plt.ylabel("Apple Subjectivity")
savefig('apple.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in AmericanExpress], [y.subjectivity for y in AmericanExpress],color='deepskyblue',alpha=0.2,s=2)
plt.xlabel("AMEX Polarity")
plt.ylabel("AMEX Subjectivity")
savefig('americanexpress.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in BankofAmerica], [y.subjectivity for y in BankofAmerica],color='salmon',alpha=0.2,s=2)
plt.xlabel("BOA Polarity")
plt.ylabel("BOA Subjectivity")
savefig('bankofamerica.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Caterpillar], [y.subjectivity for y in Caterpillar],color='gold',alpha=0.2,s=2)
plt.xlabel("Caterpillar Polarity")
plt.ylabel("Caterpillar Subjectivity")
savefig('caterpillar.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Cisco], [y.subjectivity for y in Cisco],color='dodgerblue',alpha=0.2,s=2)
plt.xlabel("Cisco Polarity")
plt.ylabel("Cisco Subjectivity")
savefig('cisco.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Chevron], [y.subjectivity for y in Chevron],color='tomato',alpha=0.2,s=2)
plt.xlabel("Chevron Polarity")
plt.ylabel("Chevron Subjectivity")
savefig('chevron.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in DuPont], [y.subjectivity for y in DuPont],color='crimson',alpha=0.2,s=2)
plt.xlabel("DuPont Polarity")
plt.ylabel("DuPont Subjectivity")
savefig('dupont.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Disney], [y.subjectivity for y in Disney],color='royalblue',alpha=0.2,s=2)
plt.xlabel("Disney Polarity")
plt.ylabel("Disney Subjectivity")
savefig('disney.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in GeneralElectric], [y.subjectivity for y in GeneralElectric],color='lavender',alpha=0.2,s=2)
plt.xlabel("GE Polarity")
plt.ylabel("GE Subjectivity")
savefig('generalelectric.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in GoldmanSachs], [y.subjectivity for y in GoldmanSachs],color='goldenrod',alpha=0.2,s=2)
plt.xlabel("GS Polarity")
plt.ylabel("GS Subjectivity")
savefig('goldmansachs.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in HomeDepot], [y.subjectivity for y in HomeDepot],color='orangered',alpha=0.2,s=2)
plt.xlabel("HD Polarity")
plt.ylabel("HD Subjectivity")
savefig('homedepot.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in IBM], [y.subjectivity for y in IBM],color='mediumblue',alpha=0.2,s=2)
plt.xlabel("IBM Polarity")
plt.ylabel("IBM Subjectivity")
savefig('ibm.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Intel], [y.subjectivity for y in Intel],color='cyan',alpha=0.2,s=2)
plt.xlabel("Intel Polarity")
plt.ylabel("Intel Subjectivity")
savefig('intel.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in JohnsonJohnson], [y.subjectivity for y in JohnsonJohnson],color='indianred',alpha=0.2,s=2)
plt.xlabel("J&J Polarity")
plt.ylabel("J&J Subjectivity")
savefig('johnsonandjohnson.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in JPMorgan], [y.subjectivity for y in JPMorgan],color='firebrick',alpha=0.2,s=2)
plt.xlabel("JPM Polarity")
plt.ylabel("JPM Subjectivity")
savefig('jpmorgan.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in CocaCola], [y.subjectivity for y in CocaCola],color='red',alpha=0.2,s=2)
plt.xlabel("Coca Cola Polarity")
plt.ylabel("Coca Cola Subjectivity")
savefig('cocacola.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in McDonald], [y.subjectivity for y in McDonald],color='yellow',alpha=0.2,s=2)
plt.xlabel("McDs Polarity")
plt.ylabel("McDs Subjectivity")
savefig('mcdonalds.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in M3], [y.subjectivity for y in M3],color='darkblue',alpha=0.2,s=2)
plt.xlabel("3M Polarity")
plt.ylabel("3M Subjectivity")
savefig('mmm.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Merck], [y.subjectivity for y in Merck],color='lime',alpha=0.2,s=2)
plt.xlabel("Merck Polarity")
plt.ylabel("Merck Subjectivity")
savefig('merck.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Microsoft], [y.subjectivity for y in Microsoft],color='turquoise',alpha=0.2,s=2)
plt.xlabel("Microsoft Polarity")
plt.ylabel("Microsoft Subjectivity")
savefig('microsoft.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Nike], [y.subjectivity for y in Nike],color='peru',alpha=0.2,s=2)
plt.xlabel("Nike Polarity")
plt.ylabel("Nike Subjectivity")
savefig('nike.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Pfizer], [y.subjectivity for y in Pfizer],color='darkseagreen',alpha=0.2,s=2)
plt.xlabel("Pfizer Polarity")
plt.ylabel("Pfizer Subjectivity")
savefig('pfizer.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in ProcterGamble], [y.subjectivity for y in ProcterGamble],color='olive',alpha=0.2,s=2)
plt.xlabel("PG Polarity")
plt.ylabel("PG Subjectivity")
savefig('proctergamble.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in TravelersCompanies], [y.subjectivity for y in TravelersCompanies],color='greenyellow',alpha=0.2,s=2)
plt.xlabel("TC Polarity")
plt.ylabel("TC Subjectivity")
savefig('travelerscompanies.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in UnitedHealth], [y.subjectivity for y in UnitedHealth],color='indigo',alpha=0.2,s=2)
plt.xlabel("UH Polarity")
plt.ylabel("UH Subjectivity")
savefig('unitedhealth.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in UnitedTechnologies], [y.subjectivity for y in UnitedTechnologies],color='orange',alpha=0.2,s=2)
plt.xlabel("UT Polarity")
plt.ylabel("UT Subjectivity")
savefig('unitedtechnologies.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Visa], [y.subjectivity for y in Visa],color='steelblue',alpha=0.2,s=2)
plt.xlabel("Visa Polarity")
plt.ylabel("Visa Subjectivity")
savefig('visa.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Verizon], [y.subjectivity for y in Verizon],color='maroon',alpha=0.2,s=2)
plt.xlabel("Verizon Polarity")
plt.ylabel("Verizon Subjectivity")
savefig('verizon.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in Walmart], [y.subjectivity for y in Walmart],color='silver',alpha=0.2,s=2)
plt.xlabel("Walmart Polarity")
plt.ylabel("Walmart Subjectivity")
savefig('walmart.png', bbox_inches='tight')
plt.figure(figsize=(20,10))
plt.axis([-1,1,0,1])
plt.scatter([x.polarity for x in ExxonMobil], [y.subjectivity for y in ExxonMobil],color='grey',alpha=0.2,s=2)
plt.xlabel("Exxon Mobil Polarity")
plt.ylabel("Exxon Mobil Subjectivity")
savefig('exxonmobil.png', bbox_inches='tight')
#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
#Variables that contains the user credentials to access Twitter API
access_token = "127325833-VAT4nTRCW9ibAxOijKT1Yrc9aIA1pwRVAkHjRmyg"
access_token_secret = "Clr0shMcklN1lA3tm1uQouS0XzIyMxYJiRbwFFWP992HS"
consumer_key = "I1LnFAczZG4BOVWH5izvUDQsb"
consumer_secret = "UoaRvjP0yEEOmDCv4TQnW3kysDhmVwajZF0uBeztjrENmyVlQ1"
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
data_json = json.loads(data)
try:
if (data_json["place"]["country_code"] != "US"):
return True
if (data_json["lang"] != "en"):
return True
except:
pass
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
US30 = ['$AAPL', 'Apple',
'$AXP', 'American Express',
'$BA', 'Bank of America',
'$CAT', 'Caterpillar',
'$CSCO', 'Cisco',
'$CVX', 'Chevron',
'$DD', 'DuPont',
'$DIS', 'Disney',
'$GE', 'General Electric',
'$GS', 'Goldman Sachs',
'$HD', 'Home Depot',
'$IBM', 'IBM',
'$INTC', 'Intel',
'$JNJ', 'Johnson & Johnson',
'$JPM', 'JPMorgan',
'$KO', 'Coca Cola',
'$MCD', "McDonald's",
'$MMM', '3M',
'$MRK', 'Merck',
'$MSFT', 'Microsoft',
'$NKE', 'Nike',
'$PFE', 'Pfizer',
'$PG', 'Procter & Gamble',
'$TRV', 'Travelers Companies',
'$UNH', 'United Health',
'$UTX', 'United Technologies',
'$V', 'Visa',
'$VZ', 'Verizon',
'$WMT', 'Walmart',
'$XOM', 'Exxon Mobil']
stream.filter(track=US30)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment