In this post, I wanted to briefly describe another interesting library I found recently –
stockstats. It is a wrapper library a
pandas DataFrame and its main objective is to provide immediate access to various stock price related measures and technical indicators. Let’s jump straight to it!
Before you can see what
stockstats must be provided, we must import libraries and download the data.
yfinance, we will charge Apple OHLC prices (+ quantity) from 2020. More information about the library is mine another article.
Tests the amount of data is more than enough to present
stockstats. The main section of the library is
StockDataFramewhich is a wrapper
pandas Data frame. We can easily convert the latter to the former with the following snippet.
stock_df = StockDataFrame.retype(df)stock_df
So far nothing has really changed and the new DataFrame contains the same columns as it, only the names are now in lowercase.
stockstats offers a rather interesting approach to calculating different metrics – we just have to select the columns we would like to see and the library uses the underlying methods to calculate the requested values.
Let’s start with some basic data that we can use in the analysis – stock returns.
stock_df[["change", "rate", "close_-1_d", "log-ret"]]
pandas wont choose the columns we got the requested values, which is quite convenient. Very briefly, a brief description of the columns:
rate– these are simple returns, ie the daily percentage change in share prices. Values are expressed as percentages.
close_-1_d– this is the price difference between the price t and t-1. We will return later to this particular way of requesting values
log-ret– the log is lit.
It is also worth mentioning that when we ask for these columns as above, we only get the requested output. However, the same columns were appended to the source object
I promised technical indicators in the subheading, so a very simple – 10-day simple moving average (SMA) is created.
Again, we used a special way to request customized data. To fully understand it, we can look at the requested column name with the following formula
columnName– the name of the column for which we want to calculate the meter. In the example above, we used a nearby price.
window– how many observations need to be taken into account. We used the 10 day window above.
statistics– which dimensions we want to calculate. In the example above, we used a simple moving average (
sma). Check the list of all available data and the corresponding column names GitHub repo /
We now combine a few elements we’ve already seen to create a simple chart of near price and two moving averages.
As a side note
StockDataFrame still retain
pandas Data frame. So requesting a column with one square bracket would result in a
pandas Series. And we can use panda indexing to cut the DataFrame, for example:
This plot would be very similar to the first, but it would only contain information
Another TI that we can easily calculate and draw are Bollinger ribbons.
Many more technical indicators are available, such as the Relative Strength Index (RSI), Moving Average Convergence Divergence (MACD), Stochastic Oscillator, William’s Overbought / Oversold Index, Mean Real Range (ATR), and many more.
All of these indicators use some hard-coded values for windows based on industry standards. For example, Bollinger Bands are calculated using a 20-day moving average (
BOLL_PERIOD) and the bands have two standard deviations (
BOLL_STD_TIMES). We can replace them by accessing the corresponding elements
Lastly, often when we play with technical indicators, we want to create signals – an indicator of whether this is a buying / selling situation. Some simple and popular strategies include checking to see if one value exceeds or falls below another.
stockstats also provides such functionality.
We get a Boolean series that tells you if the closing price was exceeded above the same closing price of the 20-day SMA. We can also request more complex crossovers, such as a crossover of two SMAs.
In this case, we get a signal when the 10-day SMA exceeds the 50-day SMA below, both are calculated using a nearby price.
stockstats is definitely an interesting library that can be useful for quickly analyzing stock quotes. It also provides a unique way to calculate the requested metrics by selecting the appropriate column names. It may take some time to familiarize yourself with the formula used to use the combined variables.
However, the library is quite poorly documented. Most of the information can be obtained
README file repoor just by checking code directly. But this also provides an opportunity to peek into the calculation of technical indicators, which is a good learning experience.
stockstatsis a wrapper a
pandasDataFrame and provides easy access to a variety of stock metrics.
- the library offers a wide range of popular technical indicators such as SMA, EMA, RSI, MACD, etc.
- you can calculate the metrics just by calling the desired column name, and everything is processed under the Hood.
If you’re interested in learning how to use Python for quantitative funding, you might want to check it out Quantra (disclaimer: affiliate link), which offers a number of different courses on the subject.
Note to editors of Data Science: Although we allow independent authors to publish articles in accordance with our own articles rules and instructions, we do not endorse the opinion of each author. You should not rely on the author’s works without seeking expert help. Look at us Reader terms for details.
If you liked this article, you may also be interested in one of the following: