简体   繁体   English

在 Quantstrat 中,我最多只能使用 add.indicator() Function 添加 7 个指标。 如何包含更多指标(mktdata object 中的列?

[英]In Quantstrat I can only add up to 7 indicators with the add.indicator() Function. How can I include more indicators (columns in the mktdata object?

The error message I receive when I add more than 7 indicators to the strategy and use the applyIndicators function:当我向策略添加超过 7 个指标并使用applyIndicators cators function 时收到的错误消息:

Error in (function (HLC, n = 20, maType, c = 0.015, ...): Price series must be either High-Low-Close, or Close/univariate. In addition: Warning message: In log(x): NaNs produced (函数(HLC,n = 20,maType,c = 0.015,...)中的错误:价格序列必须是高-低-收盘,或收盘/单变量。此外:警告消息:在日志(x)中:产生的 NaN


The code is as follows:代码如下:

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)

I think you can write your own indicator combining all indicators you want to add to your chart and use 1 add.indicator() to add them all我认为您可以编写自己的指标,结合您要添加到图表中的所有指标并使用 1 add.indicator() 将它们全部添加

I mannaged to solve the problem by separating the indicators in batches and stating the applyIndicators() function more than once.我设法通过分批分离指标并多次声明 applyIndicators() function 来解决问题。 I separated indicators by arguments.我用 arguments 分隔指标。 The code worked fine after this was done.完成后代码运行良好。 Quantstrat can support multiple indicators to build more complex strategies Quantstrat 可以支持多个指标来构建更复杂的策略

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM