Pandas Datareader using Python. Looking at stock price recovery of North American oil and gas majors .

Pandas Datareader using Python. Looking at stock price recovery of North American oil and gas majors .

Introduction:

As a data science newbie pivoting into a new career, I have recently discovered one of the cool features of Python Pandas Library called Pandas datareader. This sub package allows to create a dataframes from various internet datasources including popular Yahoo! Finance, World Bank, Google Analytics and the others.

Because I am using Yahoo Finance on a regular basis to look at the stock prices I have decided that it will be a great opportunity for me to have some fun (I meant practice) in Python and check the capabilities of that powerful package.

From the one hand, I was tempted to look at the most popular stocks from banking sector and technology companies for this exercise. From the other hand, my petroleum engineering background and natural curiosity, of course, made me make a dangerous turn towards North American oil major stock prices outlook.

I was curious to investigate the fact of how badly our Canadian oil companies suffered during downturn in comparison to US oil majors and do the oil stocks show some signs of recovery. 

1.     Downloading the sock price data from Yahoo Finance and merging US and Canada datasets for further analysis.

After importing Python data analysis and visualization libraries I used Pandas Web Datareader to download the stocks prices for large Canadian and US oil producers during the period 2016-2021.

start = datetime.datetime(2016, 3, 1)


end = datetime.datetime(2021, 3, 1)

The oil majors stock prices I was looking at:

In Canada – Suncor, Canadian Natural Resources (CNRL), Imperial and Cenovus.

Suncor = web.DataReader("SU", 'yahoo', start, end)
CNRL = web.DataReader("CNQ", 'yahoo', start, end)
Cenovus = web.DataReader("CVE", 'yahoo', start, end)
Imperial = web.DataReader("IMO", 'yahoo', start, end

In the United States – ExxonMobil, Chevron, ConocoPhillips and EOG Resources.

ExxonMobil = web.DataReader("XOM", 'yahoo', start, end)
Chevron = web.DataReader("CVX", 'yahoo', start, end)
ConocoPhillips = web.DataReader("COP", 'yahoo', start, end)
EOGResources = web.DataReader("EOG", 'yahoo', start, end)


I have eventually created 2 separate datasets for US and Canadian companies with entities and stock prices for the given period:

CAD_stocks = ["SU", "CNQ", "CVE", "IMO"]
cop = pd.concat([Suncor, CNRL, Cenovus, Imperial], axis=1, keys=CAD_stocks)
cop.columns.names = ['Entity','Stock']
cop.head()


USA_stocks = ["XOM", "CVX", "COP", "EOG"]
usop = pd.concat([ExxonMobil, Chevron, ConocoPhillips, EOGResources], axis=1, keys=USA_stocks)
usop.columns.names = ['Entity','Stock']
usop.head()

No alt text provided for this image
No alt text provided for this image

2.     Looking at the max/min stock prices for 5-year period and visualizing the results.

I generated min and max stock price output using aggregate functions. As can be observed from the Table 1 data below the highest drop in a price occurred in March 2020 when there was a pandemic hit. Canadian oil majors were hurt badly especially Cenovus who suffered 10 times stock price drop. 

max_CAD_stock = cop.xs(key='Adj Close',axis=1,level='Stock').max()
max_CAD_stock

Entity
SU     38.311535
CNQ    32.249527
CVE    15.258459
IMO    33.072918
dtype: float64

max_US_stock = usop.xs(key='Adj Close',axis=1,level='Stock').max()

Entity
XOM     75.144188
CVX    115.743240
COP     74.130875
EOG    125.900688

dtype: float64

min_CAD_stock = cop.xs(key='Close',axis=1,level='Stock').min()

Entity
SU     10.45
CNQ     7.74
CVE     1.60
IMO     7.83
dtype: float64

min_US_stock = usop.xs(key='Close',axis=1,level='Stock').min()
Entity
XOM    31.450001
CVX    54.220001
COP    22.670000
EOG    29.760000
dtype: float64

At the same time US oil majors such as ExxonMobil and Chevron showed 2 times reduction in stock prices while ConocoPhillips and EOG Resources 3 and 4 times drop, respectively. In terms of the uplift, the results look promising, most large oil producers showed a speedy recovery in stock prices (2 times from March 2020). Considering the fact of recent acquisitions for Cenovus and Canadian Natural, these companies show quite significant growth, especially CNRL that bounced back to its 5-year maximum.  

Table 1: Minimum and Maximum Stock Prices in 5-year period for Canadian and US oil producers.

No alt text provided for this image

I used bar plots for visualising the information about min and max stock prices(Figure 1).

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=[16, 8])
axes = axes.flatten()


sns.barplot(max_CAD_stock.index, max_CAD_stock.values, ax=axes[0])
axes.flatten()[0].set_ylim(0,130)
axes.flatten()[0].set_ylabel('Max_CAD_Stock_Price - $')


sns.barplot(min_CAD_stock.index, min_CAD_stock.values, ax=axes[1])
axes.flatten()[1].set_ylim(0,130)
axes.flatten()[1].set_ylabel('Min_CAD_Stock_Price - $')
plt.tight_layout()


sns.barplot(max_US_stock.index, max_US_stock.values, ax=axes[2])
axes.flatten()[2].set_ylim(0,130)
axes.flatten()[2].set_ylabel('Max_US_Stock_Price - $')


sns.barplot(min_US_stock.index, min_US_stock.values, ax=axes[3])
axes.flatten()[3].set_ylim(0,130)
axes.flatten()[3].set_ylabel('Min_US_Stock_Price - $')
plt.tight_layout()

Figure 1: Comparison of Minimum and Maximum Stock Prices for Selected Oil Majors.

No alt text provided for this image

3.     Looking at the visualization of stock prices for Canadian and US oil majors for year 2020.

To visualize the results and look at oil stock price recovery, I used Python plotting libraries such as matplotlib and plotly (Figures 2-3). 

Figure 2: Canadian Oil Companies Stock Prices Distribution for Year 2020.

for stock in CAD_stocks:
    cop[stock]['Adj Close'].loc['2020-01-01':'2021-03-01'].plot(figsize=(16,10),label=stock)
plt.legend()
No alt text provided for this image

Figure 3: US Oil Companies Stock Prices Distribution for Year 2020.

for stock in USA_stocks:
    usop[stock]['Adj Close'].loc['2020-01-01':'2021-03-01'].plot(figsize=(16,10),label=stock)
plt.legend()
No alt text provided for this image

Starting end of 2020 all oil majors started showing the stock price recovery and that tendency keeps growing.

The other nice way to visualize the same results is the candlestick chart. It is a style of financial chart describing open, high, low and close for a given time. In my case, I was curious to see the visualizations for pandemic year 2020 and, of course, hope for the best in terms of anticipated recovery.

Suncor_fltr = web.DataReader("SU", 'yahoo', start, end)
Suncor_fltr = Suncor_fltr[Suncor_fltr.index.isin(dt_range)]

fig = go.Figure(data=[go.Candlestick(x=Suncor_fltr.index,
                open=Suncor_fltr['Open'],
                high=Suncor_fltr['High'],
                low=Suncor_fltr['Low'],
                close=Suncor_fltr['Close'])])
                      


fig.update_layout(
    title='The Pandemic Downturn',
    yaxis_title='Suncor Stock Price, USD',
    shapes = [dict(
        x0='2020-03-01', x1='2020-03-01', y0=0, y1=1, xref='x', yref='paper',
        line_width=2)],
    annotations=[dict(
        x='2020-03-01', y=0.05, xref='x', yref='paper',
        showarrow=False, xanchor='right', text='Pandemic Starts')]
)


fig.show()

As an example, I used largest oil producers such as Suncor and ExxonMobil to visualize the results of stock price data(Figures 4-5).

Figure 4: Suncor Stock Price Candlestick Chart for Year 2020.

No alt text provided for this image

Figure 5: ExxonMobil Stock Price Candlestick Chart for Year 2020.

No alt text provided for this image

Both companies show fair recovery in their stock prices – thanks to global oil price recovery and the combination of other favorable conditions.

At the end, I wanted to share my exploratory stock price data analysis through my GitHub account for those who is interested in detailed coding samples:

https://github.com/vadimsavenkov/Stock-Price-Analysis/blob/master/Stock_Analysis.ipynb

I am open for any comments or suggestions on how I can improve things as a beginner and contribute more. Fill free to reach me and share your thoughts about your personal data career experience.

We live in interesting times – moving towards a renewable energy, acquisitions, takeovers, new technologies, digitalization and so on. Does the global energy market show the signs of recovery – to some extend, yes, but what it means for us as individuals (especially, for oil and gas career pivoting into new sectors) during this transitioning period of energy ecosystem? Does it show the light at the end of the tunnel or we just so exited and do not see another heavy train behind it?

#python #datascience #machinelearning #analytics

To view or add a comment, sign in

Others also viewed

Explore content categories