gpt4 book ai didi

r - 如何在 R 中引导

转载 作者:行者123 更新时间:2023-12-05 05:15:46 25 4
gpt4 key购买 nike

我在 Quantstrat 的 R 中获得了运行回测的 RUT 指数的返回。现在我想创建一个返回的 Bootstrap ,但我不知道该怎么做。我知道 boot() 函数需要数据、统计信息和 Bootstrap 重复次数。在数据中我想我可以添加“返回”(见下面的代码)但我不知道如何计算“统计”。有人可以帮忙吗?

请引用以下代码:

library(devtools)
library(quantmod)
library(quantstrat)
library(TTR)
library(png)
library(IKTrading)
install_github("braverock/blotter")
install_github("braverock/quantstrat")
install_github('IlyaKipnis/IKTrading')


initdate <- "2010-01-01"
from <- "2012-01-01" #start of backtest
to <- "2017-31-12" #end of backtest
n<- 30

Sys.setenv(TZ= "GMT") #Set up environment for timestamps

currency("USD") #Set up environment for currency to be used

symbols <- c("RUT") #symbols used in our backtest
getSymbols("^RUT",src="yahoo", from="2012-01-01", to="2017-12-31", periodicity="daily")


wma <- WMA(Cl(RUT), n=4, wts=c(1:4))
wmamaxt <- rollmaxr(wma, n, fill = NA)
wmamint <- - rollmaxr(- wma, n, fill = NA)
CNOwma <- function (RUT) {(wma - wmamint) / (wmamaxt - wmamint)}

stock(symbols, currency = "USD", multiplier = 1) #tells quanstrat what instruments present and what currency to use

tradesize <-10000 #default trade size
initeq <- 100000 #default initial equity in our portfolio

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 portfolio, account, orders and strategy 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)

add.indicator(strategy = strategy.st,
name = 'SMA',
arguments = list(x = quote(Cl(mktdata)), n=100),
label = 'SMA100')

add.indicator(strategy = strategy.st,
name = 'SMA',
arguments = list(x = quote(Cl(mktdata)), n=30),
label = 'SMA30')

add.indicator(strategy = strategy.st,
name = 'SMA',
arguments = list(x = quote(Cl(mktdata)), n=15),
label = 'SMA15')

add.signal(strategy.st, name = "sigCrossover",
arguments = list(columns=c("SMA15", "SMA30")),
relationship = "lt",
label = "Exitlong")

add.signal(strategy.st, name = "sigCrossover",
arguments = list(columns=c("SMA15", "SMA100")),
relationship = "gt",
label = "Enterlong")



add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "Exitlong", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "exit")



add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "Enterlong", sigval = TRUE,
orderqty = 1, ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open", osFUN = IKTrading::osMaxDollar,
tradeSize = tradesize, maxSize = tradesize),
type = "enter")

out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
updatePortf(portfolio.st)
daterange <- time(getPortfolio(portfolio.st)$summary)[-1]

updateAcct(account.st, daterange)
updateEndEq(account.st)

for(symbol in symbols){

chart.Posn(Portfolio = portfolio.st, Symbol = symbol,
TA= c("add_SMA(n=15, col='blue')", "add_SMA(n=30, col='red')", "add_SMA(n=100, col='green')"))
} # in “symbol” add “RUT” or “IXIC”


final_acct <- getAccount(account.st)
end_eq <- final_acct$summary$End.Eq

returns <- Return.calculate(end_eq, method="log")
library(boot)
boot(returns, k=1, R=1000)
Error in boot(returns, k = 1, R = 1000) :
l'argomento "statistic" non è specificato e non ha un valore predefinito

最佳答案

我相信以下内容可以满足您的需求。

library(boot)

boot_returns <- function(data, indices){
d <- data[indices]
Return.calculate(d, method="log")
}

bret <- boot(end_eq, boot_returns, R=1000)

dim(bret$t)
bretMeans <- colMeans(bret$t, na.rm = TRUE)

关于r - 如何在 R 中引导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com