In [1]:
import numpy as np
import pandas as pd

import matplotlib.pyplot as plt
from nsepy import get_history
from datetime import date


symbol="NIFTY 50"
year=[2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016]
month=[1,2,3,4,5,6,7,8,9,10,11,12]
In [6]:
r1=[]
#1999 december month last day close value
result=get_history(symbol = symbol,start = date(2000,12,1),end = date(2000,12,31),index=True)
if result.empty:
    r1.append(0)
else: 
    s2=result[['Close']]
    v1=s2.values.T.tolist()
    value=(v1[0][-1])
    r1.append(value)
    
In [8]:
for y in year:
    for m in month:
        if m in [1,3,5,7,8,10,12]:
            result=get_history(symbol = symbol,start = date(y,m,1),end = date(y,m,31),index=True)
            if result.empty:
                r1.append(0)
            else: 
                s2=result[['Close']]
                v1=s2.values.T.tolist()
                value=(v1[0][-1])
                r1.append(value)
                
        elif  y in [2000,2004,2008,2012,2016] and m==2: #condition for leapyear
            result=get_history(symbol = symbol,start = date(y,m,1),end = date(y,m,29),index=True)
            if result.empty:
                r1.append(0)
            else:    
                s2=result[['Close']]
                #v=s1.values.T.tolist()
                v1=s2.values.T.tolist()
                value=(v1[0][-1])
                r1.append(value)
        elif y not in [2000,2004,2008,2012,2016] and m==2:
            result=get_history(symbol = symbol,start = date(y,m,1),end = date(y,m,28),index=True)
            if result.empty:
                r1.append(0)
            else:    
                s2=result[['Close']]
                #v=s1.values.T.tolist()
                v1=s2.values.T.tolist()
                value=(v1[0][-1])
                r1.append(value)    
                
        else:
            result=get_history(symbol = symbol,start = date(y,m,1),end = date(y,m,30),index=True)
            if result.empty:
                r1.append(0)
            else:    
                s2=result[['Close']]
                #v=s1.values.T.tolist()
                v1=s2.values.T.tolist()
                value=(v1[0][-1])
                r1.append(value)


m=["January","February","March","April","May","June","July","August","September","October","November","December"]
In [9]:
value=[]
j=0
for i in r1:
    if i >0 and r1[j+1]>0:
        if j<len(r1):
            s=(r1[j+1]-i)*100/i
            value.append(s)
            j=j+1
    elif i==0 and r1[j+1]==0:
        value.append(0)
        j=j+1
In [10]:
k=(value[i:i+12] for i in range(0, len(value), 12))

#print len(k)

out=[]
for s in k:
    out.append(s)

print len(out)

m=["January","February","March","April","May","June","July","August","September","October","November","December"]
17
In [12]:
import seaborn as sns

import matplotlib.pyplot as plt

%pylab inline

import seaborn.matrix as smatrix




#actual verification
#sns.set(style="white")
#s=np.array(out)
sns.set()
data = pd.DataFrame(out, columns=m, index = range(2000, 2017, 1))
print data

fig, ax = plt.subplots()
# the size of A4 paper
fig.set_size_inches(11.7, 8.27)

# plot heatmap
sns.heatmap(data.T,cbar=False,
                square=False,robust=True,annot=True,fmt=".1f",annot_kws={"size": 10},linewidths=0.5,cmap="RdYlGn", ax=ax)
#print ax
cax = plt.gcf().axes[-1]
cax.tick_params(width=12)
cax = plt.gcf().axes[-1]
cax.tick_params(labelsize=20)
#turn the axis label
for item in ax.get_yticklabels():
    item.set_rotation(0)

for item in ax.get_xticklabels():
    item.set_rotation(90)
Populating the interactive namespace from numpy and matplotlib
        January  February      March      April        May       June  \
2000  22.369514  7.023671  -7.635364  -7.975400  -1.855604   6.592053   
2001   8.559218 -1.479915 -15.036259  -1.998781   3.790269  -5.137426   
2002   1.543836  6.197694  -1.094523  -3.988314  -5.136007   2.818818   
2003  -4.723365  2.068436  -8.012037  -4.513392   7.788662  12.648987   
2004  -3.723899 -0.522172  -1.577515   1.365766 -17.398809   1.482879   
2005  -1.100697  2.218604  -3.214073  -6.540908   9.726675   6.373500   
2006   5.801061  2.452434  10.662829   4.556876 -13.676355   1.860927   
2007   2.932130 -8.264139   2.035885   6.969685   5.085741   0.523767   
2008 -16.309093  1.674955  -9.361539   9.111839  -5.726011 -17.033531   
2009  -2.850481 -3.866356   9.310151  14.995283  28.066034  -3.548028   
2010  -6.133377  0.824449   6.639173   0.550571  -3.632058   4.447241   
2011 -10.246964 -3.135727   9.384522  -1.444183  -3.293330   1.569202   
2012  12.433233  3.576477  -1.664748  -0.895091  -6.171699   7.202112   
2013   2.195560 -5.662206  -0.184435   4.358079   0.940103  -2.401457   
2014  -3.402602  3.078249   6.806650  -0.116345   7.967714   5.275279   
2015   6.353001  1.055183  -4.615333  -3.645036   3.081953  -0.772501   
2016  -4.817306 -7.622082   7.487423   0.000000   0.000000   0.000000   

          July     August  September    October   November   December  
2000 -9.419280   4.595416  -8.783445  -7.777297   8.134726  -0.362733  
2001 -3.163643  -1.780305 -13.276394   6.352246   9.800391  -0.759031  
2002 -9.349593   5.391595  -4.695231  -1.219955  10.379441   4.127982  
2003  4.558480  14.394738   4.463529   9.794651   3.814513  16.375174  
2004  8.415250  -0.033695   6.971043   2.371813   9.620012   6.212988  
2005  4.129515   3.128919   9.089384  -8.858691  11.864443   6.948817  
2006  0.479509   8.612242   5.111456   4.338981   5.619508   0.300923  
2007  4.875761  -1.431931  12.485439  17.511227  -2.337031   6.522060  
2008  7.236639   0.624286 -10.064220 -26.410283  -4.522456   7.406265  
2009  8.048053   0.553225   9.048497  -7.322063   6.812828   3.345123  
2010  1.037176   0.648334  11.616134  -0.203153  -2.575735   4.636089  
2011 -2.928781  -8.774170  -1.154769   7.755019  -9.284534  -4.299417  
2012 -0.945273   0.564161   8.458686  -1.465818   4.629251   0.429433  
2013 -1.715107  -4.705677   4.815600   9.831221  -1.953438   2.070886  
2014  1.444553   3.018274   0.131375   4.487244   3.196871  -3.557768  
2015  1.963912  -6.581037  -0.281008   1.470644  -1.618562   0.139882  
2016  0.000000   0.000000   0.000000   0.000000   0.000000   0.000000  
In [ ]: