Predicting price using previous prices with R and Neural Networks (neuralnet)

∥☆過路亽.° 提交于 2019-12-09 07:33:28

问题


Within the R Neural Network page, I am using the neural network function to attempt to predict stock price.

Training data contains columns High,Low,Open,Close.

myformula <- close ~ High+Low+Open
neuralnet(myformula,data=train_,hidden=c(5,3),linear.output=T)

My question, Given the below data example could you tell me what the formula would look like.

I have a table with columns "High","Low","Open","Close" it has two rows of values, each row represents a candle stick for the day. So the two rows in the data are candle sticks for the previous two days.My aim is tp predict what the next candle stick is i.e. "Open","High","Low","Close" given the previous two candlesticks.

My neural network will be presented with the previous dtata 1 candle stick at a time. I want to know what the next candlestick is, so what would my R formula look like.

Thanks Let me know


回答1:


My neural network will be presented with the previous data one candle stick at a time. I want to know what the next candlestick is, so what would my R formula look like.

In a Feed-Forward Neural Network*, you have to specify the features you want to use for the prediction and the targets to predict. In your example above the feature is e.g. prev_close, and the target is close. As you can see in your training data you don't have prev_close yet, that was the whole point of my answer, you need to formulate the problem correctly first.

If all you have is close, there cannot be a formula to train a FF NN for this. You need to create prev_close then the formula would be close ~ prev_close.

*A Recurrent Neural Network (RNN) can be trained on sequences, and output a prediction based on an input sequence, but that's a whole 'nother can of worms

Simple example: predict close based on last 2 close values

I cooked up this ridiculously-simplistic* example just to illustrate the problem formulation, that predicts the close, based one the last two close values. I chose a single hidden layer with 1 neuron in it. I've set linear.output=TRUE since we are predicting continuous values (regression problem as discussed before, and in the neuralnet documentation it is stated that there will be no activation function act.fct if that value is TRUE)

*If you trade with this you will certainly lose your shirt. This is just to show how to structure such a prediction problem in a Neural Network. Don't use this for real.

Problem formulation

The point I wanted to make clear is this, if you have prices in a column you have to create the features for the prediction

prev_close_1 | prev_close_2 | close

The problem posed to the NN is to predict close based on prev_close_1 and prev_close_2, hence the formula close ~ prev_close_1 + prev_close_2

Here's the network architecture

Notice the inputs that are the previous close values, and the output: the predicted close value.

library(neuralnet)

N = 10
prices <- data.frame(close=1:N) # Dummy straight line uptrend for N periods 

print(prices)

shift <- function(x, n){
  c(x[-(seq(n))], rep(NA, n))
}

# Form the training dataframe
train <- data.frame(
  prev_close_1=prices$close,
  prev_close_2=shift(prices$close, 1),
  close=shift(prices$close, 2)
)

# When shifting the columns for time lag effect, some rows will have NAs
# Let's remove NAs
train <- na.omit(train)

print(train)

nn <- neuralnet(
  formula=close ~ prev_close_1 + prev_close_2,
  data=train,
  hidden=c(1), # 1 neuron in a single hidden layer
  linear.output=TRUE # we want regression not classification
)

print(prediction(nn))
plot(nn)

What the dummy prices look like

This is what you have, it's just the historical stock prices

   close
1      1
2      2
3      3
4      4
5      5
6      6
7      7
8      8
9      9
10    10

What the NN was trained on

This is what you need, the features and the target, try to form the rows in the training dataframe below to understand the shift/lag.

prev_close_1 prev_close_2 close
1            1            2     3
2            2            3     4
3            3            4     5
4            4            5     6
5            5            6     7
6            6            7     8
7            7            8     9
8            8            9    10

What the NN predicts

  prev_close_1 prev_close_2       close
1            1            2 2.994291864
2            2            3 4.017828301
3            3            4 5.002914789
4            4            5 5.968855729
5            5            6 6.978644849
6            6            7 8.030810042
7            7            8 9.051063456
8            8            9 9.945595495


来源:https://stackoverflow.com/questions/38010806/predicting-price-using-previous-prices-with-r-and-neural-networks-neuralnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!