【原创】R语言TMA三均线策略实现附代码数据

上传人:沈*** 文档编号:205348560 上传时间:2023-04-28 格式:DOCX 页数:37 大小:205.15KB
收藏 版权申诉 举报 下载
【原创】R语言TMA三均线策略实现附代码数据_第1页
第1页 / 共37页
【原创】R语言TMA三均线策略实现附代码数据_第2页
第2页 / 共37页
【原创】R语言TMA三均线策略实现附代码数据_第3页
第3页 / 共37页
资源描述:

《【原创】R语言TMA三均线策略实现附代码数据》由会员分享,可在线阅读,更多相关《【原创】R语言TMA三均线策略实现附代码数据(37页珍藏版)》请在装配图网上搜索。

1、source(in-sample_period.R)library(TTR)getOrders - function(store, newRowList, currentPos, params) allzero - rep(0,length(newRowList) # used for initializing vectors # # You do not need to edit this part of the code # that initializes and updates the store # if (is.null(store) store - initStore(newRo

2、wList) else store - updateStore(store, newRowList) # pos params$lookbacks$long) for (index in 1:length(params$series) current_close=last(store$clindex) close=store$clindex xtsclose=as.xts(close) GETtma=getTMA(xtsclose,params$lookbacks) pos=getPosSignFromTMA(GETtma)*getPosSize(current_close) # # You

3、do not need to edit this part of the code # that initializes and updates the store # marketOrders - -currentPos + pos return(list(store=store,marketOrders=marketOrders, limitOrders1=allzero,limitPrices1=allzero, limitOrders2=allzero,limitPrices2=allzero)# The following function should be edited to c

4、omplete steps 1 to 3# of comp22 assignment 2getTMA - function(close_prices, lookbacks) if (!(long %in% names(lookbacks) & short %in% names(lookbacks) & medium %in% names(lookbacks) ) stop(E01: At least one of short, medium, long is missing from names(lookbacks) # Replace TRUE to # check that the ele

5、ments of lookbacks are all integers if( !(class(lookbacks1)=integer & class(lookbacks2)=integer & class(lookbacks3)=integer) stop(E02: At least one of the lookbacks is not an integer according to is.integer() # Replace TRUE to # check that lookbacks$short lookbacks$medium lookbacks$long if (!(lookba

6、cks1lookbacks2 & lookbacks2lookbacks3) stop(E03: The lookbacks do not satisfy lookbacks$short lookbacks$medium lookbacks$long) # Replace TRUE to # check that close_prices is an xts if (!(class(close_prices)1=xts) stop(E04: close_prices is not an xts according to is.xts() # Replace TRUE to # check th

7、at close_prices has enough rows if (nrow(close_prices)max(lookbacks1,lookbacks2,lookbacks3) stop(E05: close_prices does not enough rows) # Replace TRUE to # check that close_prices contains a column called Close if (!(colnames(close_prices)=Close) stop(E06: close_prices does not contain a column Clo

8、se) sma=numeric(0) for(i in 1:3) smai -as.numeric(last(SMA(close_prices,n=lookbacksi) # TTR version # convert to vector from xts smalist - list(short=sma1,medium=sma2,long=sma3) # You need to replace the assignment to ret so that the # returned object: # - is a list # - has the right names (short, m

9、edium, long), and # - contains numeric and not xts objects # - and contains the correct moving average values, which should # have windows of the correct sizes which should all end in the # same period which should be the last row of close_prices return(smalist) getPosSignFromTMA - function(tma_list

10、) if(tma_list$shorttma_list$medium & tma_list$mediumtma_list$medium & tma_list$mediumtma_list$short)ret=-1 else ret=0 return(ret) getPosSize - function(current_close,constant=1000) return(floor(constant/current_close)getInSampleResult - function() TIme=getInSamplePeriod(x1xxx ) dataList -getData(dir

11、ectory=A2) dataList -lapply(dataList, function(x)xTIme1:TIme2) lookbacks -list(short=as.integer(5),medium=as.integer(10),long=as.integer(20) sMult - 0.2 # slippage multiplier newRowList=getRowList(dataList,1) numOfSeries=length(dataList) params -list(lookbacks=lookbacks,sdParam=1,series=1:numOfSerie

12、s)#,posSizes=rep(1,getPosSize(newRowList1$Close) results - backtest(dataList, getOrders, params, sMult) pfolioPnL - plotResults(dataList,results) pfolioPnL$fitAgg cat(PD ratio ,pfolioPnL$fitAgg) getInSampleOptResult - function() sMult - 0.2 # slippage multiplier TIme=getInSamplePeriod(x4wl1)# dataLi

13、st -getData(directory=A2) dataList -lapply(dataList, function(x)xTIme1:TIme2) sMult - 0.2 # slippage multiplier numOfSeries=length(dataList) 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(z in 1:length(long) for(i

14、 in 1:length(short) for(j in 1:length(medium)if(shortimediumj & mediumj longz)timerow,=c(as.integer(shorti),as.integer(mediumj),as.integer(longz) row=row+1 colnames(time)=c(short,medium,long) resultsMatrix - matrix(nrow=nrow(time),ncol=4) colnames(resultsMatrix) - c(short,medium,long,PDratio) pfolio

15、PnLList - vector(mode=list,length=nrow(time) count - 1 for (i in 1:nrow(time) newRowList=getRowList(dataList,count) short=as.integer(timei,1);medium=as.integer(timei,2);long=as.integer(timei,3); lookbacks -list(short=short,medium=medium,long=long) params -list(lookbacks=lookbacks,sdParam=1,series=1:

16、numOfSeries,posSizes=rep(1,getPosSize(newRowList1$Close) results - backtest(dataList, getOrders, params, sMult) pfolioPnL - plotResults(dataList,results) resultsMatrixcount, - c(lookbacks1,lookbacks2,lookbacks3,pfolioPnL$fitAgg) pfolioPnLListcount- pfolioPnL cat(Just completed,count,out of,time,n) p

17、rint(resultsMatrixcount,) count - count + 1 print(resultsMatrixorder(resultsMatrix,PDratio),) return(max(resultsMatrix,4)# The functions below do NOT need to be edited for comp226 assignment 2initClStore - function(newRowList) clStore - lapply(newRowList, function(x) x$Close) return(clStore)updateCl

18、Store - function(clStore, newRowList) clStore - mapply(function(x,y) rbind(x,y$Close),clStore,newRowList,SIMPLIFY=FALSE) return(clStore)initStore - function(newRowList,series) return(list(iter=1,cl=initClStore(newRowList)updateStore - function(store, newRowList) store$iter - store$iter + 1 store$cl

19、- updateClStore(store$cl,newRowList) return(store)#testlibrary(TTR)#getTMAsource(framework/data.R)# Loading required package: xts# Loading required package: zoo# # Attaching package: zoo# The following objects are masked from package:base:# # as.Date, as.Date.numeric# Version 0.4-0 included new data

20、 defaults. See ?getSymbols.source(framework/backtester.R)source(framework/processResults.R)lookbacks -list(short=as.integer(5),medium=as.integer(10),long=as.integer(20)# Read in datadataList - getData(directory=A2)# Read 3 series from DATA/A2close_prices -dataList1$Close1:20getTMA(close_prices,lookb

21、acks)# $short# 1 16.948# # $medium# 1 17.086# # $long# 1 17.1525getPosSignFromTMA(getTMA(close_prices,lookbacks)# 1 1current_close - 100.5getPosSize(current_close)# 1 9getInSampleResult()# Read 3 series from DATA/A2# PD ratio -107565.9getInSampleOptResult()# Read 3 series from DATA/A2# Just complete

22、d 1 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 1

23、25 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 105.00 110.00 -71145.22# Just completed 2 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120

24、 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 105.00 115.00 -44965.95# Just completed 3 out of 100 100 100 105 100 100 100 105 105 110 100 1

25、00 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short m

26、edium long PDratio # 100.00 110.00 115.00 -51909.61# Just completed 4 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120

27、 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 105.00 110.00 115.00 -80736.32# Just completed 5 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 1

28、05 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 105.00 120.00 -41036.18# Jus

29、t completed 6 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125

30、 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 110.00 120.00 -55139.27# Just completed 7 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 1

31、10 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 115.00 120.00 -50172.36# Just completed 8 out of 100 100 100 105 100 100 100 105 105

32、 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130

33、 # short medium long PDratio # 105.0 110.0 120.0 -76396.1# Just completed 9 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 1

34、15 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 105.00 115.00 120.00 -74760.33# Just completed 10 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 10

35、5 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 110.00 115.00 120.00 -68067.

36、44# Just completed 11 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125

37、 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 105.00 125.00 -37520.49# Just completed 12 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115

38、115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 110.00 125.00 -42490.56# Just completed 13 out of 100 100 100 105 100 100 1

39、00 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 1

40、30 130 130 # short medium long PDratio # 100.00 115.00 125.00 -37721.27# Just completed 14 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 12

41、0 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 120.00 125.00 -58276.12# Just completed 15 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100

42、 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 105.00 110.00

43、 125.00 -67416.08# Just completed 16 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120

44、120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 105.00 115.00 125.00 -61405.05# Just completed 17 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 1

45、10 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 105.00 120.00 125.00 -53643.25# Just completed 18 out of 100 100 10

46、0 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 13

47、0 130 130 130 130 130 130 # short medium long PDratio # 110.00 115.00 125.00 -53917.27# Just completed 19 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120

48、 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 110.00 120.00 125.00 -61091.66# Just completed 20 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105

49、105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio

50、# 100.00 105.00 130.00 -32146.52# Just completed 21 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 1

51、20 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 110.00 130.00 -29651.36# Just completed 22 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 10

52、5 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 115.00 130.00 -18750.82# Just completed 23 ou

53、t of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125

54、 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 100.00 120.00 130.00 -41816.79# Just completed 24 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110

55、115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 105.00 110.00 130.00 -55087.09# Just completed 25 out of 100 100 100 105 100 100 100 105 105 110 100 100 1

56、00 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 120 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130 # short medium long PDratio # 105.00 115.00 130.00 -45589.66# Just completed 26 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 1

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!