Menu
  • Home
  • About Me
  • Blog
  • Github
  • LinkedIn

Price Changes and Financial Returns

February 7, 2022February 7, 2022
import pandas as pd
import matplotlib.pyplot as plt
pd.options.display.float_format = '{:.4f}'.format
plt.style.use("seaborn")

close = pd.read_csv("close.csv", index_col = "Date", parse_dates = ["Date"])
close

Drop the NaN value without changing the original data frame

msft = close.MSFT.dropna().to_frame().copy()

Change a column label MSFT to Price:

msft.rename(columns = {"MSFT":"Price"}, inplace = True)

Last day of price:

msft["P_lag1"] = msft.shift(periods = 1)

Compare between two days of price:

msft["P_diff"] = msft.Price.sub(msft.P_lag1) # Alternative 1
msft["P_diff2"] = msft.Price.diff(periods = 1)  # Alternative 2

#print msft table
msft

Absolute price changes are not meaningful yet, this is how we can calculate relative changes

#percentage change method

msft["Returns"] = msft.Price.pct_change(periods = 1) * 100 # Alternative 2
msft

The output of percentage change:

save the file to csv format:

msft.drop(columns = ["P_lag1", "P_diff", "P_diff2"], inplace = True)
msft.to_csv("msft.csv")

Leave a Reply Cancel reply

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

Recent Posts
  • ChinguTalkBot v0.1.0: Setting up AWS Cognito with CDK for User Authentication
  • Phoenix & Elixir: Fix PostgreSQL connection refused
  • Demo: Git Log with Shell script to create a release notes
  • Metasploit
  • CyberSecurity Lab – Online Password Attack

Archives
  • March 2024
  • May 2023
  • April 2023
  • February 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
Categories
  • Amazon Interview (3)
  • Ansible (3)
  • AWS (9)
  • Azure (9)
  • Certification (2)
  • ChinguTalkBot Project (1)
  • cybersecurity (3)
  • Data analytics (6)
  • Demo Videos (6)
  • Docker (5)
  • Git (1)
  • GitLab (1)
  • Golang (3)
  • JavaScript (2)
  • Jenkins (4)
  • PowerShell (1)
  • Python (10)
  • Terraform (11)
  • Uncategorized (9)

©2025 | Powered by WordPress and Superb Themes!