Actually, you probably don't want to name your data environment "data" since
that is the name of a utils function (sorry about that)

Maybe
.data <- new.env()
or
Data <- new.env()

Garrett

On Fri, Oct 28, 2011 at 9:42 AM, G See <gsee000@gmail.com> wrote:

> I might as well do some self-promoting here.
>
> There is a really simple function called `gsa` (stands for "Get, Subset,
> Assign") in the `qmao` package that may be useful for this purpose.  You can
> store your data in an environment, then pull out the subset you want to run
> a test on and put it in .GlobalEnv.  You can add your indicators to big data
> set and then your subsets will already have them.
>
> # First I'll demonstrate qmao:::gsa
>
> > install.packages("qmao", repos='http://r-forge.r-project.org')
> > library(qmao)
> > data <- new.env()
> > getSymbols("SPY", from='2000-01-01', env=data)
> <environment: 0x0000000002b2b2f8>
> > gsa("SPY", "2001-01-01::2002-12-31", data, .GlobalEnv)
> [1] "SPY"
> > rbind(first(SPY), last(SPY))
>            SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
> 2001-01-02   132.00   132.16  127.56    128.81    8737500       106.45
> 2002-12-31    87.99    88.43   87.11     88.23   34036600        74.76
> > gsa("SPY", "2003-01-01::2005-12-31", data, .GlobalEnv)
> [1] "SPY"
> > rbind(first(SPY), last(SPY))
>            SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
> 2003-01-02    88.85    91.30   88.54     91.07   44516300        77.17
> 2005-12-30   124.80   125.06  124.36    124.51   44645600       111.20
>
> #-----------------------------------
>
> You can also pre-calculate your moving average with something like this
> with(data, SPY$SMA200 <- SMA(Ad(SPY), 200))
>
> Then, skip the "add.indicator" step since your 'mktdata' will already have
> the indicator.
>
> For example, using the maCross.R demo that comes with quantstrat, I
> commented out the two "add.indicator" lines (lines 24-25), and replaced the
> part where it gets Symbols (lines 38-40) with this:
>
> data <- new.env()
> getSymbols(stock.str, adjust=TRUE, from='2000-01-01', env=data)
> with(data, AAPL$ma50 <- SMA(Cl(AAPL), 50))
> with(data, AAPL$ma200 <- SMA(Cl(AAPL), 200))
> gsa("AAPL", "2002-01-01::", data, .GlobalEnv)
>
>
> HTH,
> Garrett
>
>
> On Fri, Oct 28, 2011 at 8:53 AM, Andreas Voellenklee <wotuzu17@gmail.com>wrote:
>
>> Hello Brian,
>>
>> thanks for the reply. Sorry for the unsafe code to restore empty
>> environments. I had a hard time becoming familiar with R, and my code
>> might be beginner-style sometimes.
>>
>> Then there is no easy way of specifying a certain start-date when a
>> strategy starts being executed? If for example you use a 200 day
>> moving average in your trading rule there won't be any trades within
>> the first 200 days of market data. Well, I could download say a year
>> of older data, then cut the dataset at startdate minus 200
>> observations, then I have the moving average ready right at the
>> beginning. But this is not a very elegant method IMHO.
>>
>> I intend to test strategies within a certain period, for example from
>> 2000 to 2003. Then to take the same setup and test it in a different
>> time period, and so on. Am I the only one that likes to have all
>> indicators calculated at the beginning of a test period?
>>
>> Greetings from Austria,
>> Andreas
>>
>> _______________________________________________
>> R-SIG-Finance@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.
>>
>
>

	[[alternative HTML version deleted]]

