[R-SIG-Finance] how to avoid loops & using xts, quantmod

Immanuel mane.desk at googlemail.com
Wed Sep 29 17:25:16 CEST 2010


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
> }
> -------------------------------------------------------------------------------------------------------- 
>



More information about the R-SIG-Finance mailing list