这是我的代码:
library(quantstrat)
suppressWarnings(try(rm(list=ls(FinancialInstrument:::.instrument),pos=FinancialInstrument:::.instrument),silent=TRUE))
currency("EUR")
stock("ATX",currency="EUR",multiplier=1)
ls(envir=FinancialInstrument:::.instrument)
ls(all=T)
initDate <- '1999-01-04'
startDate <- '1999-01-05'
endDate <- '2014-09-25'
initEq <- 1e6
Sys.setenv(TZ="UTC")
## ----echo=FALSE,results='hide'-------------------------------------------
if(file.exists("ATX.RData"))
{
load("ATX.RData")
} else {
getSymbols('^ATX', from=startDate, to=endDate, index.class="POSIXct", adjust=T)
save(list="ATX",file="ATX.RData")
}
##
b <- BBands(HLC=HLC(ATX["1999"]), n=20, sd=2)
##
rm.strat("multiAsset.bb1") # remove portfolio, account, orderbook if re-run
initPortf(name="multiAsset.bb1", "ATX", initDate=initDate)
initAcct(name="multiAsset.bb1", portfolios="multiAsset.bb1",
initDate=initDate, initEq=initEq)
initOrders(portfolio="multiAsset.bb1", initDate=initDate)
#
strategy("bbands", store=TRUE)
#
add.indicator("bbands", name = "BBands",
arguments = list(HLC = quote(HLC(ATX)), maType='SMA'), label='BBands')
#Sell short when the close crosses above the upper band
add.signal("bbands", name="sigCrossover",
arguments=list(columns=c("Close","up"),relationship="gt"),
label="Cl.gt.UpperBand")
# Buy long when the close crosses below the lower band
add.signal("bbands", name="sigCrossover",
arguments=list(columns=c("Close","dn"),relationship="lt"),
label="Cl.lt.LowerBand")
add.signal("bbands", name="sigCrossover",
arguments=list(columns=c("High","Low","mavg"),relationship="op"),
label="Cross.Mid")
###
add.rule("bbands", name='ruleSignal',
arguments=list(prefer="Close",sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100,
ordertype='market', orderside=NULL),type='enter')
add.rule("bbands", name='ruleSignal',
arguments=list(prefer="Close",sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100,
ordertype='market', orderside=NULL),type='enter')
add.rule("bbands", name='ruleSignal',
arguments=list(prefer="Close",sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all',
ordertype='market', orderside=NULL),type='exit')
##
SD = 2
N = 20
##
out <- applyStrategy("bbands",
portfolios="multiAsset.bb1",parameters=list(sd=SD,n=N))
##
updatePortf("multiAsset.bb1")
updateAcct("multiAsset.bb1")
updateEndEq("multiAsset.bb1")
当我运行updatePortf("multiAsset.bb1")
时,会出现:
updatePortf("multiAsset.bb1")
[1] "multiAsset.bb1"
Warning messages:
1: In getInstrument(p.ccy.str) :
instrument USD not found, please create it first.
2: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :
CurrencyUSD not found, using currency multiplier of 1
Question 1
:
我做错了什么,所以出现以下内容?我想使用EUR
作为货币,而不是USD
。
Question 2
:
如果我在prefer="Close"
中指定add.rule
,ruleSignal
函数将使用交易价格的下一天收盘,这是否正确?
最好的问候