# Investing with Python: Future Returns

Download the accompanying IPython Notebook for this Tutorial from Github.

Last Tutorial, we outlined steps for calculating the Stochastic Oscillator.

In this Tutorial, we walk through calculating 5-day, 10-day, and 20-day future returns, from historical data.

Caculating 5-day, 10-day, and 20-day future returns will allow us to identify relationships between current technical indicators and future returns.

Let’s use Python to compute the 5-day, 10-day, and 20-day future returns.

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):

3.) Define function for calculating the 5-day future return.

``````def fiveday(close):
fiveday = ((close.shift(-5) - close) / close) * 100
return fiveday``````

4.) Define function for calculating 10-day future return.

``````def tenday(close):
tenday = ((close.shift(-10) - close) / close) * 100
return tenday``````

5.) Define function for calculating 20-day future return.

``````def twentyday(close):
twentyday = ((close.shift(-20) - close) / close) * 100
return twentyday``````

6.) Query daily close.

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

7.) Run daily close through fiveday, tenday, and twentday functions. Save series to new columns in dataframe.

``````df['5 day'] = fiveday(df['Close'])
df['10 day'] = tenday(df['Close'])
df['20 day'] = twentyday(df['Close'])
df = df.dropna()
df.tail()``````

8.) Plot average 5-day, 10-day, and 20-day future returns.

``````df2 = df[['5 day', '10 day', '20 day']].mean()
df2.plot(kind='bar')``````

There you have it! We calculated 5-day, 10-day, and 20-day future returns. 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):

def fiveday(close):
fiveday = ((close.shift(-5) - close) / close) * 100
return fiveday

def tenday(close):
tenday = ((close.shift(-10) - close) / close) * 100
return tenday

def twentyday(close):
twentyday = ((close.shift(-20) - close) / close) * 100
return twentyday

df = pd.DataFrame(get_stock('FB', '1/1/2016', '12/31/2016'))
df['5 day'] = fiveday(df['Close'])
df['10 day'] = tenday(df['Close'])
df['20 day'] = twentyday(df['Close'])
df = df.dropna()
df.tail()``````