August 18, 2014

How to be your Own CNBC Analyst

We were having a cheeky discussion on lily today about how "You could probably be recognised as a market analyst if you just reported "Dow <rises/falls> on <top headline from news.google.com>" at the end of the day." I decided to put this to the test, and automate the talking heads out of a job by mining a random headline from the New York Times and the latest NASDAQ quote:

#!/Users/hdiwan/.virtualenvs/marketAnalyst/main.py
import cStringIO as StringIO
import csv
import feedparser
import logging
import random
import requests

if __name__ == '__main__':
    logging.basicConfig(level = logging.FATAL)
    quotes_ = requests.get('http://ichart.yahoo.com/table.csv?s=QQQ')
    quotes_ = quotes_.content
    quotes = StringIO.StringIO(quotes_)
    reader = list(csv.reader(quotes))
    todays_close = float(reader[1][3])
    yesterdays_close = float(reader[2][3])
    
    news = feedparser.parse('http://www.nytimes.com/roomfordebate/index.rss?category=business')
    entries = news.entries
    random.shuffle(entries)
    logging.info(news)
    story = entries[0]

    difference = todays_close - yesterdays_close
    reason = story.title
    print('NYSE change {} because of {}'.format(difference, reason))
python ~/.virtualenvs/marketAnalyst/main.py
NYSE change 0.05 because of Can the U.S. Still Be a Leader in the Middle East?

No comments:

Post a Comment