Advertisement
Guest User

Stock Quotes

a guest
Mar 8th, 2015
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. import json
  2. import urllib
  3.  
  4. class Trader:
  5.     IDENT = "https://fastquote.fidelity.com/service/quote/json?productid=embeddedquotes&symbols="
  6.     STQ = 'recentquote.txt'
  7.  
  8.     def __init__(self, stock):
  9.         self.writeToFile(stock)
  10.  
  11.     def checkStock(self, stock):
  12.         stock = stock.upper()
  13.         ident = self.IDENT + stock
  14.         info_request = urllib.urlopen(ident).read().replace('\n', '')[1:-1]
  15.         info = json.loads(info_request)['QUOTES'][stock]
  16.         company = info['NAME'].title()
  17.         percent = info['PCT_CHG_TODAY']
  18.         last = '%.2f' % float(info['LAST_PRICE'])
  19.         if percent >= 0:
  20.             sign = '+'
  21.         else:
  22.             sign = '-'
  23.         return (company, percent, last, sign)
  24.  
  25.     def writeToFile(self, stock):
  26.         c, p, l, s = self.checkStock(stock)
  27.         with open(self.STQ, 'w') as log:
  28.             text = '{0}: ({2} {3} {1}%)'.format(c, p, l, s)
  29.             log.write(text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement