logonetrans.png

STOCK MARKET ANALYSIS¶

We'll be analyzing stock data related to a Three Canadian Energy Companies, from Jan 1 2017 to April 21 2023. The companies are:

1) B2Gold Corp (BTO.TO)¶
2) Tamarack Valley Energy(TVE.TO)¶
3) Arbor Metals Corp (ABR.V)¶

Part 0 : Import Lybraries¶

In [1]:
import pandas as pd
import datetime as dt
import yfinance as yf
import numpy as np
import math
import matplotlib.pyplot as plt
%matplotlib inline

Part 1 : Getting Data¶

B2Gold Corp¶

In [2]:
company1 = 'BTO.TO'
start = dt.datetime(2017,1,1)
end = dt.datetime(2023,4,21)
dataBTO = yf.download(company1,start,end)
[*********************100%***********************]  1 of 1 completed
In [3]:
dataBTO.head(10)
Out[3]:
Open High Low Close Adj Close Volume
Date
2017-01-03 3.24 3.39 3.20 3.39 3.017571 7465100
2017-01-04 3.44 3.45 3.32 3.36 2.990867 7361500
2017-01-05 3.45 3.72 3.44 3.68 3.275711 16209900
2017-01-06 3.58 3.70 3.47 3.56 3.168895 11437400
2017-01-09 3.66 3.71 3.55 3.57 3.177796 8227400
2017-01-10 3.60 3.70 3.60 3.64 3.240106 7229300
2017-01-11 3.63 3.70 3.51 3.63 3.231204 8585200
2017-01-12 3.70 3.75 3.56 3.61 3.213402 6740400
2017-01-13 3.57 3.65 3.49 3.64 3.240106 6482100
2017-01-16 3.68 3.70 3.64 3.68 3.275711 1891700
In [33]:
dataBTO.to_csv(r'H:\Data Science\Day Trading\python -finances\dataBTO.csv', index=True, encoding = "utf-8")

Tamarack Valley Energy¶

In [4]:
company2 = 'TVE.TO'
start = dt.datetime(2017,1,1)
end = dt.datetime(2023,4,21)
dataTVE = yf.download(company2,start,end)
[*********************100%***********************]  1 of 1 completed
In [5]:
dataTVE.head(10)
Out[5]:
Open High Low Close Adj Close Volume
Date
2017-01-03 3.51 3.58 3.24 3.32 3.196805 1732700
2017-01-04 3.31 3.41 3.26 3.31 3.187176 717300
2017-01-05 3.35 3.48 3.33 3.47 3.341239 4660500
2017-01-06 3.50 3.59 3.45 3.53 3.399013 853600
2017-01-09 3.49 3.55 3.44 3.47 3.341239 539000
2017-01-10 3.48 3.50 3.43 3.45 3.321981 589200
2017-01-11 3.47 3.51 3.44 3.44 3.312353 480200
2017-01-12 3.53 3.53 3.43 3.50 3.370126 232700
2017-01-13 3.45 3.52 3.44 3.48 3.350868 108200
2017-01-16 3.48 3.48 3.37 3.40 3.273837 264500
In [36]:
dataTVE.to_csv(r'H:\Data Science\Day Trading\python -finances\dataTVE.csv', index=True, encoding = "utf-8")

Arbor Metals Corp¶

In [6]:
company3 = 'ABR.V'
start = dt.datetime(2017,1,1)
end = dt.datetime(2023,4,21)
dataABR = yf.download(company3,start,end)
[*********************100%***********************]  1 of 1 completed
In [7]:
dataABR.head(10)
Out[7]:
Open High Low Close Adj Close Volume
Date
2017-01-03 0.068571 0.068571 0.068571 0.068571 0.068571 0
2017-01-04 0.068571 0.068571 0.068571 0.068571 0.068571 0
2017-01-05 0.068571 0.068571 0.068571 0.068571 0.068571 0
2017-01-06 0.068571 0.068571 0.068571 0.068571 0.068571 0
2017-01-09 0.068571 0.068571 0.068571 0.068571 0.068571 0
2017-01-10 0.068571 0.068571 0.068571 0.068571 0.068571 0
2017-01-11 0.068571 0.068571 0.068571 0.068571 0.068571 3500
2017-01-12 0.068571 0.068571 0.068571 0.068571 0.068571 0
2017-01-13 0.068571 0.068571 0.068571 0.068571 0.068571 0
2017-01-16 0.068571 0.068571 0.068571 0.068571 0.068571 0
In [39]:
dataABR.to_csv(r'H:\Data Science\Day Trading\python -finances\dataABR.csv', index=True, encoding = "utf-8")

Part 2: Visualizing Data¶

In [8]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['Open'].plot(label='B2GOLD',figsize=(16,8),title='Open Price')
dataTVE['Open'].plot(label='Tamarack Valley Eneregy')
dataABR['Open'].plot(label='Arbor Metals')
plt.legend()
Out[8]:
<matplotlib.legend.Legend at 0x2086d86e1d0>

Plot the Volume of Stock Traded each day¶

In [9]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['Volume'].plot(label='B2GOLD',figsize=(16,8),title='Volume Traded')
dataTVE['Volume'].plot(label='Tamarack Valley Eneregy')
dataABR['Volume'].plot(label='Arbor Metals')
plt.legend()
Out[9]:
<matplotlib.legend.Legend at 0x2086dba2150>

** Interesting, looks like B2GOLD had a really big spike somewhere in late 2021. What was the date of this maximum trading volume for B2GOLD?**

What happened that day?

** Obtain the exactly Date of this maximum trading volume

In [10]:
dataBTO[dataBTO['Volume']==dataBTO['Volume'].max()]
Out[10]:
Open High Low Close Adj Close Volume
Date
2021-11-30 5.17 5.28 5.03 5.06 4.739866 79861800

what happened this date?(2021-11-30):¶

https://stockhouse.com/news/press-releases/2021/11/30/b2gold-completes-the-sale-of-burkina-faso-projects#:~:text=VANCOUVER%2C%20BC%2C%20November%2030%2C%202021%20%2FPRNewswire%2F%20-%20B2Gold,%28%22%20WAF%20%22%29%20%28the%20%22%20Kiaka%20Transaction%20%22%29.

"is pleased to announce that it has completed the sale of its 81% interest in the Kiaka gold project located in Burkina Faso (the "Kiaka Project") to West African Resources Limited (ASX:WAF) ("WAF")(the "Kiaka Transaction"). Pursuant to the terms of the Kiaka Transaction, on closing B2Gold received a cash payment of US$22.5 million (in addition to the US$450,000 already received), 22,190,508 ordinary shares of WAF ("WAF Shares"), and a 2.7% net smelter return ("NSR") royalty interest on the first 2,500,000 ounces of gold produced at the Kiaka Project and thereafter a 0.45% NSR royalty interest on the next 1,500,000 ounces of gold produced"

According to the company's press release, that day the sale of a business project in Burkina Faso was closed, resulting in a profit of 22 million for the company.

In [11]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['Open'].plot(figsize=(20,6))
plt.title('B2GOLD OPEN PRICE')
Out[11]:
Text(0.5, 1.0, 'B2GOLD OPEN PRICE')

Calculate the Total Amount of Money being Traded around¶

In [12]:
dataBTO['Total Traded'] = dataBTO['Open']*dataBTO['Volume']
dataTVE['Total Traded'] = dataTVE['Open']*dataTVE['Volume']
dataABR['Total Traded'] = dataABR['Open']*dataABR['Volume']
In [13]:
dataBTO.head(10)
Out[13]:
Open High Low Close Adj Close Volume Total Traded
Date
2017-01-03 3.24 3.39 3.20 3.39 3.017571 7465100 2.418692e+07
2017-01-04 3.44 3.45 3.32 3.36 2.990867 7361500 2.532356e+07
2017-01-05 3.45 3.72 3.44 3.68 3.275711 16209900 5.592416e+07
2017-01-06 3.58 3.70 3.47 3.56 3.168895 11437400 4.094589e+07
2017-01-09 3.66 3.71 3.55 3.57 3.177796 8227400 3.011228e+07
2017-01-10 3.60 3.70 3.60 3.64 3.240106 7229300 2.602548e+07
2017-01-11 3.63 3.70 3.51 3.63 3.231204 8585200 3.116428e+07
2017-01-12 3.70 3.75 3.56 3.61 3.213402 6740400 2.493948e+07
2017-01-13 3.57 3.65 3.49 3.64 3.240106 6482100 2.314110e+07
2017-01-16 3.68 3.70 3.64 3.68 3.275711 1891700 6.961456e+06
In [14]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['Total Traded'].plot(label='BTO',figsize=(16,8))
dataTVE['Total Traded'].plot(label='TVE')
dataABR['Total Traded'].plot(label='ABR')
plt.legend()
plt.ylabel('Total Traded')
plt.title('TOTAL VOLUME TRADED')
Out[14]:
Text(0.5, 1.0, 'TOTAL VOLUME TRADED')

Interesting, looks like there was huge amount of money traded for BTO somewhere in the middle of 2020 and late 2021. What date was that and what happened?

What hapenned those days?

Obtain the exactly Date of this maximum trading volume.

In [15]:
dataBTO[dataBTO['Total Traded']==dataBTO['Total Traded'].max()]
Out[15]:
Open High Low Close Adj Close Volume Total Traded
Date
2020-05-29 7.31 7.61 7.29 7.59 6.789467 72605900 5.307491e+08
In [16]:
dataBTO['Total Traded'].nlargest(2)
Out[16]:
Date
2020-05-29    5.307491e+08
2021-11-30    4.128855e+08
Name: Total Traded, dtype: float64

The results show the same previous dates as maximum volume of stock traded.

Plotting out Moving Averages (MA) for B2GOLD. MA50 and MA200¶

In [55]:
dataBTO['MA50'] = dataBTO['Open'].rolling(50).mean()
dataBTO['MA200'] = dataBTO['Open'].rolling(200).mean()
dataBTO[['Open','MA50','MA200']].plot(label='BTO',figsize=(16,8), title = 'MOVING AVERAGES FOR B2GOLD MA50 AND MA200');

Doing the Moving Averages analysis for 50 and 200 average days (MA50, MA200), we can conclude that when the MA50 line is above the MA200 line, the price tends to rise, on the contrary, when the MA50 line is below the MA200 line, the price tends to go down. Observing the graph, we can see that the price of the stock for B2GOLD is rising.

Figure out if there is a relationship between these stocks¶

In [18]:
from pandas.plotting import scatter_matrix
In [19]:
Stock_comp = pd.concat([dataBTO['Open'],dataTVE['Open'],dataABR['Open']],axis=1)
In [20]:
Stock_comp.head(-10)
Out[20]:
Open Open Open
Date
2017-01-03 3.24 3.51 0.068571
2017-01-04 3.44 3.31 0.068571
2017-01-05 3.45 3.35 0.068571
2017-01-06 3.58 3.50 0.068571
2017-01-09 3.66 3.49 0.068571
... ... ... ...
2023-03-30 5.41 4.01 3.190000
2023-03-31 5.40 3.98 3.080000
2023-04-03 5.30 4.20 3.190000
2023-04-04 5.37 4.22 2.800000
2023-04-05 5.64 4.15 2.750000

1571 rows × 3 columns

In [21]:
Stock_comp.columns = ['BTO Open','TVE Open','ABR Open']
Stock_comp.columns
Out[21]:
Index(['BTO Open', 'TVE Open', 'ABR Open'], dtype='object')
In [22]:
Stock_comp.head(10)
Out[22]:
BTO Open TVE Open ABR Open
Date
2017-01-03 3.24 3.51 0.068571
2017-01-04 3.44 3.31 0.068571
2017-01-05 3.45 3.35 0.068571
2017-01-06 3.58 3.50 0.068571
2017-01-09 3.66 3.49 0.068571
2017-01-10 3.60 3.48 0.068571
2017-01-11 3.63 3.47 0.068571
2017-01-12 3.70 3.53 0.068571
2017-01-13 3.57 3.45 0.068571
2017-01-16 3.68 3.48 0.068571
In [23]:
scatter_matrix(Stock_comp,figsize=(8,8),alpha=0.2,hist_kwds={'bins':50});

We can see in the graph that there is an inverse correlation between TVE and BTO, when the price of BTO goes up, the price of TVE goes down and vice versa.

Candlestick chart for B2GOLD: March 2023¶

In [24]:
import mplfinance
from mplfinance.original_flavor import candlestick_ohlc
from matplotlib.dates import DateFormatter, date2num, WeekdayLocator, DayLocator, MONDAY
In [25]:
dataBTO_reset=dataBTO.loc['2023-03'].reset_index()
In [26]:
dataBTO_reset
Out[26]:
Date Open High Low Close Adj Close Volume Total Traded MA50 MA200
0 2023-03-01 4.70 4.92 4.69 4.88 4.824639 5435300 2.554591e+07 5.0152 4.64210
1 2023-03-02 4.87 4.87 4.77 4.84 4.785092 3840600 1.870372e+07 5.0210 4.64165
2 2023-03-03 4.93 4.94 4.81 4.85 4.794979 2396500 1.181474e+07 5.0266 4.64065
3 2023-03-06 4.81 4.83 4.73 4.76 4.706000 2835200 1.363731e+07 5.0292 4.63835
4 2023-03-07 4.65 4.66 4.54 4.59 4.590000 2253000 1.047645e+07 5.0260 4.63585
5 2023-03-08 4.60 4.66 4.52 4.56 4.560000 4198400 1.931264e+07 5.0222 4.63285
6 2023-03-09 4.60 4.62 4.51 4.51 4.510000 2529500 1.163570e+07 5.0170 4.62915
7 2023-03-10 4.61 4.69 4.57 4.60 4.600000 3805100 1.754151e+07 5.0118 4.62550
8 2023-03-13 4.76 4.86 4.72 4.80 4.800000 5264900 2.506093e+07 5.0096 4.62280
9 2023-03-14 4.77 4.83 4.71 4.80 4.800000 2805700 1.338319e+07 5.0080 4.62020
10 2023-03-15 4.90 4.95 4.79 4.83 4.830000 3293300 1.613717e+07 5.0078 4.61845
11 2023-03-16 4.81 4.86 4.69 4.83 4.830000 2344000 1.127464e+07 5.0020 4.61670
12 2023-03-17 4.91 5.06 4.82 5.01 5.010000 8059100 3.957018e+07 4.9970 4.61565
13 2023-03-20 5.09 5.09 5.00 5.08 5.080000 3616400 1.840748e+07 4.9938 4.61570
14 2023-03-21 4.98 5.02 4.88 4.91 4.910000 3798300 1.891553e+07 4.9870 4.61485
15 2023-03-22 4.91 5.07 4.89 4.99 4.990000 3857100 1.893836e+07 4.9820 4.61295
16 2023-03-23 5.03 5.19 4.98 5.14 5.140000 4816800 2.422851e+07 4.9766 4.61175
17 2023-03-24 5.17 5.26 5.13 5.22 5.220000 3206600 1.657812e+07 4.9732 4.61185
18 2023-03-27 5.12 5.31 5.07 5.29 5.290000 3082100 1.578035e+07 4.9682 4.61175
19 2023-03-28 5.30 5.46 5.29 5.45 5.450000 3397900 1.800887e+07 4.9630 4.61255
20 2023-03-29 5.40 5.45 5.36 5.38 5.380000 3368000 1.818720e+07 4.9618 4.61465
21 2023-03-30 5.41 5.44 5.37 5.40 5.400000 2256300 1.220658e+07 4.9608 4.61605
22 2023-03-31 5.40 5.42 5.32 5.34 5.340000 2998700 1.619298e+07 4.9598 4.61805
In [27]:
dataBTO_reset['date_ax']=dataBTO_reset['Date'].apply(lambda date:date2num(date))
In [28]:
dataBTO_reset
Out[28]:
Date Open High Low Close Adj Close Volume Total Traded MA50 MA200 date_ax
0 2023-03-01 4.70 4.92 4.69 4.88 4.824639 5435300 2.554591e+07 5.0152 4.64210 19417.0
1 2023-03-02 4.87 4.87 4.77 4.84 4.785092 3840600 1.870372e+07 5.0210 4.64165 19418.0
2 2023-03-03 4.93 4.94 4.81 4.85 4.794979 2396500 1.181474e+07 5.0266 4.64065 19419.0
3 2023-03-06 4.81 4.83 4.73 4.76 4.706000 2835200 1.363731e+07 5.0292 4.63835 19422.0
4 2023-03-07 4.65 4.66 4.54 4.59 4.590000 2253000 1.047645e+07 5.0260 4.63585 19423.0
5 2023-03-08 4.60 4.66 4.52 4.56 4.560000 4198400 1.931264e+07 5.0222 4.63285 19424.0
6 2023-03-09 4.60 4.62 4.51 4.51 4.510000 2529500 1.163570e+07 5.0170 4.62915 19425.0
7 2023-03-10 4.61 4.69 4.57 4.60 4.600000 3805100 1.754151e+07 5.0118 4.62550 19426.0
8 2023-03-13 4.76 4.86 4.72 4.80 4.800000 5264900 2.506093e+07 5.0096 4.62280 19429.0
9 2023-03-14 4.77 4.83 4.71 4.80 4.800000 2805700 1.338319e+07 5.0080 4.62020 19430.0
10 2023-03-15 4.90 4.95 4.79 4.83 4.830000 3293300 1.613717e+07 5.0078 4.61845 19431.0
11 2023-03-16 4.81 4.86 4.69 4.83 4.830000 2344000 1.127464e+07 5.0020 4.61670 19432.0
12 2023-03-17 4.91 5.06 4.82 5.01 5.010000 8059100 3.957018e+07 4.9970 4.61565 19433.0
13 2023-03-20 5.09 5.09 5.00 5.08 5.080000 3616400 1.840748e+07 4.9938 4.61570 19436.0
14 2023-03-21 4.98 5.02 4.88 4.91 4.910000 3798300 1.891553e+07 4.9870 4.61485 19437.0
15 2023-03-22 4.91 5.07 4.89 4.99 4.990000 3857100 1.893836e+07 4.9820 4.61295 19438.0
16 2023-03-23 5.03 5.19 4.98 5.14 5.140000 4816800 2.422851e+07 4.9766 4.61175 19439.0
17 2023-03-24 5.17 5.26 5.13 5.22 5.220000 3206600 1.657812e+07 4.9732 4.61185 19440.0
18 2023-03-27 5.12 5.31 5.07 5.29 5.290000 3082100 1.578035e+07 4.9682 4.61175 19443.0
19 2023-03-28 5.30 5.46 5.29 5.45 5.450000 3397900 1.800887e+07 4.9630 4.61255 19444.0
20 2023-03-29 5.40 5.45 5.36 5.38 5.380000 3368000 1.818720e+07 4.9618 4.61465 19445.0
21 2023-03-30 5.41 5.44 5.37 5.40 5.400000 2256300 1.220658e+07 4.9608 4.61605 19446.0
22 2023-03-31 5.40 5.42 5.32 5.34 5.340000 2998700 1.619298e+07 4.9598 4.61805 19447.0
In [28]:
dataBTO_values = [tuple(vals) for vals in dataBTO_reset[['date_ax', 'Open', 'High', 'Low', 'Close']].values]
In [29]:
dataBTO_values
Out[29]:
[(19417.0,
  4.699999809265137,
  4.920000076293945,
  4.690000057220459,
  4.880000114440918),
 (19418.0,
  4.869999885559082,
  4.869999885559082,
  4.769999980926514,
  4.840000152587891),
 (19419.0,
  4.929999828338623,
  4.940000057220459,
  4.809999942779541,
  4.849999904632568),
 (19422.0,
  4.809999942779541,
  4.829999923706055,
  4.730000019073486,
  4.760000228881836),
 (19423.0,
  4.650000095367432,
  4.659999847412109,
  4.539999961853027,
  4.590000152587891),
 (19424.0,
  4.599999904632568,
  4.659999847412109,
  4.519999980926514,
  4.559999942779541),
 (19425.0,
  4.599999904632568,
  4.619999885559082,
  4.510000228881836,
  4.510000228881836),
 (19426.0,
  4.610000133514404,
  4.690000057220459,
  4.570000171661377,
  4.599999904632568),
 (19429.0,
  4.760000228881836,
  4.860000133514404,
  4.71999979019165,
  4.800000190734863),
 (19430.0,
  4.769999980926514,
  4.829999923706055,
  4.710000038146973,
  4.800000190734863),
 (19431.0,
  4.900000095367432,
  4.949999809265137,
  4.789999961853027,
  4.829999923706055),
 (19432.0,
  4.809999942779541,
  4.860000133514404,
  4.690000057220459,
  4.829999923706055),
 (19433.0,
  4.909999847412109,
  5.059999942779541,
  4.820000171661377,
  5.010000228881836),
 (19436.0, 5.090000152587891, 5.090000152587891, 5.0, 5.079999923706055),
 (19437.0,
  4.980000019073486,
  5.019999980926514,
  4.880000114440918,
  4.909999847412109),
 (19438.0,
  4.909999847412109,
  5.070000171661377,
  4.889999866485596,
  4.989999771118164),
 (19439.0,
  5.03000020980835,
  5.190000057220459,
  4.980000019073486,
  5.139999866485596),
 (19440.0,
  5.170000076293945,
  5.260000228881836,
  5.130000114440918,
  5.21999979019165),
 (19443.0,
  5.119999885559082,
  5.309999942779541,
  5.070000171661377,
  5.289999961853027),
 (19444.0,
  5.300000190734863,
  5.460000038146973,
  5.289999961853027,
  5.449999809265137),
 (19445.0,
  5.400000095367432,
  5.449999809265137,
  5.360000133514404,
  5.380000114440918),
 (19446.0,
  5.409999847412109,
  5.440000057220459,
  5.369999885559082,
  5.400000095367432),
 (19447.0,
  5.400000095367432,
  5.420000076293945,
  5.320000171661377,
  5.340000152587891)]
In [30]:
mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
alldays = DayLocator()              # minor ticks on the days
weekFormatter = DateFormatter('%b %d')  # e.g., Jan 12
dayFormatter = DateFormatter('%d')      # e.g., 12
In [31]:
#Plot it

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)
plt.title("Period: March 2023")
plt.suptitle(' CANDLESTICK CHART FOR B2GOLD')

candlestick_ohlc(ax, dataBTO_values, width=0.6, colorup='g',colordown='r');

Observing the candlestick chart, we can see that the first week of March was down with all the red candles pushing down, the week of March 13 begins the rise as you can see the green candles until March 28, then the three consecutive days (29,30,31) were going down, closing the month of March.

Part 3: Basic Financial Analysis¶

Calculating Daily Returns¶

Daily Returns inform us the precentage gain or loss if we buy the stock one day and sell it the another day. This ratio is very useful in analyzing the VOLATILITY of the stock. If Daily Returns have a wide distribution, the stock is more volatile, more risky but give you more return. The formula is as follow:

$ r_t = \frac{p_t}{p_{t-1}} -1$

Daily Returns: B2GOLD (BTO)¶

In [32]:
dataBTO['returns'] = (dataBTO['Close'] / dataBTO['Close'].shift(1) ) - 1
dataBTO.head()
Out[32]:
Open High Low Close Adj Close Volume Total Traded MA50 MA200 returns
Date
2017-01-03 3.24 3.39 3.20 3.39 3.017571 7465100 2.418692e+07 NaN NaN NaN
2017-01-04 3.44 3.45 3.32 3.36 2.990867 7361500 2.532356e+07 NaN NaN -0.008850
2017-01-05 3.45 3.72 3.44 3.68 3.275711 16209900 5.592416e+07 NaN NaN 0.095238
2017-01-06 3.58 3.70 3.47 3.56 3.168895 11437400 4.094589e+07 NaN NaN -0.032609
2017-01-09 3.66 3.71 3.55 3.57 3.177796 8227400 3.011228e+07 NaN NaN 0.002809
In [33]:
dataBTO['returns'] = dataBTO['Close'].pct_change(1)
dataBTO.head()
Out[33]:
Open High Low Close Adj Close Volume Total Traded MA50 MA200 returns
Date
2017-01-03 3.24 3.39 3.20 3.39 3.017571 7465100 2.418692e+07 NaN NaN NaN
2017-01-04 3.44 3.45 3.32 3.36 2.990867 7361500 2.532356e+07 NaN NaN -0.008850
2017-01-05 3.45 3.72 3.44 3.68 3.275711 16209900 5.592416e+07 NaN NaN 0.095238
2017-01-06 3.58 3.70 3.47 3.56 3.168895 11437400 4.094589e+07 NaN NaN -0.032609
2017-01-09 3.66 3.71 3.55 3.57 3.177796 8227400 3.011228e+07 NaN NaN 0.002809
In [34]:
coeff_var_BTO = np.std(dataBTO['returns']) / np.mean(dataBTO['returns'])
coeff_var_BTO
Out[34]:
39.76236729652576

Daily Returns: Tamarack Valley Energy (TVE)¶

In [35]:
dataTVE['returns'] = dataTVE['Close'].pct_change(1)
dataTVE.head()
Out[35]:
Open High Low Close Adj Close Volume Total Traded returns
Date
2017-01-03 3.51 3.58 3.24 3.32 3.196805 1732700 6.081777e+06 NaN
2017-01-04 3.31 3.41 3.26 3.31 3.187176 717300 2.374263e+06 -0.003012
2017-01-05 3.35 3.48 3.33 3.47 3.341239 4660500 1.561267e+07 0.048338
2017-01-06 3.50 3.59 3.45 3.53 3.399013 853600 2.987600e+06 0.017291
2017-01-09 3.49 3.55 3.44 3.47 3.341239 539000 1.881110e+06 -0.016997
In [36]:
coeff_var_TVE = np.std(dataTVE['returns']) / np.mean(dataTVE['returns'])
coeff_var_TVE
Out[36]:
46.62249931509346

Daily Returns: Arbor Metals Corp (ABR)¶

In [37]:
dataABR['returns'] = dataABR['Close'].pct_change(1)
dataABR.head()
Out[37]:
Open High Low Close Adj Close Volume Total Traded returns
Date
2017-01-03 0.068571 0.068571 0.068571 0.068571 0.068571 0 0.0 NaN
2017-01-04 0.068571 0.068571 0.068571 0.068571 0.068571 0 0.0 0.0
2017-01-05 0.068571 0.068571 0.068571 0.068571 0.068571 0 0.0 0.0
2017-01-06 0.068571 0.068571 0.068571 0.068571 0.068571 0 0.0 0.0
2017-01-09 0.068571 0.068571 0.068571 0.068571 0.068571 0 0.0 0.0
In [38]:
coeff_var_ABR = np.std(dataABR['returns']) / np.mean(dataABR['returns'])
coeff_var_ABR
Out[38]:
14.47676557139699

Plotting Histograms for each Stock¶

B2Gold (BTO)¶

In [39]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['returns'].hist(bins=50)
plt.title('HISTOGRAM CHART FOR B2GOLD');

Moderate Distribution

Tamarack Valley Energy (TVE)¶

In [40]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataTVE['returns'].hist(bins=50)
plt.title('HISTOGRAM CHART FOR TAMARACK VALLEY ENERGY');

Moderate Distribution

Arbor Metals Corp (ABR)¶

In [41]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataABR['returns'].hist(bins=50)
plt.title('HISTOGRAM CHART FOR ARBOR METALS CORP');

Apparently show a Wide Distribution but we should take into account that for three years the Stock had not change

Comparing the three stocks¶

In [42]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['returns'].hist(bins=100,label='BTO',figsize=(25,15),alpha=0.5)
dataTVE['returns'].hist(bins=100,label='TVE',alpha=0.5)
dataABR['returns'].hist(bins=100,label='ABR',alpha=0.5)
plt.legend();
plt.title('COMPARING THREE STOCKS')
Out[42]:
Text(0.5, 1.0, 'COMPARING THREE STOCKS')

Arbor Metals Corp show a Wide Distributions but the most of Returns are around Cero. TVE looks like a little be more risky than BTO.

In [43]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['returns'].plot(kind='kde',label='BTO',figsize=(12,6))
dataTVE['returns'].plot(kind='kde',label='TVE')
dataABR['returns'].plot(kind='kde',label='ABR')
plt.legend();

Box and whisker chart¶

In [60]:
box_df = pd.concat([dataBTO['returns'],dataTVE['returns'],dataABR['returns']],axis=1)
box_df.columns = ['BTO Returns',' TVE Returns','ABR Returns']
box_df.plot(kind='box',figsize=(7,8),colormap='jet')
plt.title("BOX AND WHISKER CHART FOR THE THREE STOCKS");

If we only analyze the graph, at first glance we could conclude that Arbor Metal Corp Stock is more volatile and therefore, riskier. But previously we calculated the coefficient of variation for each of the stocks, resulting in the following:

Coefficient of variation BTO = 39.76

Coeffiecient of Variations TVE = 46.62

Coefficient of Variations ABR = 14.48

We can see thet Tamarack Valley Energy has the highest value of the coefficient, being the most risky Stock, but could give you the highest Return.

scatter_matrix(box_df,figsize=(8,8),alpha=0.2,hist_kwds={'bins':50});

In [45]:
box_df.plot(kind='scatter',x='BTO Returns',y=' TVE Returns',alpha=0.4,figsize=(10,8));

The scatter graph betwee BTO and TVE Stock shows very similar Returns for both.

Cumulative Daily Returns¶

In [46]:
dataBTO['Cumulative Return'] = (1 + dataBTO['returns']).cumprod()
In [47]:
dataBTO.head()
Out[47]:
Open High Low Close Adj Close Volume Total Traded MA50 MA200 returns Cumulative Return
Date
2017-01-03 3.24 3.39 3.20 3.39 3.017571 7465100 2.418692e+07 NaN NaN NaN NaN
2017-01-04 3.44 3.45 3.32 3.36 2.990867 7361500 2.532356e+07 NaN NaN -0.008850 0.991150
2017-01-05 3.45 3.72 3.44 3.68 3.275711 16209900 5.592416e+07 NaN NaN 0.095238 1.085546
2017-01-06 3.58 3.70 3.47 3.56 3.168895 11437400 4.094589e+07 NaN NaN -0.032609 1.050147
2017-01-09 3.66 3.71 3.55 3.57 3.177796 8227400 3.011228e+07 NaN NaN 0.002809 1.053097
In [48]:
dataTVE['Cumulative Return'] = (1 + dataTVE['returns']).cumprod()
dataABR['Cumulative Return'] = (1 + dataABR['returns']).cumprod()
In [49]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['Cumulative Return'].plot(label='BTO',figsize=(16,8),title='Cumulative Return')
dataTVE['Cumulative Return'].plot(label='TVE')
dataABR['Cumulative Return'].plot(label='ABR')
plt.legend()
Out[49]:
<matplotlib.legend.Legend at 0x2087363f110>
In [50]:
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['Cumulative Return'].plot(label='BTO',figsize=(16,8),title='Cumulative Return')
dataTVE['Cumulative Return'].plot(label='TVE')
plt.legend()
Out[50]:
<matplotlib.legend.Legend at 0x2086a2fead0>

The cumulative return shows the percentage gain or loss of the stock over time. We can see that looking at the cumulative returns of the three stocks under analysis, Arbor Metal Corp is the one that has generated the highest cumulative returns.

In [ ]: