I'm trying to develop a model to predict a commodity price movement direction based on previous observations. The model should learn common technical analysis patterns, e.g. head and shoulders. So, I think I should use a stateful LSTM so that it maintain a long term state to keep track of technical analysis patterns.

On the other side, as the data set is updated daily, i.e. new observations are added, I need the model to keep learning and making predictions every day. So, in order to update the model parameters on each new observation, I think I should use batch_size=1.

If I use the last N observations to predict the next M steps, the input tensor to the model would have the shape of (1, N, num_features):

$$ X_1, X_2, ..., X_N \rightarrow Y_1, Y_2, ..., Y_M $$


  • Why should we reset_states after training on train set and prior to predication on test set? I think we shouldn't as a TA pattern may be half in the train set and half in the test set. If we reset the states, the model can't recognize this pattern.

  • Can I reset_states and feed in data of a different commodity? as I only need the model to learn TA patterns not commodity specific characteristics. If so, how can I restore the states to make prediction on the first commodity?


1 Answer 1


To answer your questions in order:

  1. The whole point of LSTM (or any time series model) is to predict previously unseen values. If states are not reset - then there is a risk of data leakage - whereby forecasts from the training set will lead into the test set. This would mean that your model would perform quite well on the existing set of data - but would perform badly on new/unseen data. This defeats the purpose of implementing a time series model.

  2. Depending on how similar the pattern is, it might be an option. However, it is just my personal experience that LSTMs are not particularly effective at forecasting commodity prices - the main reason being that these models tend to carry forward too much volatility in the forecasts, which tends to interfere with the overall trend of the data. Here is an example of the use of LSTMs in forecasting oil prices, which you might find of interest: Are LSTMs Always Effective At Time Series Analysis? A Case Study of Oil Prices

  • $\begingroup$ "... but would perform badly on new/unseen data. " I think the model should maintain the states (i.e. LSTM cell states) from the beginning of the series and never reset them, no matter it's trainset ot testset or unseen incoming data. The states should keep track of chart patterns (e.g. head and shoulders) as a pattern may begin in the trainset and ends in future data. Am I right? Regarding statement "... these models tend to carry forward too much volatility in the forecasts" yes, I know, but I'm looking for a model to merely suggest trend reversals not the exact prices. $\endgroup$
    – frogatto
    Commented Jan 20, 2020 at 8:58
  • $\begingroup$ The true test is whether your model built today is effective at forecasting fluctuations in price for the next week/month/year, etc. However, your model will suffer from overfitting if the training and test data is not partitioned properly - i.e. it performs well on forecasting existing data but not unseen data. $\endgroup$ Commented Jan 20, 2020 at 12:38
  • $\begingroup$ Additionally, a limitation of such a model is that it will not account for significant deviations from the trend. e.g. When the Swiss Franc soared against other currencies in January 2015, this marked an abrupt shift in trend. A model that relies too much on prior training data would not take this into account when it comes to forecasting future values. $\endgroup$ Commented Jan 20, 2020 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.