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
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
dataBTO.head(10)
| 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 |
dataBTO.to_csv(r'H:\Data Science\Day Trading\python -finances\dataBTO.csv', index=True, encoding = "utf-8")
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
dataTVE.head(10)
| 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 |
dataTVE.to_csv(r'H:\Data Science\Day Trading\python -finances\dataTVE.csv', index=True, encoding = "utf-8")
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
dataABR.head(10)
| 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 |
dataABR.to_csv(r'H:\Data Science\Day Trading\python -finances\dataABR.csv', index=True, encoding = "utf-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()
<matplotlib.legend.Legend at 0x2086d86e1d0>
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()
<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
dataBTO[dataBTO['Volume']==dataBTO['Volume'].max()]
| Open | High | Low | Close | Adj Close | Volume | |
|---|---|---|---|---|---|---|
| Date | ||||||
| 2021-11-30 | 5.17 | 5.28 | 5.03 | 5.06 | 4.739866 | 79861800 |
"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.
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['Open'].plot(figsize=(20,6))
plt.title('B2GOLD OPEN PRICE')
Text(0.5, 1.0, 'B2GOLD OPEN PRICE')
dataBTO['Total Traded'] = dataBTO['Open']*dataBTO['Volume']
dataTVE['Total Traded'] = dataTVE['Open']*dataTVE['Volume']
dataABR['Total Traded'] = dataABR['Open']*dataABR['Volume']
dataBTO.head(10)
| 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 |
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')
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.
dataBTO[dataBTO['Total Traded']==dataBTO['Total Traded'].max()]
| 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 |
dataBTO['Total Traded'].nlargest(2)
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.
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.
from pandas.plotting import scatter_matrix
Stock_comp = pd.concat([dataBTO['Open'],dataTVE['Open'],dataABR['Open']],axis=1)
Stock_comp.head(-10)
| 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
Stock_comp.columns = ['BTO Open','TVE Open','ABR Open']
Stock_comp.columns
Index(['BTO Open', 'TVE Open', 'ABR Open'], dtype='object')
Stock_comp.head(10)
| 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 |
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.
import mplfinance
from mplfinance.original_flavor import candlestick_ohlc
from matplotlib.dates import DateFormatter, date2num, WeekdayLocator, DayLocator, MONDAY
dataBTO_reset=dataBTO.loc['2023-03'].reset_index()
dataBTO_reset
| 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 |
dataBTO_reset['date_ax']=dataBTO_reset['Date'].apply(lambda date:date2num(date))
dataBTO_reset
| 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 |
dataBTO_values = [tuple(vals) for vals in dataBTO_reset[['date_ax', 'Open', 'High', 'Low', 'Close']].values]
dataBTO_values
[(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)]
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
#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.
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$
dataBTO['returns'] = (dataBTO['Close'] / dataBTO['Close'].shift(1) ) - 1
dataBTO.head()
| 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 |
dataBTO['returns'] = dataBTO['Close'].pct_change(1)
dataBTO.head()
| 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 |
coeff_var_BTO = np.std(dataBTO['returns']) / np.mean(dataBTO['returns'])
coeff_var_BTO
39.76236729652576
dataTVE['returns'] = dataTVE['Close'].pct_change(1)
dataTVE.head()
| 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 |
coeff_var_TVE = np.std(dataTVE['returns']) / np.mean(dataTVE['returns'])
coeff_var_TVE
46.62249931509346
dataABR['returns'] = dataABR['Close'].pct_change(1)
dataABR.head()
| 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 |
coeff_var_ABR = np.std(dataABR['returns']) / np.mean(dataABR['returns'])
coeff_var_ABR
14.47676557139699
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataBTO['returns'].hist(bins=50)
plt.title('HISTOGRAM CHART FOR B2GOLD');
Moderate Distribution
ax=plt.axes()
ax.set_facecolor('whitesmoke')
dataTVE['returns'].hist(bins=50)
plt.title('HISTOGRAM CHART FOR TAMARACK VALLEY ENERGY');
Moderate Distribution
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
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')
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.
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_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});
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.
dataBTO['Cumulative Return'] = (1 + dataBTO['returns']).cumprod()
dataBTO.head()
| 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 |
dataTVE['Cumulative Return'] = (1 + dataTVE['returns']).cumprod()
dataABR['Cumulative Return'] = (1 + dataABR['returns']).cumprod()
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()
<matplotlib.legend.Legend at 0x2087363f110>
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()
<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.