以下是我的库存测试项目脚本。我写了一篇测试买入/卖出策略的2 SMA交叉收益。现在,我想自动输入x和y的值,并为不同的结果生成一张表。 X值从(1:30)更改,Y值从(1:30)更改,并输出到Excel,如数据框文件,该行与列从1到30相对。 R
中的数据帧library(quantmod)
library(lubridate)
library(xlsx)
stock0<-getSymbols("^HSI",src="yahoo",from="1988-01-01",auto.assign=F)
stock0 <- to.weekly(stock0)
x<-1
y<-30
stock1<-na.locf(stock0)
stock1$SMA1<-SMA(Cl(stock1),n=x)
stock1$SMA30<-SMA(Cl(stock1),n=y)
stock1$SMACheck<-ifelse(stock1$SMA1>stock1$SMA30,1,0)
stock1$SMA_CrossOverUp<-ifelse(diff(stock1$SMACheck)==1,1,0)
stock1$SMA_CrossOverDown<-ifelse(diff(stock1$SMACheck)==-1,-1,0)
stock1<-stock1[index(stock1)>="1998-01-01",]
stock1_df<-data.frame(index(stock1),coredata(stock1))
colnames(stock1_df)<-c("Date","Open","High","Low","Close","Volume","Adj","SMA1","SMA30","SMACheck","SMACheck_up","SMACheck_down")
sum(stock1_df$SMACheck_up==1 & index(stock1)>="2010-01-01",na.rm=T)
stock1_df$Date[stock1_df$SMACheck_up==1 & index(stock1)>="2010-01-01"]
sum(stock1_df$SMACheck_down==-1 & index(stock1)>="2010-01-01",na.rm=T)
stock1_df$Date[stock1_df$SMACheck_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:
stock1_df <- na.locf(stock1_df)
transactionDates<-transaction_dates(stock1_df,"SMACheck_up","SMACheck_down")
num_transaction1<-length(transactionDates[[1]])
Open_price<-function(df,x) {
df[which(df[["Date"]]==x)+1,][["Open"]]
}
transactions_date<-function(df,x) {
df[which(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
result<-sum(transaction_summary$Return,na.rm=T)
result
答案 0 :(得分:0)
我不会重写整个脚本,但这应该可以帮助您。
首先,我创建了一个SMA_crossover函数,该函数提供向上和向下交叉的输出,并为快速sma [1]和慢速sma [25]命名为“ SMA_CrossOverUp_1_25”。 接下来,您将获得一个x和y值的double for循环。完成此操作后,我会将所有内容转换为具有正确名称的data.frame。在整个事务生成过程中运行此操作,我将由您决定。
# data should be a ohlcv timeseries from quantmad
# fast_period is the fast SMA
# slow_period is the slow SMA
# function returns only the sma up and down crossovers.
SMA_crossover <- function(data, fast_period, slow_period) {
Fast <- SMA(Cl(data), n = fast_period)
Slow <- SMA(Cl(data), n = slow_period)
SMACheck <- ifelse(Fast > Slow, 1, 0)
fast_var <- paste("SMA_CrossOverUp", fast_period, slow_period, sep = "_")
slow_var <- paste("SMA_CrossOverDown", fast_period, slow_period, sep = "_")
data$SMA_CrossOverUp <- ifelse(diff(SMACheck) == 1, 1, 0)
data$SMA_CrossOverDown <- ifelse(diff(SMACheck) == -1, -1, 0)
data <- setNames(data[, c("SMA_CrossOverUp", "SMA_CrossOverDown")], c(fast_var, slow_var))
return(data)
}
# adjust length of x and y to create check more crossovers.
# loops merge the crossover data to the input data
for(x in 1:5){
for(y in 25:30)
Weekly_data <- merge(Weekly_data, SMA_crossover(Weekly_data, x, y))
}
# transform into data.frame
stock1_df <- data.frame(index(Weekly_data), coredata(Weekly_data))
stock1_df <- setNames(stock1_df, c("Date", colnames(Weekly_data)))
# remove NA's
stock1_df <- na.locf(stock1_df)
str(stock1_df)
'data.frame': 1572 obs. of 67 variables:
$ Date : Date, format: "1988-08-05" "1988-08-12" "1988-08-19" "1988-08-26" ...
$ stock0.Open : num 2703 2659 2601 2564 2440 ...
$ stock0.High : num 2703 2659 2601 2564 2450 ...
$ stock0.Low : num 2671 2579 2571 2465 2433 ...
$ stock0.Close : num 2671 2601 2580 2465 2450 ...
$ stock0.Volume : num 0 0 0 0 0 0 0 0 0 0 ...
$ stock0.Adjusted : num 2671 2601 2580 2465 2450 ...
$ SMA_CrossOverUp_1_25 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SMA_CrossOverDown_1_25: num 0 -1 0 0 0 0 0 0 0 0 ...
$ SMA_CrossOverUp_1_26 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SMA_CrossOverDown_1_26: num 0 0 -1 0 0 0 0 0 0 0 ...
$ SMA_CrossOverUp_1_27 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SMA_CrossOverDown_1_27: num 0 0 -1 0 0 0 0 0 0 0 ...
$ SMA_CrossOverUp_1_28 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SMA_CrossOverDown_1_28: num 0 0 -1 0 0 0 0 0 0 0 ...
$ SMA_CrossOverUp_1_29 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SMA_CrossOverDown_1_29: num 0 0 0 -1 0 0 0 0 0 0 ...
$ SMA_CrossOverUp_1_30 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SMA_CrossOverDown_1_30: num 0 0 0 -1 0 0 0 0 0 0 ...
$ SMA_CrossOverUp_2_25 : num 0 0 0 0 0 0 0 0 0 0 ...
$ SMA_CrossOverDown_2_25: num 0 0 -1 0 0 0 0 0 0 0 ...