[R-SIG-Finance] Blotter example by kafka from R-bloggers

Brian G. Peterson brian at braverock.com
Wed Dec 22 20:06:41 CET 2010


Pull his code directly, don't cut and paste HTML:

https://github.com/kafka399/Rproject/blob/master/3lowDays.R

That code Works for Me(tm)

If it doesn't work for you, then you may need to call

update.packages()

or

update.packages('blotter', repos="http://R-Forge.R-project.org")

Regards.

   - Brian

On 12/22/2010 01:01 PM, Stephen Choularton wrote:
> Hi
>
> I'm working on the Blotter example by kafka from R-bloggers. The code
> goes like this:
>
> require(xts)
> require(quantmod)
> Sys.setenv(TZ="GMT")
> initDate='2000-01-01'
> getSymbols('SPY',from=initDate,index.class=c("POSIXt","POSIXct"))
> SPY<-adjustOHLC(SPY,use.Adjusted=T)
>
> #yesterday's price
> tmp<-lag(SPY,1)
>
> # if today's low is higher than yesterday's close 1, else 0
> signal<-ifelse(Lo(SPY)>Cl(tmp),1,0)
> signal[1]<-0
>
> #let's plot cumulitative return to make sure, that we are on the right path
> tmp<-lag(Delt(Cl(SPY)),-1)
> tmp[length(tmp)]<-0
> plot(cumprod(signal*tmp+1))
>
> SPY<-Cl(SPY)
> symbols<-c('SPY')
>
> rm(list=ls(envir=.blotter),envir=.blotter)
> initPortf(ltportfolio,symbols, initDate=initDate)
> initAcct(ltaccount,portfolios=c(ltportfolio),
> initDate=initDate,initEq=initEq)
>
> signal<-signal['2000-02-01::']
>
> for(iin 1:length(signal))
> {
> currentDate= time(signal)[i]
> equity= getEndEq(ltaccount, currentDate)
> position= getPosQty(ltportfolio, Symbol=symbols[1], Date=currentDate)
>
> if(position==0)
> {
> #open a new position if signal is>0
> if(signal[i]>0)
> {
> closePrice<-as.double(Cl(SPY[currentDate]))
> unitSize= as.numeric(trunc((equity/closePrice)))
> # 5$ per transaction
> addTxn(ltportfolio, Symbol=symbols[1], TxnDate=currentDate,
> TxnPrice=closePrice, TxnQty= -unitSize , TxnFees=5, verbose=T)
> }
>
> }
> else
> {
> #position is open. If signal is 0 - close it.
> if(as.double(signal[i])==0 && position<0)
> {
> position= getPosQty(ltportfolio, Symbol=symbols[1], Date=currentDate)
> closePrice<-as.double((Cl(SPY[currentDate])))#as.double(get(symbols[1])[i+100])
>
>
> # 5$ per transaction
> addTxn(ltportfolio, Symbol=symbols[1], TxnDate=currentDate,
> TxnPrice=closePrice, TxnQty= -position , TxnFees=5, verbose=T)
>
> }
>
> }
> updatePortf(ltportfolio, Dates= currentDate)
> updateAcct(ltaccount, Dates= currentDate)
> updateEndEq(ltaccount, Dates= currentDate)
>
>
>
> There appears to be some sort of transcription problem at:
>
> if(as.double(signal[i])==0 && position<0)
>
> so I have changed it to:
>
> if(as.double(signal[i])==0 && position<0)
>
> so the code now reads:
>
> require(xts)
> require(quantmod)
>
> Sys.setenv(TZ="GMT")
> initDate='2000-01-01'
> getSymbols('SPY',from=initDate,index.class=c("POSIXt","POSIXct"))
> SPY<-adjustOHLC(SPY,use.Adjusted=T)
>
> #yesterday's price
> tmp<-lag(SPY,1)
>
> # if today's low is higher than yesterday's close 1, else 0
> signal<-ifelse(Lo(SPY)>Cl(tmp),1,0)
> signal[1]<-0
>
> #let's plot cumulitative return to make sure, that we are on the right path
> tmp<-lag(Delt(Cl(SPY)),-1)
> tmp[length(tmp)]<-0
> plot(cumprod(signal*tmp+1))
>
> SPY<-Cl(SPY)
> symbols<-c('SPY')
>
> rm(list=ls(envir=.blotter),envir=.blotter)
> initPortf(ltportfolio,symbols, initDate=initDate)
> initAcct(ltaccount,portfolios=c(ltportfolio),
> initDate=initDate,initEq=initEq)
>
> signal<-signal['2000-02-01::']
>
> for(iin 1:length(signal))
> {
> currentDate= time(signal)[i]
> equity= getEndEq(ltaccount, currentDate)
> position= getPosQty(ltportfolio, Symbol=symbols[1], Date=currentDate)
>
> if(position==0)
> {
> #open a new position if signal is>0
> if(signal[i]>0)
> {
> closePrice<-as.double(Cl(SPY[currentDate]))
> unitSize= as.numeric(trunc((equity/closePrice)))
> # 5$ per transaction
> addTxn(ltportfolio, Symbol=symbols[1], TxnDate=currentDate,
> TxnPrice=closePrice, TxnQty= -unitSize , TxnFees=5, verbose=T)
> }
>
> }
> else
> {
> #position is open. If signal is 0 - close it.
> if(as.double(signal[i])==0 && position<0)
> {
> position= getPosQty(ltportfolio, Symbol=symbols[1], Date=currentDate)
> closePrice<-as.double((Cl(SPY[currentDate])))#as.double(get(symbols[1])[i+100])
>
>
> # 5$ per transaction
> addTxn(ltportfolio, Symbol=symbols[1], TxnDate=currentDate,
> TxnPrice=closePrice, TxnQty= -position , TxnFees=5, verbose=T)
>
> }
>
> }
> updatePortf(ltportfolio, Dates= currentDate)
> updateAcct(ltaccount, Dates= currentDate)
> updateEndEq(ltaccount, Dates= currentDate)
>
> equity= getEndEq(ltaccount, currentDate)
>
> }
> plot(getAccount(ltaccount)[["TOTAL"]]$End.Eq)
>
> equity= getEndEq(ltaccount, currentDate)
>
> }
> plot(getAccount(ltaccount)[["TOTAL"]]$End.Eq)
>
> but I keep on getting error messages like this:
>
> Error in getEndEq(ltaccount, currentDate) : object 'ltaccount' not found
>  > plot(getAccount(ltaccount)[["TOTAL"]]$End.Eq)
> Error in getAccount(ltaccount) : object 'ltaccount' not found
>  >
>  > equity = getEndEq(ltaccount, currentDate)
> Error in getEndEq(ltaccount, currentDate) : object 'ltaccount' not found
>  >
>  > }
> Error: unexpected '}' in "}"
>  > plot(getAccount(ltaccount)[["TOTAL"]]$End.Eq)
> Error in getAccount(ltaccount) : object 'ltaccount' not found
>
> I have tried references to blotter like library(blotter) and
> require(blotter) but it makes no difference.
>
> I appreciate I haven't declared these objects/variables but often with R
> and finance they seem to come with the package. Can anyone point out
> what I am doing wrong?
>


-- 
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock



More information about the R-SIG-Finance mailing list