Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import praw
- import pdb
- import re
- import os
- import time
- import pandas_datareader.data as web
- import numpy
- import openpyxl
- import time
- from datetime import datetime
- import requests
- import bs4
- import warnings
- warnings.simplefilter("ignore", ResourceWarning)
- TickerList = []
- CompanyNameList = []
- IPOYearList = []
- posts_replied_to = []
- #------------------------------------------------------------------#
- #import company names and tickers from Excel
- wb = openpyxl.load_workbook(filename = 'CompanyList_nondup2.xlsx')
- sheet = wb.active
- print('Getting info from cells...')
- for row in range(2, sheet.max_row + 1):
- Ticker = sheet['A' + str(row)].value
- CompanyName = sheet['B' + str(row)].value
- IPOYear = sheet['D' + str(row)].value
- TickerList.append(Ticker)
- CompanyNameList.append(CompanyName)
- IPOYearList.append(IPOYear)
- #-----------------------------------------------------------------#
- #Define the function to get the stocks info
- #Get current price using Requests and BS4
- def getCurrentPrice(ticker):
- res = requests.get('http://finance.yahoo.com/q?s=' + ticker)
- soup = bs4.BeautifulSoup(res.text, 'html.parser')
- ticker = ticker.lower()
- elems = soup.select('#yfs_l84_'+str(ticker))
- current_price = elems[0].getText()
- return current_price
- def getWeeklyChange(ticker, current_price):
- #Get historical prices using Pandas
- start = datetime(1900, 1, 1)
- today = datetime.now()
- hours = "%d"%(today.hour)
- minutes = '%d'%(today.minute)
- if int(hours) > 13 and int(minutes) > 00:
- end = "%d, %d, %d"%(today.year, today.month, today.day)
- else:
- end = "%d, %d, %d"%(today.year, today.month, today.day-1)
- f = web.DataReader(str(ticker), 'yahoo', start, end)
- #IF MONTH != 1
- if today.month != 1:
- if today.day > 7:
- try:
- try:
- week = "%d, %d, %d"%(today.year, today.month, today.day-7)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- try:
- week = "%d, %d, %d"%(today.year, today.month, today.day-8)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- try:
- week = "%d, %d, %d"%(today.year, today.month, today.day-9)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- try:
- week = "%d, %d, %d"%(today.year, today.month, today.day-10)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 7:
- try:
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+24)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- print('somethin')
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+23)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+22)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+21)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 6:
- try:
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+25)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+24)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+23)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+22)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 5:
- try:
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+26)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+25)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+24)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+23)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 4:
- try:
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+27)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+26)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+25)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+24)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 3:
- try:
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+28)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+27)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+26)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+25)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 2:
- try:
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+29)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+28)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+27)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+26)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 1:
- try:
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+28)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+27)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+26)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year, today.month-1, today.day+25)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- #IF MONTH == 1
- if today.month == 1:
- if today.day > 7:
- try:
- try:
- week = "%d, %d, %d"%(today.year, today.month, today.day-7)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- try:
- week = "%d, %d, %d"%(today.year, today.month, today.day-8)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- try:
- week = "%d, %d, %d"%(today.year, today.month, today.day-9)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- try:
- week = "%d, %d, %d"%(today.year, today.month, today.day-10)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 7:
- try:
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+24)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+23)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+22)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+21)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 6:
- try:
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+25)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+24)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+23)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+22)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 5:
- try:
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+26)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+25)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+24)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+23)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 4:
- try:
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+27)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+26)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+25)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+24)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 3:
- try:
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+28)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+27)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+26)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+25)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 2:
- try:
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+29)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+28)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+27)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+26)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- elif today.day == 1:
- try:
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+28)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+27)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+26)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- pass
- try:
- week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+25)
- weekly_price = f.ix[str(week)]['Adj Close']
- weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
- weekly_change = '{:.2f}'.format(weekly_change)
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- except:
- weekly_change = ''
- return weekly_change
- def getMonthlyChange(ticker, current_price):
- #Get historical prices using Pandas
- start = datetime(1900, 1, 1)
- today = datetime.now()
- hours = "%d"%(today.hour)
- minutes = '%d'%(today.minute)
- if int(hours) > 13 and int(minutes) > 00:
- end = "%d, %d, %d"%(today.year, today.month, today.day)
- else:
- end = "%d, %d, %d"%(today.year, today.month, today.day-1)
- f = web.DataReader(str(ticker), 'yahoo', start, end)
- try:
- try:
- if today.month == 1:
- month = "%d, %d, %d"%(today.year-1, today.month+11, today.day-1)
- monthly_price = f.ix[str(month)]['Adj Close']
- monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
- monthly_change = '{:.2f}'.format(monthly_change)
- return monthly_change
- else:
- month = "%d, %d, %d"%(today.year, today.month-1, today.day-1)
- monthly_price = f.ix[str(month)]['Adj Close']
- monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
- monthly_change = '{:.2f}'.format(monthly_change)
- return monthly_change
- except:
- try:
- if today.month == 1:
- month = "%d, %d, %d"%(today.year-1, today.month+11, today.day-2)
- monthly_price = f.ix[str(month)]['Adj Close']
- monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
- monthly_change = '{:.2f}'.format(monthly_change)
- return monthly_change
- else:
- month = "%d, %d, %d"%(today.year, today.month-1, today.day-2)
- monthly_price = f.ix[str(month)]['Adj Close']
- monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
- monthly_change = '{:.2f}'.format(monthly_change)
- return monthly_change
- except:
- try:
- if today.month == 1:
- month = "%d, %d, %d"%(today.year-1, today.month+11, today.day-3)
- monthly_price = f.ix[str(month)]['Adj Close']
- monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
- monthly_change = '{:.2f}'.format(monthly_change)
- return monthly_change
- else:
- month = "%d, %d, %d"%(today.year, today.month-1, today.day-3)
- monthly_price = f.ix[str(month)]['Adj Close']
- monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
- monthly_change = '{:.2f}'.format(monthly_change)
- return monthly_change
- except:
- try:
- if today.month == 1:
- month = "%d, %d, %d"%(today.year, today.month+11, today.day-4)
- monthly_price = f.ix[str(month)]['Adj Close']
- monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
- monthly_change = '{:.2f}'.format(monthly_change)
- return monthly_change
- else:
- month = "%d, %d, %d"%(today.year, today.month-1, today.day-4)
- monthly_price = f.ix[str(month)]['Adj Close']
- monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
- monthly_change = '{:.2f}'.format(monthly_change)
- return monthly_change
- except:
- pass
- return monthly_change
- except:
- monthly_change = ''
- return monthly_change
- def getYearlyChange(ticker, current_price):
- #Get historical prices using Pandas
- start = datetime(1900, 1, 1)
- today = datetime.now()
- hours = "%d"%(today.hour)
- minutes = '%d'%(today.minute)
- if int(hours) > 13 and int(minutes) > 00:
- end = "%d, %d, %d"%(today.year, today.month, today.day)
- else:
- end = "%d, %d, %d"%(today.year, today.month, today.day-1)
- f = web.DataReader(str(ticker), 'yahoo', start, end)
- try:
- try:
- year = "%d, %d, %d"%(today.year-1, today.month, today.day)
- yearly_price = f.ix[str(year)]['Adj Close']
- yearly_change = (float(current_price)-float(yearly_price))/float(yearly_price)*100
- yearly_change = '{:.2f}'.format(yearly_change)
- return yearly_change
- except:
- try:
- year = "%d, %d, %d"%(today.year-1, today.month, today.day-1)
- yearly_price = f.ix[str(year)]['Adj Close']
- yearly_change = (float(current_price)-float(yearly_price))/float(yearly_price)*100
- yearly_change = '{:.2f}'.format(yearly_change)
- return yearly_change
- except:
- year = "%d, %d, %d"%(today.year-1, today.month, today.day-2)
- yearly_price = f.ix[str(year)]['Adj Close']
- yearly_change = (float(current_price)-float(yearly_price))/float(yearly_price)*100
- yearly_change = '{:.2f}'.format(yearly_change)
- return yearly_change
- try:
- year = "%d, %d, %d"%(today.year-1, today.month, today.day-3)
- yearly_price = f.ix[str(year)]['Adj Close']
- yearly_change = (float(current_price)-float(yearly_price))/float(yearly_price)*100
- yearly_change = '{:.2f}'.format(yearly_change)
- return yearly_change
- except:
- try:
- year = "%d, %d, %d"%(today.year-1, today.month, today.day-4)
- yearly_price = f.ix[str(year)]['Adj Close']
- yearly_change = (float(current_price)-float(yearly_price))/float(yearly_price)*100
- yearly_change = '{:.2f}'.format(yearly_change)
- return yearly_change
- except:
- pass
- except:
- yearly_change = ''
- return yearly_change
- def current_output_text(ticker, CompanyName, current_price):
- current_price_text = ('Current price: $' + str(current_price) + '.')
- return current_price_text
- def weekly_output_text(ticker, weekly_change):
- weekly_change = float(weekly_change)
- if weekly_change > 0.0:
- change_marker = '▲ +'
- elif weekly_change < 0.0:
- change_marker = '▼'
- else:
- change_marker = 'even at'
- weekly_text = ('Over the past week, ' + ticker + ' is ' + change_marker + str(weekly_change) + '%.')
- return weekly_text
- def monthly_output_text(ticker, monthly_change):
- try:
- monthly_change = float(monthly_change)
- if monthly_change > 0.0:
- month_marker = '▲ +'
- elif monthly_change < 0.0:
- month_marker = '▼'
- else:
- month_marker = 'even at'
- except:
- pass
- if monthly_change != '':
- monthly_text = ('Over the past month, ' + ticker + ' is ' + month_marker + str(monthly_change) + '%.')
- else:
- monthly_text = ''
- return monthly_text
- def yearly_output_text(ticker, yearly_change):
- print(yearly_change)
- try:
- yearly_change = float(yearly_change)
- if yearly_change > 0.0:
- year_marker = '▲ +'
- elif yearly_change < 0.0:
- year_marker = '▼'
- else:
- year_marker = 'even at'
- except:
- pass
- if yearly_change != '':
- yearly_text = ('Over the past year, ' + ticker + ' is ' + year_marker + str(yearly_change) + '%.')
- else:
- yearly_text = ''
- return yearly_text
- #------------------------------------------------------------------#
- #Boot up the Reddit Bot by logging in and scanning subreddits
- REDDIT_USERNAME = 'StockTrackerBot' #YOUR USERNAME as string
- REDDIT_PASS = 'PythonEric!@#$' # YOUR PASSWORD as string
- user_agent = ("StockTracker v1")
- r = praw.Reddit(user_agent = user_agent)
- subredditList = ['business', 'stocks', 'technology', 'investing', 'news', 'worldnews']
- r.login(REDDIT_USERNAME, REDDIT_PASS, disable_warning=True)
- def job():
- if not os.path.isfile("posts_replied_to.txt"):
- posts_replied_to = []
- else:
- with open("posts_replied_to.txt", "r") as f:
- posts_replied_to = f.read()
- posts_replied_to = posts_replied_to.split("\n")
- posts_replied_to = list(filter(None, posts_replied_to))
- for subreddit in subredditList:
- print('Switchin to ' + subreddit + '...')
- subreddit = r.get_subreddit(subreddit)
- for submission in subreddit.get_new(limit=5):
- if submission.id not in posts_replied_to:
- for CompanyName, ticker in zip(CompanyNameList, TickerList):
- CompanyName = re.escape(str(CompanyName))
- CompanyName = '\\b'+CompanyName+'\\b'
- ticker = re.escape(str(ticker))
- if re.search(CompanyName, submission.title, re.IGNORECASE):
- CompanyName = CompanyName.replace('\\b', '')
- current_price = getCurrentPrice(ticker)
- weekly_change = getWeeklyChange(ticker, current_price)
- monthly_change = getMonthlyChange(ticker, current_price)
- yearly_change = getYearlyChange(ticker, current_price)
- current_text = current_output_text(ticker, CompanyName, current_price)
- print(current_text)
- weekly_text = weekly_output_text(ticker, weekly_change)
- print(weekly_text)
- monthly_text = monthly_output_text(ticker, monthly_change)
- print(monthly_text)
- yearly_text = yearly_output_text(ticker, yearly_change)
- if yearly_text != '':
- output = '**' + CompanyName + ' (' + ticker + ')**' + '\n\n' + current_text + '\n\n' + weekly_text + '\n\n' + monthly_text + '\n\n' + yearly_text + '\n\n' + '*****' + '\n\n' + 'Beep Boop, I am a bot. Message [HomerG](\/u\/HomerG) with any suggestions, ideas, death threats, etc.' + '\n\n' + 'To see source code and how I was made, click [here.](http:\/\/www.hofdata.com)'
- elif yearly_text == '':
- output = '**' + CompanyName + ' (' + ticker + ')**' + '\n\n' + current_text + '\n\n' + weekly_text + '\n\n' + monthly_text + '\n\n' + '*****' + '\n\n' + 'Beep Boop, I am a bot. Message [HomerG](\/u\/HomerG) with any suggestions, ideas, death threats, etc.' + '\n\n' + 'To see source code and how I was made, click [here.](http:\/\/www.hofdata.com)'
- elif monthly_text == '':
- output = '**' + CompanyName + ' (' + ticker + ')**' + '\n\n' + current_text + '\n\n' + weekly_text + '\n\n' + '*****' + '\n\n' + 'Beep Boop, I am a bot. Message [HomerG](\/u\/HomerG) with any suggestions, ideas, death threats, etc.' + '\n\n' + 'To see source code and how I was made, click [here.](http:\/\/www.hofdata.com)'
- elif weekly_text == '':
- output = '**' + CompanyName + ' (' + ticker + ')**' + '\n\n' + current_text + '\n\n' + '*****' + '\n\n' + 'Beep Boop, I am a bot. Message [HomerG](\/u\/HomerG) with any suggestions, ideas, death threats, etc.' + '\n\n' + 'To see source code and how I was made, click [here.](http:\/\/www.hofdata.com)'
- try:
- submission.add_comment(output)
- print ("Bot replying to : ", submission.title)
- if posts_replied_to != '':
- posts_replied_to.append(str(submission.id))
- except Exception as e:
- e = str(e)
- wait_time = [int(s) for s in e.split() if s.isdigit()]
- if wait_time != '':
- for item in wait_time:
- wait_time = item*60
- print('Sleeping for ' + str(item) + ' minutes...')
- time.sleep(wait_time)
- continue
- else:
- time.sleep(600)
- continue
- with open("posts_replied_to.txt", "w") as f:
- for post_id in posts_replied_to:
- f.write(str(post_id) + "\n")
- time.sleep(60)
- while True:
- job()
Add Comment
Please, Sign In to add comment