This tutorial cover how to retrieve stock price from google finance using pandas data reader. The analysis of stock is done by plotting its high, low, close, volume values in talble and a chart. Charts are of two types,
- Line Chart
- Bar Chart
If you don't know what is stock then we highly recommend that you first watch the video below to find out what is Stock.
%%HTML
<iframe width="560" height="315" src="https://www.youtube.com/embed/7EUbrFLef7M" frameborder="0" allowfullscreen></iframe>
# Setting keybooard shorcut for ease.
# e.g Run : Shift + Enter
from pandas_datareader import data as web
data_frame = web.DataReader('AAPL', 'google', '2016/1/1', '2017/1/1')
data_frame.head()
<style>
.dataframe thead tr:only-child th {
text-align: right;
}
</style>
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
Open | High | Low | Close | Volume | |
---|---|---|---|---|---|
Date | |||||
2016-01-04 | 102.61 | 105.37 | 102.00 | 105.35 | 67281190 |
2016-01-05 | 105.75 | 105.85 | 102.41 | 102.71 | 55790992 |
2016-01-06 | 100.56 | 102.37 | 99.87 | 100.70 | 68457388 |
2016-01-07 | 98.68 | 100.13 | 96.43 | 96.45 | 81094428 |
2016-01-08 | 98.55 | 99.11 | 96.76 | 96.96 | 70798016 |
# % is magic command
# inline will plot the chart as soon as execution finish.
%matplotlib inline
data_frame.plot(y="Close", color="purple")
<matplotlib.axes._subplots.AxesSubplot at 0x7f0e058a33c8>
# Show all available line magics
%lsmagic
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%markdown %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
# '%time?' to open %time docstring
%time?
# Check runtime of for loop
%time for i in range(100000): i*i+i
CPU times: user 24 ms, sys: 0 ns, total: 24 ms
Wall time: 23.7 ms
# Run UNIX command
%system?
# Show present working directory
%system pwd
['/home/lhuynh/Desktop/nb-ipynb']
# We can run UNIX command using shorcut ! instead of %system
!pwd
/home/lhuynh/Desktop/nb-ipynb
# Continue with the stock plot
# We can plot a bar chart
data_frame.plot.bar(y="Volume")
<matplotlib.axes._subplots.AxesSubplot at 0x7f0e0301dd30>
%%python?