Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将quantstrat中的时间框架从每日更改为每周_R_Quantmod_Quantstrat - Fatal编程技术网

将quantstrat中的时间框架从每日更改为每周

将quantstrat中的时间框架从每日更改为每周,r,quantmod,quantstrat,R,Quantmod,Quantstrat,因此,我只是在quantstrat中执行EMA50交叉策略,效果很好,但我希望将时间框架从每天改为每周。 我尝试将stock()函数存储为到.weekly(SPY),但他们不允许我这样做。我想稍后对多个股票进行尝试,因此必须在投资组合中应用 library(quantstrat) rm(list=ls(.blotter), envir=.blotter) strategy.st<-"firststrat" portfolio.st<-"firststrat" account.st&

因此,我只是在
quantstrat
中执行EMA50交叉策略,效果很好,但我希望将时间框架从每天改为每周。 我尝试将
stock()
函数存储为
到.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)
。。。。
我可以改成每周吗

你在正确的轨道上。如果您执行
SPY=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
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
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