使用 R 和神经网络 (neuralnet) 使用之前的价格预测价格
Predicting price using previous prices with R and Neural Networks (neuralnet)
在 R 神经网络页面中,我正在使用神经网络函数来尝试预测股票价格。
训练数据包含高、低、开盘价、收盘价列。
myformula <- close ~ High+Low+Open
neuralnet(myformula,data=train_,hidden=c(5,3),linear.output=T)
我的问题,鉴于下面的数据示例,你能告诉我公式是什么样子的吗?
我有一个 table 列 "High","Low","Open","Close" 它有两行值,每行代表一根蜡烛坚持这一天。所以数据中的两行是前两个烛台 days.My 目标是预测下一个烛台是什么,即 "Open"、"High"、"Low"、"Close" 给定前两个烛台。
我的神经网络将一次呈现先前的 dtata 1 个蜡烛条。我想知道下一个烛台是什么,那么我的 R 公式会是什么样子。
谢谢
让我知道
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.
在前馈神经网络*中,您必须指定要用于预测的特征和要预测的目标。在您上面的示例中,该功能是例如prev_close
,目标是close
。正如您在训练数据中看到的那样,您还没有 prev_close
,这就是我回答的重点,您需要先正确地表述问题。
如果你只有 close
,就没有公式可以为此训练 FF NN。您需要创建 prev_close
然后公式将是 close ~ prev_close
.
*递归神经网络 (RNN) 可以在序列上进行训练,并根据输入序列输出预测,但这完全是另一回事
简单示例:根据最后 2 个收盘价预测收盘价
我编造了这个简单得可笑的*示例,只是为了说明问题的表述,它根据最后两个 close
值预测 close
。我选择了一个包含 1 个神经元的隐藏层。我设置了 linear.output=TRUE
,因为我们预测的是连续值(, and in the neuralnet
documentation 据说如果该值为 TRUE,将没有激活函数 act.fct
)
*如果你用这个交易,你肯定会亏本。这只是为了展示如何在神经网络中构建这样的预测问题。请勿将其用于真实。
问题表述
我想说明的一点是,如果您在一列中有价格,则必须为预测创建特征
prev_close_1 | prev_close_2 | close
NN面临的问题是根据prev_close_1
和prev_close_2
预测close
,因此公式close ~ prev_close_1 + prev_close_2
这是网络架构
注意输入是之前的收盘值,输出是:预测的收盘值。
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)
虚拟价格是什么样的
这就是你所拥有的,只是历史股价
close
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
NN 的训练内容
这就是你需要的,特征和目标,尝试在下面的训练数据框中形成行以理解 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
NN 预测的内容
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
在 R 神经网络页面中,我正在使用神经网络函数来尝试预测股票价格。
训练数据包含高、低、开盘价、收盘价列。
myformula <- close ~ High+Low+Open
neuralnet(myformula,data=train_,hidden=c(5,3),linear.output=T)
我的问题,鉴于下面的数据示例,你能告诉我公式是什么样子的吗?
我有一个 table 列 "High","Low","Open","Close" 它有两行值,每行代表一根蜡烛坚持这一天。所以数据中的两行是前两个烛台 days.My 目标是预测下一个烛台是什么,即 "Open"、"High"、"Low"、"Close" 给定前两个烛台。
我的神经网络将一次呈现先前的 dtata 1 个蜡烛条。我想知道下一个烛台是什么,那么我的 R 公式会是什么样子。
谢谢 让我知道
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.
在前馈神经网络*中,您必须指定要用于预测的特征和要预测的目标。在您上面的示例中,该功能是例如prev_close
,目标是close
。正如您在训练数据中看到的那样,您还没有 prev_close
,这就是我回答的重点,您需要先正确地表述问题。
如果你只有 close
,就没有公式可以为此训练 FF NN。您需要创建 prev_close
然后公式将是 close ~ prev_close
.
*递归神经网络 (RNN) 可以在序列上进行训练,并根据输入序列输出预测,但这完全是另一回事
简单示例:根据最后 2 个收盘价预测收盘价
我编造了这个简单得可笑的*示例,只是为了说明问题的表述,它根据最后两个 close
值预测 close
。我选择了一个包含 1 个神经元的隐藏层。我设置了 linear.output=TRUE
,因为我们预测的是连续值(neuralnet
documentation 据说如果该值为 TRUE,将没有激活函数 act.fct
)
*如果你用这个交易,你肯定会亏本。这只是为了展示如何在神经网络中构建这样的预测问题。请勿将其用于真实。
问题表述
我想说明的一点是,如果您在一列中有价格,则必须为预测创建特征
prev_close_1 | prev_close_2 | close
NN面临的问题是根据prev_close_1
和prev_close_2
预测close
,因此公式close ~ prev_close_1 + prev_close_2
这是网络架构
注意输入是之前的收盘值,输出是:预测的收盘值。
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)
虚拟价格是什么样的
这就是你所拥有的,只是历史股价
close
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
NN 的训练内容
这就是你需要的,特征和目标,尝试在下面的训练数据框中形成行以理解 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
NN 预测的内容
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