I need to add new columns called 'Volume % Change', and "Close % Change" for this dataframe in PYTHON.
The data is from yahoo finance, so it's always changable depends on the days entered.
the syntax is below:
import pandas_datareader as pdr
import datetime
import pandas as pd
# Allow the full width of the data frame to show.
pd.set_option('display.max_columns', None)
pd.set_option('display.width', 1000)
def getStock(stk):
# Set and show dates.
dt = datetime.date.today()
dtPast = dt + datetime.timedelta(days=-5)
print(dt)
print(dtPast)
# Call Yahoo finance to get stock data for the stock provided.
df = pdr.get_data_yahoo(stk,
start= datetime.datetime(dtPast.year, dtPast.month, dtPast.day),
end = datetime.datetime(dt.year, dt.month, dt.day))
# Return a dataframe containing stock data to the calling instruction.
return df
dfMSFT = getStock("MSFT")
print(dfMSFT)
--------------------------------------------------------------------------
The formula for Volume % Change is(Current day volume-previous day volume)/previous day volume
The formula for Close % Change is (Current day Close - previous day)/previous day Close
The output is like the attached image.
Please assist!
Thanks
Extracted text: Close Volume Volume * Change close Change Date 2021-01-15 212.649994 31691500 0.0000 0.0000 2021-01-19 216.440002 30480900 -0.0382 0.0178 2021-01-20 224.339996 37777260 0.2394 0.0365