daily pastebin goal
1%
SHARE
TWEET

StockTrackerBot for Reddit

a guest Jan 28th, 2016 205 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. import praw
  3. import pdb
  4. import re
  5. import os
  6. import time
  7. import pandas_datareader.data as web
  8. import numpy
  9. import openpyxl
  10. import time
  11. from datetime import datetime
  12. import requests
  13. import bs4
  14. import warnings
  15. warnings.simplefilter("ignore", ResourceWarning)
  16.  
  17. TickerList = []
  18. CompanyNameList = []
  19. IPOYearList = []
  20. posts_replied_to = []
  21.  
  22. #------------------------------------------------------------------#
  23. #import company names and tickers from Excel
  24.  
  25. wb = openpyxl.load_workbook(filename = 'CompanyList_nondup2.xlsx')
  26. sheet = wb.active
  27.  
  28. print('Getting info from cells...')
  29. for row in range(2, sheet.max_row + 1):
  30.     Ticker       = sheet['A' + str(row)].value
  31.     CompanyName  = sheet['B' + str(row)].value
  32.     IPOYear      = sheet['D' + str(row)].value
  33.     TickerList.append(Ticker)
  34.     CompanyNameList.append(CompanyName)
  35.     IPOYearList.append(IPOYear)
  36.  
  37. #-----------------------------------------------------------------#
  38. #Define the function to get the stocks info
  39.  
  40. #Get current price using Requests and BS4
  41. def getCurrentPrice(ticker):
  42.     res = requests.get('http://finance.yahoo.com/q?s=' + ticker)
  43.     soup = bs4.BeautifulSoup(res.text, 'html.parser')
  44.     ticker = ticker.lower()
  45.     elems = soup.select('#yfs_l84_'+str(ticker))
  46.     current_price = elems[0].getText()
  47.     return current_price
  48.        
  49. def getWeeklyChange(ticker, current_price):
  50.     #Get historical prices using Pandas
  51.     start = datetime(1900, 1, 1)
  52.     today = datetime.now()
  53.     hours = "%d"%(today.hour)
  54.     minutes = '%d'%(today.minute)
  55.     if int(hours) > 13 and int(minutes) > 00:
  56.         end = "%d, %d, %d"%(today.year, today.month, today.day)
  57.     else:
  58.         end = "%d, %d, %d"%(today.year, today.month, today.day-1)
  59.     f = web.DataReader(str(ticker), 'yahoo', start, end)
  60.     #IF MONTH != 1
  61.     if today.month != 1:
  62.         if today.day > 7:
  63.             try:
  64.                 try:
  65.                     week = "%d, %d, %d"%(today.year, today.month, today.day-7)
  66.                     weekly_price = f.ix[str(week)]['Adj Close']
  67.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  68.                     weekly_change = '{:.2f}'.format(weekly_change)
  69.                     return weekly_change
  70.                 except:
  71.                     try:
  72.                         week = "%d, %d, %d"%(today.year, today.month, today.day-8)
  73.                         weekly_price = f.ix[str(week)]['Adj Close']
  74.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  75.                         weekly_change = '{:.2f}'.format(weekly_change)
  76.                         return weekly_change
  77.                     except:
  78.                         try:
  79.                             week = "%d, %d, %d"%(today.year, today.month, today.day-9)
  80.                             weekly_price = f.ix[str(week)]['Adj Close']
  81.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  82.                             weekly_change = '{:.2f}'.format(weekly_change)
  83.                             return weekly_change
  84.                         except:
  85.                             try:
  86.                                 week = "%d, %d, %d"%(today.year, today.month, today.day-10)
  87.                                 weekly_price = f.ix[str(week)]['Adj Close']
  88.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  89.                                 weekly_change = '{:.2f}'.format(weekly_change)
  90.                                 return weekly_change
  91.                             except:
  92.                                 weekly_change = ''
  93.                                 return weekly_change
  94.             except:
  95.                 weekly_change = ''
  96.                 return weekly_change
  97.         elif today.day == 7:
  98.             try:
  99.                 try:
  100.                     week = "%d, %d, %d"%(today.year, today.month-1, today.day+24)
  101.                     weekly_price = f.ix[str(week)]['Adj Close']
  102.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  103.                     weekly_change = '{:.2f}'.format(weekly_change)
  104.                     return weekly_change
  105.                 except:
  106.                     pass
  107.                     try:
  108.                         print('somethin')
  109.                         week = "%d, %d, %d"%(today.year, today.month-1, today.day+23)
  110.                         weekly_price = f.ix[str(week)]['Adj Close']
  111.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  112.                         weekly_change = '{:.2f}'.format(weekly_change)
  113.                         return weekly_change
  114.                     except:
  115.                         pass
  116.                         try:
  117.                             week = "%d, %d, %d"%(today.year, today.month-1, today.day+22)
  118.                             weekly_price = f.ix[str(week)]['Adj Close']
  119.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  120.                             weekly_change = '{:.2f}'.format(weekly_change)
  121.                             return weekly_change
  122.                         except:
  123.                             pass
  124.                             try:
  125.                                 week = "%d, %d, %d"%(today.year, today.month-1, today.day+21)
  126.                                 weekly_price = f.ix[str(week)]['Adj Close']
  127.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  128.                                 weekly_change = '{:.2f}'.format(weekly_change)
  129.                                 return weekly_change
  130.                             except:
  131.                                 weekly_change = ''
  132.                                 return weekly_change
  133.             except:
  134.                 weekly_change = ''
  135.                 return weekly_change        
  136.         elif today.day == 6:
  137.             try:
  138.                 try:
  139.                     week = "%d, %d, %d"%(today.year, today.month-1, today.day+25)
  140.                     weekly_price = f.ix[str(week)]['Adj Close']
  141.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  142.                     weekly_change = '{:.2f}'.format(weekly_change)
  143.                     return weekly_change
  144.                 except:
  145.                     pass
  146.                 try:
  147.                     week = "%d, %d, %d"%(today.year, today.month-1, today.day+24)
  148.                     weekly_price = f.ix[str(week)]['Adj Close']
  149.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  150.                     weekly_change = '{:.2f}'.format(weekly_change)
  151.                     return weekly_change
  152.                 except:
  153.                     pass
  154.                     try:
  155.                         week = "%d, %d, %d"%(today.year, today.month-1, today.day+23)
  156.                         weekly_price = f.ix[str(week)]['Adj Close']
  157.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  158.                         weekly_change = '{:.2f}'.format(weekly_change)
  159.                         return weekly_change
  160.                     except:
  161.                         pass
  162.                         try:
  163.                             week = "%d, %d, %d"%(today.year, today.month-1, today.day+22)
  164.                             weekly_price = f.ix[str(week)]['Adj Close']
  165.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  166.                             weekly_change = '{:.2f}'.format(weekly_change)
  167.                             return weekly_change
  168.                         except:
  169.                             weekly_change = ''
  170.                             return weekly_change
  171.             except:
  172.                 weekly_change = ''
  173.                 return weekly_change  
  174.         elif today.day == 5:
  175.             try:
  176.                 try:
  177.                     week = "%d, %d, %d"%(today.year, today.month-1, today.day+26)
  178.                     weekly_price = f.ix[str(week)]['Adj Close']
  179.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  180.                     weekly_change = '{:.2f}'.format(weekly_change)
  181.                     return weekly_change
  182.                 except:
  183.                     pass
  184.                     try:
  185.                         week = "%d, %d, %d"%(today.year, today.month-1, today.day+25)
  186.                         weekly_price = f.ix[str(week)]['Adj Close']
  187.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  188.                         weekly_change = '{:.2f}'.format(weekly_change)
  189.                         return weekly_change
  190.                     except:
  191.                         pass
  192.                         try:
  193.                             week = "%d, %d, %d"%(today.year, today.month-1, today.day+24)
  194.                             weekly_price = f.ix[str(week)]['Adj Close']
  195.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  196.                             weekly_change = '{:.2f}'.format(weekly_change)
  197.                             return weekly_change
  198.                         except:
  199.                             pass
  200.                             try:
  201.                                 week = "%d, %d, %d"%(today.year, today.month-1, today.day+23)
  202.                                 weekly_price = f.ix[str(week)]['Adj Close']
  203.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  204.                                 weekly_change = '{:.2f}'.format(weekly_change)
  205.                                 return weekly_change
  206.                             except:
  207.                                 weekly_change = ''
  208.                                 return weekly_change
  209.             except:
  210.                 weekly_change = ''
  211.                 return weekly_change  
  212.         elif today.day == 4:
  213.             try:
  214.                 try:
  215.                     week = "%d, %d, %d"%(today.year, today.month-1, today.day+27)
  216.                     weekly_price = f.ix[str(week)]['Adj Close']
  217.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  218.                     weekly_change = '{:.2f}'.format(weekly_change)
  219.                     return weekly_change
  220.                 except:
  221.                     pass
  222.                     try:
  223.                         week = "%d, %d, %d"%(today.year, today.month-1, today.day+26)
  224.                         weekly_price = f.ix[str(week)]['Adj Close']
  225.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  226.                         weekly_change = '{:.2f}'.format(weekly_change)
  227.                         return weekly_change
  228.                     except:
  229.                         pass
  230.                         try:
  231.                             week = "%d, %d, %d"%(today.year, today.month-1, today.day+25)
  232.                             weekly_price = f.ix[str(week)]['Adj Close']
  233.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  234.                             weekly_change = '{:.2f}'.format(weekly_change)
  235.                             return weekly_change
  236.                         except:
  237.                             pass
  238.                             try:
  239.                                 week = "%d, %d, %d"%(today.year, today.month-1, today.day+24)
  240.                                 weekly_price = f.ix[str(week)]['Adj Close']
  241.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  242.                                 weekly_change = '{:.2f}'.format(weekly_change)
  243.                                 return weekly_change
  244.                             except:
  245.                                 weekly_change = ''
  246.                                 return weekly_change
  247.             except:
  248.                 weekly_change = ''
  249.                 return weekly_change  
  250.         elif today.day == 3:
  251.             try:
  252.                 try:
  253.                     week = "%d, %d, %d"%(today.year, today.month-1, today.day+28)
  254.                     weekly_price = f.ix[str(week)]['Adj Close']
  255.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  256.                     weekly_change = '{:.2f}'.format(weekly_change)
  257.                     return weekly_change
  258.                 except:
  259.                     pass
  260.                     try:
  261.                         week = "%d, %d, %d"%(today.year, today.month-1, today.day+27)
  262.                         weekly_price = f.ix[str(week)]['Adj Close']
  263.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  264.                         weekly_change = '{:.2f}'.format(weekly_change)
  265.                         return weekly_change
  266.                     except:
  267.                         pass
  268.                         try:
  269.                             week = "%d, %d, %d"%(today.year, today.month-1, today.day+26)
  270.                             weekly_price = f.ix[str(week)]['Adj Close']
  271.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  272.                             weekly_change = '{:.2f}'.format(weekly_change)
  273.                             return weekly_change
  274.                         except:
  275.                             pass
  276.                             try:
  277.                                 week = "%d, %d, %d"%(today.year, today.month-1, today.day+25)
  278.                                 weekly_price = f.ix[str(week)]['Adj Close']
  279.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  280.                                 weekly_change = '{:.2f}'.format(weekly_change)
  281.                                 return weekly_change
  282.                             except:
  283.                                 weekly_change = ''
  284.                                 return weekly_change
  285.             except:
  286.                 weekly_change = ''
  287.                 return weekly_change  
  288.         elif today.day == 2:
  289.             try:
  290.                 try:
  291.                     week = "%d, %d, %d"%(today.year, today.month-1, today.day+29)
  292.                     weekly_price = f.ix[str(week)]['Adj Close']
  293.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  294.                     weekly_change = '{:.2f}'.format(weekly_change)
  295.                     return weekly_change
  296.                 except:
  297.                     pass
  298.                     try:
  299.                         week = "%d, %d, %d"%(today.year, today.month-1, today.day+28)
  300.                         weekly_price = f.ix[str(week)]['Adj Close']
  301.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  302.                         weekly_change = '{:.2f}'.format(weekly_change)
  303.                         return weekly_change
  304.                     except:
  305.                         pass
  306.                         try:
  307.                             week = "%d, %d, %d"%(today.year, today.month-1, today.day+27)
  308.                             weekly_price = f.ix[str(week)]['Adj Close']
  309.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  310.                             weekly_change = '{:.2f}'.format(weekly_change)
  311.                             return weekly_change
  312.                         except:
  313.                             pass
  314.                             try:
  315.                                 week = "%d, %d, %d"%(today.year, today.month-1, today.day+26)
  316.                                 weekly_price = f.ix[str(week)]['Adj Close']
  317.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  318.                                 weekly_change = '{:.2f}'.format(weekly_change)
  319.                                 return weekly_change
  320.                             except:
  321.                                 weekly_change = ''
  322.                                 return weekly_change
  323.             except:
  324.                 weekly_change = ''
  325.                 return weekly_change  
  326.         elif today.day == 1:
  327.             try:
  328.                 try:
  329.                     week = "%d, %d, %d"%(today.year, today.month-1, today.day+28)
  330.                     weekly_price = f.ix[str(week)]['Adj Close']
  331.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  332.                     weekly_change = '{:.2f}'.format(weekly_change)
  333.                     return weekly_change
  334.                 except:
  335.                     pass
  336.                     try:
  337.                         week = "%d, %d, %d"%(today.year, today.month-1, today.day+27)
  338.                         weekly_price = f.ix[str(week)]['Adj Close']
  339.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  340.                         weekly_change = '{:.2f}'.format(weekly_change)
  341.                         return weekly_change
  342.                     except:
  343.                         pass
  344.                         try:
  345.                             week = "%d, %d, %d"%(today.year, today.month-1, today.day+26)
  346.                             weekly_price = f.ix[str(week)]['Adj Close']
  347.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  348.                             weekly_change = '{:.2f}'.format(weekly_change)
  349.                             return weekly_change
  350.                         except:
  351.                             pass
  352.                             try:
  353.                                 week = "%d, %d, %d"%(today.year, today.month-1, today.day+25)
  354.                                 weekly_price = f.ix[str(week)]['Adj Close']
  355.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  356.                                 weekly_change = '{:.2f}'.format(weekly_change)
  357.                                 return weekly_change
  358.                             except:
  359.                                 weekly_change = ''
  360.                                 return weekly_change
  361.             except:
  362.                 weekly_change = ''
  363.                 return weekly_change
  364.            
  365.     #IF MONTH == 1
  366.     if today.month == 1:
  367.         if today.day > 7:
  368.             try:
  369.                 try:
  370.                     week = "%d, %d, %d"%(today.year, today.month, today.day-7)
  371.                     weekly_price = f.ix[str(week)]['Adj Close']
  372.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  373.                     weekly_change = '{:.2f}'.format(weekly_change)
  374.                     return weekly_change
  375.                 except:
  376.                     try:
  377.                         week = "%d, %d, %d"%(today.year, today.month, today.day-8)
  378.                         weekly_price = f.ix[str(week)]['Adj Close']
  379.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  380.                         weekly_change = '{:.2f}'.format(weekly_change)
  381.                         return weekly_change
  382.                     except:
  383.                         try:
  384.                             week = "%d, %d, %d"%(today.year, today.month, today.day-9)
  385.                             weekly_price = f.ix[str(week)]['Adj Close']
  386.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  387.                             weekly_change = '{:.2f}'.format(weekly_change)
  388.                             return weekly_change
  389.                         except:
  390.                             try:
  391.                                 week = "%d, %d, %d"%(today.year, today.month, today.day-10)
  392.                                 weekly_price = f.ix[str(week)]['Adj Close']
  393.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  394.                                 weekly_change = '{:.2f}'.format(weekly_change)
  395.                                 return weekly_change
  396.                             except:
  397.                                 weekly_change = ''
  398.                                 return weekly_change
  399.             except:
  400.                 weekly_change = ''
  401.                 return weekly_change
  402.         elif today.day == 7:
  403.             try:
  404.                 try:
  405.                     week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+24)
  406.                     weekly_price = f.ix[str(week)]['Adj Close']
  407.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  408.                     weekly_change = '{:.2f}'.format(weekly_change)
  409.                     return weekly_change
  410.                 except:
  411.                     pass
  412.                     try:
  413.                         week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+23)
  414.                         weekly_price = f.ix[str(week)]['Adj Close']
  415.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  416.                         weekly_change = '{:.2f}'.format(weekly_change)
  417.                         return weekly_change
  418.                     except:
  419.                         pass
  420.                         try:
  421.                             week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+22)
  422.                             weekly_price = f.ix[str(week)]['Adj Close']
  423.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  424.                             weekly_change = '{:.2f}'.format(weekly_change)
  425.                             return weekly_change
  426.                         except:
  427.                             pass
  428.                             try:
  429.                                 week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+21)
  430.                                 weekly_price = f.ix[str(week)]['Adj Close']
  431.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  432.                                 weekly_change = '{:.2f}'.format(weekly_change)
  433.                                 return weekly_change
  434.                             except:
  435.                                 weekly_change = ''
  436.                                 return weekly_change
  437.             except:
  438.                 weekly_change = ''
  439.                 return weekly_change        
  440.         elif today.day == 6:
  441.             try:
  442.                 try:
  443.                     week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+25)
  444.                     weekly_price = f.ix[str(week)]['Adj Close']
  445.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  446.                     weekly_change = '{:.2f}'.format(weekly_change)
  447.                     return weekly_change
  448.                 except:
  449.                     pass
  450.                 try:
  451.                     week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+24)
  452.                     weekly_price = f.ix[str(week)]['Adj Close']
  453.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  454.                     weekly_change = '{:.2f}'.format(weekly_change)
  455.                     return weekly_change
  456.                 except:
  457.                     pass
  458.                     try:
  459.                         week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+23)
  460.                         weekly_price = f.ix[str(week)]['Adj Close']
  461.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  462.                         weekly_change = '{:.2f}'.format(weekly_change)
  463.                         return weekly_change
  464.                     except:
  465.                         pass
  466.                         try:
  467.                             week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+22)
  468.                             weekly_price = f.ix[str(week)]['Adj Close']
  469.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  470.                             weekly_change = '{:.2f}'.format(weekly_change)
  471.                             return weekly_change
  472.                         except:
  473.                             weekly_change = ''
  474.                             return weekly_change
  475.             except:
  476.                 weekly_change = ''
  477.                 return weekly_change  
  478.         elif today.day == 5:
  479.             try:
  480.                 try:
  481.                     week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+26)
  482.                     weekly_price = f.ix[str(week)]['Adj Close']
  483.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  484.                     weekly_change = '{:.2f}'.format(weekly_change)
  485.                     return weekly_change
  486.                 except:
  487.                     pass
  488.                     try:
  489.                         week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+25)
  490.                         weekly_price = f.ix[str(week)]['Adj Close']
  491.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  492.                         weekly_change = '{:.2f}'.format(weekly_change)
  493.                         return weekly_change
  494.                     except:
  495.                         pass
  496.                         try:
  497.                             week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+24)
  498.                             weekly_price = f.ix[str(week)]['Adj Close']
  499.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  500.                             weekly_change = '{:.2f}'.format(weekly_change)
  501.                             return weekly_change
  502.                         except:
  503.                             pass
  504.                             try:
  505.                                 week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+23)
  506.                                 weekly_price = f.ix[str(week)]['Adj Close']
  507.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  508.                                 weekly_change = '{:.2f}'.format(weekly_change)
  509.                                 return weekly_change
  510.                             except:
  511.                                 weekly_change = ''
  512.                                 return weekly_change
  513.             except:
  514.                 weekly_change = ''
  515.                 return weekly_change  
  516.         elif today.day == 4:
  517.             try:
  518.                 try:
  519.                     week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+27)
  520.                     weekly_price = f.ix[str(week)]['Adj Close']
  521.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  522.                     weekly_change = '{:.2f}'.format(weekly_change)
  523.                     return weekly_change
  524.                 except:
  525.                     pass
  526.                     try:
  527.                         week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+26)
  528.                         weekly_price = f.ix[str(week)]['Adj Close']
  529.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  530.                         weekly_change = '{:.2f}'.format(weekly_change)
  531.                         return weekly_change
  532.                     except:
  533.                         pass
  534.                         try:
  535.                             week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+25)
  536.                             weekly_price = f.ix[str(week)]['Adj Close']
  537.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  538.                             weekly_change = '{:.2f}'.format(weekly_change)
  539.                             return weekly_change
  540.                         except:
  541.                             pass
  542.                             try:
  543.                                 week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+24)
  544.                                 weekly_price = f.ix[str(week)]['Adj Close']
  545.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  546.                                 weekly_change = '{:.2f}'.format(weekly_change)
  547.                                 return weekly_change
  548.                             except:
  549.                                 weekly_change = ''
  550.                                 return weekly_change
  551.             except:
  552.                 weekly_change = ''
  553.                 return weekly_change  
  554.         elif today.day == 3:
  555.             try:
  556.                 try:
  557.                     week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+28)
  558.                     weekly_price = f.ix[str(week)]['Adj Close']
  559.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  560.                     weekly_change = '{:.2f}'.format(weekly_change)
  561.                     return weekly_change
  562.                 except:
  563.                     pass
  564.                     try:
  565.                         week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+27)
  566.                         weekly_price = f.ix[str(week)]['Adj Close']
  567.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  568.                         weekly_change = '{:.2f}'.format(weekly_change)
  569.                         return weekly_change
  570.                     except:
  571.                         pass
  572.                         try:
  573.                             week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+26)
  574.                             weekly_price = f.ix[str(week)]['Adj Close']
  575.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  576.                             weekly_change = '{:.2f}'.format(weekly_change)
  577.                             return weekly_change
  578.                         except:
  579.                             pass
  580.                             try:
  581.                                 week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+25)
  582.                                 weekly_price = f.ix[str(week)]['Adj Close']
  583.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  584.                                 weekly_change = '{:.2f}'.format(weekly_change)
  585.                                 return weekly_change
  586.                             except:
  587.                                 weekly_change = ''
  588.                                 return weekly_change
  589.             except:
  590.                 weekly_change = ''
  591.                 return weekly_change  
  592.         elif today.day == 2:
  593.             try:
  594.                 try:
  595.                     week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+29)
  596.                     weekly_price = f.ix[str(week)]['Adj Close']
  597.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  598.                     weekly_change = '{:.2f}'.format(weekly_change)
  599.                     return weekly_change
  600.                 except:
  601.                     pass
  602.                     try:
  603.                         week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+28)
  604.                         weekly_price = f.ix[str(week)]['Adj Close']
  605.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  606.                         weekly_change = '{:.2f}'.format(weekly_change)
  607.                         return weekly_change
  608.                     except:
  609.                         pass
  610.                         try:
  611.                             week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+27)
  612.                             weekly_price = f.ix[str(week)]['Adj Close']
  613.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  614.                             weekly_change = '{:.2f}'.format(weekly_change)
  615.                             return weekly_change
  616.                         except:
  617.                             pass
  618.                             try:
  619.                                 week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+26)
  620.                                 weekly_price = f.ix[str(week)]['Adj Close']
  621.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  622.                                 weekly_change = '{:.2f}'.format(weekly_change)
  623.                                 return weekly_change
  624.                             except:
  625.                                 weekly_change = ''
  626.                                 return weekly_change
  627.             except:
  628.                 weekly_change = ''
  629.                 return weekly_change  
  630.         elif today.day == 1:
  631.             try:
  632.                 try:
  633.                     week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+28)
  634.                     weekly_price = f.ix[str(week)]['Adj Close']
  635.                     weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  636.                     weekly_change = '{:.2f}'.format(weekly_change)
  637.                     return weekly_change
  638.                 except:
  639.                     pass
  640.                     try:
  641.                         week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+27)
  642.                         weekly_price = f.ix[str(week)]['Adj Close']
  643.                         weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  644.                         weekly_change = '{:.2f}'.format(weekly_change)
  645.                         return weekly_change
  646.                     except:
  647.                         pass
  648.                         try:
  649.                             week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+26)
  650.                             weekly_price = f.ix[str(week)]['Adj Close']
  651.                             weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  652.                             weekly_change = '{:.2f}'.format(weekly_change)
  653.                             return weekly_change
  654.                         except:
  655.                             pass
  656.                             try:
  657.                                 week = "%d, %d, %d"%(today.year-1, today.month+11, today.day+25)
  658.                                 weekly_price = f.ix[str(week)]['Adj Close']
  659.                                 weekly_change = (float(current_price)-float(weekly_price))/float(weekly_price)*100
  660.                                 weekly_change = '{:.2f}'.format(weekly_change)
  661.                                 return weekly_change
  662.                             except:
  663.                                 weekly_change = ''
  664.                                 return weekly_change
  665.             except:
  666.                 weekly_change = ''
  667.                 return weekly_change
  668.  
  669. def getMonthlyChange(ticker, current_price):
  670.     #Get historical prices using Pandas
  671.     start = datetime(1900, 1, 1)
  672.     today = datetime.now()
  673.     hours = "%d"%(today.hour)
  674.     minutes = '%d'%(today.minute)
  675.     if int(hours) > 13 and int(minutes) > 00:
  676.         end = "%d, %d, %d"%(today.year, today.month, today.day)
  677.     else:
  678.         end = "%d, %d, %d"%(today.year, today.month, today.day-1)
  679.     f = web.DataReader(str(ticker), 'yahoo', start, end)
  680.     try:
  681.         try:
  682.             if today.month == 1:
  683.                 month = "%d, %d, %d"%(today.year-1, today.month+11, today.day-1)
  684.                 monthly_price = f.ix[str(month)]['Adj Close']
  685.                 monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
  686.                 monthly_change = '{:.2f}'.format(monthly_change)
  687.                 return monthly_change
  688.             else:
  689.                 month = "%d, %d, %d"%(today.year, today.month-1, today.day-1)
  690.                 monthly_price = f.ix[str(month)]['Adj Close']
  691.                 monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
  692.                 monthly_change = '{:.2f}'.format(monthly_change)
  693.                 return monthly_change
  694.         except:
  695.             try:
  696.                 if today.month == 1:
  697.                     month = "%d, %d, %d"%(today.year-1, today.month+11, today.day-2)
  698.                     monthly_price = f.ix[str(month)]['Adj Close']
  699.                     monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
  700.                     monthly_change = '{:.2f}'.format(monthly_change)
  701.                     return monthly_change
  702.                 else:
  703.                     month = "%d, %d, %d"%(today.year, today.month-1, today.day-2)
  704.                     monthly_price = f.ix[str(month)]['Adj Close']
  705.                     monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
  706.                     monthly_change = '{:.2f}'.format(monthly_change)
  707.                     return monthly_change
  708.             except:
  709.                 try:
  710.                     if today.month == 1:
  711.                         month = "%d, %d, %d"%(today.year-1, today.month+11, today.day-3)
  712.                         monthly_price = f.ix[str(month)]['Adj Close']
  713.                         monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
  714.                         monthly_change = '{:.2f}'.format(monthly_change)
  715.                         return monthly_change
  716.                     else:
  717.                         month = "%d, %d, %d"%(today.year, today.month-1, today.day-3)
  718.                         monthly_price = f.ix[str(month)]['Adj Close']
  719.                         monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
  720.                         monthly_change = '{:.2f}'.format(monthly_change)
  721.                         return monthly_change
  722.                 except:
  723.                     try:
  724.                         if today.month == 1:
  725.                             month = "%d, %d, %d"%(today.year, today.month+11, today.day-4)
  726.                             monthly_price = f.ix[str(month)]['Adj Close']
  727.                             monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
  728.                             monthly_change = '{:.2f}'.format(monthly_change)
  729.                             return monthly_change
  730.                         else:
  731.                             month = "%d, %d, %d"%(today.year, today.month-1, today.day-4)
  732.                             monthly_price = f.ix[str(month)]['Adj Close']
  733.                             monthly_change = (float(current_price)-float(monthly_price))/float(monthly_price)*100
  734.                             monthly_change = '{:.2f}'.format(monthly_change)
  735.                             return monthly_change                        
  736.                     except:
  737.                         pass
  738.                         return monthly_change
  739.     except:
  740.         monthly_change = ''
  741.         return monthly_change
  742.  
  743. def getYearlyChange(ticker, current_price):
  744.     #Get historical prices using Pandas
  745.     start = datetime(1900, 1, 1)
  746.     today = datetime.now()
  747.     hours = "%d"%(today.hour)
  748.     minutes = '%d'%(today.minute)
  749.     if int(hours) > 13 and int(minutes) > 00:
  750.         end = "%d, %d, %d"%(today.year, today.month, today.day)
  751.     else:
  752.         end = "%d, %d, %d"%(today.year, today.month, today.day-1)
  753.     f = web.DataReader(str(ticker), 'yahoo', start, end)
  754.     try:
  755.         try:
  756.             year = "%d, %d, %d"%(today.year-1, today.month, today.day)
  757.             yearly_price = f.ix[str(year)]['Adj Close']
  758.             yearly_change = (float(current_price)-float(yearly_price))/float(yearly_price)*100
  759.             yearly_change = '{:.2f}'.format(yearly_change)
  760.             return yearly_change
  761.         except:
  762.             try:
  763.                 year = "%d, %d, %d"%(today.year-1, today.month, today.day-1)
  764.                 yearly_price = f.ix[str(year)]['Adj Close']
  765.                 yearly_change = (float(current_price)-float(yearly_price))/float(yearly_price)*100
  766.                 yearly_change = '{:.2f}'.format(yearly_change)
  767.                 return yearly_change
  768.             except:
  769.                 year = "%d, %d, %d"%(today.year-1, today.month, today.day-2)
  770.                 yearly_price = f.ix[str(year)]['Adj Close']
  771.                 yearly_change = (float(current_price)-float(yearly_price))/float(yearly_price)*100
  772.                 yearly_change = '{:.2f}'.format(yearly_change)
  773.                 return yearly_change
  774.                 try:
  775.                     year = "%d, %d, %d"%(today.year-1, today.month, today.day-3)
  776.                     yearly_price = f.ix[str(year)]['Adj Close']
  777.                     yearly_change = (float(current_price)-float(yearly_price))/float(yearly_price)*100
  778.                     yearly_change = '{:.2f}'.format(yearly_change)
  779.                     return yearly_change
  780.                 except:
  781.                     try:
  782.                         year = "%d, %d, %d"%(today.year-1, today.month, today.day-4)
  783.                         yearly_price = f.ix[str(year)]['Adj Close']
  784.                         yearly_change = (float(current_price)-float(yearly_price))/float(yearly_price)*100
  785.                         yearly_change = '{:.2f}'.format(yearly_change)
  786.                         return yearly_change
  787.                     except:
  788.                         pass
  789.     except:
  790.         yearly_change = ''
  791.         return yearly_change
  792.  
  793. def current_output_text(ticker, CompanyName, current_price):
  794.     current_price_text = ('Current price: $' + str(current_price) + '.')
  795.     return current_price_text
  796.  
  797. def weekly_output_text(ticker, weekly_change):
  798.     weekly_change = float(weekly_change)
  799.     if weekly_change > 0.0:
  800.         change_marker = '▲ +'
  801.     elif weekly_change < 0.0:
  802.         change_marker = '&#x25BC;'
  803.     else:
  804.         change_marker = 'even at'
  805.        
  806.     weekly_text = ('Over the past week, ' + ticker + ' is ' + change_marker + str(weekly_change) + '%.')
  807.     return weekly_text
  808.  
  809. def monthly_output_text(ticker, monthly_change):
  810.     try:
  811.         monthly_change = float(monthly_change)
  812.         if monthly_change > 0.0:
  813.             month_marker = '&#x25B2; +'
  814.         elif monthly_change < 0.0:
  815.             month_marker = '&#x25BC;'
  816.         else:
  817.             month_marker = 'even at'
  818.     except:
  819.         pass
  820.  
  821.     if monthly_change != '':
  822.         monthly_text = ('Over the past month, ' + ticker + ' is ' + month_marker + str(monthly_change) + '%.')
  823.     else:
  824.         monthly_text = ''
  825.     return monthly_text
  826.  
  827. def yearly_output_text(ticker, yearly_change):
  828.     print(yearly_change)
  829.     try:
  830.         yearly_change = float(yearly_change)
  831.         if yearly_change > 0.0:
  832.             year_marker = '&#x25B2; +'
  833.         elif yearly_change < 0.0:
  834.             year_marker = '&#x25BC;'
  835.         else:
  836.             year_marker = 'even at'
  837.     except:
  838.         pass
  839.  
  840.     if yearly_change != '':
  841.         yearly_text = ('Over the past year, ' + ticker + ' is ' + year_marker + str(yearly_change) + '%.')
  842.     else:
  843.         yearly_text = ''
  844.     return yearly_text
  845.  
  846. #------------------------------------------------------------------#
  847. #Boot up the Reddit Bot by logging in and scanning subreddits
  848.  
  849. REDDIT_USERNAME = 'StockTrackerBot' #YOUR USERNAME as string
  850. REDDIT_PASS = 'PythonEric!@#$' # YOUR PASSWORD as string
  851.  
  852. user_agent = ("StockTracker v1")
  853. r = praw.Reddit(user_agent = user_agent)
  854. subredditList = ['business', 'stocks', 'technology', 'investing', 'news', 'worldnews']
  855.    
  856. r.login(REDDIT_USERNAME, REDDIT_PASS, disable_warning=True)
  857.  
  858. def job():
  859.     if not os.path.isfile("posts_replied_to.txt"):
  860.         posts_replied_to = []
  861.     else:
  862.         with open("posts_replied_to.txt", "r") as f:
  863.            posts_replied_to = f.read()
  864.            posts_replied_to = posts_replied_to.split("\n")
  865.            posts_replied_to = list(filter(None, posts_replied_to))
  866.            
  867.     for subreddit in subredditList:
  868.         print('Switchin to ' + subreddit + '...')
  869.         subreddit = r.get_subreddit(subreddit)
  870.         for submission in subreddit.get_new(limit=5):
  871.             if submission.id not in posts_replied_to:
  872.                 for CompanyName, ticker in zip(CompanyNameList, TickerList):
  873.                     CompanyName = re.escape(str(CompanyName))
  874.                     CompanyName = '\\b'+CompanyName+'\\b'
  875.                     ticker = re.escape(str(ticker))
  876.                     if re.search(CompanyName, submission.title, re.IGNORECASE):
  877.                         CompanyName = CompanyName.replace('\\b', '')
  878.                         current_price = getCurrentPrice(ticker)
  879.                         weekly_change = getWeeklyChange(ticker, current_price)
  880.                         monthly_change = getMonthlyChange(ticker, current_price)
  881.                         yearly_change = getYearlyChange(ticker, current_price)
  882.                         current_text = current_output_text(ticker, CompanyName, current_price)
  883.                         print(current_text)
  884.                         weekly_text = weekly_output_text(ticker, weekly_change)
  885.                         print(weekly_text)
  886.                         monthly_text = monthly_output_text(ticker, monthly_change)
  887.                         print(monthly_text)
  888.                         yearly_text = yearly_output_text(ticker, yearly_change)
  889.                         if yearly_text != '':
  890.                             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)'
  891.                         elif yearly_text == '':
  892.                             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)'
  893.                         elif monthly_text == '':
  894.                             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)'
  895.                         elif weekly_text == '':
  896.                             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)'
  897.                         try:
  898.                             submission.add_comment(output)
  899.                             print ("Bot replying to : ", submission.title)
  900.                             if posts_replied_to != '':
  901.                                 posts_replied_to.append(str(submission.id))
  902.                         except Exception as e:
  903.                             e = str(e)
  904.                             wait_time = [int(s) for s in e.split() if s.isdigit()]
  905.                             if wait_time != '':
  906.                                 for item in wait_time:
  907.                                     wait_time = item*60
  908.                                     print('Sleeping for ' + str(item) + ' minutes...')
  909.                                     time.sleep(wait_time)
  910.                                     continue
  911.                             else:
  912.                                 time.sleep(600)
  913.                                 continue
  914.                            
  915.         with open("posts_replied_to.txt", "w") as f:
  916.             for post_id in posts_replied_to:
  917.                 f.write(str(post_id) + "\n")
  918.         time.sleep(60)
  919.    
  920. while True:
  921.     job()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top