Immanuel,

This isn't really the right board for this sort of issue. You'd want the
R Help board in future- this board is specifically for finance. To
answer your questions: there is a very powerful set of functions in R
that will recursively apply a function to dimensions of a
matrix/data.frame, vector, list, or multidimensional array. Look up the
uses of the functions "apply", "tapply", "sapply", and "lapply". They
are very useful if you want to avoid loops and they will greatly speed
up your code.

Also, remember that R is a vectorised language. As you gain experience
with R you'll realise that you can often use vectorised arguments to
perform operations very quickly. It's hard to explain without examples;
I recommend "S Programming" by Ripley & Venables.

The code "rm(list = ls())" is very obviously dangerous. It will purge
every single variable that you've declared in the current R instance. So
don't use it unless you know what you're doing or you really need to
purge all of your variables for some reason.

On Wed, 2010-09-29 at 17:25 +0200, Immanuel wrote:

> Hello,
> 
> I just read, that providing sample code
> with " rm(list=ls()) " is dangerous, please be careful with
> the sample I just posted.
> 
> best regards,
> Immanuel
> 
> On 09/29/2010 05:14 PM, Immanuel wrote:
> > Hey all,
> >
> > I'm struggling with my first lines of R code.
> > It would be great if someone could help me to get the code
> > below runing, or show me how to do this task more efficient.
> >
> > - can I avoid to use the loops?
> > - why does "Lo(GDAXI[i]) < tmp.lowest" not return a boolean?
> >
> > best regards
> > Immanuel
> >
> > ----------------------------------------------------------------------------- 
> >
> > # clear workspace
> > rm(list = ls())
> > # set working directory
> > setwd("~/Ue1")
> >
> > # load libraries
> > library(quantmod)
> > library(TTR)
> >
> > # load costum functions
> > source("A2_func.R")
> >
> > underlying = "GDAXI"
> >
> > getSymbols("^GDAXI")
> > # trendfilter: -1 down-trend, 1 up-trend
> > # nothing smart, just an xts object containing 1 & -1
> > # according to trendfilter
> > direction = dir.macd(GDAXI)
> >
> > period.lowest = xts(Inf, Sys.Date())
> > period.highest = xts(-Inf, Sys.Date())
> > tmp.lowest <- xts(Inf , Sys.Date()) # max value nehmen...
> > tmp.highest <- xts(-Inf, Sys.Date())
> > # now i want to find the max value (increasing) for every 
> > up-trend-periode (1,1,1)
> > # and min value for every down-trend-periode (decreasing) (-1,-1,-1)
> > for ( i in 1 : length(direction) ) {
> >
> >     if (direction[i] == -1)    {
> >         tmp.highest <- -Inf
> >         if ( Lo(GDAXI[i]) < tmp.lowest)
> >             period.lowest[i] <- tmp.lowest
> >     }
> >     if (direction[i] == 1)    {
> >         tmp.lowest <- Inf
> >         if (Hi(GDAXI[i] > tmp.highest)
> >             period.highest <- tmp.highest
> >     }
> >
> > }
> >
> > ------------------------------------------------------------------------------------------------ 
> >
> >
> > ---------------------------------------------------------------------------------------------- 
> >
> > dir.macd <- function(priceSeries) {
> >     library(quantmod)
> >     library(TTR)
> >     # MACD settings
> >     slow = 10
> >     fast = 25
> >     av = 5
> >
> >     # Calculate the MACD indicator
> >     macd <- MACD(priceSeries, fast,slow,av )
> >
> >     # Create the long (up) and short (dn) signals
> >     sigup <- ifelse(macd$macd < macd$signal, 1, 0)
> >     sigdn <- ifelse(macd$macd > macd$signal, -1, 0)
> >
> >     # Lag signals to align with days in market,
> >     # not days signals were generated
> >     #sigup <- Lag(sigup,1) # Use lag() to avoid Toby's error
> >     #sigdn <- Lag(sigdn,1) # Use lag() to avoid Toby's error
> >     sigup <- lag(sigup,1) # Note k=1 implies a move *forward*
> >     sigdn <- lag(sigdn,1) # Note k=1 implies a move *forward*
> >
> >     # Replace missing signals with no position
> >     # (generally just at beginning of series)
> >     sigup[is.na(sigup)] <- 0
> >     sigdn[is.na(sigdn)] <- 0
> >
> >     # Combine both signals into one vector
> >     sig <- sigup + sigdn
> >
> >     # Calculate SAR-direction
> >     for ( i in 2 : length(sig) ) {
> >         if (sig[i] == 0)
> >             sig[i]=sig[i-1]
> >     }
> >     sig
> > }
> > -------------------------------------------------------------------------------------------------------- 
> >
> 
> _______________________________________________
> R-SIG-Finance@stat.math.ethz.ch 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]]

