# Create Excel Sheet with Stock Prices and Moving Average with Chart all from Python

## What will we cover in this tutorial?

In this tutorial we will read a historic stock prices, calculate the moving average and export that to an Excel sheet and insert a chart with prices and moving average. And all will be done from Python using Pandas and Pandas-datareader.

## Step 1: Get the historic stock prices

A great place to get historic stock prices is using Pandas-datareader. It has a great interface to various places to get the data. In this tutorial we will use Yahoo API through the Pandas-datareader.

It doesn’t require any registration to use the API. It works straight out of the box.

To get stock prices in time series you need to find the ticker of your favorite stock. In this tutorial we will use Apple, which has ticker AAPL.

```import pandas_datareader as pdr
import datetime as dt
start = dt.datetime(2020, 1, 1)
ticker = pdr.get_data_yahoo("AAPL", start)
```

You also need to set the date to set how far back you want historic stock prices. By default, you will get up to the most current date. Also, you will data for each day open. You can use more arguments to configure what if you want weekly or monthly prices. Also, you can set a end-date, if you like.

The above code should give output similar to the following.

```                 High        Low       Open      Close       Volume  Adj Close
Date
2020-01-02  75.150002  73.797501  74.059998  75.087502  135480400.0  73.840042
2020-01-03  75.144997  74.125000  74.287498  74.357498  146322800.0  73.122154
2020-01-06  74.989998  73.187500  73.447502  74.949997  118387200.0  73.704819
2020-01-07  75.224998  74.370003  74.959999  74.597504  108872000.0  73.358185
2020-01-08  76.110001  74.290001  74.290001  75.797501  132079200.0  74.538239
```

## Step 2: Calculate the Moving Average

To calculate the moving average (also called the simple moving average), we can use the rolling method on a DataFrame.

The rolling method takes one argument, which is the window size. This indicates how big a window we want to apply a function on. In this case we want to apply the mean function on a window of size 50.

```import pandas_datareader as pdr
import pandas as pd
import datetime as dt
start = dt.datetime(2020, 1, 1)
ticker = pdr.get_data_yahoo("AAPL", start)
ticker['MA'] = ticker['Close'].rolling(50).mean()
```

This calculates the simple moving average of window size 50. This will give the same result as the moving average with the default window of 50 will give in Yahoo! Finance.

The output will be as follows.

```                 High        Low       Open  ...       Volume  Adj Close        MA
Date                                         ...
2020-03-16  64.769997  60.000000  60.487499  ...  322423600.0  59.687832  76.16100
2020-03-17  64.402496  59.599998  61.877499  ...  324056000.0  62.312309  75.93815
2020-03-18  62.500000  59.279999  59.942501  ...  300233600.0  60.786911  75.67250
2020-03-19  63.209999  60.652500  61.847500  ...  271857200.0  60.321156  75.40445
2020-03-20  62.957500  57.000000  61.794998  ...  401693200.0  56.491634  75.03470
```

Notice we removed the first 50 rows (actually 51, as we index from 0). This is because the MA (moving average) column will not have numbers before we reach here.

## Step 3: Export data to Excel and create a chart with close prices and moving average

Now this is where it all get’s a bit more complicated. It takes some reading in the manual to figure all this out.

The code is commented to explain what happens.

```import pandas_datareader as pdr
import pandas as pd
import datetime as dt
# Read the stock prices from Yahoo! Finance
start = dt.datetime(2020, 1, 1)
ticker = pdr.get_data_yahoo("AAPL", start)
# Calculate the moving average with window size 50
ticker['MA'] = ticker['Close'].rolling(50).mean()
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('apple.xlsx', engine='xlsxwriter')
# Name the sheet
sheet_name = "Apple"
# We convert the index from datetime to date
# This makes the data in Excel only have the date and
# not the date with time: 00:00:00:0000
ticker.index = ticker.index.date
# Skip the first 51 rows (to be pedantic, 49 is the correct number)
ticker = ticker[50:]
# Convert the dataframe to an XlsxWriter Excel object.
ticker.to_excel(writer, sheet_name=sheet_name)
# Get the xlsxwriter workbook and worksheet objects.
workbook = writer.book
worksheet = writer.sheets[sheet_name]
# Widen the first column to display the dates.
worksheet.set_column('A:A', 12)
# Get the number of rows and column index
max_row = len(ticker)
col_ma = ticker.columns.get_loc('MA') + 1
col_close = ticker.columns.get_loc('Close') + 1
# Create a chart object of type line
# Insert the first dataset into chart
'name': "MA",
'categories': [sheet_name, 1, 0, max_row, 0],
'values': [sheet_name, 1, col_ma, max_row, col_ma],
})
# Insert the second dataset in the same chart
'name': "Close",
'values': [sheet_name, 1, col_close, max_row, col_close],
})
# Configure axis
chart.set_x_axis({
'name': 'Date',
'date_axis': True,
})
chart.set_y_axis({
'name': '\$',
'major_gridlines': {'visible': False}
})
# Insert the chart into the worksheet.
worksheet.insert_chart('I2', chart)
# Close the Pandas Excel writer and output the Excel file.
writer.save()
```

The above code will create an Excel sheet looking like this.

## Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON

• 70 pages to get you started on your journey to master Python.
• How to install your setup with Anaconda.
• Written description and introduction to all concepts.
• Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

1. How to get started with this 8 hours Python 101: A CRASH COURSE.
2. Best practices for learning Python.
4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

## Expert Data Science Blueprint

Expert Data Science Blueprint

• Master the Data Science Workflow for actionable data insights.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7175533,"math_prob":0.70594615,"size":6344,"snap":"2022-40-2023-06","text_gpt3_token_len":1727,"char_repetition_ratio":0.09842271,"word_repetition_ratio":0.090539165,"special_character_ratio":0.3389029,"punctuation_ratio":0.1802903,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95572,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-02T21:08:15Z\",\"WARC-Record-ID\":\"<urn:uuid:0c4c9f58-8b6d-4fa2-a622-bdfd1de4ef87>\",\"Content-Length\":\"84048\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7148d2c0-6f15-4358-8268-1044c0d0471a>\",\"WARC-Concurrent-To\":\"<urn:uuid:e8ce3faa-e3a4-4694-9ace-848fd1d133e4>\",\"WARC-IP-Address\":\"34.86.59.15\",\"WARC-Target-URI\":\"https://www.learnpythonwithrune.org/create-excel-sheet-with-stock-prices-and-moving-average-with-chart-all-from-python/\",\"WARC-Payload-Digest\":\"sha1:27NPA6JRZMYYUP6K2K66HGT5IJQZUQK5\",\"WARC-Block-Digest\":\"sha1:XZJKYT6S5NMAHSEFMNPXNWOBTULIBMLD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500041.18_warc_CC-MAIN-20230202200542-20230202230542-00611.warc.gz\"}"}