Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def macd():
- # Initialize table, and clear contents from previous loop
- buy_table = []
- sell_table = []
- hold_table = []
- garbage_table = []
- buy_neg_macd_table = []
- hold_neg_macd_table = []
- red_rabbit = []
- #Starts counter at zero to track iterations.
- counter_macd=0
- for stock in ticker_symbols: #Runs each ticker in watch list, names ticker stock
- print 'Now Processing: {}'.format(ticker_symbols[counter_macd])
- quandl.ApiConfig.api_key = 'zSjx8xHwjkqYQPddH64b'
- data = quandl.get_table('WIKI/PRICES', paginate = True, ticker = stock, qopts={'columns':['adj_close']})
- data_list = data['adj_close'].values.tolist()
- recent_data = data_list[-100:]
- b = recent_data #Use this array if most recent data should be at end of array
- c = b[:-1]
- reversed_b= np.fliplr([b])[0] #Use this array if most recent data should be at beginning of array
- def ema(s, n):
- """returns an n period exponential moving average for the time series s
- s is a list ordered from oldest (index 0) to most recent (index -1) n is an integer
- returns a numeric array of the exponential moving average"""
- s = np.asarray(s)
- ema = []
- j = 1
- #get n sma first and calculate the next n period ema
- sma = sum(s[:(n)]/n)
- multiplier = 2 / float(1 + n)
- ema.append(sma)
- #EMA(current) = ( (Price(current) - EMA(prev) ) x Multiplier) + EMA(prev)
- ema.append(( (s[n] - sma) * multiplier) + sma)
- #now calculate the rest of the values
- for i in s[n+1:]:
- tmp = ( (i - ema[j]) * multiplier) + ema[j]
- j = j + 1
- ema.append(tmp)
- return ema
- #Today's Data
- fast = ema(b, 12)
- slow = ema(b, 26)
- fast1 = fast[-len(slow):]
- slow_array = np.asarray(slow)
- fast1_array= np.asarray(fast1)
- #Yesterday's Data
- yesterday_fast = ema(c, 12)
- yesterday_slow = ema(c, 26)
- yesterday_fast1 = yesterday_fast[-len(yesterday_slow):]
- yesterday_slow_array = np.asarray(yesterday_slow)
- yesterday_fast1_array= np.asarray(yesterday_fast1)
- #Today's Calculations
- MACD = fast1_array-slow_array
- signal = ema(MACD,9)
- MACD = float(MACD[-1])
- signal = float(signal[-1])
- #Yesterday's Calculations
- yesterday_MACD = yesterday_fast1_array-yesterday_slow_array
- yesterday_signal = ema(yesterday_MACD,9)
- yesterday_MACD = float(yesterday_MACD[-1])
- yesterday_signal = float(yesterday_signal[-1])
- #Defining variables for buy/sell determination
- buy_today = MACD > signal and MACD > 0
- buy_today_neg_macd = MACD > signal and MACD<=0
- negative_buy_today = MACD > signal and MACD > 0
- buy_yesterday = yesterday_MACD > yesterday_signal and yesterday_MACD > 0
- buy_yesterday_neg_macd = yesterday_MACD > yesterday_signal and yesterday_MACD <= 0
- #Bucketing which stock meets which criteria
- if buy_today is True and buy_yesterday is True:
- hold_table.append(stock)
- elif buy_today_neg_macd is True and buy_yesterday_neg_macd is True:
- hold_neg_macd_table.append(stock)
- elif buy_today_neg_macd is True and buy_yesterday_neg_macd is False:
- buy_neg_macd_table.append(stock)
- elif buy_today is False and buy_yesterday is False:
- garbage_table.append(stock)
- elif buy_today is True:
- buy_table.append(stock)
- elif buy_today is False:
- sell_table.append(stock)
- else:
- red_rabbit.append(stock)
- #Update counter, and provide percentage complete
- counter_macd = counter_macd+1
- print 'You are ', 100*counter_macd/len(ticker_symbols),'% complete'
- print 'Buys:'
- print buy_table
- print 'Buy, But Negative MACD:'
- print buy_neg_macd_table
- print 'Sells:'
- print sell_table
- print 'Holds:'
- print hold_table
- print 'Hold Negative MACD:'
- print hold_neg_macd_table
- print 'Garbage:'
- print garbage_table
- print 'Red Rabit:'
- print red_rabbit
- import smtplib
- import time
- ######Email#######
- fromaddr = 'wessel.christopher@gmail.com'
- #To address is defined at beginning of code
- SUBJECT = "Stock Report For: " + time.strftime("%x")
- 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)
- msg = 'Subject: {}\n\n{}'.format(SUBJECT, text)
- # Credentials
- username = 'wessel.christopher@gmail.com'
- password = 'hjqsgcnmglgmrvpu'
- # The actual mail send
- server = smtplib.SMTP('smtp.gmail.com:587')
- server.starttls()
- server.login(username, password)
- server.sendmail(fromaddr, toaddrs, msg)
- server.quit()
- return
- #####Runs MACD on demand when uncommented#####
- macd()
- #Runs MACD function at start_time which is defined at the beginning of the code
- schedule.every().day.at(start_time).do(macd)
- #Loops for eternity
- while True:
- schedule.run_pending()
- time.sleep(30) # wait 30 seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement