Advertisement
Guest User

Untitled

a guest
Jun 20th, 2017
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.64 KB | None | 0 0
  1. def macd():
  2. # Initialize table, and clear contents from previous loop
  3. buy_table = []
  4. sell_table = []
  5. hold_table = []
  6. garbage_table = []
  7. buy_neg_macd_table = []
  8. hold_neg_macd_table = []
  9. red_rabbit = []
  10.  
  11. #Starts counter at zero to track iterations.
  12. counter_macd=0
  13.  
  14.  
  15. for stock in ticker_symbols: #Runs each ticker in watch list, names ticker stock
  16. print 'Now Processing: {}'.format(ticker_symbols[counter_macd])
  17. quandl.ApiConfig.api_key = 'zSjx8xHwjkqYQPddH64b'
  18. data = quandl.get_table('WIKI/PRICES', paginate = True, ticker = stock, qopts={'columns':['adj_close']})
  19. data_list = data['adj_close'].values.tolist()
  20. recent_data = data_list[-100:]
  21.  
  22. b = recent_data #Use this array if most recent data should be at end of array
  23. c = b[:-1]
  24.  
  25. reversed_b= np.fliplr([b])[0] #Use this array if most recent data should be at beginning of array
  26.  
  27. def ema(s, n):
  28. """returns an n period exponential moving average for the time series s
  29.  
  30. s is a list ordered from oldest (index 0) to most recent (index -1) n is an integer
  31.  
  32. returns a numeric array of the exponential moving average"""
  33.  
  34. s = np.asarray(s)
  35. ema = []
  36. j = 1
  37.  
  38. #get n sma first and calculate the next n period ema
  39. sma = sum(s[:(n)]/n)
  40. multiplier = 2 / float(1 + n)
  41. ema.append(sma)
  42.  
  43. #EMA(current) = ( (Price(current) - EMA(prev) ) x Multiplier) + EMA(prev)
  44. ema.append(( (s[n] - sma) * multiplier) + sma)
  45.  
  46. #now calculate the rest of the values
  47. for i in s[n+1:]:
  48. tmp = ( (i - ema[j]) * multiplier) + ema[j]
  49. j = j + 1
  50. ema.append(tmp)
  51.  
  52. return ema
  53. #Today's Data
  54. fast = ema(b, 12)
  55. slow = ema(b, 26)
  56.  
  57. fast1 = fast[-len(slow):]
  58. slow_array = np.asarray(slow)
  59. fast1_array= np.asarray(fast1)
  60.  
  61. #Yesterday's Data
  62. yesterday_fast = ema(c, 12)
  63. yesterday_slow = ema(c, 26)
  64.  
  65. yesterday_fast1 = yesterday_fast[-len(yesterday_slow):]
  66. yesterday_slow_array = np.asarray(yesterday_slow)
  67. yesterday_fast1_array= np.asarray(yesterday_fast1)
  68.  
  69. #Today's Calculations
  70. MACD = fast1_array-slow_array
  71. signal = ema(MACD,9)
  72. MACD = float(MACD[-1])
  73. signal = float(signal[-1])
  74.  
  75. #Yesterday's Calculations
  76. yesterday_MACD = yesterday_fast1_array-yesterday_slow_array
  77. yesterday_signal = ema(yesterday_MACD,9)
  78. yesterday_MACD = float(yesterday_MACD[-1])
  79. yesterday_signal = float(yesterday_signal[-1])
  80.  
  81. #Defining variables for buy/sell determination
  82. buy_today = MACD > signal and MACD > 0
  83. buy_today_neg_macd = MACD > signal and MACD<=0
  84. negative_buy_today = MACD > signal and MACD > 0
  85. buy_yesterday = yesterday_MACD > yesterday_signal and yesterday_MACD > 0
  86. buy_yesterday_neg_macd = yesterday_MACD > yesterday_signal and yesterday_MACD <= 0
  87.  
  88. #Bucketing which stock meets which criteria
  89. if buy_today is True and buy_yesterday is True:
  90. hold_table.append(stock)
  91. elif buy_today_neg_macd is True and buy_yesterday_neg_macd is True:
  92. hold_neg_macd_table.append(stock)
  93. elif buy_today_neg_macd is True and buy_yesterday_neg_macd is False:
  94. buy_neg_macd_table.append(stock)
  95. elif buy_today is False and buy_yesterday is False:
  96. garbage_table.append(stock)
  97. elif buy_today is True:
  98. buy_table.append(stock)
  99. elif buy_today is False:
  100. sell_table.append(stock)
  101. else:
  102. red_rabbit.append(stock)
  103.  
  104. #Update counter, and provide percentage complete
  105. counter_macd = counter_macd+1
  106. print 'You are ', 100*counter_macd/len(ticker_symbols),'% complete'
  107.  
  108.  
  109. print 'Buys:'
  110. print buy_table
  111.  
  112. print 'Buy, But Negative MACD:'
  113. print buy_neg_macd_table
  114.  
  115. print 'Sells:'
  116. print sell_table
  117.  
  118. print 'Holds:'
  119. print hold_table
  120.  
  121. print 'Hold Negative MACD:'
  122. print hold_neg_macd_table
  123.  
  124. print 'Garbage:'
  125. print garbage_table
  126.  
  127. print 'Red Rabit:'
  128. print red_rabbit
  129.  
  130.  
  131. import smtplib
  132. import time
  133.  
  134.  
  135. ######Email#######
  136. fromaddr = 'wessel.christopher@gmail.com'
  137. #To address is defined at beginning of code
  138.  
  139. SUBJECT = "Stock Report For: " + time.strftime("%x")
  140. text = "Buy: \n{}\n\nBuy But Negative MACD: \n{}\n\nSell: \n{}\n\nHold: \n{}\n\nHold Negative MACD:\n{}\n\nGarbage: \n{}\n\nStocks Being Watched:\n{}\n\n\nBrought to you by Chris Wessel and Dan Shriber".format(buy_table,buy_neg_macd_table, sell_table, hold_table,hold_neg_macd_table, garbage_table, ticker_symbols)
  141. msg = 'Subject: {}\n\n{}'.format(SUBJECT, text)
  142.  
  143. # Credentials
  144. username = 'wessel.christopher@gmail.com'
  145. password = 'hjqsgcnmglgmrvpu'
  146.  
  147. # The actual mail send
  148. server = smtplib.SMTP('smtp.gmail.com:587')
  149. server.starttls()
  150. server.login(username, password)
  151. server.sendmail(fromaddr, toaddrs, msg)
  152. server.quit()
  153.  
  154. return
  155.  
  156. #####Runs MACD on demand when uncommented#####
  157. macd()
  158.  
  159.  
  160. #Runs MACD function at start_time which is defined at the beginning of the code
  161. schedule.every().day.at(start_time).do(macd)
  162.  
  163. #Loops for eternity
  164. while True:
  165. schedule.run_pending()
  166. time.sleep(30) # wait 30 seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement