Machine learning/AI : This experiment uses artificial neural networks to reveal stock market trends and demonstrates the ability of time series forecasting to predict future stock prices based on past historical data.
Disclaimer: 100% beta ; past performance is not an indicator of future performance...That is...until now...
We will:
Get historical Stock price data via api
Prepare training data for our neural network model
Train the neural network
Predict future price of the selected Ticker.
This project has been Audited by Psico communications and blockchain development.and has 0 vulnerabilities.
Prior to training the neural network we first aquired historical data via API.
In this case, time series: a sequence of numbers in chronological order.
First we aquire the data via the Alpha Vantage Stock API.
This API allows us to retrieve chronological data on specific company stocks prices from the last 20 years.
Free sources of Hisorical data include:Yahoo Finance
and
Finviz.
TICKERS.
NEWS.
Using Standard and poors 500 index was the choice we made due to liquidity and the wealth of historical data.
You can either pick daily adjusted or weekly adjusted, open/high/low/close/volume values, daily adjusted close values, and historical split/dividend events of the global equity specified, covering 20+ years of historical data.
As suggested by Put a link here......, using adjusted close price is more robust to stock split compared to using closing price.
Although the code can be adapted to include any index or asset class or financial instrument within the global financial system as long as the data
sets are available via api.
NeuralNetwork.genesis ; Configure Your Neural Network using the Hyperparameters in the fields below.
import ; historical time series data
If you are unfamiliar with "Tickers" you can find a map of the S&P500 Here.
The API yields the following fields:
Opening price
Highest price of that day
Lowest price of that day
Closing price
Adjusted close price (this is used in this project)
Volume
To prepare training dataset for our neural network, we will use adjusted close stocks price as this takes into consideration any aftermarket moves; therefore we will be aiming to predict future closing price data.
Use demo API key fetches Microsoft Corporation prices or get your own API key for other tickers and paste in to field 'Alpha Vantage API Key'.
For this experiment, we are using supervised learning, which means feeding data to the neural network and it learns by mapping input data to the output label. One way to prepare the training dataset is to extract Simple Moving Average from that time series data.
Simple Moving Average (SMA) is a method to identify trends direction for a certain period of time, by looking at the average of all the values within that time window. The number of prices in a time window is selected experimentally. For example, let's assume the closing prices for past 5 days were 13, 15, 14, 16, 17, the SMA would be (13+15+14+16+17)/5 = 15. So the input for our training dataset is the set of prices within a single time window, and label is the computed moving average of those prices.
Try ItBut first, fetch stocks data from the previous step.
Now that we have the training data, we can create a model for time series prediction, to achieve this we will use TensorFlow.js framework.
Sequential model
is selected which connects each layer and passes the data from input to the output during the training process.
In order for the model to learn time series data which are sequential, recurrent neural network (RNN) layer
layer is created and a number of LSTM cells are added to the RNN.
The model will be trained using Adam (read more), a popular optimisation algorithm for machine learning. Root-means-squared error which determine the difference between predicted values and the actual values, so model is able to learn by minimising the error during the training process.
These are the hyperparameters (parameters used in the training process) available for tweaking:
Training Dataset Size (%): the amount of data used for training, and remaining data will be used for prediction
Epochs: number of times the dataset is used to train the model (learn more)
Learning Rate: amount of change in the weights during training in each step (learn more)
Hidden LSTM Layers: to increase the model complexity to learn in higher dimensional space (learn more)
You may tweak the hyperparameters and then hit the Begin Training Model button to train the model.
Need training data? Explore the previous section to prepare training data.
Now that you have trained your model, it is time to use the model.predict function from TFJS to predict future values. We have split the data into 2 sets, a subset of the data is training and the rest is the validation set. The training set is for training the model, the validation set to validate the model. The model has not seen the data in the validation set before this experiment.We will be examining the correlation.
TRY itSo let us use the remaining data for prediction which allow us to see how closely our predicted values are compared to the actual values.
But if the model did not predict values that map closely to its true values, check the training loss graph. Generally, this model should converge with the loss to be less than 1. You can increase the number of epochs, or tweak the other learning hyperparameters.
Don’t have a model to perform prediction? Train your model.
The model has now been validated... We will apply the same model.predict function and use the last {{input_windowsize}} data points as the input, because that is our window size. This means that, if our training data is increment daily, we will use the past {{input_windowsize}} days as input, to predict the next day.
Try ItDon’t have a model to perform prediction? Train your model.
More info
Pre training requires large datasets.Historical market data is great for this as data is freely available from many sources.
The essential technical indicators are Moving average convergence divergence (MACD), Relative strength index (RSI), and a range of moving averages 50dma 100dma 200dma.
Add even more features. One amazing thing that Alpha Vantage API provides is Fundamental Data. This means that you can also include annual and quarterly income statements and cash flows for the company of interest. External factors ESG make the market move.
There are many ways to do time series prediction other than using a simple moving average.
RSI , MACD , 50/100/200 DMA , NEWS , ESG , Options expiry e.t.c. will all be added over time.
Options expiry dates will be incorporated at a later date as the Gamma produced on zero days are a major contributing factor to market movement.
Please check out this repo PyTorch for a similar python version.
More data inputs will be added over time in order to fine tune the algorithm.
With TensorFlow.js, machine learning in a web browser using javascript is now a possibility.
Please consider sponsering this project as resources are an issue .
Check out this open source project if you are interested in Finance.OpenBB