[R-SIG-Finance] questions about adaptive indicator, intra-day trading and package 'parallel'

domodo 1111938 at qq.com
Fri Sep 5 18:31:56 CEST 2014


hi,guys,I got some questions in these days,any help would be appreciated.

1. about coding adaptive technical indicator.
For example, adaptive sma for dynamic break out, the Pseudro codes are:

for ( bar in 1: nrow(data)){
    If( bar == 1 ) {
        lookBackDays = 20;
    }
    Else {
        lastVolatility = currentVolatility;
        currentVolatility = StdDev(Close,30);
        
        deltaVolatility = (currentVolatility -
lastVolatility)/currentVolatility;
        lookBackDays = (1 + deltaVolatility) * lookBackDays;
        lookBackDays = MinList(lookBackDays,60);
        lookBackDays = MaxList(lookBackDays,20)
    }

    adapSMA[bar] = SMA(Close, lookBackDays) ;
}

for not quite familiar with R coding, I got error when calculating variable
'adapsma', the code are below:

adapSMA <- function(x, SDlen, n, ceiling, floor, multi){
    #error treatments
    x <- try.xts(x, error = as.matrix)
    if (n < 1 || n > NROW(x)) 
        stop("Invalid 'n'")
    if (any(nNonNA <- n > colSums(!is.na(x)))) 
        stop("n > number of non-NA values in column(s) ",
paste(which(nNonNA), collapse = ", "))   
    
    #calculate dynamic parameter 
    currentV  <-  runSD(x, SDlen, sample=T)
    deltaV  <-  -Delt(currentV)
    deltav[is.na(deltav)] = 0
    
    series <- 1+deltav
    series[1] <- n
    lookback <- cumprod(series)-0.5  
    
    # limit the parameter in a specified range between (floor, celling)
    for( i in 1:NROW(lookback)){
        lookback[i] <- min(lookback[i], ceiling)
        lookback[i] <- max(lookback[i], floor)
    }
    
    #get int portion of the value
    lookback <- round(lookback)
    
    #calculate adaptive sma according dynamic parameter,but get error here
    adapsma  <-  runMean(x,lookBack)

    reclass(adapsma, x)
    return(adapsma)
}

2. intra-day trading strategy by specified time 
for example, an intra-day strategy is:
1) market opens from 9:00 to 15:30,the strategy's trading time is from 9:31
to 15:25 on 1-minute chart.

2) at 9:30, get the highest high and lowest low of the day (that's, the
highest high from 9:00 to 9:30, the lowest low from 9:00 to 9:30 of the
1-minute bar ), and store them in variable ‘todayHigh' and 'todayLow'

3) during the trading time, when last bar's close cross above todayHigh,
enter long on current bar,when last bar's close cross under todayLow,enter
shorton current bar

4) when last bar's close cross todayHigh/2+todayLow/2, exit long/short. 

5) if 2 trades on the same direction in total are in loss, stop trading this
direction until tomorrow.

the rules are not very complicated, my problem is, have no idea about how to
code rule 2) and 5) 

3. It's very time-consuming when back-testing strategy on long range of
intraday historical data (especially on 1-minute data), is it possible to
accelerate the back-testing by package 'parallel' or other method ? 

regards, and have a nice weekend.   ( ^ ^ ) /



--
View this message in context: http://r.789695.n4.nabble.com/questions-about-adaptive-indicator-intra-day-trading-and-package-parallel-tp4696562.html
Sent from the Rmetrics mailing list archive at Nabble.com.



More information about the R-SIG-Finance mailing list