[R-SIG-Finance] Account object not updating. Ending Equity remains the same.
Bos, Roger
roger.bos at rothschild.com
Thu May 11 22:24:32 CEST 2017
Brian,
Almost there, you just need to update the portfolio, account, and ending equity after you apply the strategy. You code works fine after you add those lines as shown below:
out <- applyStrategy.rebalancing(strategy='simplemoving',
portfolios='multisimplemoving')
# START NEW CODE
#update
tradeStrategy="multisimplemoving"
updatePortf(tradeStrategy)
updateAcct(tradeStrategy)
updateEndEq(tradeStrategy)
#END NEW CODE
source("checkBlotterUpdate.R")
checkBlotterUpdate('multisimplemoving', 'multisimplemoving')
Thanks,
Roger
-----Original Message-----
From: R-SIG-Finance [mailto:r-sig-finance-bounces at r-project.org] On Behalf Of Brian Aaron
Sent: Wednesday, May 10, 2017 12:03 PM
To: r-sig-finance at r-project.org
Subject: [R-SIG-Finance] Account object not updating. Ending Equity remains the same.
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]]
_______________________________________________
R-SIG-Finance at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
This message and any attachments are for the intended recipient’s use only. This message may contain confidential, proprietary or legally privileged information. No right to confidential or privileged treatment of this message is waived or lost by an error in transmission.
If you have received this message in error, please immediately notify the sender by e-mail, delete the message, any attachments and all copies from your system and destroy any hard copies. You must not, directly or indirectly, use, disclose, distribute, print or copy any part of this message or any attachments if you are not the intended recipient.
More information about the R-SIG-Finance
mailing list