文档库 最新最全的文档下载
当前位置:文档库 › 【原创】R语言TMA三均线策略实现附代码数据

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

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

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(newRowList)

else

store <-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 when you have enough close prices

# for the long moving average

################################################

if (store$iter >params$lookbacks$long) {

for (index in 1: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)

}

}

################################################

# You 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 complete steps 1 to 3

# of comp22 assignment 2

getTMA <-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 f rom names(lookbacks)")

# Replace TRUE to

# check that the elements of lookbacks are all integers

if( !(class(lookbacks[[1]])=="integer"&&class(lookbacks[[2]])=="integ er"&&class(lookbacks[[3]])=="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 (!(lookbacks[[1]]

ium < 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 that close_prices has enough rows

if (nrow(close_prices)

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 \"Close\"")

sma=numeric(0)

for(i in 1:3){

sma[i] <-as.numeric(last(SMA(close_prices,n=lookbacks[[i]]))) # TTR version # convert to vector from xts

}

smalist <-list(short=sma[1],medium=sma[2],long=sma[3])

# You need to replace 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 which should be the last row of close_prices

return(smalist)

}

getPosSignFromTMA <-function(tma_list) {

if(tma_list$short

相关文档