R: Quantstrat examples of Guy Yollin. Indicators necessary? And what is stored in these financial instruments?

隐身守侯 提交于 2019-12-13 05:06:45

问题


Hi I'm working through this code (this works and is reproducible)

if (!exists('.blotter')) .blotter <- new.env()
if (!exists('.strategy')) .strategy <- new.env()
if (!exists('.instrument')) .instrument <- new.env()
currency("USD")
stock("SPY",currency="USD",multiplier=1)
ls(envir=FinancialInstrument:::.instrument)


initDate <- '1997-12-31'
startDate <- '1998-01-01'
endDate <- '2013-07-31'
initEq <- 1e6
Sys.setenv(TZ="UTC")
getSymbols('SPY', from=startDate, to=endDate, adjust=T)
SPY=to.monthly(SPY, indexAt='endof')
SPY$SMA10m <- SMA(Cl(SPY), 10)

# inz portfolio, account
qs.strategy <- "qsFaber"
rm.strat(qs.strategy) # remove strategy etc. if this is a re-run
initPortf(qs.strategy,'SPY', initDate=initDate)
initAcct(qs.strategy,portfolios=qs.strategy, initDate=initDate, initEq=initEq)


initOrders(portfolio=qs.strategy,initDate=initDate)
# instantiate a new strategy object
strategy(qs.strategy,store=TRUE)
add.indicator(strategy = qs.strategy, name = "SMA",
              arguments = list(x = quote(Cl(mktdata)), n=10), label="SMA10")
add.signal(qs.strategy,name="sigCrossover",
           arguments = list(columns=c("Close","SMA10"),relationship="gt"),
           label="Cl.gt.SMA")
add.signal(qs.strategy,name="sigCrossover",
           arguments = list(columns=c("Close","SMA10"),relationship="lt"),
           label="Cl.lt.SMA")

add.rule(qs.strategy, name='ruleSignal',
         arguments = list(sigcol="Cl.gt.SMA", sigval=TRUE, orderqty=900,
                          ordertype='market', orderside='long', pricemethod='market'),
         type='enter', path.dep=TRUE)
add.rule(qs.strategy, name='ruleSignal',
         arguments = list(sigcol="Cl.lt.SMA", sigval=TRUE, orderqty='all',
                          ordertype='market', orderside='long', pricemethod='market'),
         type='exit', path.dep=TRUE)


out <- applyStrategy(strategy=qs.strategy , portfolios=qs.strategy)
updatePortf(qs.strategy)
updateAcct(qs.strategy)
updateEndEq(qs.strategy)

myTheme<-chart_theme()
myTheme$col$dn.col<-'lightblue'
myTheme$col$dn.border <- 'lightgray'
myTheme$col$up.border <- 'lightgray'
# plot performance
chart.Posn(qs.strategy, Symbol = 'SPY', Dates = '1998::',theme=myTheme)
plot(add_SMA(n=10,col=4, on=1, lwd=2))

Since I want to use this on more complex strategies I have several questions:

  1. Are the indicators necessary?

  2. In Connection to 1: What is actually stored by these Strategy objects etc.? First he creates a column SMA10m directly in the SPY table. As far as I understand he then constructs the indicator which is basically the same as already created in the SPY table in order for the signals to work correct? So the code

    arguments = list(columns=c("Close","SMA10")

accesses the Close (which is obviously stored as well?) as the SMA10 which is the indicator am I right? Is there a way to ommit the indicator if not needed? Or is the indicator just another column in the SPY table since he accesses it with the columns command?


回答1:


First, to directly answer the two points:

  1. indicators are not necessary to a strategy, the only hard and fast requirement is that you need at least one rule that will create orders, or at least one rule in the order slot that will create transactions.

  2. the strategy object contains only the specification of the strategy, nothing more, see below.

Next, to explain what's going on:

quantstrat makes extensive use of delayed execution to allow for code reuse. The strategy object is a storage place for the specification of the strategy. It may be applied to one or more portfolios (as created by initPortf()) by using the portfolios= argument.

The strategy specification is only a storehouse of how you want to apply the strategy later. Until you call applyStrategy(...), nothing is evaluated. This allows for useful properties like using the same strategy object to test multiple different parameter sets, or applying against different portfolio constructions and constituents, without changing the strategy specification.

The strategy object itself is not changed by applyStrategy. Inside applyStrategy, a special internal object called mktdata is created which will be modified by the execution of the indicators, signals, and rules contained in the strategy specification.

The mktdata object is constructed, by default, from retrieving the object containing your historical data from the .GlobalEnv, or some other environment specified by the user. There will be one of these objects created for each symbol in the portfolio, and maintained inside the function scope of applyStrategy.

When the indicators and signals are applied, the most common pattern is for these functions is to return a vector of the same length as the mktdata time series, or a time series object with the same index as the mktdata time series. If this pattern is followed, these columns will be added to the mktdata object and available for later indicator, signal, and rule functions to make use of. indicators and signals are presumed to always be not path dependent, and rules, by default, are path dependent.

The example signal functions, such as sigCrossover and sigThreshold, make use of this pattern to access and compare columns that exist in mktdata. ruleSignal, an example rule, looks for points where signals have some particular value, and then acts on that information to create orders.

External references:

Using quantstrat (R/Finance 2013)

Introduction to quantstrat (FOSS trading blog)



来源:https://stackoverflow.com/questions/19489122/r-quantstrat-examples-of-guy-yollin-indicators-necessary-and-what-is-stored-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!