# How can machine learning be applied to stock price prediction?

In machine learning, for a given input instances you get an output what are present at the same time. But in stock market you have to predict the next price based on previous inputs. So if you want to predict the next price (output) with machine learning, how you do it lacking new input instances (for example: high price, low price, open price, close price, volume, etc.)?
I want to use a simple example to be clear what I want to understand here.

For example :

I use high price, low price, open price and volume as inputs and close price as output. I train the algorithm with input-output samples. Then I want to predict an output (close price). But the problem is that inputs and output appears together so this way I can`t predict the next price because it has already appeared with inputs. So how is that, how do they apply?

• Learn this one weird kernel trick. Stockbrokers hate him! – Kodiologist Aug 20 '16 at 5:31
• @Kodiologist, I've read a few times about this "weird kernel trick" joke. Is it a meme among statisticians? Ps I know what's a "kernel trick". I'm specifically referring to the joke. – DeltaIV Aug 20 '16 at 6:21
• The title makes this sound to be overly broad, about finance, and a duplicate of stats.stackexchange.com/questions/21395, but the actual issue seems on-topic. Can you edit this to clarify that the question is about how to predict future outputs when future inputs have not appeared (though that may be duplicate too, but finding a duplicate would give you useful answers). Also, I don't understand in the example how the next price "has already appeared with inputs". – Juho Kokkala Aug 20 '16 at 8:54
• @JuhoKokkala, good points. I think the question is pretty simple, and I answered it as such. Let us see if that satisfies the OP. – Richard Hardy Aug 20 '16 at 8:58
• – Kodiologist Aug 20 '16 at 14:15

If you want to predict future values knowing only the current and past values, that is also how you specify the model. If the variable of interest is $y$ and the variables that can be used for prediction are $x_1,\dotsc,x_K$, you formulate the model as

$$y_{t+1} = f(x_{1,t},\dotsc,x_{K,t},\dotsc,x_{1,t-p},\dotsc,x_{K,t-p}) + \varepsilon_{t+1}$$

for some maximum lag $p$, where $f$ is the function the machine learning algorithm is trying to learn and $\varepsilon$ is something unpredictable. This way you can predict future $y_{t+1}$ using data that is available today (at time $t$).

When training your model, you will have a sample spanning $1,\dotsc,T$. Then your $t$ runs from $p+1$ to $T-1$ in the training sample. You can verify that by noticing that for $t<p+1$ or $t>T-1$ you would have to use data that you do not have within $1,\dotsc,T$.

Once you have formulated the model, training and prediction goes as usual.

Recurrent Neural Networks are quite well suited for this job.

Let's say we have an LSTM Recurrent Neural Network with 1 layer and 128 hidden units. Instead of predicting price at t+1 based on ohlc data, and then t+2 based on ohlc data up until t in addition to the output of t+1, you can use a dense layer to map those 128 hidden units to, let's say, 48 dense outputs, where 48 is the number of hours we want to predict.

I am using this approach with a pretty good success rate in terms of accuracy.