[R-SIG-Finance] quantstrat example stragey error
Brian G. Peterson
brian at braverock.com
Wed Mar 2 20:56:14 CET 2011
Fixed in R-Forge svn r565. Thanks for the report.
Thanks also to Josh and Jeff for helping track it down.
Regards,
- Brian
On 03/02/2011 01:26 PM, Immanuel wrote:
> Hello,
>
> since I'm talking about the demo examples that come with quantstrat
> saw no point in attaching them. I assumed that the error comes
> from one of the quantstrat internal functions thank's for looking into it.
>
> regards,
> Immanuel
>
> here the error & code
>
> ----------
> Loading required package: quantstrat
> Loading required package: xts
> Loading required package: zoo
> Loading required package: TTR
> Loading required package: blotter
> Loading required package: quantmod
> Loading required package: Defaults
> Loading required package: FinancialInstrument
> [1] "2007-03-15 AAPL 100 @ 89.57"
> Error in if (getPosQty(Portfolio = portfolio, Symbol = symbol, Date =
> timestamp) == :
> argument is of length zero
> In addition: There were 15 warnings (use warnings() to see them)
> Time difference of 0.2658639 secs
> [1] "trade blotter portfolio update:"
> Time difference of 0.2728000 secs
> Warning message:
> In max(i) : no non-missing arguments to max; returning -Inf
> > sessionInfo()
> R version 2.12.0 (2010-10-15)
> Platform: i486-pc-linux-gnu (32-bit)
>
> locale:
> [1] LC_CTYPE=en_US.utf8 LC_NUMERIC=C
> [3] LC_TIME=en_US.utf8 LC_COLLATE=en_US.utf8
> [5] LC_MONETARY=en_US.utf8 LC_MESSAGES=en_US.utf8
> [7] LC_PAPER=en_US.utf8 LC_NAME=en_US.utf8
> [9] LC_ADDRESS=en_US.utf8 LC_TELEPHONE=en_US.utf8
> [11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=en_US.utf8
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
> other attached packages:
> [1] quantstrat_0.4.0 blotter_0.8 FinancialInstrument_0.4
> [4] quantmod_0.3-14 Defaults_1.1-1 TTR_0.20-2
> [7] xts_0.8-0 zoo_1.6-3 rj_0.5.0-5
>
> loaded via a namespace (and not attached):
> [1] grid_2.12.0 lattice_0.19-13 rJava_0.8-7 tools_2.12.0
> ---------------------------------------
>
> # Simple MACD strategy
> #
> # MACD may be used in many ways, this will demonstrate a trend indicator.
> #
> # traditionally, when the MACD signal crosses zero, this indicated a
> establishment of a positive trend
> #
> # we'll buy on positive treshold crossover of the 'signal' column, and
> sell on negative threshold crossover
> #
> # Author: brian
> ###############################################################################
>
>
>
> require(quantstrat)
> try(rm("order_book.macd",pos=.strategy),silent=TRUE)
> try(rm("account.macd","portfolio.macd",pos=.blotter),silent=TRUE)
> try(rm("account.st","portfolio.st","stock.str","stratMACD","initDate","initEq",'start_t','end_t'),silent=TRUE)
>
>
> stock.str='AAPL' # what are we trying it on
>
> #MA parameters for MACD
> fastMA = 12
> slowMA = 26
> signalMA = 9
> maType="EMA"
>
> currency('USD')
> stock(stock.str,currency='USD',multiplier=1)
>
> #or use fake data
> #stock.str='sample_matrix' # what are we trying it on
> #data(sample_matrix) # data included in package xts
> #sample_matrix<-as.xts(sample_matrix)
>
>
> initDate='2006-12-31'
> initEq=1000000
> portfolio.st='macd'
> account.st='macd'
>
> initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
> initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
> initOrders(portfolio=portfolio.st,initDate=initDate)
>
>
> stratMACD <- strategy(portfolio.st)
>
> stratMACD <- add.indicator(strategy = stratMACD, name = "MACD",
> arguments = list(x=quote(Cl(mktdata))) )
>
> stratMACD <- add.signal(strategy =
> stratMACD,name="sigThreshold",arguments =
> list(column="signal",relationship="gt",threshold=0,cross=TRUE),label="signal.gt.zero")
>
> stratMACD <- add.signal(strategy =
> stratMACD,name="sigThreshold",arguments =
> list(column="signal",relationship="lt",threshold=0,cross=TRUE),label="signal.lt.zero")
>
>
> stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments
> = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=100,
> ordertype='market', orderside='long', threshold=NULL),type='enter')
> stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments
> = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=-100,
> ordertype='stoplimit', orderside='long',
> threshold=.85,tmult=TRUE),type='risk')
> # alternately, use a trailing order
> # stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal',
> arguments = list(sigcol="signal.gt.zero",sigval=TRUE, orderqty=-100,
> ordertype='stoptrailing', orderside='long',
> threshold=.9,tmult=TRUE),type='risk')
> stratMACD <- add.rule(strategy = stratMACD,name='ruleSignal', arguments
> = list(sigcol="signal.lt.zero",sigval=TRUE, orderqty='all',
> ordertype='market', orderside='long', threshold=NULL),type='exit')
>
> getSymbols(stock.str,from=initDate)
> start_t<-Sys.time()
> out<-try(applyStrategy(strategy=stratMACD ,
> portfolios=portfolio.st,parameters=list(nFast=fastMA, nSlow=slowMA,
> nSig=signalMA,maType=maType)))
> end_t<-Sys.time()
> print(end_t-start_t)
>
> start_t<-Sys.time()
> updatePortf(Portfolio=portfolio.st,Dates=paste('::',as.Date(Sys.time()),sep=''))
>
> end_t<-Sys.time()
> print("trade blotter portfolio update:")
> print(end_t-start_t)
>
> chart.Posn(Portfolio=portfolio.st,Symbol=stock.str)
> plot(add_MACD(fast=fastMA, slow=slowMA, signal=signalMA,maType="EMA"))
>
> #look at the order book
> getOrderBook('macd')
>
> ###############################################################################
>
> # R (http://r-project.org/) Quantitative Strategy Model Framework
> #
> # Copyright (c) 2009-2010
> # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and
> Joshua Ulrich
> #
> # This library is distributed under the terms of the GNU Public License
> (GPL)
> # for full details see the file COPYING
> #
> # $Id: macd.R 450 2010-11-13 18:30:40Z braverock $
> #
> ##############################################################################
>
> -----------
>
>
> On 03/02/2011 07:17 PM, Joshua Ulrich wrote:
>> Have you tried converting the indexes of your xts objects to POSIXct?
>> You haven't provided any code, but my guess is that one of xts objects
>> you're using as input has a Date-classed index.
>> --
>> Joshua Ulrich | FOSS Trading: www.fosstrading.com
>>
>>
>>
>> On Tue, Mar 1, 2011 at 6:52 AM, Immanuel<mane.desk at googlemail.com> wrote:
>>> Hello,
>>>
>>> what are the implications of this error, is there a workaround?
>>> I'm just starting with quantstrat and really like the idea to implement
>>> strategies not directly with blotter anymore.
>>>
>>> regards,
>>> Immanuel
>>>
>>> On 02/26/2011 09:48 PM, Jeffrey Ryan wrote:
>>>> Yes, that isn't an xts issue - just happens to be taking place during a
>>>> subset.
>>>>
>>>> The issue is somewhere else
>>>>
>>>> 3: In `[.xts`(PosData, index(PosData)< Date) :
>>>> Incompatible methods ("Ops.POSIXt", "Ops.Date") for "<"
>>>> Time difference of 0.387223 secs
>>>>
>>>> The lhs [index(PosData)] is POSIX and the rhs is Date, which R can't
>>>> compare
>>>> correctly. The warning is happening - and probably resulting in all
>>>> TRUE
>>>> values - which isn't likely the desired outcome.
>>>>
>>>> I'm not looking at the code at the moment, but you can rule out xts.
>>>> There
>>>> are likely better ways to do this with xts, and I'll see what I can
>>>> do to
>>>> make the quantstrat code use more of the built-in xts functionality.
>>>>
>>>> Jeff
>>>>
>>>>
>>>> On Sat, Feb 26, 2011 at 12:34 PM, Immanuel<mane.desk at googlemail.com>
>>>> wrote:
>>>>
>>>>> hello,
>>>>> seems to be already installed:
>>>>> ----------
>>>>> [1] "2001-06-26 AAPL 100 @ 11.88"
>>>>> Error in if (getPosQty(Portfolio = portfolio, Symbol = symbol, Date =
>>>>> timestamp) == :
>>>>> argument is of length zero
>>>>> In addition: Warning messages:
>>>>> 1: In match.names(columns, colnames(data)) :
>>>>> all columns not located in ma50 ma200 for AAPL.Open AAPL.High AAPL.Low
>>>>> AAPL.Close AAPL.Volume AAPL.Adjusted ma50 ma200 ma50.gt.ma200
>>>>> 2: In max(i) : no non-missing arguments to max; returning -Inf
>>>>> 3: In `[.xts`(PosData, index(PosData)< Date) :
>>>>> Incompatible methods ("Ops.POSIXt", "Ops.Date") for "<"
>>>>> Time difference of 0.387223 secs
>>>>> [1] "trade blotter portfolio update:"
>>>>> Time difference of 0.7028229 secs
>>>>> Warning message:
>>>>> In max(i) : no non-missing arguments to max; returning -Inf
>>>>>> sessionInfo()
>>>>> R version 2.12.1 (2010-12-16)
>>>>> Platform: i486-pc-linux-gnu (32-bit)
>>>>>
>>>>> locale:
>>>>> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
>>>>> [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
>>>>> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
>>>>> [7] LC_PAPER=en_US.UTF-8 LC_NAME=en_US.UTF-8
>>>>> [9] LC_ADDRESS=en_US.UTF-8 LC_TELEPHONE=en_US.UTF-8
>>>>> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=en_US.UTF-8
>>>>>
>>>>> attached base packages:
>>>>> [1] stats graphics grDevices utils datasets methods base
>>>>>
>>>>> other attached packages:
>>>>> [1] quantstrat_0.4.0 blotter_0.8 FinancialInstrument_0.4
>>>>> [4] quantmod_0.3-15 Defaults_1.1-1 TTR_0.20-2
>>>>> [7] xts_0.8-0 zoo_1.6-4 rj_0.5.0-5
>>>>>
>>>>> loaded via a namespace (and not attached):
>>>>> [1] grid_2.12.1 lattice_0.19-17 rJava_0.8-7 tools_2.12.1
>>>>> ------------------
>>>>>
>>>>> On 02/26/2011 06:07 PM, Brian G. Peterson wrote:
>>>>>> On 02/26/2011 11:03 AM, Immanuel wrote:
>>>>>>> I just run the examples from the quantstrat package and
>>>>>>> received following error for every example:
>>>>>>> ----------
>>>>>> <...>
>>>>>>
>>>>>>> 2: In `[.xts`(PosData, index(PosData)< Date) :
>>>>>>> Incompatible methods ("Ops.POSIXt", "Ops.Date") for "<"
>>>>>>> any ideas?
>>>>>> Try upgrading xts to the latest version (0.8) from R-Forge or CRAN
>>>>>>
More information about the R-SIG-Finance
mailing list