In [27]:
%matplotlib inline
import matplotlib.pylab
import numpy as no
import pandas as pd
import pandas_datareader.data as web
start = pd.Timestamp('2013-1-1')
end = pd.Timestamp('2018-1-1')
In [28]:
f = web.DataReader("F", 'iex', start, end)
In [29]:
f.head()
Out[29]:
In [103]:
type(f.index)
Out[103]:
In [108]:
f.index = pd.to_datetime(f.index)
In [109]:
type(f.index)
Out[109]:
In [110]:
#plot high and low for 2 mnths in 2016
f.loc['2016-02': '2016-03'][['high','low']].plot()
Out[110]:
In [111]:
# is variance of trading volume relatively stable over time?
# one possible translation of question
r = f.rolling(50).var()['volume'].plot()
In [112]:
# another version
r = f.expanding().var()['volume'].plot()
In [113]:
# how days did stock close higher than it opened
len( f[f.close > f.open])
Out[113]:
In [114]:
# did the up days become more or less frequent overtime
f['DayGain'] = f.close - f.open
f.rolling(window = 100)['DayGain'].apply(
lambda x: len([x_i for x_i in x if x_i > 0]) / len(x)).plot()
Out[114]:
In [116]:
f.expanding()['DayGain'].apply(
lambda x: len([x_i for x_i in x if x_i > 0]) / len(x)).plot()
Out[116]:
In [120]:
# compute and plot mean monthly high value of stock
f.resample('M').mean()['high'].plot()
Out[120]:
In [127]:
# explore the variance of differenced volume
vol = f.volume - f.volume.shift()
vol.head()
Out[127]:
In [128]:
vol.rolling(window=20).var().plot()
Out[128]:
In [131]:
# does the lagged time series correlate with itself
pd.DataFrame( {'real': f.volume, 'lagged': f.volume.shift()} ).corr()
Out[131]:
In [ ]: