Preliminary Design

The preliminary proposed a machine learning framework implemented in Python using the Keras library (on top of Tensorflow). The first portion of the code is the predictive algorithm, which is outlined in the flow diagram in Fig. 13 below:

 

Fig. 13: Initial Code Flow Diagram

This code is able to read in a csv file the desired inputs downloaded from Yahoo Finance (for a stock over a desired timespan). The user specifies the desired number of “lag” days, the input features, and the number of days we want to predict ahead. The input data is time series data, so it must be converted into a supervised learning problem (i.e., a target variable must be created). This is done by copying our desired output data (the closing price) and assigning it to be the output variable. Then, this copied data is shifted up by the number of days we want to predict out on.

Once the data has been converted into a supervised learning problem, the data is split into a training and a test set. The training set is by the algorithm to train on, while the test set is used to validate the algorithm’s predictions as it trains. These sets are re-shaped to be three dimensional: [number of samples, timesteps, features].

After preparing the data, the LSTM network is designed. The various hyperparameters of the networks (which will be discussed later) are specified, and the input dimensions are set. Then, the training and testing data is fed into the model. Once the model is finished training, new data can be input to predict out on. The output are the closing day stock prices (for however many days the user specified). Given the output of these predictions, the model accuracy is evaluated using percent error between the predicted price and actual price as well as a financial cost function. This cost metric is outlined in the diagram in Fig. 14 below:

Fig. 14: Cost Metric

In general, the cost metric uses the algorithm’s predictions to determine how well an investor would do by buying and selling stocks based on the predictions. The inputs to the function are the predicted stock values and the true stock values. (Since I was only training and predicting on historical data, I was able to compare the predictions to actual stock prices). The other inputs are a theoretical initial investment value and the cost of executing a trade. (Because trades are mostly executed by third parties, there is a cost to this trade execution). It should be noted that this function does daily tracking of how much money the investor has leftover and the value of the investor’s stock holdings; combining these two values provides the total portfolio. Basically, the algorithm first checks to see if the projected stock prices is predicted to go up or down. If the price is predicted to go up and the investor has money left, investor “buys” as many stocks as possible at its current price. If the price is predicted to go down, then the investor sells if the investor is holding stock or holds off if no stock is held. If no change is projected in the price or the investor does not have any money leftover, the investor holds. The output of this function is the investor’s daily portfolio value and a calculation of the percent returns.

The output of this cost metric is compared with another function that creates a market baseline. Recall that one of the goals is to use the machine learning framework to make trading decision that will outperform the market benchmark. This market benchmark is determined by creating a function that will hold a stock over the time frame predicted out on. The function track the daily price fluctuations in the investor’s stock holds as well as the money leftover that was not spent on the initial investment. Again, the output is the daily portfolio value and percent returns. These values are compared to the outputs of the cost function based on market predictions. The code is outlined in the diagram in Fig. 15 below:

Fig. 15: Market Baseline (Holding Stock) Metric

 

 

 


 

Skip to toolbar