0

运行以下代码时出现以下错误。“if (inherits(sret$indicators, "xts") & nrow(mktdata) == nrow(sret$indicators)) { 中的错误:参数长度为零”。我的猜测是我在线性回归曲线 (LRC) 函数中遗漏了一些东西。请帮忙。

# rm(list = ls(all = TRUE))
library(timeSeries)
library(timeDate)
library(tseries)
library(PerformanceAnalytics)
library(quantmod)
library(xts)
library(blotter)
library(TTR)
library(quantstrat)
library(FinancialInstrument)
library(reshape2)
library(ggplot2)
library(alphavantager)
library(dplyr)

startDate = "2001-01-01"

LRC <- function(x, n) {
  price <- Cl(x)
  m <- 1:n;
  y <- ts(coredata(price))[,1] 
  ab = rollapply(y,n,function(yt){coef(lm(yt~m))},align = "right")                                                                                                                    
  z <- ab[,2]*m[n]+ab[,1]
  price <- price[n:length(price)]
  z <- xts(unclass(z), order.by = as.Date(index(price)))
  colnames(z) <-"LRC"
  return(z)
}


symbols <- c("SPY", "GLD")

getSymbols(symbols, src="av", api.key="P3KV99S52NH4N3YZ", output.size="full", periodicity="daily", from = startDate, adjusted=FALSE, auto.assign = TRUE)

initDate <- startDate
initEq <- 100000
tradeSize <- initEq/length(symbols)
currency("USD")
stock(symbols, currency="USD",multiplier=1)


myTheme<-chart_theme() 
myTheme$col$dn.col<- 'lightblue'  
myTheme$col$dn.border <-  'lightgray'  
myTheme$col$up.border <-  'lightgray' 


if(exists('.strategy')) rm.strat(qs.strategy) 
if(!exists('.blotter')) .blotter <- new.env()
if(!exists('.strategy')) .strategy <- new.env()

qs.strategy <- "AD26"
initPortf(qs.strategy, symbols, initDate=initDate)
initAcct(qs.strategy,portfolios=qs.strategy, initDate=initDate, initEq=initEq)
initOrders(portfolio=qs.strategy,initDate=initDate)
strategy(qs.strategy,store=TRUE)


add.indicator(strategy = qs.strategy, name = "LRC",
              arguments = list(x = quote(OHLC(mktdata)), n=21), label="LR21")

add.indicator(strategy = qs.strategy, name = "LRC",
              arguments = list(x = quote(OHLC(mktdata)), n=5), label="LR5")


summary(getStrategy(qs.strategy))

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


add.signal(qs.strategy, name="sigCrossover", arguments=list(columns=c("LR5", "LR21"), relationship="gte"), label="Buy")

add.signal(qs.strategy, name="sigCrossover", arguments=list(columns=c("LR5", "LR21"), relationship="lt"), label="Sell")


summary(getStrategy(qs.strategy))

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

add.rule(qs.strategy, name='ruleSignal',
         arguments = list(sigcol="Buy", sigval=TRUE,  
                          prefer ="open",
                          replace = FALSE,
                          orderqty = 100, 
                          ordertype='market',
                          orderside = 'long'
                          
                          
         ),
         type='enter',
         label = 'LE'
)



add.rule(qs.strategy, name='ruleSignal',
         arguments = list(sigcol="Sell", sigval=TRUE,
                          replace = TRUE,
                          prefer ="Close",
                          ordertype='market',
                          orderqty="all",
                          orderside='long'
                          
                          
         ),
         type='exit',
         parent = "LE",
         label = "ProfitTake",
         enable = TRUE
)

summary(getStrategy(qs.strategy))

applyStrategy(strategy=qs.strategy , portfolios=qs.strategy)

sessionInfo() 返回以下内容:

R version 4.0.4 (2021-02-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] dplyr_1.0.4                alphavantager_0.1.2        highfrequency_0.8.0.1      anytime_0.3.9              forecast_8.13              jsonlite_1.7.2            
 [7] tidyquant_1.0.2            lubridate_1.7.9.2          riingo_0.3.1               pracma_2.3.3               urca_1.3-0                 Quandl_2.10.0             
[13] reshape2_1.4.4             Hmisc_4.4-2                ggplot2_3.3.3              Formula_1.2-4              survival_3.2-7             quantstrat_0.16.8         
[19] foreach_1.5.1              blotter_0.15.0             FinancialInstrument_1.3.1  quantmod_0.4.18            TTR_0.24.2                 PerformanceAnalytics_2.0.4
[25] xts_0.12.1                 zoo_1.8-8                  tseries_0.10-48            timeSeries_3062.100        timeDate_3043.102          lattice_0.20-41

4

0 回答 0