# Real-Time Stock Price with Python

In this article, I will take you through, how you can visualize real-time stock price data with Python by using the Yahoo finance API known as yahoo_fin.

I will use the Plotly package in python to visualize real-time stock price using python as using Plotly we can see an interactive result. I will also use the cufflinks package to create the candlestick chart which will visualize the real-time stock price using python.

Also, Read – Build and Deploy a Chatbot with Python.

## Visualize Real-Time Stock Price with Python

I will start this task by importing all the necessary package that we need to get an visualize the real-time stock prices with Python. Now let’s import all the necessary libraries to get started with the task:

## Real-Time Stock Price

Getting the real-time stock prices is quite easy in Python. We just need to use the yahoo_fin package for this task. Let’s see how we can get the real-time stock price by using the Yahoo Finance API:

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```print(stock_info.get_live_price('AAPL'))
```Code language: PHP (php)```

497.4800109863281

Let’s see what google says if we find the stock price of Apple on Google:

As you can see we have got the real-time price using python in the output above. But this is not what we need, we need to visualize the real-time stock price. Let’s see how we can visualize the real-time data with python. But before that let’s have a look at the data to have a quick look at what we need to plot in the graph:

``````open        high         low  ...    adjclose    volume  ticker
2020-08-03  432.799988  446.549988  431.570007  ...  434.965759  77037800    AAPL
2020-08-04  436.529999  443.160004  433.549988  ...  437.870514  43267900    AAPL
2020-08-05  437.510010  441.570007  435.589996  ...  439.457642  30498000    AAPL
2020-08-06  441.619995  457.649994  439.190002  ...  454.790009  50607200    AAPL
2020-08-07  452.820007  454.700012  441.170013  ...  444.450012  49453300    AAPL```Code language: CSS (css)```

## Plotting the Data

As you can see in the output above we have got a dataframe as an output which is the real-time stock prices data of Apple. Now let’s see how we can visualize the data using Plotly to get interactive result:

Also, Read – Emotion Detection Model with Machine Learning.

The output looks very nice and interactive. I hope you liked this article on getting and visualizing the real-time stock price data using python. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning.