In [1]:
import pandas as pd
from pandas import Series,DataFrame
import numpy as np

# For Visualization
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline

from datetime import datetime, timedelta

import pandas.io.data as web

In [2]:
end = datetime.now()
start = end + timedelta(weeks=-52)

In [3]:
ctx = web.DataReader('^AXJO', 'yahoo', start, end)

In [4]:
ctx['Adj Close'].plot(legend=True,figsize=(15,7))


Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x10a387e48>

In [5]:
# Calc moving averages

periods = [20, 50, 100]

for p in periods:
    ctx["MA{0}".format(p)] = pd.rolling_mean(ctx['Adj Close'], p)
    
ctx[['Adj Close', 'MA20', 'MA50', 'MA100']].plot(subplots=False, figsize=(15,7))


Out[5]:
<matplotlib.axes._subplots.AxesSubplot at 0x10ad51978>

In [6]:
# Calc Daily Returns

ctx['Daily Return'] = ctx['Adj Close'].pct_change()
ctx['Daily Return'].describe()


Out[6]:
count    250.000000
mean       0.000372
std        0.007294
min       -0.020466
25%       -0.004470
50%        0.000556
75%        0.004868
max        0.024526
Name: Daily Return, dtype: float64

In [7]:
ctx['Daily Return'].plot(figsize=(15,7), marker='o')


Out[7]:
<matplotlib.axes._subplots.AxesSubplot at 0x10b04f400>

In [8]:
ctx['Daily Return'].hist(figsize=(15,7), bins=100)


Out[8]:
<matplotlib.axes._subplots.AxesSubplot at 0x10b849fd0>

In [9]:
sns.set_palette("bright")
sns.set_context(rc={"figure.figsize": (15, 7)})
sns.distplot(ctx['Daily Return'].dropna(),bins=100)


Out[9]:
<matplotlib.axes._subplots.AxesSubplot at 0x10b8c3cc0>

In [ ]: