gpt4 book ai didi

r - 使用 R 进行股票分析的回溯测试

转载 作者:行者123 更新时间:2023-11-28 21:20:49 25 4
gpt4 key购买 nike

我是“R”的新手,我想根据 EMA 编写一个脚本来回测我的买卖策略。我根据Web上的一些引用编写了以下代码。但是,脚本在第 72 行收到一条错误消息,但我无法找出问题所在。任何人都可以帮助解决我的问题吗?提前致谢。

library(quantmod)
stock1<-getSymbols("^DJI",src="yahoo",from="2010-01-01",auto.assign=F)


stock1<-na.locf(stock1)
stock1$EMA9<-EMA(Cl(stock1),n=9)
stock1$EMA19<-EMA(Cl(stock1),n=19)
stock1$EMACheck<-ifelse(stock1$EMA9>stock1$EMA19,1,0)
stock1$EMA_CrossOverUp<-ifelse(diff(stock1$EMACheck)==1,1,0)
stock1$EMA_CrossOverDown<-ifelse(diff(stock1$EMACheck)==-1,-1,0)


stock1<-stock1[index(stock1)>="2010-01-01",]

stock1_df<-data.frame(index(stock1),coredata(stock1))

colnames(stock1_df)<-c("Date","Open","High","Low","Close","Volume","Adj","EMA9","EMA19","EMACheck","EMACheck_up","EMACheck_down")
head(stock1_df)

#To calculate the number of crossoverup transactions during the duration from 2010-01-01

sum(stock1_df$EMACheck_up==1 & index(stock1)>="2010-01-01",na.rm=T)

stock1_df$Date[stock1_df$EMACheck_up==1 & index(stock1)>="2010-01-01"]

sum(stock1_df$EMACheck_down==-1 & index(stock1)>="2010-01-01",na.rm=T)

stock1_df$Date[stock1_df$EMACheck_down==-1 & index(stock1)>="2010-01-01"]

#To generate the transcation according to the strategy

transaction_dates<-function(stock2,Buy,Sell)
{
Date_buy<-c()
Date_sell<-c()
hold<-F
stock2[["Hold"]]<-hold
for(i in 1:nrow(stock2)) {
if(hold == T) {
stock2[["Hold"]][i]<-T
if(stock2[[Sell]][i] == -1) {
#stock2[["Hold"]][i]<-T
hold<-F
}
} else {
if(stock2[[Buy]][i] == 1) {
hold<-T
stock2[["Hold"]][i]<-T
}
}
}


stock2[["Enter"]]<-c(0,ifelse(diff(stock2[["Hold"]])==1,1,0))
stock2[["Exit"]]<-c(ifelse(diff(stock2[["Hold"]])==-1,-1,0),0)

Buy_date <- stock2[["Date"]][stock2[["Enter"]] == 1]
Sell_date <- stock2[["Date"]][stock2[["Exit"]] == -1]

if (length(Sell_date)<length(Buy_date)){
#Sell_date[length(Sell_date)+1]<-tail(stock2[["Date"]],n=2)[1]
Buy_date<-Buy_date[1:length(Buy_date)-1]

}

return(list(DatesBuy=Buy_date,DatesSell=Sell_date))
}

#transaction dates generate:

transactionDates<-transaction_dates(stock1_df,"EMACheck_up","EMACheck_down")

transactionDates

num_transaction1<-length(transactionDates[[1]])

Open_price<-function(df,x) {df[as.integer(rownames(df[df[["Date"]]==x,]))+1,][["Open"]]}
transactions_date<-function(df,x) {df[as.integer(rownames(df[df[["Date"]]==x,]))+1,][["Date"]]}

transactions_generate<-function(df,num_transaction)
{
price_buy<-sapply(1:num_transaction,function(x) {Open_price(df,transactionDates[[1]][x])})
price_sell<-sapply(1:num_transaction,function(x) {Open_price(df,transactionDates[[2]][x])})
Dates_buy<-as.Date(sapply(1:num_transaction,function(x) {transactions_date(df,transactionDates[[1]][x])}))
Dates_sell<-as.Date(sapply(1:num_transaction,function(x) {transactions_date(df,transactionDates[[2]][x])}))


transactions_df<-data.frame(DatesBuy=Dates_buy,DatesSell=Dates_sell,pricesBuy=price_buy,pricesSell=price_sell)
#transactions_df$return<-100*(transactions_df$pricesSell-transactions_df$pricesBuy)/transactions_df$pricesBuy
transactions_df$Stop_loss<-NA
return(transactions_df)
}

transaction_summary<-transactions_generate(stock1_df,num_transaction1)
transaction_summary$Return<-100*(transaction_summary$pricesSell-transaction_summary$pricesBuy)/transaction_summary$pricesBuy
transaction_summary

最佳答案

您的代码在此行失败:

transactionDates<-transaction_dates(stock1_df,"EMACheck_up","EMACheck_down")

原因是 stock1_df 的前 19 条记录在“EMACheck_up”和“EMACheck_down”列中包含 NA 值。

head(stock1_df)
EMACheck_up EMACheck_down
1 NA NA
2 NA NA
3 NA NA
4 NA NA
5 NA NA
6 NA NA

您可以通过在运行有问题的代码行之前运行 na.locf 来解决您的问题。

stock1_df <- na.locf(stock1_df)

transactionDates <-
transaction_dates(stock1_df, "EMACheck_up", "EMACheck_down")

跳过前 19 行(或第一个月)也可以。

您可能想查看 quantstrat如果你想在回测策略上做更多。但您现在拥有的东西可以解决问题。

关于r - 使用 R 进行股票分析的回溯测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52227176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com