0% found this document useful (0 votes)
9 views8 pages

lab_1_solution

Lab 1 focuses on stock analysis using a dataset of daily performances from major stock exchanges, sourced from Kaggle. It involves loading and manipulating numpy arrays containing trade data, extracting information about the Canadian market, and visualizing trading trends over 40 years. The lab also explores investment strategies, comparing long-term holding gains to a blind daily trading approach.

Uploaded by

baron.schitka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

lab_1_solution

Lab 1 focuses on stock analysis using a dataset of daily performances from major stock exchanges, sourced from Kaggle. It involves loading and manipulating numpy arrays containing trade data, extracting information about the Canadian market, and visualizing trading trends over 40 years. The lab also explores investment strategies, comparing long-term holding gains to a blind daily trading approach.

Uploaded by

baron.schitka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

 Lab 1 > Lab 1: Stock analysis with NumPy 

Lab 1: Stock analysis with NumPy


In this lab, we will be working with a open data set on the daily performances of major stock exchanges
around the world. The data is collected from Kaggle: https://www.kaggle.com/datasets/mattiuzc/stock-
exchange-data

import numpy as np
import matplotlib.pyplot as plt

Loading dataset
You will find stock_indexes.npz in the directory /home/jovyan/public/datasets/world-stock-markets/ .

It contains several numpy arrays.


trades: daily trade information.
dates: the dates of each trading day in trades ndarray.
markets: a numpy array storing which exchange each trade belongs to.
info: a numpy array that matches the market names to the country they belong to.

data = np.load('/home/jovyan/public/datasets/world-stock-markets/stock_indexes.npz',
allow_pickle=True)

trades = data['trades']
dates = data['dates']
markets = data['markets']
info = data['info']

trades.shape, dates.shape, markets.shape, info.shape

((104224, 4), (104224,), (104224,), (14, 4))

Getting to know the numpy arrays


If we examine info , we see that it contains description of 14 exchanges including the country, exchange full
name, exchange short name, and currency used by the exchange.

info

array([['United States', 'New York Stock Exchange', 'NYA', 'USD'],


['United States', 'NASDAQ', 'IXIC', 'USD'],
['Hong Kong', 'Hong Kong Stock Exchange', 'HSI', 'HKD'],
['China', 'Shanghai Stock Exchange', '000001.SS', 'CNY'],
['Japan', 'Tokyo Stock Exchange', 'N225', 'JPY'],
['Europe', 'Euronext', 'N100', 'EUR'],
['China', 'Shenzhen Stock Exchange', '399001.SZ', 'CNY'],
['Canada', 'Toronto Stock Exchange', 'GSPTSE', 'CAD'],
['India', 'National Stock Exchange of India', 'NSEI', 'INR'],
['Germany', 'Frankfurt Stock Exchange', 'GDAXI', 'EUR'],
['Korea', 'Korea Exchange', 'KS11', 'KRW'],
['Switzerland', 'SIX Swiss Exchange', 'SSMI', 'CHF'],
['Taiwan', 'Taiwan Stock Exchange', 'TWII', 'TWD'],
['South Africa', 'Johannesburg Stock Exchange', 'J203.JO', 'ZAR']],
dtype=object)

The trades numpy array has the shape of (104224, 4) . The rows are the days of trade for all 14 markets.
The four columns corresponds to Open, Close, High and Low.
Open: the price at the opening of the market on that day
Close: the price at the closing of the market on that day

trades

array([[ 2568.300049, 2568.300049, 2568.300049, 2568.300049],


[ 2540.100098, 2540.100098, 2540.100098, 2540.100098],
[ 2552.399902, 2552.399902, 2552.399902, 2552.399902],
...,
[66108.22656 , 66940.25 , 66940.25 , 66102.54688 ],
[66940.25 , 67554.85938 , 67726.5625 , 66794.60938 ],
[67554.85938 , 67964.03906 , 68140.85156 , 67554.85938 ]])

Work Unit: Numpy array access


Get the Canadian exchange short name.

# @workUnit

market_name = ...

# @solution

market_name = info[info[:, 0] == 'Canada', 2][0]


market_name

'GSPTSE'

# @check
# @title: Canadian exchange name

market_name
'GSPTSE'

Work Unit: Constructing selection mask


Construct a boolean mask mask_canada which will select all the row indexes corresponding to the Canadian
market. You should be using the markets numpy array and the market_name value.

# @workUnit

mask_canada = ...

# @solution

mask_canada = (markets == market_name)

# @check
# @title: mask shape and value

mask_canada.shape, np.sum(mask_canada)

((104224,), 10526)

Note: You should have a boolean mask of length 104224, with 10526 entries that are true .

Work Unit: getting the Canadian trade days


We will use the mask to extract the Canadian trade days. This can be done by mask-based selection of the
trades numpy array.

# @workUnit

trades_canada = trades[ ... ]

# @solution

trades_canada = trades[mask_canada, :]

# @check
# @title: Canada trades

trades_canada.shape

(10526, 4)

We can also extract the rows corresponding to the trades that happened in Canada.
This requires masked selection on the dates numpy array.

# @workUnit

dates_canada = dates[ ... ]

# @solution

dates_canada = dates[mask_canada]

# @check
# @title: Canada trading dates range

dates_canada[0], dates_canada[-1]

('1979-06-29', '2021-05-31')

Work Unit: plotting the trades


Let’s visualize what the Canadian market has been like in the last 40 years.

# @workUnit

plt.plot(trades_canada[:, 1]);
plt.title(f'Closing prices from {dates_canada[0]} to {dates_canada[-1]}');
plt.xlabel('Dates')
plt.ylabel('$ CAD')

Text(0, 0.5, '$ CAD')


But wait, the x-axis of the plot is not dates, but just index values from 0 to 10526, since there are 10526
trading days in the last 40 years.
Let’s add the dates to the label. This requires a few things:
Generate tickers at evenly spaced intervals (every 1000 days).
tickers = np.arange(0, 10526, 1000)

Generate a numpy array of the dates at these intervals.


labels = dates_canada[tickers]

# @workUnit

tickers = ...
labels = ...

# @solution

tickers = np.arange(0, 10526, 1000)


labels = dates_canada[tickers]

Now, we can reset the xticks of the plot.


plt.plot(trades_canada[:, 0])
plt.xticks(tickers, labels, rotation=90);

Note: Can you spot the last two major recessions?

Bonus section: investment strategy


In this section, we will simulate simple investment strategies.

Long term hold


In this strategy, we assume you buy at the opening of the first day of market in 1979-06-29 , and sell at the
last day of the recorded trades at the closing of the market.

# @workUnit

buy_price = ...
sell_price = ...

# @solution
buy_price = trades_canada[0, 0]
sell_price = trades_canada[-1, 1]

# @check
# @title: gain of long term hold

gain = sell_price / buy_price * 100


np.round(gain, 2)

1221.96

Note: over the last 40 years, this strategy would get about 1200% gain, averaging at about 6.5% return.

Blind Daily trade


For this strategy, we do the following:
Buy on the even days at the opening price, and sell at the closing price on the same day.
Don’t trade on the odd days.

# @workUnit

buy_prices = ...
sell_prices = ...

# @solution

buy_prices = trades_canada[::2, 0]
sell_prices = trades_canada[::2, 1]

Let’s plot the gains for each trade over time.


Each trade’s gain is given as:
gains = sell_prices / buy_prices

# @workUnit

# Plot the gains. What do you think? Is it good or bad?

# @solution

plt.plot(sell_prices / buy_prices);
Now, let’s see how you have done using this strategy. For this, we need to multiply all the gains together:
gains = sell_prices / buy_prices
gain = gains[0] * gains[1] * gains[2] * ...

Luckily we have a numpy function np.prod that does the multiplication for us.

# @workUnit

gains = sell_prices / buy_prices


gain = ...

# @solution

gains = sell_prices / buy_prices


gain = np.prod(gains) * 100
gain

114.01953486649484

Note:
So, compare to the 1200% long term hold, how well did the blind daily trading do?

You might also like