Finance Analysis

Timetk is designed to work with any time series domain. Arguably the most important is Finance. This tutorial showcases how you can perform Financial Investment and Stock Analysis at scale with pytimetk. This applied tutorial covers financial analysis with:

Load the following packages before proceeding with this tutorial.

Code
import pytimetk as tk
import pandas as pd
import numpy as np

1 3 Core Properties: Financial Data

Financial data from sources like openbb or yfinance come in OHLCV format and typically include an β€œadjusted” price (adjusted for stock splits). This data has the 3 core properties of time series:

  1. Timestamp: daily, hourly frequencies
  2. Value: A price (or returns)
  3. Groups: Stock symbols

Let’s take a look with the tk.glimpse() function.

Code
stocks_df = tk.load_dataset("stocks_daily", parse_dates = ['date'])

stocks_df.glimpse()
<class 'pandas.core.frame.DataFrame'>: 16194 rows of 8 columns
symbol:    object            ['META', 'META', 'META', 'META', 'META', 'M ...
date:      datetime64[ns]    [Timestamp('2013-01-02 00:00:00'), Timestam ...
open:      float64           [27.440000534057617, 27.8799991607666, 28.0 ...
high:      float64           [28.18000030517578, 28.46999931335449, 28.9 ...
low:       float64           [27.420000076293945, 27.59000015258789, 27. ...
close:     float64           [28.0, 27.770000457763672, 28.7600002288818 ...
volume:    int64             [69846400, 63140600, 72715400, 83781800, 45 ...
adjusted:  float64           [28.0, 27.770000457763672, 28.7600002288818 ...

2 Visualizing Financial Data

Visualizing financial data is critical for:

  1. Quick Insights
  2. Enhanced Decision Making
  3. Performance Monitoring
  4. Ease of Reporting

We can visualize financial data over time with tk.plot_timeseries():

  • An interactive plotly plot is returned by default. A static plot can be returned by setting engine = "plotnine".
  • A blue smoother is added by default. The smoother can be removed with smooth = False.
  • Click here to see our Data Visualization Guide
  • Use help(tk.plot_timeseries) to review additional helpful documentation.

An interactive plotly plot is returned by default. Interactive is useful for fast data exploration and for use in web apps (e.g. streamlit, shiny, dash), Click to expand code template.

Code
# plotly engine
stocks_df \
    .groupby('symbol') \
    .plot_timeseries(
        'date', 'adjusted',
        facet_ncol = 2,
        smooth = True,
        smooth_frac = 0.10,
        width = 900,
        height = 700,
        engine = 'plotly',
    )