R:Quantstrat盖伊·尤林(Guy Yollin)的例子.指标必要吗?这些金融工具中存储了什么? [英] R: Quantstrat examples of Guy Yollin. Indicators necessary? And what is stored in these financial instruments?

查看:92
本文介绍了R:Quantstrat盖伊·尤林(Guy Yollin)的例子.指标必要吗?这些金融工具中存储了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过这段代码进行工作(此方法有效且可重现)

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))

由于我想将其用于更复杂的策略,所以我有几个问题:

  1. 指标是否必要?

  2. 在与1的连接中:这些Strategy对象等实际存储了什么?首先,他直接在SPY表中创建一列SMA10m.据我了解,他随后构造了与SPY表中已创建的指示器基本相同的指示器,以使信号正常工作?因此,代码

    参数= list(columns = c("Close","SMA10")

访问SMA10的Close(显然也被存储了吗?),这是指标吗?如果不需要,是否可以忽略该指标?还是由于指示器使用columns命令访问该指示器,它只是SPY表中的另一列?

解决方案

首先,直接回答两点:

  1. 指标不是策略所必需的,唯一的硬性要求是您至少需要一个规则来创建订单,或者至少要在order槽中创建一个交易规则./p>

  2. strategy对象仅包含策略的规范,仅此而已,请参见下文.

接下来,解释发生了什么事情:

quantstrat广泛使用了延迟执行以允许代码重用. strategy对象是该策略的规范的存储位置.通过使用portfolios=参数,它可以应用于一个或多个投资组合(由initPortf()创建).

策略规范只是您以后如何应用策略的仓库.在调用applyStrategy(...)之前,不会评估任何内容.这允许使用有用的属性,例如使用同一策略对象来测试多个不同的参数集,或针对不同的投资组合构造和组成部分应用,而无需更改策略规范.

applyStrategy不会更改策略对象本身.在applyStrategy内部,创建了一个名为mktdata的特殊内部对象,该对象将由策略规范中包含的指标,信号和规则的执行进行修改.

默认情况下,mktdata对象是通过从.GlobalEnv或用户指定的其他环境中检索包含历史数据的对象而构造的.将为项目组合中的每个符号创建这些对象之一,并将其​​保留在applyStrategy的功能范围内.

应用指示器和信号时,这些功能最常见的模式是返回长度与mktdata时间序列相同的向量,或返回与mktdata时间序列具有相同索引的时间对象.如果遵循此模式,则这些列将被添加到mktdata对象,并且可供以后的指示符,信号和规则函数使用.假定指示器和信号始终与路径无关,默认情况下,规则与路径有关.

示例信号功能(例如sigCrossoversigThreshold)利用此模式来访问和比较mktdata中存在的列. ruleSignal(一个示例规则)查找信号具有某些特定值的点,然后根据该信息创建订单.

外部参考:

使用Quantstrat(R/Finance 2013)

Quantstrat简介(FOSS交易博客)

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?

解决方案

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)

这篇关于R:Quantstrat盖伊·尤林(Guy Yollin)的例子.指标必要吗?这些金融工具中存储了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆