Finance: Learning how to use stockstats, python ver. 2

Last Updated on August 24, 2022 by shibatau

I. What shall we learn?

I’m begin to learn finance and how to use Python for it again. In this post, we will learn how to use stockstats from the turorial:

Stockstats — a handy, stocks-oriented pandas DataFrame wrapper

II. Getting change, rate, close_-1_d and log-ret

Here are simple explanations about change, rate, close_-1_d and log-ret quoted from the link above.

change / rate — these are the simple returns, that is the daily percentage change between the stock prices. Values are expressed in percentages.
close_-1_d — this is the price difference between time t and t-1. We will get back later to this special way of requesting values is stockstats.
log-ret — the log returns.

You can get these values very easily with stockstas. Let’s take Toyota Motor Corporation (TM) as an example.

1.Getting data

import yfinance as yf
df ="TM", start="2021-01-01", end="2022-08-20)

2.Convert Pandas DataFrame to StockDataFrame

stock_df = StckDataFrame.retype(df) 

3.Calculating changes, rates, close_-1_days and log returns.

stock_df[["change", "rate", "close_-1_d", "log-ret"]]

Now you will get the following table:

4.Creating a multiple line charts

# creating a multiple line chart
stock_df[["close", "close_10_sma", "close_50_sma"]].plot(title="Toyota Motor Corporationd")

You can learn about SMA from my post:

Simple Moving Average

I have also created the chart with Pandas and Plotly Express.

You can see the scripts here:

To be continued.

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.