【原創】R語言TMA三均線策略實現附代碼數據_第1頁
【原創】R語言TMA三均線策略實現附代碼數據_第2頁
【原創】R語言TMA三均線策略實現附代碼數據_第3頁
【原創】R語言TMA三均線策略實現附代碼數據_第4頁
【原創】R語言TMA三均線策略實現附代碼數據_第5頁
免費預覽已結束,剩余35頁可下載查看

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、Active on 29% of days; PD ratio = -107565.07source ('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

2、and updates the store#if (is.null(store)store<-initStore (newRowList)elsestore<-updateStore (store, newRowList)#pos <- allzero# This next code section is the only one you# need to edit for getOrders # The if condition is already correct:# you should only start computing the moving# averages

3、 when you have enough close prices# for the long moving average#if (store$iter >params$lookbacks$long) for (index in1 : length (params$series) current_close= last (store$clindex)close=store$clindexxtsclose= as.xts (close)GETtma= getTMA(xtsclose,params$lookbacks)pos= getPosSignFromTMA (GETtma)* ge

4、tPosSize (current_close)# You do not need to edit this part of the code# that initializes and updates the store#marketOrders <- -currentPos + posreturn ( list ( store= store, marketOrders= marketOrders, limitOrders1=allzero,limitPrices1=allzero,limitOrders2=allzero,limitPrices2=allzero)# # The fo

5、llowing function should be edited to complete steps 1 to 3# of comp22 assignment 2getTMA <- function(close_prices, lookbacks) if (!( "long" %in% names(lookbacks) && "short" %in% names(lookba cks) && "medium" %in% names(lookbacks) )stop ( "E01: At

6、 least one of "short", "medium", "long" is missi ng from names(lookbacks)" )# Replace TRUE to# check that the elements of lookbacks are all integersif( !( class (lookbacks 1 )= "integer" && class (lookbacks 2)= "integer" && class

7、 (lookbacks 3)= "integer" )stop ( "E02: At least one of the lookbacks is not an integer according to eger()" )# Replace TRUE to# check that lookbacks$short < lookbacks$medium < lookbacks$longif (!(lookbacks1<lookbacks 2 && lookbacks 2<lookbacks 3)stop (

8、"E03: The lookbacks do not satisfy lookbacks$short < lookbacks$medium < lookbacks$long" )# Replace TRUE to# check that close_prices is an xtsif (!( class (close_prices) 1= "xts" )stop ( "E04: close_prices is not an xts according to is.xts()")# Replace TRUE to# che

9、ck that close_prices has enough rowsif ( nrow (close_prices)< max(lookbacks 1,lookbacks 2,lookbacks 3)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" )s

10、top ( "E06: close_prices does not contain a column "Close"") 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= sma 1, medium=sma 2, long= sma 3)# You need to replace

11、 the assignment to ret so that the# returned object:# - is a list# - has the right names (short, medium, 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 whic

12、h should be the last row of close_pricesreturn (smalist)getPosSignFromTMA <- function(tma_list) if(tma_list$short<tma_list$medium && tma_list$medium<tma_list$long)ret= 1else if(tma_list$long>tma_list$medium &&rt)ret=- 1else ret= 0return (ret)tma_list$medium>tma_list$sh

13、ogetPosSize <- function(current_close,constant= 1000) return ( floor (constant/current_close) getInSampleResult <- function() TIme= getInSamplePeriod ( 'x1xxx ')dataList <- getData ( directory= "A2" )dataList <- lapply (dataList, function(x)xTIme1:TIme 2)lookbacks <-

14、 list ( short= eger ( 5), medium= eger ( 10), long= as.in teger (20)sMult <-0.2 # slippage multipliernewRowList= getRowList (dataList, 1)numOfSeries= length (dataList)params <- list ( lookbacks= lookbacks, sdParam= 1 , series= 1 :numOfSeries) #,posSizes=rep(1,getPosSize(newRowList1

15、$Close)results <- backtest (dataList, getOrders, params, sMult)pfolioPnL <- plotResults (dataList,results)pfolioPnL$fitAggcat ( "PD ratio " ,pfolioPnL$fitAgg)getInSampleOptResult <-function() sMult <-0.2 # slippage multiplierTIme= getInSamplePeriod( 'x4wl1' )#dataList &

16、lt;- getData ( directory= "A2" ) dataList <- lapply (dataList, function(x)xTIme sMult <-0.2 # slippage multipliernumOfSeries= length (dataList)1:TIme 2)medium <- seq ( from= 105, to= 120, by=5) short <- seq ( from= 100, to= 110, by= 5) long <- seq ( from= 110, to= 130, by=5)

17、 time <- matrix ( 0, 28, 3)row= 1for(z in 1:for(i infor(j in gz)timerow,= r (longz)row=rowlength (long)1 : length (short)1 : length (medium)if(shorti<mediumj && c( eger (shorti), eger+1mediumj< lon(mediumj), egecolnames (time)= c( "short", "medium&qu

18、ot; , "long" )resultsMatrix <-matrix ( nrow= nrow (time), ncol= 4)colnames (resultsMatrix) <-c( "short" , "medium" , "long" , "PDratio" )pfolioPnLList <- vector ( mode="list" , length= count <-1for (i in 1 : nrow (time) newRowLi

19、st= getRowList (dataList,count)nrow (time)short= eger (timei,1);medium= eger (timei, 2);long= eger (timei, lookbacks <params <-3);list ( short= list ( lookbacks=short, medium=medium, long= long)lookbacks, sdParam= 1, series= 1:numOfSeries, posSizes= rep ( 1, getPosSize (newRo

20、wList 1$Close)results <- backtest (dataList, getOrders, params, sMult) pfolioPnL <- plotResults (dataList,results)resultsMatrixcount, <- 3,pfolioPnL$fitAgg)pfolioPnLListcount<- cat ( "Just completed"c(lookbacks 1,lookbackspfolioPnL,count, "out of" ,time, "n"

21、 )2,lookbacksprint (resultsMatrixcount,)count <- count +1"PDratio" ),)print (resultsMatrix order (resultsMatrix, return ( max(resultsMatrix, 4) # The functions below do NOT need to be edited for comp226 assignment 2initClStore <- function(newRowList) clStore <- lapply (newRowList,

22、 function(x) x$Close)return (clStore)updateClStore <- function(clStore, newRowList) clStore <- mapply (function(x,y) rbind (x,y$Close),clStore,newRowList,SIMPLIFY= FALSE)return (clStore)initStore <-return ( listfunction(newRowList,series) ( iter= 1 , cl= initClStore (newRowList)updateStore

23、<-store$iter <- store$cl <-function(store, newRowList) store$iter +1updateClStore (store$cl,newRowList)return (store)#test library (TTR)# #getTMAsource ( 'framework/data.R' )# # Loading required package: xts# # Loading required package: zoo# # # Attaching package: 'zoo'# # T

24、he following objects are masked from 'package:base':# # # as.Date, as.Date.numeric# # Version 0.4-0 included new data defaults. See ?getSymbols.source ( 'framework/backtester.R' )source ( 'framework/processResults.R' )lookbacks <- list ( short= eger ( 5), medium=as.i

25、nteger ( 10), long= e ger ( 20)# Read in datadataList <- getData ( directory= "A2" )# # Read 3 series from DATA/A2close_prices <-dataList1 $Close 1: 20getTMA(close_prices,lookbacks)# # $short# # 1 16.948# # # $medium# # 1 17.086# # # $long# # 1 17.1525getPosSignFromTMA (getTMA(

26、close_prices,lookbacks)# # 1 1current_close <-100.5getPosSize (current_close)# # 1 9getInSampleResult ()# # Read 3 series from DATAA2口一-30000 -60000-Acfive on 29% of days; PD ratio = -107565.071971-071975-011971-01C2 : PD ratio = J2C4 2990000-# # PD ratio -107565.9getInSampleOptResult ()# # Read

27、3 series from DATA/A2Active on W 特 of d5ys: pd ratio = -71145 22# # Just completed 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 12 0 105 110 115 120 110

28、 115 120 115 120 110 115 115 115 120 120 120 120 1 20 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 110.00 -71145.22oooo0-0001234. 一 一 f# # Just completed 2 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105

29、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 12 0 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 1 20 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130# # short medium long P

30、Dratio# # 100.00 105.00 115.00 -44965.95# # Just completed 3 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 12 0 105 110 115 120 110 115 120 115 120 110 115

31、 115 115 120 120 120 120 1 20 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 115.00 -51909.61:PD ratio = 574 85;3407.63 = 002:PD ratio =-909 的10000-1000-2000# # Just completed 4 out of 100 100 100 105 100 100 100 105 105 110

32、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 12 0 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 1 20 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 130 130-40

33、000-0, 10000-# # 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 105 110 110 105 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 12 0 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 1

34、20 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.180 -10000-20000-3000 D- -40000-50000JQ2 : PD ratio = -378.2103. PD ration -4B03S.S21971-C11971-07 1972-A1# # Just completed 6 out of 100 100 100 105 100 100 100 105

35、 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 12 0 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 1 20 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130 1

36、30 130# # short medium long PDratio# # 100.00 110.00 120.00 -55139.2703 . PD ratio = -50016.32Active on 18 of d5ys: PD ratio = -50172 36-300-60C-1000-2000-10001Q71-C11971-071Q72-011971-011971-071972-011971-C11971-07 1972-A120 120 125 125 125 125 125 125 125 125 125 130 130 130 130 130 130 130130 130

37、1971-011971-071972-01-10000-20000-30000 -40000-50000 -# Just completed 7 out of 100 100 100 105 100 100 100 105 105 110 100100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 105105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 12 0 105 110 115 120 110 115 120 115 1

38、20 110 115 115 115 120 120 120 120 1PDratifl = 919.01 J 40gg 89 = 03OQO-02 PDratio = -1075 05# short medium long PDratio# 100.00 115.00 120.00 -50172.36Active on 18 % of days:PD ratio = -76396 12000 0-2000TWO-60000-2000D-40000-5000019 7 1-011971-07 1372-01# # Just completed 8 out of 100 100 100 105

39、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 12 0 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 1 20 120 125 125 125 125 125 125 125 125 125 130 130 130 13

40、0 130 130 130 130 130Active on 19 of d5ys: pd ratio = -74760 3302 PDratio = -1073 03# # 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 12 0

41、 105 110 115 120 110 115 120 115 120 110 115 115 115 120 120 120 120 1 20 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.33Q 一-20000-60000-0-2000 D-40000-600001971-C1 1971-07 1972-01# # Just completed 10 out of 100

42、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 10 5 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 1 20 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 13

43、0 130 130 130 130 130 130 130 130Active on 13% of days; PD ratio = -37520 4901: PD ratio = -2550 41Q71-C11971-071972-fll03. PD ration -34670.45# # 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 10 5 105 110 110

44、 105 110 115 110 115 115 105 110 115 120 110 115 120 115 1 20 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 125.00 -37520.49# # Just completed 12 out of 100

45、 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 10 5 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 1 20 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 1

46、30 130 130 130 130 130 130 130 1300 一'10000 -4DOOO ”Active on 15 疆 of days; PD ratio = -37721 271971-011971-071972-01# # Just completed 13 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 10 5 105 110 110 105 110 115 110 115 1

47、15 105 110 115 120 110 115 120 115 1 20 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 125.00 -37721.27Active on 16% of days; PD ratio = -53276 12-20000-4000

48、0 -6DOOO,03. PD ration -52539.411971-C11971-07 1972-A11971-015 -1QOOO- -2QOQO- 30000 -40000-50000-# # 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 10 5 105 110 110 105 110 115 110 115 115 105 110 115 120 110

49、115 120 115 1 20 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 130Active on 16 % of days; PD ratio = -67416.fl# # Just completed 15 out of 100 100 100 105 100 100 100 105 105 110 100 100 100 100 105 105

50、 105 110 110 100 100 100 100 105 105 105 110 110 10 5 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 1 20 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 PDrati

51、o# 105.00 110.00 125.00 -67416.080 -20000 -400D0-60000-PD ratio = 173,2J4191.99 = 0.(1Q71-C11971-071Q72-01100小0-1000-2000-3000-# # 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 10 5 105 110 110 105 110 115 110

52、 115 115 105 110 115 120 110 115 120 115 1 20 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 1300-20000 *-40000 -03 . PD ratio = -50641.91# # Just completed 17 out of 100 100 100 105 100 100 100 105 105

53、110 100 100 100 100 105 105 105 110 110 100 100 100 100 105 105 105 110 110 10 5 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 1 20 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 13

54、0# short medium long PDratio# 105.00 120.00 125.00 -53643.250 -20000 *-40000 -PD ratio = 1S78 39J3d19 66 =0# # Just completed 18 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 10 5 105 110 110 105 110 115 110 115 115 105 110 115

55、 120 110 115 120 115 1 20 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 1300 -20000-40000-6D000-1971-C11971-07 1972-A1# # 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 10 5 105 110 110 105 110 115 110 115 115 105 110 115 120 110 115 120 115 1 20 105 110

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論