Skip to main content

food stocks comparisons

·1354 words·7 mins
Jaime Carlos Velez
Author
Jaime Carlos Velez

Before Getting Started make sure you got pip and python 3.8 or higher

I am using a Jupyter Notebook and I might already have these libraries but going to run it just in case

pip install pandas numpy matplotlib yfinance seaborn

These are the following companies we are going to use for analyzing: San Miguel Corporation (SMGBF) Jollibee Foods Corporation (JBFCY) Universal Robina Corporation (UVRBF) Mondelez International, Inc.(MDLZ)

yfinance is a library for fetching historical stock data

pandas for Data Exploration and Data Cleaning

seaborn and matplotlib for Data Visualization (Honestly when I heard about these in 2016 or 17 I thought these were YouTubers)

#pandas and NumPy imports
import pandas as pd
from pandas import Series,DataFrame
import numpy as np
# For Visualization
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline
import os #for files read/write
from datetime import datetime # For time stamps
import yfinance as yf #fetching the data stock
from __future__ import division# For division in Python 3
# Fetch stock data
def fetch_stock_data(ticker, start_date, end_date):
    stock_data = yf.download(ticker, start=start_date, end=end_date)
    return stock_data
smg_data = fetch_stock_data('SMGBF', '2021-01-01', '2023-12-31')
jbf_data = fetch_stock_data('JBFCY', '2021-01-01', '2023-12-31')
uvr_data = fetch_stock_data('UVRBF', '2021-01-01', '2023-12-31')
delm_data = fetch_stock_data('MDLZ', '2021-01-01', '2023-12-31')
[*********************100%***********************]  1 of 1 completed
[*********************100%***********************]  1 of 1 completed
[*********************100%***********************]  1 of 1 completed
[*********************100%***********************]  1 of 1 completed

checking data

print(smg_data.head())
print(jbf_data.head())
print(uvr_data.head())
print(delm_data.head())
Price          Close      High       Low      Open Volume
Ticker         SMGBF     SMGBF     SMGBF     SMGBF  SMGBF
Date                                                     
2021-01-04  2.550820  2.550820  2.550820  2.550820      0
2021-01-05  2.479167  2.479167  2.479167  2.479167   1100
2021-01-06  2.479167  2.479167  2.479167  2.479167      0
2021-01-07  2.479167  2.479167  2.479167  2.479167      0
2021-01-08  2.479167  2.479167  2.479167  2.479167      0
Price           Close       High        Low       Open Volume
Ticker          JBFCY      JBFCY      JBFCY      JBFCY  JBFCY
Date                                                         
2021-01-04  15.941961  15.941961  15.941961  15.941961    500
2021-01-05  15.941961  15.941961  15.941961  15.941961      0
2021-01-06  15.941961  15.941961  15.941961  15.941961      0
2021-01-07  14.946193  15.139546  14.946193  14.946193   1100
2021-01-08  14.946193  14.946193  14.946193  14.946193      0
Price          Close      High       Low      Open Volume
Ticker         UVRBF     UVRBF     UVRBF     UVRBF  UVRBF
Date                                                     
2021-01-04  2.765748  2.765748  2.765748  2.765748      0
2021-01-05  2.765748  2.765748  2.765748  2.765748      0
2021-01-06  2.765748  2.765748  2.765748  2.765748      0
2021-01-07  2.765748  2.765748  2.765748  2.765748      0
2021-01-08  2.844769  2.844769  2.844769  2.844769   5000
Price           Close       High        Low       Open   Volume
Ticker           MDLZ       MDLZ       MDLZ       MDLZ     MDLZ
Date                                                           
2021-01-04  52.686432  54.578488  52.149747  53.204931  9187200
2021-01-05  52.741009  52.868358  52.140646  52.659141  5421900
2021-01-06  52.640957  53.059395  52.449933  52.750116  7663200
2021-01-07  52.540890  53.077578  52.167937  52.504507  8589100
2021-01-08  52.932041  53.004814  52.149752  52.186135  6642700

checking for dupes

print(smg_data.isnull().sum())
print(jbf_data.isnull().sum())
print(uvr_data.isnull().sum())
print(delm_data.isnull().sum())
type(smg_data) #I just like to check what type of data we are working with

checking excel file

path = 'fnbStocks.xlsx'
isExist = os.path.exists(path)
print(isExist)
True

So there was an error making the excel and here you would see that the columns for our data frame has two values

display(smg_data)

PriceCloseHighLowOpenVolume
TickerSMGBFSMGBFSMGBFSMGBFSMGBF
Date
2021-01-042.5508202.5508202.5508202.5508200
2021-01-052.4791672.4791672.4791672.4791671100
2021-01-062.4791672.4791672.4791672.4791670
2021-01-072.4791672.4791672.4791672.4791670
2021-01-082.4791672.4791672.4791672.4791670
..................
2023-12-221.9204051.9204051.9204051.9204050
2023-12-261.9204051.9204051.9204051.9204050
2023-12-271.8808091.9699001.8808091.9699003000
2023-12-281.8808091.8808091.8808091.8808090
2023-12-291.8808091.8808091.8808091.8808090

753 rows × 5 columns

display(smg_data.columns)
MultiIndex([( 'Close', 'SMGBF'),
            (  'High', 'SMGBF'),
            (   'Low', 'SMGBF'),
            (  'Open', 'SMGBF'),
            ('Volume', 'SMGBF')],
           names=['Price', 'Ticker'])

reassigned every dataframe with a new column

smg_data.columns= [col[0] for col in smg_data.columns]
jbf_data.columns=[col[0] for col in jbf_data.columns]
uvr_data.columns=[col[0] for col in uvr_data.columns]
delm_data.columns=[col[0] for col in delm_data.columns]
display(smg_data.columns)
Index(['Close', 'High', 'Low', 'Open', 'Volume'], dtype='object')
display(smg_data)

CloseHighLowOpenVolume
Date
2021-01-042.5508202.5508202.5508202.5508200
2021-01-052.4791672.4791672.4791672.4791671100
2021-01-062.4791672.4791672.4791672.4791670
2021-01-072.4791672.4791672.4791672.4791670
2021-01-082.4791672.4791672.4791672.4791670
..................
2023-12-221.9204051.9204051.9204051.9204050
2023-12-261.9204051.9204051.9204051.9204050
2023-12-271.8808091.9699001.8808091.9699003000
2023-12-281.8808091.8808091.8808091.8808090
2023-12-291.8808091.8808091.8808091.8808090

753 rows × 5 columns

with pd.ExcelWriter('fnbStocks.xlsx') as writer:
    smg_data.to_excel(writer, sheet_name='SanMiguel')
    jbf_data.to_excel(writer, sheet_name='Jollibee')
    uvr_data.to_excel(writer, sheet_name='Robina')
    delm_data.to_excel(writer, sheet_name='DelMonte')

check to see if your excel file is good.

smg_data['Volume'].plot(legend=True,figsize=(10,6))
#historical view of volume for San Miguel Corp
<Axes: xlabel='Date'>
png

Pivot a table from datetime

plt.figure(figsize=(10, 6))
sns.lineplot(x='Date', y='Volume', data=jbf_data, marker='o')
plt.xticks(rotation=45)
plt.title('Jollibee Foods Corps')
plt.xlabel('Volume over Year and Month')
plt.tight_layout()
plt.show()
png

historical view of volume for Jollibee

display(uvr_data)

CloseHighLowOpenVolume
Date
2021-01-042.7657482.7657482.7657482.7657480
2021-01-052.7657482.7657482.7657482.7657480
2021-01-062.7657482.7657482.7657482.7657480
2021-01-072.7657482.7657482.7657482.7657480
2021-01-082.8447692.8447692.8447692.8447695000
..................
2023-12-221.8703271.8703271.8703271.8703270
2023-12-261.8319611.8319611.8319611.8319617600
2023-12-271.8319611.8319611.8319611.8319610
2023-12-281.8319611.8319611.8319611.8319610
2023-12-291.9278751.9278751.9278751.9278752500

753 rows × 5 columns

# Convert 'date_column' to datetime format
uvr_data = uvr_data.reset_index(drop=False)
display(uvr_data)

DateCloseHighLowOpenVolume
02021-01-042.7657482.7657482.7657482.7657480
12021-01-052.7657482.7657482.7657482.7657480
22021-01-062.7657482.7657482.7657482.7657480
32021-01-072.7657482.7657482.7657482.7657480
42021-01-082.8447692.8447692.8447692.8447695000
.....................
7482023-12-221.8703271.8703271.8703271.8703270
7492023-12-261.8319611.8319611.8319611.8319617600
7502023-12-271.8319611.8319611.8319611.8319610
7512023-12-281.8319611.8319611.8319611.8319610
7522023-12-291.9278751.9278751.9278751.9278752500

753 rows × 6 columns

sns.set_theme(rc={'figure.figsize':(11.7,8.27)})
sns.lineplot(data=smg_data, x='Date', y='Close', label='San Miguel')
sns.lineplot(data=delm_data, x='Date', y='Close', label='Mondelez')
sns.lineplot(data=uvr_data, x='Date', y='Close', label='Robina')
sns.lineplot(data=jbf_data, x='Date', y='Close', label='Jabee')
<Axes: xlabel='Date', ylabel='Close'>
png

historical comparison of Closing price of Stocks

smg_data = smg_data.reset_index(drop=False)
jbf_data = jbf_data.reset_index(drop=False)
delm_data = delm_data.reset_index(drop=False)
smg_data.insert(0, 'Name', 'San Miguel Corp')
jbf_data.insert(0, 'Name', 'Jollibee Food Corp')
delm_data.insert(0, 'Name', 'Mondelez International')
uvr_data.insert(0, 'Name', 'Universal Robina Corp')
#adding a columns Name in the data frame
# The comp stocks we'll use for this analysis
comp_list = [smg_data,jbf_data,uvr_data,delm_data]
#merging all the companies
comp_listGlobal = pd.concat(comp_list)
display(comp_listGlobal)

NameDateCloseHighLowOpenVolume
0San Miguel Corp2021-01-042.5508202.5508202.5508202.5508200
1San Miguel Corp2021-01-052.4791672.4791672.4791672.4791671100
2San Miguel Corp2021-01-062.4791672.4791672.4791672.4791670
3San Miguel Corp2021-01-072.4791672.4791672.4791672.4791670
4San Miguel Corp2021-01-082.4791672.4791672.4791672.4791670
........................
748Mondelez International2023-12-2268.92128069.26971068.47606568.5534964109000
749Mondelez International2023-12-2669.40521269.58910868.71803368.9116024002900
750Mondelez International2023-12-2769.88914569.93754169.24068169.4632844061500
751Mondelez International2023-12-2870.35160870.43922869.79666269.8842824095300
752Mondelez International2023-12-2970.51711370.73130470.20556470.2347714658600

3012 rows × 7 columns

sns.lineplot(data=comp_listGlobal,x="Date", y="Close", hue="Name", style="Name")
<Axes: xlabel='Date', ylabel='Close'>
png

This is a much simple code of historical comparison of Closing price of Stocks

#pivot a new data frame here
CloseDF = comp_listGlobal.pivot(index='Date', columns='Name', values=['Close'])
display(CloseDF)

Close
NameJollibee Food CorpMondelez InternationalSan Miguel CorpUniversal Robina Corp
Date
2021-01-0415.94196152.6864322.5508202.765748
2021-01-0515.94196152.7410092.4791672.765748
2021-01-0615.94196152.6409572.4791672.765748
2021-01-0714.94619352.5408902.4791672.765748
2021-01-0814.94619352.9320412.4791672.844769
...............
2023-12-2217.65476268.9212801.9204051.870327
2023-12-2617.76417269.4052121.9204051.831961
2023-12-2717.76417269.8891451.8808091.831961
2023-12-2817.76417270.3516081.8808091.831961
2023-12-2918.29132770.5171131.8808091.927875

753 rows × 4 columns

# Calculate the daily return percent of all stocks and store them 
comp_returns = CloseDF.pct_change()
display(comp_returns)

Close
NameJollibee Food CorpMondelez InternationalSan Miguel CorpUniversal Robina Corp
Date
2021-01-04NaNNaNNaNNaN
2021-01-050.0000000.001036-0.0280900.000000
2021-01-060.000000-0.0018970.0000000.000000
2021-01-07-0.062462-0.0019010.0000000.000000
2021-01-080.0000000.0074450.0000000.028571
...............
2023-12-220.0189440.0106440.0000000.000000
2023-12-260.0061970.0070220.000000-0.020513
2023-12-270.0000000.006973-0.0206190.000000
2023-12-280.0000000.0066170.0000000.000000
2023-12-290.0296750.0023530.0000000.052356

753 rows × 4 columns

sns.pairplot(comp_returns.dropna())
#correlation analysis for all possible pairs of stocks in our food stock ticker list.
<seaborn.axisgrid.PairGrid at 0x1e5652c4500>
png
# Mixed plot to visualize the correlation between all food stocks
comp_fig = sns.PairGrid(comp_returns.dropna())
comp_fig.map_upper(plt.scatter,color='green')
comp_fig.map_lower(sns.kdeplot,cmap='Purples_d')
comp_fig.map_diag(plt.hist,bins=30)
<seaborn.axisgrid.PairGrid at 0x1e56934adb0>
png
# Correlation plot for the daily returns of all stocks
comp_returns_corr= comp_returns.dropna().corr()
sns.heatmap(comp_returns_corr,annot=True)
<Axes: xlabel='None-Name', ylabel='None-Name'>
png