Tim,

Try this syntax:

###
stratRSI4 <-
add.signal(stratRSI4,
    name="sigFormula",
    arguments = list(columns=c("Close","SMA200","RSI"),
                     formula = "(SPY.Close > SMA200) & (RSI < 25)",
                     label="trigger",
                     cross=TRUE),
    label="Cl.gt.SMA")

###
At least, that will not throw an errror.  But, I've never used
sigFormula, so hopefully someone that has will chime in if it's wrong.

HTH,
Garrett

On Mon, Jan 9, 2012 at 8:46 AM, Tim Meggs <[email protected]> wrote:
> Hi Garrat. Thanks for the tip. Unfortunately it still throws the same error.
>
> Hi Julien,
> Thank you for the pointer to the posting guide. I will endeavour to follow it 
> as best I can.
>
> I'm running Windows XP, R version 2.14.0 which I access trough the current 
> version of R Studio. All my packages are up to date.
>
> In terms of examples, the exact code segment I am running starts at "# This 
> code is an attempt to..." and ends at the call to the tradeStats() function. 
> If you copy out that block from the below you should be able to replicate the 
> same error.
>
> I can get the code to run fine before I introduce the addition of the signal 
> that uses sigFormula. As there are no direct examples of how to use 
> sigFormula, I am someway guessing the syntax.
>
> Given I'm sure this is where the problem lies, perhaps someone could provide 
> a full example of how to build a custom indicator using the sigFormula 
> functionally available in Quantstrat?
>
> Thanks
>
>
>
> Sent from my iPhone
>
> On 4 Jan 2012, at 22:05, G See <[email protected]> wrote:
>
>> 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 <[email protected]> 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]]
>>>
>>> _______________________________________________
>>> [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.
>

_______________________________________________
[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