Getting real-time stock market data and visualization

Goal

This post aims to introduce how to get real time stock market data using Yahoo finance API yahoo_fin and visualize it as candle chart using cufflinks. image

Reference

Libraries

In [70]:
import pandas as pd
from yahoo_fin import stock_info
from plotly.offline import plot, init_notebook_mode
init_notebook_mode()
import cufflinks as cf
%matplotlib inline
cf.set_config_file(offline=True)

Get real time stock price data

In [76]:
stock_info.get_live_price('msft')
Out[76]:
135.69000244140625

Get stock tables as pandas table

In [61]:
d_df = {}
d_df['msft'] = stock_info.get_data('msft', start_date='01/01/2019', end_date='06/01/2019')
d_df['msft'].head()
Out[61]:
open high low close adjclose volume ticker
date
2019-01-02 99.550003 101.750000 98.940002 101.120003 100.318642 35329300.0 MSFT
2019-01-03 100.099998 100.190002 97.199997 97.400002 96.628120 42523600.0 MSFT
2019-01-04 99.720001 102.510002 98.930000 101.930000 101.122223 44060600.0 MSFT
2019-01-07 101.639999 103.269997 100.980003 102.059998 101.251190 35656100.0 MSFT
2019-01-08 103.040001 103.970001 101.709999 102.800003 101.985329 31514400.0 MSFT

Visualize the obtained stock data

In [75]:
# visualiz stock data 
qf = cf.QuantFig(d_df['msft'])
qf.add_bollinger_bands() 
qf.add_macd()
qf.iplot()

Comments

Comments powered by Disqus