将 quantstrat 中的时间范围从每天更改为每周

Changing time frame in quantstrat from daily to weekly

所以我只是在 quantstrat 中执行 EMA50 交叉策略,它工作正常,但我希望将时间范围从每天更改为每周。 我尝试将 stock() 函数存储为 to.weekly(SPY),但他们不允许我这样做。我想稍后对多个股票尝试这个,所以它必须在投资组合中应用。

library(quantstrat)
rm(list=ls(.blotter), envir=.blotter)

strategy.st<-"firststrat"
portfolio.st<-"firststrat"
account.st<-"firststrat"
rm.strat(strategy.st)


#assignsymbol
getSymbols("SPY",auto.assign=TRUE,adjust=TRUE)

initdate<-"2009-01-01"
from<-"2010-01-01"
to<-"2016-11-01"
Sys.setenv(TZ="UTC")
currency("USD")
stock("SPY",currency="USD",multiplier=1)
tradesize<-10000
inieq<-100000

rm.strat(portfolio.st)
initPortf(portfolio.st,symbols="SPY",initDate=initdate,currency='USD')
initAcct(account.st,portfolios = portfolio.st,initDate = initdate,initEq = inieq,currency="USD")
initOrders(portfolio = portfolio.st,initDate = initdate)
strategy(strategy.st,store=TRUE)



add.indicator(strategy = strategy.st,name="EMA",arguments=list(x=quote(Cl(mktdata)),n=50),label="EMA50")
#if closing price goes over moving average 50 and TSi fference is less then 0.15, then long
#short when closing price touches below original closing price by x(depends on atr? previous lows?) 


add.signal(strategy.st,name="sigCrossover",
           arguments = list(columns=c("Close","EMA50"),
                            relationship="gt"),
           label="crossentry"    
)
add.signal(strategy.st,name="sigCrossover",
           arguments = list(columns=c("Close","EMA50"),
                            relationship="lt"),
           label="crossexit"    
)


add.rule(strategy.st,name="ruleSignal",
         arguments=list(sigcol = "crossentry",
                        sigval=TRUE,
                        orderqty=100,
                        ordertype="market",
                        orderside="long",
                        replace=FALSE,
                        prefer="Open",
                        path.dep=TRUE
         ),
         type="enter"
)           

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


out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)

.....

[1] "2016-01-04 00:00:00 SPY -100 @ 197.432029165538"
[1] "2016-02-23 00:00:00 SPY 100 @ 191.041013032617"
[1] "2016-02-24 00:00:00 SPY -100 @ 187.72241891553"
[1] "2016-02-26 00:00:00 SPY 100 @ 193.571820974787"
[1] "2016-03-01 00:00:00 SPY -100 @ 192.035603073637"

..... 有什么办法可以将其更改为每周一次吗?

你走在正确的轨道上。如果您在 运行 宁 applyStrategy 之前执行 SPY <- to.weekly(SPY),那么您将 运行 每周柱上的策略。请记住,quantstrat 使用符号的名称来查找数据,而不管基础数据的存储频率如何(在您的情况下,在名为 SPY 的对象中是每天还是每周)。

这是适合您的更通用的自动化方法,因为您想对证券投资组合执行此操作。为了提高可读性,我只是展示了您问题中需要修改为 运行 with >= 2 securities:

的代码部分
symbols <- c("SPY", "XLE")

getSymbols(symbols,auto.assign=TRUE,adjust=TRUE)
# Change to weekly frequency, using the same names for the symbols in the global environment (which is where you have assigned them in your getSymbols call)
lapply(symbols, function(x) assign(x = x, value = to.weekly(get(x, envir = globalenv()), name = x), envir = globalenv()))

initdate<-"2009-01-01"
from<-"2010-01-01"
to<-"2016-11-01"
Sys.setenv(TZ="UTC")
currency("USD")


# Use your symbols to construct the correct instrument types for your strategy.  You are running on stocks only, so simply pass in the vector named `symbols` to stock:
stock(symbols,currency="USD",multiplier=1)
tradesize<-10000
inieq<-100000

rm.strat(portfolio.st)
# When initializing your portfolios, pass in the symbols you want the strategy to run on (the vector named `symbols` here):
initPortf(portfolio.st,symbols=symbols,initDate=initdate,currency='USD')

# ... run the rest of your original code

applyStrategy 成功 运行 之后,您可以检查 mktdata 是您想要的格式(每周)最近的交易品种 运行(XLE 本例中符号向量中的 ETF)。你应该看到这样的东西:

mktdata["2016-04/2016-05"]
# XLE.Open XLE.High  XLE.Low XLE.Close XLE.Volume XLE.Adjusted EMA.EMA50 crossentry crossexit
# 2016-04-01 61.08612 61.83661 59.70363  60.29613   70527800     60.29613  64.16397         NA        NA
# 2016-04-08 60.18750 61.92548 59.19014  61.58974   81670700     61.58973  64.06302         NA        NA
# 2016-04-15 62.08348 63.73259 61.33299  62.74510   94225700     62.74510  64.01134         NA        NA
# 2016-04-22 61.45149 66.44819 61.30337  66.20132   94215100     66.20131  64.09722          1        NA
# 2016-04-29 65.97419 67.95905 64.96695  66.65556   92445000     66.65556  64.19754         NA        NA
# 2016-05-06 66.53706 66.81356 63.69309  64.45345   76727000     64.45345  64.20758         NA        NA
# 2016-05-13 64.08809 65.90507 62.70560  64.17696   65436100     64.17696  64.20638         NA         1
# 2016-05-20 65.01632 66.07294 63.68321  65.33233   79205200     65.33233  64.25053          1        NA
# 2016-05-27 64.94720 67.07030 64.72008  66.29019   55806400     66.29018  64.33052         NA        NA