问题
Here is my code:
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")
When I run updatePortf("multiAsset.bb1")
, this appears:
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
:
What I am doing wrong, so the following above appears? I want to use EUR
for the currency, not USD
.
Question 2
:
Is it correct that when I specify prefer="Close"
in add.rule
the ruleSignal
function will use the next days close for the trading price?
Best Regards
来源:https://stackoverflow.com/questions/26062096/warning-in-quantstrat