Python Tutorial: ROC

Download the accompanying IPython Notebook for this Tutorial from Github. 

Python streamlines tasks requiring multiple steps in a single block of code. For this reason, it is a great tool for querying and performing analysis on data.

Last Tutorial, we outlined steps for calculating Relative Strength Index (RSI).
In this Tutorial, we introduce a new technical indicator, the Rate of Change (ROC).

‘The only thing constant is change’

The Rate of Change (ROC) is a technical indicator of momentum that measures the percentage change in price between the current price and the price n periods in the past.

The Rate of Change (ROC) is calculated as follows:

ROC = ((Most recent closing price - Closing price n periods ago) / Closing price n periods ago) x 100

The Rate of Change (ROC) is classed as a momentum indicator because it measures strength of price momentum. For example, if a stock’s price at the close of trading today is 10, and the closing price five trading days prior was 7, then the Rate of Change (ROC) over that time frame is approximately 43, calculated as (10 – 7 / 7) x 100 = 42.85.

 Positive values indicate upward buying pressure or momentum, while negative values below zero indicate selling pressure or downward momentum. Increasing values in either direction, positive or negative, indicate increasing momentum, and decreasing values indicate waning momentum.

The Rate of Change (ROC) is also sometimes used to indicate overbought or oversold conditions for a security. Positive values that are greater than 30 are generally interpreted as indicating overbought conditions, while negative values lower than negative 30 indicate oversold conditions.

 Let’s use Python to compute the Rate of Change (ROC).

1.) Import modules.

import pandas as pd
import numpy as np
from pandas_datareader import data as web
import matplotlib.pyplot as plt
%matplotlib inline

2.) Define function for querying daily close.

def get_stock(stock,start,end):
     return web.DataReader(stock,'google',start,end)['Close']

3.) Define function for Rate of Change (ROC).

def ROC(df, n):  
    M = df.diff(n - 1)  
    N = df.shift(n - 1)  
    ROC = pd.Series(((M / N) * 100), name = 'ROC_' + str(n))   
    return ROC

How does the ROC function work?

3.a.) Function calculates difference in most recent closing price from closing price n periods ago. Sets the value to variable M.
#M = df.diff(n - 1)

3.b.) Function calculates closing price n periods ago. Sets the value to variable N.

#N = df.shift(n - 1)

3.c.) Function creates series called ROC that is ((M/N) * 100)

#ROC = pd.Series(((M / N) * 100), name = 'ROC_' + str(n))

3.d.) Function returns ROC

#return ROC

4.) Query daily close for ‘FB’ during 2016.

df = pd.DataFrame(get_stock('FB', '1/1/2016', '12/31/2016'))

5.) Run daily close through ROC function. Save series to new column in dataframe.

df['ROC'] = ROC(df['Close'], 12)
df.tail()

6.) Plot daily close and ROC.

df.plot(y=['Close'])
df.plot(y=['ROC'])

There you have it! We created our ROC indicator. Here’s the full code:

import pandas as pd
import numpy as np
from pandas_datareader import data as web
import matplotlib.pyplot as plt
%matplotlib inline

def get_stock(stock,start,end):
     return web.DataReader(stock,'google',start,end)['Close']
    
def ROC(df, n):  
    M = df.diff(n - 1)  
    N = df.shift(n - 1)  
    ROC = pd.Series(((M / N) * 100), name = 'ROC_' + str(n))   
    return ROC
    
df = pd.DataFrame(get_stock('FB', '1/1/2016', '12/31/2016'))
df['ROC'] = ROC(df['Close'], 12)
df.tail()

Python Tutorial: RSI

Download the accompanying IPython Notebook for this Tutorial from Github. 

Python streamlines tasks requiring multiple steps in a single block of code. For this reason, it is a great tool for querying and performing analysis on data.

Last Tutorial, we outlined steps for calculating Price Channels.

In this Tutorial, we introduce a new technical indicator, the Relative Strenght Index (RSI).

The Relative Strength Index (RSI) is a momentum indicator developed by noted technical analyst Welles Wilder, that compares the magnitude of recent gains and losses over a specified time period to measure speed and change of price movements of a security. It is primarily used to identify overbought or oversold conditions in the trading of an asset.

The Relative Strength Index (RSI) is calculated as follows:

RSI = 100 - 100 / (1 + RS)

RS = Average gain of last 14 trading days / Average loss of last 14 trading days

RSI values range from 0 to 100.

Traditional interpretation and usage of the RSI is that RSI values of 70 or above indicate that a security is becoming overbought or overvalued, and therefore may be primed for a trend reversal or corrective pullback in price. On the other side, an RSI reading of 30 or below is commonly interpreted as indicating an oversold or undervalued condition that may signal a trend change or corrective price reversal to the upside.

Let’s use Python to compute the Relative Strenght Index (RSI).

1.) Import modules (numpy included).

import pandas as pd
import numpy as np
from pandas_datareader import data as web
import matplotlib.pyplot as plt
%matplotlib inline

2.) Define function for querying daily close.

def get_stock(stock,start,end):
 return web.DataReader(stock,'google',start,end)['Close']

3.) Define function for RSI.

def RSI(series, period):
 delta = series.diff().dropna()
 u = delta * 0
 d = u.copy()
 u[delta > 0] = delta[delta > 0]
 d[delta < 0] = -delta[delta < 0]
 u[u.index[period-1]] = np.mean( u[:period] ) #first value is sum of avg gains
 u = u.drop(u.index[:(period-1)])
 d[d.index[period-1]] = np.mean( d[:period] ) #first value is sum of avg losses
 d = d.drop(d.index[:(period-1)])
 rs = pd.stats.moments.ewma(u, com=period-1, adjust=False) / \
 pd.stats.moments.ewma(d, com=period-1, adjust=False)
 return 100 - 100 / (1 + rs)

How does the RSI function work?

– 3.a.) Function creates two series of daily differences.

– 3.b.) One series is daily positive differences, i.e. gains.

– 3.c.) One series is daily negative difference, i.e. losses.

– 3.d.) Average daily positive differences for the period specified.

– 3.e.) Average daily negative difference for the period specified.

– 3.f.) RS is set equal to Exponential Moving Average of daily positive differences for the period sepcified / Exponential Moving Average of daily positive differences for the period sepcified.

– 3.g) Return 100 – 100 / (1 + RS)

 4.) Query daily close for ‘FB’ during 2016.

df = pd.DataFrame(get_stock('FB', '1/1/2016', '12/31/2016'))

5.) Run daily close through RSI function. Save series to new column in dataframe.

df['RSI'] = RSI(df['Close'], 14)
df.tail()

6.) Plot daily close and RSI.

df.plot(y=['Close'])
df.plot(y=['RSI'])

There you have it! We created our RSI indicator. Here’s the full code:

import pandas as pd
import numpy as np
from pandas_datareader import data as web
import matplotlib.pyplot as plt
%matplotlib inline

def get_stock(stock,start,end):
 return web.DataReader(stock,'google',start,end)['Close']
 
def RSI(series, period):
 delta = series.diff().dropna()
 u = delta * 0
 d = u.copy()
 u[delta > 0] = delta[delta > 0]
 d[delta < 0] = -delta[delta < 0]
 u[u.index[period-1]] = np.mean( u[:period] ) #first value is sum of avg gains
 u = u.drop(u.index[:(period-1)])
 d[d.index[period-1]] = np.mean( d[:period] ) #first value is sum of avg losses
 d = d.drop(d.index[:(period-1)])
 rs = pd.stats.moments.ewma(u, com=period-1, adjust=False) / \
 pd.stats.moments.ewma(d, com=period-1, adjust=False)
 return 100 - 100 / (1 + rs)
 
df = pd.DataFrame(get_stock('FB', '1/1/2016', '12/31/2016'))
df['RSI'] = RSI(df['Close'], 14)
df.tail()

If they Google You, Do you Win?

In a way, this election is a referendum on “do actions speak louder than words”, is what people do in the privacy of their internet browsing more reflective of their future behavior than what they tell pollsters? And while I have focused on twitter as a barometer of public opinion, there are other data sources that could signal the private thoughts and future actions of voters. The linked NYT article, “If they Google you, Do you Win?”, mentions using the Google queries “Trump Clinton” vs. “Clinton Trump” as signals of voter interest, with the respective queries reflecting bias towards the candidate listed first, i.e. “Clinton Trump” would reflect bias towards Clinton. Using this methodology, I researched Google trends for Battleground states to see where public opinion may be. The data are displayed below.

screen-shot-2016-11-03-at-5-49-10-pm

For the month of October 2016, “Trump Clinton” leads “Clinton Trump” in every state with the exception of Nevada.

You might say Trump is a polarizing celebrity, and for that reason he may be top of mind even if the individual plans to vote for Clinton. Okay, well then let’s penalize Trump 10%. Even in that case, ‘Factored “Trump Clinton”‘ indicates that, with the exception of Nevada, the three states that are in play are Virginia, Iowa, and Florida.

So while it is unclear in which direction the election will result, I believe we may be surprised at how close the results turn out to be, and that one thing we may remember is the discrepancy between what was reported in the polls leading up to the election and what actually happened online. We only have 4 days left to see which source provides a clearer signal of truth, and until then….Good luck to both candidates!

Sentiment Analysis: Donald Trump & Hillary Clinton Tweets, Oct 5 – Oct 11, 2015

Emotion drives our decision-making. By appealing to emotion, others can persuade us to make decisions. We experience this during political campaigns.

Donald Trump knows the power of emotion. A charismatic leader, Trump infuses his speeches with appeals to emotion. Sentiment analysis makes this clear.

Comparing Donald Trump and Hillary Clinton, I sampled tweets from their respective profiles, published between Monday October 5, 2015, and Sunday October 11, 2015. Using sentiment analysis, each tweet was given a score between -1, the most negative, and +1, the most positive. Plotted across the 7 days, the results are displayed below, with Trump in red and Hillary in blue.
sentimentanalysisfinal

Trump exhibits a noisier sentiment artifact. Trump has almost no tweets with a sentiment score of 0. Trump peaks at +1 nine times; Hillary peaks at +1 three times. Using statistics, we see with Trump there is a greater range of sentiment, with a tendency towards positive sentiment. The Median Sentiment for Donald Trump is 0.21, whereas the Median Sentiment for Hillary Clinton is 0. The Standard Deviation for Donald Trump is 0.39, whereas the Standard Deviation for Hillary Clinton is 0.30.

Donald Trump Hillary Clinton
Median Sentiment 0.21 0.00
Standard Deviation 0.39 0.30

boxandwhiskers

So why does this matter? Noisy sentiment drives engagement. 

The chart below shows average tweet engagement for the respective profiles, for tweets published between Monday October 5, 2015, and Sunday October 11, 2015.

Donald Trump Hillary Clinton
Avg. Retweets 1028 783
Avg. Favorites 2136 1196

– Trump received 1.31 retweets for every 1 retweet Clinton received

– Trump received 1.79 favorites for every 1 favorite Clinton received

So while some political analysts doubt Trump’s ability to win over the Republican Establishment, these findings clearly show Trump resonates with the people who have direct access to him on Twitter. Like television before it, social media has ushered in a new era of political campaign strategy, and we must ask, how will this new means of communication influence the selection of the Republican Presidential Nominee.