[R-SIG-Finance] Blotter example by kafka from R-bloggers
Stephen Choularton
stephen at organicfoodmarkets.com.au
Tue Dec 28 08:14:09 CET 2010
Hi
I'm still not winning with this. I installed blotter again from
https://r-forge.r-project.org/R/?group_id=316 but now I am getting this
inside the loop:
There were 50 or more warnings (use warnings() to see the first 50)
> warnings()
Warning messages:
1: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
2: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
3: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
4: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
5: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
6: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
7: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
8: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
9: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
10: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
11: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
12: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
13: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
14: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
15: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
16: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
17: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
18: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
19: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
20: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
21: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
22: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
23: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
24: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
25: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
26: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
27: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
28: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
29: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
30: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
31: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
32: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
33: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
34: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
35: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
36: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
37: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
38: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
39: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
40: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
41: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
42: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
43: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
44: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
45: In getInstrument(symbol) : Instrument SPY not found, please create
it first.
46: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
47: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), ... :
Instrument SPY not found, things may break
48: In updatePortf(ltportfolio, Dates = currentDate) :
Incompatible methods ("Ops.Date", "Ops.POSIXt") for ">="
49: In getInstrument(Symbol) : Instrument SPY not found, please create
it first.
50: In addTxn(ltportfolio, Symbol = symbols[1], TxnDate = currentDate,
... :
Instrument SPY not found, using contract multiplier of 1
>
getInstrument doesn't even appear in the code so I'm not sure where this
is going wrong.
Stephen Choularton Ph.D., FIoD
On 23/12/2010 9:12 AM, Brian G. Peterson wrote:
> Error in if (ncol(Prices)> 1) Prices = getPrice(Prices, Symbol) :
> argument is of length zero
>
> This bug was fixed in svn r502, from a couple days ago. It looked like
> R-Forge had the new version earlier today.
>
> So, I'd say your best bet might be to download and install manually, or
> wait 24 hours and see if R-Forge catches up.
>
> On Thu, 23 Dec 2010 08:50:30 +1100, Stephen Choularton
> <stephen at organicfoodmarkets.com.au> wrote:
>> Hi Brian
>>
>> Thanks. I did go there and ran the code but I still got a problem so I
> did
>>
>> update.packages()
>>
>> and
>>
>> update.packages('blotter', repos="http://R-Forge.R-project.org")
>>
>> but still ended up with problems when I came out of the loop:
>>
>> }
>> + updatePortf(ltportfolio, Dates = currentDate)
>> + updateAcct(ltaccount, Dates = currentDate)
>> + updateEndEq(ltaccount, Dates = currentDate)
>> + }
>> [1] 0
>> [1] "1995-01-04"
>> Error in if (ncol(Prices)> 1) Prices = getPrice(Prices, Symbol) :
>> argument is of length zero
>> In addition: Warning messages:
>> 1: In getInstrument(symbol) :
>> Instrument SPY not found, please create it first.
>> 2: In getInstrument(Symbol) :
>> Instrument SPY not found, please create it first.
>> 3: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol), :
>> Instrument SPY not found, things may break
>> > rez1<-(getPortfolio(ltaccount))
>> > plot(cumsum(rez1$symbols$SPY$txn[,9]))
>> Error in if (on == "years") { : missing value where TRUE/FALSE needed
>> >
>> > #gross profit
>> > result<-rez1$symbols$SPY$txn[,7]
>> > result<-result[result!=0]
>> >
>> > #fix commission rate 2*3
>> > plot(cumsum(result-6))
>> Error in try.xts(x, error = "must be either xts-coercible or timeBased")
> :
>> must be either xts-coercible or timeBased
>> >
>> > chart.CumReturns(cbind((result)/10000,(result-6)/10000))
>> Error in `[.xts`(x, start.row, 1) : subscript out of bounds
>> >
>> >
>>
>> The full code I used was:
>>
>> require('xts')
>> require('quantmod')
>> require('blotter')
>> require('PerformanceAnalytics')
>> require('FinancialInstrument')
>> Sys.setenv(TZ="GMT")
>>
>> getSymbols('SPY',from='1995-01-01',index.class=c("POSIXt","POSIXct"))
>>
> dividends<-getDividends('SPY',from='1995-01-01',index.class=c("POSIXt","POSIXct"))
>> temp<-cbind(dividends,SPY)
>> temp[,1][is.na(temp[,1])]<-0
>>
>> SPY<-cbind(temp[,2],temp[,3],temp[,4],temp[,1]+temp[,5])
>> colnames(SPY)<-c('Open','High','Low','Close')
>>
>> #one day before
>> lag1<-lag((SPY),1)
>>
>> #two days defore
>> lag2<-lag((SPY),2)
>>
>> signal<-ifelse( (Cl(lag2)>Cl(lag1)& Cl(lag1)>Cl(SPY))&
>> (Hi(lag2)>Hi(lag1)& Hi(lag1)>Hi(SPY))&
>> (Op(lag2)>Op(lag1)& Op(lag1)>Op(SPY)),
>> 1,0
>> )
>> #one day later
>> lag3<-lag(Cl(SPY),-1)
>>
>>
>> profit<-(lag3/Cl(SPY)-1)*signal
>> chart.CumReturns(profit)
>>
>> #------------------
>> tmp<-(Hi(lag2)/Lo(SPY)-1)*signal
>>
>> #blotter code
>> symbols<-c('SPY')
>> SPY<-Cl(SPY)
>> initDate=time(get(symbols)[1])
>> initEq=10000
>> rm(list=ls(envir=.blotter),envir=.blotter)
>> ltportfolio='3days'
>> ltaccount='3days'
>> initPortf(ltportfolio,symbols, initDate=initDate)
>> initAcct(ltaccount,portfolios=c(ltportfolio),
>> initDate=initDate,initEq=initEq)
>> currency("USD")
>> stock(symbol,currency="USD",multiplier=1)
>>
>> signal[is.na(signal)]<-0
>>
>>
>> counter<-0
>>
>> for(i in 2:length(signal))
>> {
>> currentDate= time(signal)[i]
>> equity = 10000 #getEndEq(ltaccount, currentDate)
>> position = getPosQty(ltportfolio, Symbol=symbols[1],
> Date=currentDate)
>> print(position)
>> print(currentDate)
>> if(position==0)
>> {
>> #open a new position if signal is>0
>> if(signal[i]>0&counter ==0)
>> {
>> print('open position')
>> closePrice<-as.double(Cl(SPY[currentDate]))
>> print(closePrice)
>> unitSize = as.numeric(trunc((equity/closePrice)))
>> print(unitSize)
>> commssions=-unitSize*closePrice*0.0003
>> addTxn(ltportfolio, Symbol=symbols[1],
>> TxnDate=currentDate, TxnPrice=closePrice, TxnQty = unitSize ,
>> TxnFees=commssions, verbose=T)
>> counter<-1
>> }
>>
>> }
>> else
>> {
>> #position is open. If signal is 0 - close it.
>> if(position>0&counter>=1)
>> {
>> position = getPosQty(ltportfolio, Symbol=symbols[1],
>> Date=currentDate)
>>
>>
> closePrice<-as.double((Cl(SPY[currentDate])))#as.double(get(symbols[1])[i+100])
>> commssions=-position*closePrice*0.0003
>> addTxn(ltportfolio, Symbol=symbols[1],
>> TxnDate=currentDate, TxnPrice=closePrice, TxnQty = -position ,
>> TxnFees=commssions, verbose=T)
>> counter<-0
>> }
>> else
>> {
>> print(counter)
>> counter<-counter+1
>> }
>> }
>> updatePortf(ltportfolio, Dates = currentDate)
>> updateAcct(ltaccount, Dates = currentDate)
>> updateEndEq(ltaccount, Dates = currentDate)
>> }
>> rez1<-(getPortfolio(ltaccount))
>> plot(cumsum(rez1$symbols$SPY$txn[,9]))
>>
>> #gross profit
>> result<-rez1$symbols$SPY$txn[,7]
>> result<-result[result!=0]
>>
>> #fix commission rate 2*3
>> plot(cumsum(result-6))
>>
>> chart.CumReturns(cbind((result)/10000,(result-6)/10000))
>>
>> Does it work for you?
>>
>> Stephen Choularton Ph.D., FIoD
>>
>> On 23/12/2010 6:06 AM, Brian G. Peterson wrote:
>>> 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?
>>>>
>>>
More information about the R-SIG-Finance
mailing list