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

时间:2016-12-12 02:39:23

标签: r quantmod quantstrat

所以我只是在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"

.... 我可以用任何方式将其改为每周一次吗?

1 个答案:

答案 0 :(得分:5)

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

这是一种更通用的自动化方法,因为您希望为证券组合执行此操作。为了提高可读性,我只是在您的问题中显示您需要修改以与&gt; = 2证券一起运行的代码部分:

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