r-在Quantstrat中,我最多只能用add.indicator()函数添加7个指示符.如何在mktdata对象中包



当我向策略添加7个以上指标并使用applyIndicators函数时收到的错误消息:

(函数(HLC,n=20,maType,c=0.015,…(中的错误:价格序列必须是高位-低位收盘,或收盘/单变量。此外:警告消息:在log(x(中:NaNs产生


代码如下:

library(tsibble)
library(rlang)
library(dplyr)
library(tawny)
library(kernlab)
library(corpcor)
library(xts)
library(tidyquant)
library(rsample)
library(tidyr)
library(dplyr)
library(ggplot2)
library(dplyr)
library(magrittr)
library(scales)
library(tsibble)
library(purrr)
library(timetk)
library(kableExtra)
library(quantmod)
library(TTR)
library(PerformanceAnalytics)
library(FinancialInstrument)
library(foreach)
library(blotter)
library(quantstrat)
library(ggplot2)
library(dplyr)
library(IKTrading)
library(png)
library(devtools)
####### Part 1: Boiler Plate Set Up ####### 
# Set up Blotter
rm(list = ls(.blotter), envir = .blotter)
# Parameters and Dates
initdate <- "2010-01-01"
from <- "2011-01-01" #start of backtest
to <- "2021-12-01" #end of backtest
Sys.setenv(TZ= "EST") #Set up environment for timestamps
currency("USD") #Set up environment for currency to be used

# Get Data
symbols <- c("SPY","AAPL", "MSFT", "GOOG", "FB", "TWTR", "AMZN", "IBM", "NFLX", "NVDA","BAC", "UNH", "TSLA", "ZM", "PTON", "QCOM", "GE") #symbols used in our backtest
getSymbols(Symbols = symbols, src = "yahoo", from=from, to=to, adjust = TRUE) #receive data from google finance,  adjusted for splits/dividends
# Tells quanstrat what instruments present and what currency to use
stock(symbols, currency = "USD", multiplier = 1) 
# removes old portfolio and strategy from environment
rm.strat(portfolio.st)
rm.strat(strategy.st) 

###Set Up Strategy Parameters
# Portfolio Size and Trade Size
tradesize <-10000 # default trade size. What if i do it in terms of % of the Portfolio?
initeq <- 100000 # default initial equity in our portfolio
# Naming Strategy, Portfolio and Account
strategy.st <- portfolio.st <- account.st <- "firststrat" #naming strategy, portfolio and account
# Removes old portfolio and strategy from environment.
rm.strat(portfolio.st)
rm.strat(strategy.st) 
# Initialize Strategy, Portfolio and Account Objects
initPortf(portfolio.st, symbols = symbols, initDate = initdate, currency = "USD")
initAcct(account.st, portfolios = portfolio.st, initDate = initdate, currency = "USD", initEq = initeq)
initOrders(portfolio.st, initDate = initdate)
strategy(strategy.st, store=TRUE)

####### Part 2: Indicators, Signals and Rules ####### 
##### 2.1 Indicators
chartSeries(TSLA,type="line",subset='2019-02::2021-12',theme=chartTheme('white'))
candleChart(IBM, up.col = "black", dn.col = "red", theme = "white")
addSMA(n = c(200,50), on = 1, col = c("red", "blue"))
plot(RSI(Cl(AMZN), n=10)) #Plots the RSI with lookback equal to 10 days 
## The add.indicator() Function
# Parameter List
.RSI.n = 3
.RSI.Up = 80
.RSI.Down = 20
.MACDSlow = 26
.MACDFast = 12
.Bbands.n = 20
.Bbands.sd = 2
.ROCSlow = 30
.ROCFast = 14

## The add.indicator() Function
add.indicator(strategy = strategy.st, name = "RSI", arguments = list(price = quote(Cl(mktdata)), n=.RSI.n), label = 'RSI')
add.indicator(strategy = strategy.st, name = "MACD", arguments = list(x=quote(Cl(mktdata)),nFast=.MACDFast, nSlow=.MACDSlow),label='MACD' )
add.indicator(strategy = strategy.st, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), n = .Bbands.n, maType = "SMA", sd = .Bbands.sd), label = "Bbands")
add.indicator(strategy = strategy.st, name = "ROC", arguments = list(x=quote(Cl(mktdata)),n=.ROCFast,type='continuous'),label="ROCFAST")
add.indicator(strategy = strategy.st, name = "ROC", arguments = list(x=quote(Cl(mktdata)),n=.ROCSlow,type='continuous'),label="ROCSLOW")
add.indicator(strategy = strategy.st, name = "momentum", arguments = list(x=quote(Cl(mktdata)),n=3),label="MOMFAST")
add.indicator(strategy = strategy.st, name = "momentum", arguments = list(x=quote(Cl(mktdata)),n=20),label="MOMSLOW")
add.indicator(strategy = strategy.st, name = "CCI", arguments = list(HLC = quote(HLC(mktdata)), n = 20, maType = "SMA", c = 0.015), label = "CCI")
test <- applyIndicators(strategy.st, mktdata=OHLC(AAPL))
tail(test,10)

我认为你可以编写自己的指标,将你想添加到图表中的所有指标组合起来,并使用1 add.indicator((将它们全部添加到

我通过分批分离指标并多次声明applyIndicators((函数来解决问题。我用论据把指标分开。完成此操作后,代码运行良好。Quantstrat可以支持多个指标来构建更复杂的策略

最新更新