[R-SIG-Finance] Fwd: blotter, quantstrat: initDate without effect?

Brian G. Peterson brian at braverock.com
Fri Oct 28 14:36:10 CEST 2011


On Fri, 2011-10-28 at 07:12 +0200, Andreas Voellenklee wrote:
> Hi,
> I spend the last couple of days trying out the functionality of the
> TradeAnalytics packages. There are some observations that I would like
> to share. I use RStudio as GUI and derived the attached code from a
> demo example.
> 
> 1) since the trading system operates with moving averages, I download
> data prior the start of applying the strategy to have all moving
> averages calculated at the start time. But although the Portfolio,
> Account and Orders get initialized at the beginning of 2002, the
> execution of the strategy seems to start from the beginning of 2001

>From your code:

retrieveDate<- '2001-01-01'  # retrieve older data to allow calculation
of MA prior to start of trading
initDate    <- '2002-01-01'  # date to start trading strategy

>From the documentation:

initDate: A date prior to the first close price given, used to
          contain initial account equity and initial position

set initDate to the beginning of your data, or at least before you
expect any transactions to be entered.  Otherwise, bad things may
happen.

Of course the 'execution' of the strategy starts from the beginning of
your market data.  This is exactly the documented and intended
behavior.  

> 2) the getSymbols command returns a warning I don't understand:
> 
> Warning message:
> In if (as.character(sc[[1]]) != calling.fun) return() :
>  the condition has length > 1 and only the first element will be used:

I don't get this warning on my machine when using getSymbols, so you'd
need to do more digging to find out where/why it is happening.  It is
also just a warning, and likely harmless if the data is actually being
returned.

Also, I'd like to comment on these lines:

# rebuild empty environment if RStudio's "Clear All"" has been used
.instrument <- new.env()
.blotter <- new.env()
.strategy <- new.env()

PLEASE do not do dangerous things like removing everything in an
environment.  I think 'Clear All' is a dumb/dangerous 'feature' of
RStudio (which I otherwise quite like), just as "rm -rf /*" is dumb, or
"rm(list = ls(all.names=TRUE))" is dumb.  Please don't send example code
to the list that could potentially wipe out the work space of someone
who is considering helping you.

You could just as easily do this:

# rebuild empty environment if RStudio's "Clear All"" has been used
if(!exists('.instrument') .instrument <- new.env()
if(!exists('.blotter') .blotter <- new.env()
if(!exists('.strategy') .strategy <- new.env()

and have a safer alternative.

Regards,

   - Brian

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



More information about the R-SIG-Finance mailing list