From the course: Advanced Python in Excel for Finance: A Hands-On Approach
Time series analysis
From the course: Advanced Python in Excel for Finance: A Hands-On Approach
Time series analysis
- [Instructor] We will now dive into the dynamic world of financial time-series analysis using Python. This skill is indispensable for understanding market trends, making predictions and informing investment decisions. Our first step is to understand what time-series data is and why it's critical in finance. We'll explore how Python can be used to handle this data type, focusing on the pandas library for data manipulation and datetime for handling dates and times. With our data ready, we dive into the analysis part. We start by calculating the moving averages, a fundamental technique for identifying trends in stock prices and other financial metrics. Next, we'll explore how to decompose time-series data into trend, seasonal and residual components, providing a deeper understanding of underlying patterns. Analysis alone isn't enough. Interpretation is key. We learn how to read and interpret our analytical findings, turning numerical data into comprehensible insights. Understanding these interpretations enables us to make informed predictions about future behaviors in the market. To further our analysis, we'll introduce more advanced techniques such as autoregressive integrated moving average modeling for forecasting financial time series. By mastering these advanced techniques, you'll be equipped to not only analyze, but also focus financial time-series data. So, once you are in Excel, watch it, you come to Formulas model, and then you can just come to Insert Python, Python in Excel. Click on it and your tab is activated for Python scripting. So here, we plan to use the data frame that we introduced called stock_data. As you can see, it can remember, and let me just call the head function in pandas method to see what is in the data frame. Once I Control + Enter, you should be able to see the top five entries of the data frame. Okay, I can just print it so I can display it. Control + Enter. It'll give a busy for a second before it gives the answer. On your right hand side, you can see the displayed five entries, the top five entries of your data frame. Yeah, with Date, Open, High, Low, Close, Adjusted Close Volume for the stock. Next, we'd want to plot just to see the time-series component, how the price evolves over time. So, we still activate our cell Python environment and we can just call the stock_data. In the column, we can use Close or Adjusted Close, whatever is you want to work with. And then you call the plot function method and usually text the size of the figure. And normally, you just use 10,5 or 10,6 and then Control + Enter. Now, this should give you an image and I can click on the Show card. And that alternative is just to go to this box and then put display over cells. As you can see, this is the time-series plot of the stock values over time. Now, next is to do a decomposition, a time-series decomposition, just to see other aspects of the time-series data like trends, seasonality, and residuals. So for that, we also activate the Python environment. Awesome. Now, I can just go to the table and plot over the cells. Now, you have the decomposed components of the stock price and also mentioning. Now, you have the original and then one that is smooth. And then you have the trend, of course, the seasonality, the seasonal aspect. Then we have the residual. And then the other concept we can talk about is moving average. Let me just define a function that you can see that explains that. So, by actually coding it, you are able to understand how it works, right? So, if I just write this function that calculates the moving average, and then this will just take in the data and the window, right? And let's say the window size is a default of 50. Yeah. And now we can create our column called Moving_Average. Yeah, so there's the new inch in the, and then this is just texting in the stock_data. And we can use the Close column, right? And then we call the rolling method. And from the rolling method, we just need to define the window, which is our window size. And yeah, that's it. And then just find the ridge. It's called from main function. And then this one should just return data. And now, we can apply the function. But once you've written the function, you've defined your function, you still have to apply it to your dataset. So, we just call the function calculate_moving_average, and we give it data, which is stock_data, and the window sizes is 50. We can use the default, so we just Control + Enter. You can see there's a new data frame. So, our stock data data frame has actually changed, an additional column with moving average, so that's how you do the moving average for time-series. You're now capable of transforming time-series data into meaningful insights and predictions. Whether you are analyzing stock prices, economic indicators, or any other financial time series data, the skills you've acquired today are a solid foundation for your financial analysis toolkit. Continue to practice and explore the vast possibilities that financial time-series analysis offers.
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.