A price correlation means the differences of the price of two or more assets over a certain period of time. In financial markets we frequently calculate the correlation coefficient which has a value between -1.0 and 1.0. The value 1.0 means a perfect positive correlation that implies the assets have been moving around in the same direction 100% while a perfect negative correlation -1.0 means the assets are taking the opposite direction always. The value 0 means there is no linear relationship amonth the assets at all. It’s vital to understand what pattern of correlations have your holding assets taken and will look like.
In this article, I’d like to cover 3 computations in Python Pandas library. What does “Rolling” mean? This means we will use the function providing rolling window calculation, which is used to compute moving average often.
- Rolling change of financial assets
- Rolling standard deviation of financial assets
- Rolling correlations of financial assets
I went through how to retrieve financial data by using Pandas Datareader and store data as DataFrame object indexed with datetime in the previous writing. This time we fetch not only American stock price but also indices of bond and DawJones. Here’s the code I tested.
I ran tail() function on this DataFrame object. OK Sweet. Now we’ve got the close price data for the stocks Apple, Amazon, Facebook and the indices DawJones and US Bond respectively.
We will apply Pandas rolling function to compute the rolling change, the rolling change’s standard deviation and rolling correlation with other assets in next section.
Rolling change of financial assets
pandas.DataFrame.rolling is useful function to use a window function to wrap a certain amount of rows. The rolling function itself does not calculate anything but returns a object of window.Rolling.
Here’s just one method chain to calculate the sum of 21 days rolling percentage change in Pandas. Each window will be a variable sized based on the observations included in the time-period but does not include the one it labels. For example, at the row ‘2020-08-31’ does not contain the calculated value of data it labels itself.
rolling_pct_change = df.pct_change().rolling(21).sum().fillna(0)
We can just call plot to draw the rolling daily change of financial data. What does this look like for you? DawJones seems to be smoothed and looks low volatility compared to the normal stock prices to me. All assets experienced plunge during COVID-19 turmoil.
Rolling standard deviation of financial assets
Here’s the command to calculate the standard deviation of 21 days rolling percentage change with the same data.
rolling_std = df.pct_change().rolling(21).std().fillna(0)
Let’s draw it. What happened in September 2018 for Facebook?? It might be interesting if you change the window to more longer timeframe or shorter timeframe from 21 days. You will get a different perspective about price move.
Rolling correlations of financial assets
At last let’s compute the rolling change’s correlations of financial data. We have to use the same pandas.DataFrame.rolling function and combine with corr function to compute correlation between the columns.
In this calculation the key is DawJones series to compare with other financial assets so that y axis (which is the standard Pearson correlation coefficient) scale is against DawJones percent change.
Let’s draw it. US Bond index price was obviously negative-correlated with DawJones index price except COVID-19 season.
Here’s the Jupyter notebook of all samples in gist for your reference.