问题描述
我想基于不同的信号(例如SMA50 > SMA10
和MACD > 0
)添加多个规则.但是,使用sigComparision
时出现错误.有人可以建议一种更好的方法吗?
I want to add multiple rules based on different signals like SMA50 > SMA10
and MACD > 0
. However, I am getting an error using sigComparision
. Can anyone suggest a better way to do it?
推荐答案
您可以使用两种显而易见的方法:您可以在添加规则中构建复合信号功能,也可以使用sigFormula
.已知后者很慢.例如,请参见以下线程:
There are two obvious approaches you could use: You can build a composite signal function in add rules, or you could use sigFormula
. The latter is known to be slow. For example see this thread:
https://stat.ethz.ch/pipermail/r-sig-finance/2012q1/009310.html
我在这里突出显示一个关键部分:
I highlight a key section here:
....
我会警告您,尽管sigFormula非常灵活,但R并不是很灵活 快速使用此方法.这似乎是这种方式的副作用 data.frames被存储为列表,并且 eval(parse(text = formula),x)语法由R内部管理.
I will warn you that while sigFormula is very flexible, R isn't very fast using this methodology. It seems to be a side effect of the way data.frames are stored as lists, and of the way that the eval(parse(text=formula),x) syntax is managed internally by R.
对于每日或低频数据,这可能很好,但是对于 通常我发现较高的频率写自定义是有意义的 信号功能指示器,用于更复杂的比较.
For daily or lower-frequency data, that's probably fine, but for higher frequencies I usually find it makes sense to write a custom indicator of signal function for more complex comparisons.
在以下示例中(基于quantstrat软件包中的macd.R
演示),您可以尝试两种方法:
In the following example (based off the macd.R
demo in the quantstrat package) you can experiment with both approaches:
require(quantstrat)
suppressWarnings(rm("order_book.macd",pos=.strategy))
suppressWarnings(rm("account.macd","portfolio.macd",pos=.blotter))
suppressWarnings(rm("account.st","portfolio.st","stock.str","stratMACD","startDate","initEq",'start_t','end_t'))
stock.str='AAPL' # what are we trying it on
fastMA = 12
slowMA = 26
signalMA = 8
maType="EMA"
currency('USD')
stock(stock.str,currency='USD',multiplier=1)
startDate='2006-12-31'
initEq=1000000
portfolio.st='macd'
account.st='macd'
getSymbols(stock.str,from=startDate)
initPortf(portfolio.st,symbols=stock.str)
initAcct(account.st,portfolios=portfolio.st)
initOrders(portfolio=portfolio.st)
strat.st<-portfolio.st
# define the strategy
strategy(strat.st, store=TRUE)
#one indicator
add.indicator(strat.st, name = "MACD",
arguments = list(x=quote(Cl(mktdata)),
nFast=fastMA,
nSlow=slowMA),
label='_'
)
add.indicator(strat.st, name = "SMA",
arguments = list(x=quote(Cl(mktdata)),
n=10),
label='SMA10'
)
add.indicator(strat.st, name = "SMA",
arguments = list(x=quote(Cl(mktdata)),
n = 50),
label='SMA50'
)
# Create your own signal for entry:
macdSMAsig <- function(data) {
# first condition:
sig <- data[, "SMA.SMA50"] > data[, "SMA.SMA10"] & data[, "macd._"] > 0
colnames(sig) <- "upSig"
sig
}
# Activate (uncomment) only ONE of the following signals. Both do the same thing:
#OPTION 1 for entry signal based on combining signals:
add.signal(strat.st,name="macdSMAsig",
arguments = list(data = quote(mktdata)),
label="enterSig"
)
#OPTION 2 for entry signal based on combining signals:
# add.signal(strat.st, name = "sigFormula",
# arguments = list(data = quote(mktdata),
# formula = "SMA.SMA50 > SMA.SMA10 & macd._ > 0"),
# label = "upSig.enterSig"
# )
add.signal(strat.st,name="sigThreshold",
arguments = list(column="signal._",
relationship="lt",
threshold=0,
cross=TRUE),
label="signal.lt.zero"
)
####
# add rules
# entry
add.rule(strat.st,name='ruleSignal',
# be careful to get the label of the signal column correct:
arguments = list(sigcol="upSig.enterSig",
sigval=TRUE,
orderqty=100,
ordertype='market',
orderside='long',
threshold=NULL),
type='enter',
label='enter',
storefun=FALSE
)
# exit
add.rule(strat.st,name='ruleSignal',
arguments = list(sigcol="signal.lt.zero",
sigval=TRUE,
orderqty='all',
ordertype='market',
orderside='long',
threshold=NULL,
orderset='exit2'),
type='exit',
label='exit'
)
#end rules
####
out<-applyStrategy(strat.st , portfolios=portfolio.st,verbose=TRUE)
updatePortf(Portfolio=portfolio.st,Dates=paste('::',as.Date(Sys.time()),sep=''))
chart.Posn(Portfolio=portfolio.st,Symbol=stock.str)
tx <- getTxns(portfolio.st, stock.str)
sum(tx$Net.Txn.Realized.PL)
这篇关于Quantstrat:如何创建多个指标,信号规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!