原创R语言TMA三均线策略实现附代码数据Word文档下载推荐.docx
《原创R语言TMA三均线策略实现附代码数据Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《原创R语言TMA三均线策略实现附代码数据Word文档下载推荐.docx(39页珍藏版)》请在冰点文库上搜索。
params$lookbacks$long){
for(indexin1:
length(params$series)){
current_close=last(store$cl[[index]])
close=store$cl[[index]]
xtsclose=as.xts(close)
GETtma=getTMA(xtsclose,params$lookbacks)
pos=getPosSignFromTMA(GETtma)*getPosSize(current_close)
}
marketOrders<
--currentPos+pos
return(list(store=store,marketOrders=marketOrders,
limitOrders1=allzero,limitPrices1=allzero,
limitOrders2=allzero,limitPrices2=allzero))
}
########################################################################
#Thefollowingfunctionshouldbeeditedtocompletesteps1to3
#ofcomp22assignment2
getTMA<
-function(close_prices,lookbacks){
if(!
("
long"
%in%names(lookbacks)&
&
"
short"
medium"
%in%names(lookbacks)))
stop("
E01:
Atleastoneof\"
short\"
\"
medium\"
long\"
ismissingfromnames(lookbacks)"
#ReplaceTRUEto
#checkthattheelementsoflookbacksareallintegers
if(!
(class(lookbacks[[1]])=="
integer"
&
class(lookbacks[[2]])=="
class(lookbacks[[3]])=="
))
E02:
Atleastoneofthelookbacksisnotanintegeraccordingtois.integer()"
#checkthatlookbacks$short<
lookbacks$medium<
lookbacks$long
(lookbacks[[1]]<
lookbacks[[2]]&
lookbacks[[2]]<
lookbacks[[3]]))
E03:
Thelookbacksdonotsatisfylookbacks$short<
lookbacks$long"
#checkthatclose_pricesisanxts
(class(close_prices)[1]=="
xts"
E04:
close_pricesisnotanxtsaccordingtois.xts()"
#checkthatclose_priceshasenoughrows
if(nrow(close_prices)<
max(lookbacks[[1]],lookbacks[[2]],lookbacks[[3]]))
E05:
close_pricesdoesnotenoughrows"
#checkthatclose_pricescontainsacolumncalled"
Close"
(colnames(close_prices)=="
E06:
close_pricesdoesnotcontainacolumn\"
Close\"
"
sma=numeric(0)
for(iin1:
3){
sma[i]<
-as.numeric(last(SMA(close_prices,n=lookbacks[[i]])))#TTRversion#converttovectorfromxts
smalist<
-list(short=sma[1],medium=sma[2],long=sma[3])
#Youneedtoreplacetheassignmenttoretsothatthe
#returnedobject:
#-isalist
#-hastherightnames(short,medium,long),and
#-containsnumericandnotxtsobjects
#-andcontainsthecorrectmovingaveragevalues,whichshould
#havewindowsofthecorrectsizeswhichshouldallendinthe
#sameperiodwhichshouldbethelastrowofclose_prices
return(smalist)
getPosSignFromTMA<
-function(tma_list){
if(tma_list$short<
tma_list$medium&
tma_list$medium<
tma_list$long)ret=1
elseif(tma_list$long>
tma_list$medium>
tma_list$short)ret=-1
elseret=0
return(ret)
getPosSize<
-function(current_close,constant=1000){
return(floor(constant/current_close))
getInSampleResult<
-function(){
TIme=getInSamplePeriod('
x1xxx'
)
dataList<
-getData(directory="
A2"
-lapply(dataList,function(x)x[TIme[1]:
TIme[2]])
lookbacks<
-list(short=as.integer(5),medium=as.integer(10),long=as.integer(20))
sMult<
-0.2#slippagemultiplier
newRowList=getRowList(dataList,1)
numOfSeries=length(dataList)
params<
-list(lookbacks=lookbacks,sdParam=1,series=1:
numOfSeries)#,posSizes=rep(1,getPosSize(newRowList[[1]]$Close)))
results<
-backtest(dataList,getOrders,params,sMult)
pfolioPnL<
-plotResults(dataList,results)
pfolioPnL$fitAgg
cat("
PDratio"
pfolioPnL$fitAgg)
getInSampleOptResult<
x4wl1'
)#####
medium<
-seq(from=105,to=120,by=5)
short<
-seq(from=100,to=110,by=5)
long<
-seq(from=110,to=130,by=5)
time<
-matrix(0,28,3)
row=1
for(zin1:
length(long))
length(short))
for(jin1:
length(medium))if(short[i]<
medium[j]&
medium[j]<
long[z]){time[row,]=c(as.integer(short[i]),as.integer(medium[j]),as.integer(long[z]))
row=row+1}
colnames(time)=c("
"
resultsMatrix<
-matrix(nrow=nrow(time),ncol=4)
colnames(resultsMatrix)<
-c("
PDratio"
pfolioPnLList<
-vector(mode="
list"
length=nrow(time))
count<
-1
for(iin1:
nrow(time)){
newRowList=getRowList(dataList,count)
short=as.integer(time[i,1]);
medium=as.integer(time[i,2]);
long=as.integer(time[i,3]);
-list(short=short,medium=medium,long=long)
numOfSeries,posSizes=rep(1,getPosSize(newRowList[[1]]$Close)))
resultsMatrix[count,]<
-c(lookbacks[[1]],lookbacks[[2]],lookbacks[[3]],pfolioPnL$fitAgg)
pfolioPnLList[[count]]<
-pfolioPnL
Justcompleted"
count,"
outof"
time,"
\n"
print(resultsMatrix[count,])
-count+1
print(resultsMatrix[order(resultsMatrix[,"
]),])
return(max(resultsMatrix[,4]))
#ThefunctionsbelowdoNOTneedtobeeditedforcomp226assignment2
initClStore<
-function(newRowList){
clStore<
-lapply(newRowList,function(x)x$Close)
return(clStore)
updateClStore<
-function(clStore,newRowList){
-mapply(function(x,y)rbind(x,y$Close),clStore,newRowList,SIMPLIFY=FALSE)
initStore<
-function(newRowList,series){
return(list(iter=1,cl=initClStore(newRowList)))
updateStore<
-function(store,newRowList){
store$iter<
-store$iter+1
store$cl<
-updateClStore(store$cl,newRowList)
return(store)
#############################test
############getTMA
framework/data.R'
##Loadingrequiredpackage:
xts
zoo
##
##Attachingpackage:
'
zoo'
##Thefollowingobjectsaremaskedfrom'
package:
base'
:
##as.Date,as.Date.numeric
##Version0.4-0includednewdatadefaults.See?
getSymbols.
framework/backtester.R'
framework/processResults.R'
lookbacks<
#Readindata
dataList<
-getData(directory="
##Read3seriesfromDATA/A2
close_prices<
-dataList[[1]]$Close[1:
20]
getTMA(close_prices,lookbacks)
##$short
##[1]16.948
##$medium
##[1]17.086
##$long
##[1]17.1525
getPosSignFromTMA(getTMA(close_prices,lookbacks))
##[1]1
current_close<
-100.5
getPosSize(current_close)
##[1]9
getInSampleResult()
##PDratio-107565.9
getInSampleOptResult()
##Justcompleted1outof100100100105100100100105105110100100100100105105105110110100100100100105105105110110105105110110105110115110115115105110115120110115120115120105110115120110115120115120110115115115120120120120120120125125125125125125125125125130130130130130130130130130
##shortmediumlongPDratio
##100.00105.00110.00-71145.22
##Justcompleted2outof100100100105100100100105105110100100100100105105105110110100100100100105105105110110105105110110105110115110115115105110115120110115120115120105110115120110115120115120110115115115120120120120120120125125125125125125125125125130130130130130130130130130
##100.00105.00115.00-44965.95
##Justcompleted3outof100100100105100100100105105110100100100100105105105110110100100100100105105105110110105105110110105110115110115115105110115120110115120115120105110115120110115120115120110115115115120120120120120120125125125125125125125125125130130130130130130130130130
##100.00110.00115.00-51909.61
##Justcompleted4outof100100100105100100100105105110100100100100105105105110110100100100100105105105110110105105110110105110115110115115105110115120110115120115120105110115120110115120115120110115115115120120120120120120125125125125125125125125125130130130130130130130130130
##105.00110.00115.00-80736.32
##Justcompleted5outof1001001001051001001001051051101001001001001051051051101101001001001001051051051101101051051101101051101151101151151051