Python stock market charts

Python stock market charts

Posted: Rosta Date: 18.07.2017

Once you have a basic understanding of how Matplotlib works, you might have an interest in taking your knowledge a bit further.

Some of the most complex graphing needs come in the form of stock analysis and charting, or Forex.

In this tutorial series, we're going to cover where and how to automatically grab, sort, and organize some free stock and forex pricing data. Next, we're going to chart it using some of the more popular indicators as an example.

python stock market charts

Here, we'll do MACD Moving Average Convergence Divergence and the RSI Relative Strength Index. To help us calculate these, we will use NumPy, but otherwise we will calculate these all on our own.

To acquire the data, we're going to use the Yahoo finance API. This API returns historical price data for the ticker symbol we specify and for the time length we ask for. The larger the time frame, the lower the resolution of data we get.

So, if you ask for a 1-day time frame for AAPL, you will get 3-minute OHLC open high low close data. If you ask for 10 years worth, you will get daily data, or even 3 day time frames. Keep this in mind and choose a time frame that fits your goals.

python - how to calculate trendline for stock charts - Stack Overflow

Also, if you choose a low enough time frame and get high enough granularity, the API will return the time in a unix time stamp, as compared to a date stamp. Once we have the data, we will want to graph it. To start, we'll just plot the lines, but most people will want to plot a candlestick instead. We will use Matplotlib's candlestick function, and make a simple edit to it to improve it slightly. On this same chart, we'll also overlay a few moving average calculations.

After this, we're going to create a subplot, and graph the volume.

We cannot plot volume on the same subplot immediately, because the scale is different. To start, we will plot the volume underneath in another sub plot, but eventually we'll actually overlay volume on the same figure and make it somewhat transparent. Then, we're going to add 2 sub plots and plot an RSI indicator on top and the MACD indicator on the bottom.

yahoo-finance : Python Package Index

For all of these, we're going to share the X axis, so we can zoom in and out in 1 plot and they will all match the same time frame. We're going to plot in date format for the X axis, and customize just about all of the things we can for aesthetics.

python stock market charts

Here's the end-result I have both a Python 3 and a Python 2 version for this. Python 3 first, then Python 2. Make sure you're using the one that matches your Python Version!

Money A2Z

That's all for now. Head to the Home Page. Advanced Matplotlib Series videos and ending source only. You've reached the end!

Terms and Conditions Privacy Policy.

inserted by FC2 system