[R-SIG-Finance] Account object not updating. Ending Equity remains the same.
Brian Aaron
rbrianaaron at hotmail.com
Wed May 10 18:02:46 CEST 2017
I am trying to implement a simple moving average strategy with rebalancing.
I have tried to follow the Guy Yollin tutorial as closely as possible.
I run the CheckBlotterUpdate function and get:
"portfolio P&L doesn't match account P&L"
A review of the ending equity in the account object shows that it has not been updated.
Codes is below.
Thank you for any feedback.
Brian Aaron
library(quantstrat)
library(blotter)
startdate <- "1995-08-18"
enddate <- "1999-01-01"
initDate <- "1995-08-17"
symbols <- c('SPY', 'MDY')
getSymbols(symbols,
src="yahoo",
from=startdate,
to=enddate,
index.class=c("POSIXt","POSIXct"),
adjust=TRUE)
#Initial equity
initEq <- 1000000
currency("USD")
Sys.setenv(TZ="UTC")
stock(primary_id= symbols, currency="USD", multiplier=1)
strategy("simplemoving", store=TRUE)
add.indicator(strategy = "simplemoving",
name = "SMA",
arguments = list(x=quote(Cl(mktdata)),
n=50),
label='firstsma')
add.indicator(strategy = "simplemoving",
name = "SMA",
arguments = list(x=quote(Cl(mktdata)),
n=200),
label="secondsma")
#Add Buying Signal
add.signal(strategy="simplemoving",
name="sigCrossover",
arguments=list(columns=c("firstsma","secondsma"),
relationship="gt"),
label="buysignal")
#Add Selling Signal
add.signal(strategy="simplemoving",
name="sigCrossover",
arguments=list(columns=c("firstsma","secondsma"),
relationship="lt"),
label="sellsignal")
#Add Enter Rule
add.rule(strategy="simplemoving",
name='ruleSignal',
arguments=list(sigcol="buysignal",
sigval=TRUE,
orderqty=100000,
ordertype='market',
orderside='long',
osFUN='osMaxPos'),
type='enter',
path.dep=TRUE)
#Add Exit Rule
add.rule(strategy="simplemoving",
name='ruleSignal',
arguments=list(sigcol="sellsignal",
sigval=TRUE,
orderqty='all',
ordertype='market',
orderside='long',
pricemethod="market"),
type='exit',
path.dep=TRUE)
# add rebalancing
add.rule('simplemoving', 'rulePctEquity',
arguments=list(rebalance_on='months',
trade.percent=1/length(symbols),
refprice=quote(
last(getPrice(mktdata)[paste('::',as.character(curIndex),sep='')][,1])
),
digits=0
),
type='rebalance',
label='rebalance'
)
rm.strat(name="multisimplemoving")
initPortf(name="multisimplemoving",
symbols = symbols,
initDate=initDate)
initAcct(name="multisimplemoving",
portfolios="multisimplemoving",
initDate=initDate,
initEq=initEq)
initOrders(portfolio="multisimplemoving",
initDate=initDate)
#Add position limits
posval <- initEq/length(symbols)
for(symbol in symbols){
pos<-round((posval/first(getPrice(get(symbol)))),-2)
addPosLimit('multisimplemoving',symbol,initDate, maxpos=pos,minpos=-pos)
}
out <- applyStrategy.rebalancing(strategy='simplemoving',
portfolios='multisimplemoving')
source("checkBlotterUpdate.R")
checkBlotterUpdate('multisimplemoving', 'multisimplemoving')
#Strategy Equity Curve
acct <- getAccount(Account="multisimplemoving")
print(acct$summary)
[[alternative HTML version deleted]]
More information about the R-SIG-Finance
mailing list