alberduris / SirajsCodingChallenges

Code for Siraj Raval's Coding Challenges!
187 stars 80 forks source link

How do I make the model predict for future stock or currency prices? #4

Open MaanavKhaitan opened 6 years ago

MaanavKhaitan commented 6 years ago

Hello Alberto, I am Maanav from India, and have a small query:

If I had the dataset for a certain stock or for EUR/USD till today, and wished to make your model make a prediction for one day or some particular time period in the future, how can I program your model to do so?

I would greatly appreciate it if you could please guide me regarding how I can code this. I would be highly grateful and forever indebted to you if you could please help me.

Thanking you, Maanav

alberduris commented 6 years ago

Hi @MaanavKhaitan

To predict a new data point you just need to feed the test data from you want your prediction in a trained model. As this is Recurrent Neural Network you'll need to feed n-steps-back data, according to the variable _"truncated_backproplength" (that is the variable that fixes this value) you'll have to feed 3 past days in order to predict the next day. The training also works in that way.

So, for example, if you want to predict tomorrow's EURUSD price, you need the last 3 days prices and feed them to the RNN, like this:

Last three days EURUSD data

    Price  PriceTarget      MACD  Stochastics     ATR
2  1.1994       1.1893  0.000169    76.704545  0.0058
1  1.1893       1.1938 -0.000267    19.318182  0.0171
0  1.1938       1.1938 -0.000297    34.899329  0.0087
MaanavKhaitan commented 6 years ago

Thank you so much Alberto! This is a lifesaver!

CaesarChen1991 commented 6 years ago

Hi Alberto, your code is neat and elegant!

I also met with the problem of predicting future data points. So how can we feed the data to the RNN exactly, could you please give a demo code of this?

Thank you so much~

alberduris commented 6 years ago

Hey @CaesarChen1991, the explanation how what data to feed is in the other post and the function that you could use in order to do that is this

def load_model_predict():
    with tf.Session() as sess:
        predictions = []

        new_saver = tf.train.import_meta_graph('../data/models/CMP_RNN20170914-204417/model-10.meta')  
        new_saver.restore(sess, tf.train.latest_checkpoint('../data/models/CMP_RNN20170914-204417/'))

        graph = tf.get_default_graph()

        #Get the Tensors
        batchx = graph.get_tensor_by_name("data_ph:0")
        pred = graph.get_tensor_by_name("prediction:0")

        #Run
        batches = serve_batches(x_pred_data)
        for batch in batches:
            feed_dict ={batchx : batch}
            _pred = sess.run(pred,feed_dict)

            #Save the results
            predictions.append(_pred)

        #Reshape for better match
        predictions = np.array(predictions).ravel()

    return predictions

With that function you are able to load a pre-saved TF model and then pass the inference data in order to get the predictions. Go ahead :D

I hope you find this useful! <3

williamsrAndrew commented 6 years ago

First off, thanks for this code! I'm just starting with Deep Learning and Neural Networks and this is the most comprehensive program I have played around with yet, good job on this!

Question about your comment from November, you mention that you need to feed 3 past days in order to predict the next day. However, in your original graph of price vs predicted using the RNN, the predicted graph is offset 3 days prior to price graph (the start date is the same, but the end date is 3 days early). Because it is taking past data and making a future prediction, shouldn't the "Predicted" graph be shifted 3-4 spaces to the right? So that the predicted graph ends at the same time as the price graph, but starts 3 days after?

Thanks again!

urhero commented 6 years ago

Hi @alberduris,

First of all, thank you for the code sharing. I was wondering the same thing which was mentioned by @williamsrAndrew. Why does the red prediction line finish earlier than the actual price line? Do you mean the red prediction line should shift to the right side?

plt.plot(range(truncated_backprop_length+1,truncated_backprop_length+1+len(test_pred_list)),test_pred_list,label='Predicted',color='red')

This is my modification to the last part of RNN File.