Bharath Reddy

# Moving Window Functions

**TIME SERIES IN PYTHON | Part 3 of 3**

Part 1 of the series | __Creating Datetime objects and time Series in Python__ *gave a basic** understanding on date time objects, time series implementation in Python and*

Part 2 of the series | __Resampling and Frequency Conversion__* **gave a basic understanding of resampling or frequency conversion techniques.*

Final part of Time series in Python explores the moving window functions.

Time series data is generally used to analyze data points or observations generated over multiple time periods or instances of time aka. timestamps. In this article I discuss an important statistics and functions evaluated over a window of time or with exponentially decaying weights. What better data than closing prices for a stock â€” Lets analyse Facebook stock to understand these concepts better.

Letâ€™s start with a package called **datareader** to pull stock prices of Facebook, i will pull stock prices of multiple stock prices just to show how this package works.

Simple and Expanding Moving Averages

I will now introduce the **rolling** method which would act just as **resample **method. So imagine it as an aggregate on a bucket of timestamps - but this bucket is a rolling period bucket i.e. for each observation a window is created. Let's plot a 30 day rolling average price for the above dataset.

**rolling(200)** creates a a 200 day sliding window and **.mean()** calculates mean of the prices. Some keen eyed amongst you might have noticed that the SMA (Simple Moving Averages) started from 2016 while the closing prices are available from March 2015. This is because the calculations are on window created backwards and hence the first 200 odd days might not have enough data points for a 200 day moving average.

Another method available is to compute expanding window mean , for this we use **expanding** method instead of the **rolling** method. The expanding mean starts the time window from the beginning of the time series and increases the size of the window until it encompasses the whole series.

Exponentially Weighted functions

## An Alternate to SMA which uses a fixed window size with equally weighted observations is to specify a decay factor which gives more importance/weight to more recent observations. This method adapts faster to changes compared with equally weighted methods. Lets analyze Facebook stock again to understand this better. You would immediately see that the exponentially weighted functions track the stock more tightly still maintaining the averaging properties of the price movement.

I hope you enjoyed this third part of the Time Series Analysis series or articles. I chose the data set as price of Facebook deliberately to move closer to application of there seemingly dry concepts.

Traders create complex trading signals and charting patterns but in essence they are mostly using concepts discussed in this writeup.

Explore and try creating your own trading signals using these techniques and stay curious.

*Teaser : Up next would be series on probability distributions with a story telling narrative - Please do check it out as well.*