首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Quantstrat中,我只能用add.indicator()函数将7个指标相加。如何包含更多指示符( mktdata对象中的列)?

在Quantstrat中,我只能用add.indicator()函数将7个指标相加。如何包含更多指示符( mktdata对象中的列)?
EN

Stack Overflow用户
提问于 2021-12-21 23:40:03
回答 2查看 82关注 0票数 0

当我向策略添加超过7个指示符并使用applyIndicators函数时收到的错误消息:

误差(函数(HLC,n= 20,maType,c= 0.015,.):价格序列必须是高-低收盘,或封闭/单变量。此外:警告消息: In log(x):生成NaNs

守则如下:

代码语言:javascript
复制
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)
EN

Stack Overflow用户

发布于 2021-12-23 20:45:13

我认为您可以编写自己的指示符,将所有要添加到图表中的指标组合在一起,并使用1 add.indicator()将它们全部添加

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70442738

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档