Quantstrat:交易执行问题。
Quantstrat: trades execution problems.
所以我只是想测试简单的策略,当开盘价向上穿过移动平均线时买入,反之亦然;
Signal 似乎与 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
您需要指定一个 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"
)
所以我只是想测试简单的策略,当开盘价向上穿过移动平均线时买入,反之亦然;
Signal 似乎与 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
您需要指定一个 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"
)