I think a fresh look this morning has solved the problem I was having. 

I needed to add the defined sub-range xts "myXts" as the mktdata argument in 
the applyStrategy call, as below:

out <- try(applyStrategy(strategy=strattemp, portfolios="temp", mktdata=myXts))



 


On 24 Jan 2012, at 16:59, Tim Meggs <[email protected]> wrote:

> Hi list,
>  
> I am running R on windows, via R studio with all the packages I require 
> currently up to date.
>  
> In an effort to create a softly adaptive trading system I am trying to code a 
> custom indicator that returns some statistics about how the strategy would 
> have performed if run over the previous preceding x number of days.   i.e. 
> the indicator performs a rolling window backtest across all the dates in the 
> provided xts (minus the first x days where the window is not big enough) and 
> returns some information on the performance of the strategy (currently set to 
> be the Sharpe Ratio). 
>  
> I am new to R, and I have taken the idea as far as I can, and how need to 
> turn to some of you R wizards out there for help.  The below code returns 
> just NaN’s for my Sharpe Ratios when I know that the range of SPY data I 
> provide should give some non-NaN backtest result across the 90 day window I 
> have specified. 
>  
> Any help would be very much appreciated.
>  
> Thank you in advance,
> Tim
> CODE BELOW:
>  
> ####################################################################################################################################
> # Code to create a custom indicator which, for each date (i) in a time 
> series, will return the Sharpe Ratio of a defined trading   #
> # strategy that has been run over a smaller subset of the data up to date 
> (i).  The idea is to eventually be able to use this      #
> # indicator itself as part of a quantstrat backtest i.e. the previous 
> performance of a strategy becomes an input to the strategy   #
> ####################################################################################################################################
>  
> #install required packages
> require(quantstrat)
> require(PerformanceAnalytics)
>  
> # Set initial date for the market data from which we will build our indicator 
> time series
> initDate <- "2008-01-30"
> endDate <- "2010-12-31"
>  
> # Pull Yahoo Finance data
> symbols <- c("SPY")
> getSymbols(symbols, from=initDate, to=endDate, 
> index.class=c("POSIXt","POSIXct"))
>  
>  
> # simple custom function identify when a series of close prices has performed 
> a certain number (n)
> # of up closes or down closes
> # returns 1 (TRUE) if the runSum number, n (+ve & -ve), is achieved in the 
> run window (abs(n)) days
> runCounter <- function(myXts,n) {
>   # myXts in an xts object with a Close column included
>   # calc 1-day rate of change and translate into 1 for +ve, -1 for -ve
>   temp <- ifelse(ROC(Cl(myXts),1,type="discrete") > 0,1,-1)
>   # if the run sum equals the desired level return 1, else 0
>   temp <- ifelse(runSum(temp,abs(n)) == n,1,0)
>   temp
> }
>  
>  
> # function to run the strategy backtest on a sub-set of the data and return 
> the Sharpe Ratio of the strategy up to that date
> # Sharpe Ratio to be used as indicator
> # days is the subset window within the overall backtest, within which to run 
> the strategy backtest
> # inn and outt are variables to be passed to runCounter
> # prices is the xts
> runBTstrat <- function(prices,days,inn,outt) {
>      
>       .runBT <- function(i) {
>        
>         #do not run the backtest if we are not far enough into the overall 
> xts data for the correct size backtest window to exist
>         if(i < days) {
>           my <- NaN
>         }
>         else {
>        
>           # select the beginning of the sub-backtest window
>           firstIndex <- (i - (days-1))
>        
>           # extract the subset from the xts upon which we are going to run 
> the strategy
>           myXts <- prices[firstIndex:i]
>          
>           # beginning & end dates of the subset
>           beginDate <- as.character.Date(index(first(myXts)))
>           finalDate <- as.character.Date(index(last(myXts)))
>        

        [[alternative HTML version deleted]]

_______________________________________________
[email protected] 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.

Reply via email to