Update – Python Tutorial: MACD (Moving Average Convergence/Divergence)

Download the accompanying IPython Notebook for this Tutorial from Github. 

I received a question from Sam Khorsand about applying the Python Tutorial: MACD (Moving Average Convergence/Divergence) Tutorial   to multiple stocks. The code below will produce yesterday’s MACD Crossover for a list of stocks. Also, by adding ‘to_string(index = False’), you can clean up the date formatting. Enjoy!

import pandas as pd
from pandas_datareader import data as web
import matplotlib.pyplot as plt
import datetime as dt
%matplotlib inline

def MACD(stock, start, end):
    df = pd.DataFrame(web.DataReader(stock,'google',start,end)['Close'])
    df = df.reset_index()
    df['30 mavg'] = pd.rolling_mean(df['Close'], 30)
    df['26 ema'] = pd.ewma(df['Close'], span=26)
    df['12 ema'] = pd.ewma(df['Close'], span=12)
    df['MACD'] = (df['12 ema'] - df['26 ema'])
    df['Signal'] = pd.ewma(df['MACD'], span=9)
    df['Crossover'] = df['MACD'] - df['Signal']
    return stock, df['Date'][-1:].to_string(),df['Crossover'][-1:].mean()
    

stocks = ['FB', 'AAPL', 'GOOG', 'AMZN', 'TSLA']

d = []

for stock in stocks:
    stock, date, macd = MACD(stock, '1/1/2016', dt.datetime.today())
    d.append({'Stock':stock, 'Date':date, 'MACD':macd})
    
df2 = pd.DataFrame(d)
df2[['Date', 'Stock', 'MACD']]
Date Stock MACD
0 249 2017-09-20 FB -0.211440
1 249 2017-09-20 AAPL -0.828956
2 249 2017-09-20 GOOG -0.069812
3 249 2017-09-20 AMZN 1.028655
4 249 2017-09-20 TSLA 2.287354

 

Hillary: Age Perception Problem?

“I come from the ‘60s, a long time ago,” Hillary Clinton said at Saturday’s Democratic Presidential debate, in response to a question about student activism. The gaffe has mostly fallen on deaf ears.

Hillary Clinton is 68 years old. She was born October 26, 1947. Here is a visualization showing the age distribution of US Presidents upon assuming the Oval Office.

Screen Shot 2015-11-19 at 12.53.10 PM (2)

To date, only three US Presidents between the ages 65-69 have assumed the Oval Office. Once again, Hillary Clinton is 68 years old. She “come(s) from the ‘60s, a long time ago.”

Granted, the US population is aging. However, when selecting Presidents, this aging US population has trended towards younger Presidents.

Age upon assuming Oval Office: Barack Obama, 47 years old; George W. Bush, 54 years old; Bill Clinton, 46 years old.

Hillary wishes to dispel the age perception. HFA recently posted the SnapChat logo wearing a most recognizable pantsuit.

Screen Shot 2015-11-19 at 12.55.00 PM (2)

Of all the social networks, Snapchat skews youngest. According to Business Insider, 45% of Snapchat users are between the ages 18-24.Screen Shot 2015-11-19 at 12.44.44 PM (2)

Thus, a strategy has emerged: capture the young mind. Win the unspoiled voter, who is excited by the prospect of casting her first vote. Btw, Hillary uses Snapchat.

Further evidence of the strategy:

Twitter Banner displaying young prospective voters

Screen Shot 2015-11-19 at 12.53.58 PM (2)

HFA blog post appealing to youth and “a new age”

Screen Shot 2015-11-19 at 1.43.10 PM (2)

We should expect age to become a louder issue as the campaigns unfold. For reference, here are the ages of all the candidates, both Democrats and Republicans. Those between the ages 44- 60 are in bold.

Hilary Clinton, 68 years old

Bernie Sanders, 74 yeards old

Martin O’Malley, 52 years old

Donald Trump, 69 years old

Ben Carson, 64 years old

Marco Rubio, 44 years old

Ted Cruz, 44 years old

Jeb Bush, 62 years old

Carly Fiorina, 61 years old

John Kasich, 63 years old

Rand Paul, 52 years old

Republican Debate Nov 10, 2015: Google Search Correlation Matrix

Screen Shot 2015-11-10 at 1.37.37 PM (2)Where does the competition stand leading to tonight’s Republican Debate? Using Google Trends data from the past three months, I plotted a correlation matrix comparing search queries among the candidates.

republicancorrelationmatriz

The matrix reveals trends. Notably, Donald Trump searches are the least related to the other candidates. This supports Trump having created his own news cycle, apart from the Republican news cycle. As you may see in the time series plot, the Trump search artifact exhibits peaks and nuance not seen in the other artifacts. Furthermore, Trump searches are the least related to Ben Carson searches, with a 0.01 correlation. Other patterns to note, Ted Cruz and Marco Rubio searches are highly related, 0.94 correlation, and there is high inter-relatedness among searches for Jeb Bush, Carly Fiorina, and Rand Paul. Screen Shot 2015-11-10 at 12.55.19 PM (2)

Screen Shot 2015-11-10 at 12.56.07 PM (2)

It becomes clear that, aside from Trump and Carson, Google Searches for Republican Candidates hit inflection points around the debates. Furthermore, there is clustering among candidates, which can be considered as different news cycles:

  1. Donald Trump
  2. Ben Carson
  3. Ted Cruz, Marco Rubio
  4. Jeb Bush, Carly Fiorina, Rand Paul

We will see tonight if any of the Candidates makes a move to shake up the trends.

Twitter – US Presidential Campaigns, Nov 7, 2015: Heat Map

Screen Shot 2015-11-07 at 11.28.09 PM (2)

This heat map visualizes the US Presidential Campaigns on Twitter as of November 7, 2015. Darkest blue indicates the highest value. Grey indicates the lowest value.

Takeaways:

  • Bernie Sanders receives the most engagement per Tweet
  • Donald Trump Tweets the most of any candidate
  • Donald Trump and Hillary Clinton have the most Followers
  • Hillary Clinton, Ben Carson, Jeb Bush, and Chris Christie have curated new Twitter feeds for the 2016 election, as indicated by low Total Tweets
  • With the exception of Donald Trump, the Democrats rank highest across the board, indicating digital competency
  • With the exception of Carly Fiorina, all candidates Tweet a moderate amount per day