Overview
- Evaluate the risk and return of 10 stocks of The World’s Biggest Technology Companies 5 years ago to date based on annual Sharpe Ratios.
- Create a tool that functions to provide stock price predictions from companies that have the highest annual sharpe ratio evaluation results in previous evaluations.
- Machine learning with LSTM (Long Short Term Memory) model is used to predict stock prices with 99.3% accuration.
- All datasets are provided from Yahoo! Finance’s API. So, each daily runtime will provide the latest evaluation and prediction results.
- All graph in interactive for better visualization dan analyst experience.
Code and Resources Used
Python Version: 3.10.3
Packages: numpy, pandas, tensorflow, matplotlib, yfinance, plotly, cufflinks, sklearn, keras, datetime
Dataset source: Yahoo! Finance’s API
Notebook Interactive View
Here the link for Notebook Interactive Viewer
Collecting Data
First thing to do is to determine the 10 largest technology companies in the world, here I get a list from the Companiesmarketcap website (accessed on 5 september 2022), The world’s top 10 technology companies are:
- Apple (USA)
- Microsoft (USA)
- Google (USA)
- Amazon (USA)
- Tesla (USA)
- Meta Platforms (USA)
- Taiwan Semiconductor Manufacturing Company (Taiwan)
- Tencent (China)
- Nvidia (USA)
- Alibaba (China)
For comparison, the S&P 500 is used as a benchmark here.
Then, the stock price dataset is taken from Yahoo! Finance’s API via the yfinance package, here we will use stock prices from the past 5 years.
Visualized into a graph
Sharpe Ratio
Annual Sharpe Ratio
Here, we can see Microsoft, Apple, and Tesla have the highest annual sharpe ratio from the past 5 years data stock price.
So, if you want to buy some chips in the world’s technology Companies. The 3 Companies are the best choices based on the theory put forward by Professor William F. Sharpe.
Build Stock Price Machine Learning Model
Because Microsoft has the highest sharpe ratio, it will be interesting if we can predict the stock price of Microsoft in the future. So, here I will create a machine learning model to predict stock prices from Microsoft.
Here stock price of Microsoft from 5 years later. For interactive graph can be seen on this link
In this Model, I use machine learning regression model from Keras and TensorFlow for create Neural Network layers of Sequential with 2 LSTM (Long Short Term Memory) Layer and 2 Dense Layer.
Here the summary of model :
Layer (type) | Output Shape | Param # |
---|---|---|
lstm_10 (LSTM) | (None, 60, 128) | 66560 |
lstm_11 (LSTM) | (None, 64) | 49408 |
dense_10 (Dense) | (None, 25) | 1625 |
dense_11 (Dense) | (None, 1) | 26 |
For each 1 prediction, using 60 stock prices from 60 active market days, or basically 2 months data stock price is for 1 predictions stock price. To get high prediction accuracy, model machine learning is trained using 1 batch data with 10 epoch. For 5 years dataset, it takes 5 minutes to complete tha training with loss around 0.0004, which is very nice.
Result
Prediction Price on 5 years Stock price
Zoom for better visualization:
Here we can see the predictions and the actual stock price is very close. Here the table result:
Accuration | MAE | MSE |
---|---|---|
99,3 % | 0.858 | 1.322 |