因此,我试图仅仅测试简单的策略,当开放时移动平均线买入,反之亦然;
信号似乎与View(mktdata)
一起正常工作但由于某种原因它没有执行交易。
任何见解?
rm(list=ls(.blotter), envir=.blotter)
rm.strat(strategy.st)
strategy.st<-"firststrat"
portfolio.st<-"firststrat"
account.st<-"firststrat"
#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="all",
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)
View(mktdata)
> out
NULL
答案 0 :(得分:2)
您需要指定一个orderqty
值,表示市场订单进入仓位的每笔交易的大小(不是"all"
,可以用于退出,止损,获利)。否则,该策略不知道交易规模应该是多少。
如果您修改输入规则,则应运行代码:
add.rule(strategy.st,name="ruleSignal",
arguments=list(sigcol = "crossentry",
sigval=TRUE,
orderqty= 100, # numeric qty value
ordertype="market",
orderside="long",
replace=FALSE,
prefer="Open",
path.dep=TRUE
),
type="enter"
)