Notebook

This is my first notebook. My initial aims for this notebook is to import the last one years stock data for AAPL and plot the stock prices on a graph with 50d and 200d SMA. I will then add some EMA to the chart. This is all going to be practice for finding piviot points etc moving forward.

In [2]:
# Import our libraries

# This is for numerical processing
import numpy as np
import pandas as pd
# This is the library most commonly used for plotting in Python.
# Notice how we import it 'as' plt, this enables us to type plt
# rather than the full string every time.
import matplotlib.pyplot as plt
In [3]:
start = '2016-01-01'
end = '2016-10-12'
# set the dataframe to fetch APPL price from dates defined
#data = get_pricing(['AAPL'], start_date=start, end_date=end)
data = get_pricing('AAPL', start_date='2016-01-01', end_date='2016-10-12')
X = data['price']
X.tail() 
Out[3]:
2016-10-06 00:00:00+00:00    113.91
2016-10-07 00:00:00+00:00    114.03
2016-10-10 00:00:00+00:00    116.05
2016-10-11 00:00:00+00:00    116.34
2016-10-12 00:00:00+00:00    117.35
Name: price, dtype: float64

So looks like we have some data in a dataframe. Now am going to try plot it on a line graph using matplotlib

In [4]:
plt.plot(X.index, X.values)
plt.ylabel('Price $')
plt.legend(['AAPL']);

Now I would like to define my moving averages and store them somewhere so I can try plot them also.

In [5]:
# LETS CHECK SOME VALUES OF X WHICH IS OUR PANDAS DATA FRAME OR TIME AND PRICE
In [6]:
np.mean(X)
Out[6]:
102.19159289340098
In [7]:
np.std(X)
Out[7]:
6.68604305849514

Now we need to define our moving averages and plot them on the same chart as the stock price.

In [8]:
# Take the average of the last 50 days at each timepoint.
SMA50D = pd.rolling_mean(X, window=50)
SMA100D = pd.rolling_mean(X, window=100)

# LETS CHECK OUTPUT
SMA50D.tail()
SMA100D
Out[8]:
2016-01-04 00:00:00+00:00           NaN
2016-01-05 00:00:00+00:00           NaN
2016-01-06 00:00:00+00:00           NaN
2016-01-07 00:00:00+00:00           NaN
2016-01-08 00:00:00+00:00           NaN
2016-01-11 00:00:00+00:00           NaN
2016-01-12 00:00:00+00:00           NaN
2016-01-13 00:00:00+00:00           NaN
2016-01-14 00:00:00+00:00           NaN
2016-01-15 00:00:00+00:00           NaN
2016-01-19 00:00:00+00:00           NaN
2016-01-20 00:00:00+00:00           NaN
2016-01-21 00:00:00+00:00           NaN
2016-01-22 00:00:00+00:00           NaN
2016-01-25 00:00:00+00:00           NaN
2016-01-26 00:00:00+00:00           NaN
2016-01-27 00:00:00+00:00           NaN
2016-01-28 00:00:00+00:00           NaN
2016-01-29 00:00:00+00:00           NaN
2016-02-01 00:00:00+00:00           NaN
2016-02-02 00:00:00+00:00           NaN
2016-02-03 00:00:00+00:00           NaN
2016-02-04 00:00:00+00:00           NaN
2016-02-05 00:00:00+00:00           NaN
2016-02-08 00:00:00+00:00           NaN
2016-02-09 00:00:00+00:00           NaN
2016-02-10 00:00:00+00:00           NaN
2016-02-11 00:00:00+00:00           NaN
2016-02-12 00:00:00+00:00           NaN
2016-02-16 00:00:00+00:00           NaN
                                ...    
2016-08-31 00:00:00+00:00    100.407678
2016-09-01 00:00:00+00:00    100.370578
2016-09-02 00:00:00+00:00    100.327478
2016-09-06 00:00:00+00:00    100.283478
2016-09-07 00:00:00+00:00    100.269038
2016-09-08 00:00:00+00:00    100.250138
2016-09-09 00:00:00+00:00    100.212038
2016-09-12 00:00:00+00:00    100.195138
2016-09-13 00:00:00+00:00    100.215638
2016-09-14 00:00:00+00:00    100.276638
2016-09-15 00:00:00+00:00    100.381538
2016-09-16 00:00:00+00:00    100.487638
2016-09-19 00:00:00+00:00    100.644838
2016-09-20 00:00:00+00:00    100.832338
2016-09-21 00:00:00+00:00    101.030138
2016-09-22 00:00:00+00:00    101.239938
2016-09-23 00:00:00+00:00    101.415187
2016-09-26 00:00:00+00:00    101.601987
2016-09-27 00:00:00+00:00    101.800687
2016-09-28 00:00:00+00:00    102.012987
2016-09-29 00:00:00+00:00    102.206887
2016-09-30 00:00:00+00:00    102.403187
2016-10-03 00:00:00+00:00    102.603187
2016-10-04 00:00:00+00:00    102.829987
2016-10-05 00:00:00+00:00    103.055187
2016-10-06 00:00:00+00:00    103.255487
2016-10-07 00:00:00+00:00    103.460987
2016-10-10 00:00:00+00:00    103.675787
2016-10-11 00:00:00+00:00    103.897287
2016-10-12 00:00:00+00:00    104.118587
Name: price, dtype: float64

Now lets plot them on the same chart.

In [9]:
plt.plot(X.index, X.values)
plt.plot(SMA50D.index, SMA50D.values)
plt.plot(SMA100D.index, SMA100D.values)
plt.ylabel('Price')
plt.legend(['AAPL', '50d SMA', '100d SMA']);

Since we have SMA now it would be good to find out how to do EMA - so lets figure that out. From searching it seems we have to use the Ta-lib class to do this kind of stuff.

In [10]:
import talib
In [33]:
a = np.array([1.0,2,3,4,5])
talib.MA(a, 3) 
# Seems Talib needs np array as input and outputs an array
Out[33]:
array([ nan,  nan,   2.,   3.,   4.])
In [38]:
# Output EMA in ARRAY FORMAT
EMA10_ARRAY = talib.EMA(X.values, 10)
EMA13_ARRAY = talib.EMA(X.values, 13)
EMA21_ARRAY = talib.EMA(X.values, 21)
# make 4D array
EMA_4D = {'Time' : X.index,
            'EMA_10': EMA10_ARRAY,
            'EMA_13': EMA13_ARRAY,
            'EMA_21': EMA21_ARRAY} 
# Make a pandas data frame to make plotting easier
df = pd.DataFrame(EMA_4D)
# Set index of df to be the timestamp
df.set_index('Time', inplace=True)
# Check outout
df.tail()
Out[38]:
EMA_10 EMA_13 EMA_21
Time
2016-10-06 00:00:00+00:00 113.005260 112.762739 111.956070
2016-10-07 00:00:00+00:00 113.191576 112.943776 112.144609
2016-10-10 00:00:00+00:00 113.711290 113.387522 112.499645
2016-10-11 00:00:00+00:00 114.189237 113.809305 112.848768
2016-10-12 00:00:00+00:00 114.763921 114.315118 113.257971

Now lets plot out EMA along with SP and SMA

In [41]:
plt.plot(X.index, X.values)
plt.plot(SMA50D.index, SMA50D.values)
plt.plot(SMA100D.index, SMA100D.values)
plt.plot(df.index, df.EMA_10)
plt.plot(df.index, df.EMA_13)
plt.plot(df.index, df.EMA_21)
plt.ylabel('Price')
plt.legend(['AAPL', '50d SMA', '100d SMA','10d EMA','13d EMA','21d EMA'], loc=2);
In [ ]: