[R-SIG-Finance] Quantstrat using sigFormula

G See gsee000 at gmail.com
Wed Jan 4 23:05:09 CET 2012


Do you still have a problem if you use Date as the index class?

i.e. instead of:
getSymbols(symbols, from=initDate, to=endDate,
index.class=c("POSIXt","POSIXct"))

simply do this:
getSymbols(symbols, from=initDate, to=endDate)

HTH,
Garrett

On Wed, Jan 4, 2012 at 2:49 PM, Tim Meggs <twmeggs at gmail.com> wrote:
> Hi - I'm fairly new to R.  Have been reading up a lot and am impressed with
> the language's versatility and hope to spend much time getting to grips
> with it.  (I'd like to thank those that have built out the really useful
> Quantmod, Quantstrat, TTR, PerformanceAnalytics, Blotter packages for all
> their hard work meaning I, and those with similar interests, dont have to
> reinvent the wheel!)
>
> Having a problem with my understanding of how to use sigFornula, as my code
> has started to throw an error after I'd tried to implement it - any help or
> guidance would be greatly appreciated - thanks!:
>
> Getting the following error:
>
>> out <- try(applyStrategy(strategy=stratRSI4, portfolios="RSI4"))
>
> Error in .xts(e, .index(e1), .indexCLASS = indexClass(e1),
> .indexFORMAT = indexFormat(e1),  :
>
>  index length must match number of observations
>
>> updatePortf("RSI4")
>
> [1] "RSI4"
>
>
> when running the following adaption of one of the Quantstrat demos:
>
> # This code is an attempt to backtest Connor's RSI(4) filter for long trades
>
> #http://forums.stockfetcher.com/sfforums/?q=view&tid=93830&start=0
>
> #install.packages(c("quantstrat","blotter","FinancialInstrument"), repos="
> http://r-forge.r-project.org")
>
> require(quantstrat)
>
> require(PerformanceAnalytics)
>
>
>
> # Set initial values
>
> initDate <- "2006-07-31"
>
> endDate <- "2011-10-31"
>
> initEq <- 100000
>
>
>
>
>
> # Pull Yahoo Finance data
>
> symbols <- c("SPY")
>
> getSymbols(symbols, from=initDate, to=endDate,
> index.class=c("POSIXt","POSIXct"))
>
>
>
> # Set up instruments with FinancialInstruments package
>
> currency("USD")
>
> for(symbol in symbols) {
>
>  stock(symbol, currency="USD", multiplier=1)
>
> }
>
>
>
> # Delete portfolio, account, and order book if they already exist
>
> suppressWarnings(rm("account.RSI4","portfolio.RSI4",pos=.blotter))
>
> suppressWarnings(rm("order_book.RSI4",pos=.strategy))
>
>
>
>
>
> # Initialize portfolio and account
>
> initPortf("RSI4", symbols=symbols, initDate=initDate)
>
> initAcct("RSI4", portfolios="RSI4", initDate=initDate, initEq=initEq)
>
> initOrders(portfolio="RSI4", initDate=initDate)
>
>
>
> # Initialize a strategy object
>
> stratRSI4 <- strategy("RSI4")
>
>
>
>
>
> ####INDICATORS####
>
> # Add the 200-day SMA indicator
>
> stratRSI4 <- add.indicator(strategy=stratRSI4, name="SMA", arguments =
> list(x=quote(Cl(mktdata)), n=200), label="SMA200")
>
> # Add the RSI4 indicator
>
> stratRSI4 <- add.indicator(strategy=stratRSI4, name="RSI", arguments =
> list(price = quote(getPrice(mktdata)), n=4), label="RSI")
>
>
>
>
>
> ####SIGNALS####
>
> # There are two signals:
>
> # The first is when close price is above the 200 day moving average and
> RSI4 is below 25
>
> stratRSI4 <- add.signal(stratRSI4, name="sigFormula", arguments = list(data
> = columns=c("Close","SMA200","RSI"), formula = "(Close > SMA200) & (RSI <
> 25)", label="trigger"), cross=TRUE), label="Cl.gt.SMA")
>
> # The second is when the RSI4 closes above 55
>
> stratRSI4 <- add.signal(stratRSI4,
> name="sigThreshold",arguments=list(threshold=55, column="RSI",
> relationship="lt", cross=TRUE), label="Cl.lt.RSI")
>
>
>
>
>
> ####RULES####
>
> # There are two rules:
>
> # The first is to buy when the price is above the SMA and RSI4 below 25
> (the first signal)
>
> stratRSI4 <- add.rule(stratRSI4, name="ruleSignal",
> arguments=list(sigcol="Cl.gt.SMA", sigval=TRUE, orderqty=1000,
> ordertype="market", orderside="long", pricemethod="market", TxnFees=-5,
> osFUN=osMaxPos), type="enter", path.dep=TRUE)
>
> # The second is to sell when the RSI climbs above 55
>
> stratRSI4 <- add.rule(stratRSI4, name="ruleSignal",
> arguments=list(sigcol="Cl.lt.RSI", sigval=TRUE, orderqty="all",
> ordertype="market", orderside="long", pricemethod="market", TxnFees=-5),
> type="exit", path.dep=TRUE)
>
>
>
>
>
> # Set position limits so we don't add to the position every month Close >
> SMA10
>
> addPosLimit("RSI4", "SPY", timestamp=initDate, maxpos=1000, minpos=0)
>
>
>
> # Process the indicators and generate trades
>
> out <- try(applyStrategy(strategy=stratRSI4, portfolios="RSI4"))
>
> updatePortf("RSI4")
>
>
>
> # Evaluate results
>
> portRet <- PortfReturns("RSI4")
>
> portRet$Total <- rowSums(portRet, na.rm=TRUE)
>
> charts.PerformanceSummary(portRet$Total)
>
> tradeStats("RSI4")[,c("Symbol","Num.Trades","Net.Trading.PL<http://net.trading.pl/>
> ","maxDrawdown")]
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at 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.



More information about the R-SIG-Finance mailing list