Quantcast

Quantstrat - Error while applying strategy

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Quantstrat - Error while applying strategy

varcovar
This post has NOT been accepted by the mailing list yet.
Hi guys,

With the purpose of testing a random entry/exit strategy in Quantstrat, I developed a basic random indicator. Here is the error I get in the console when I apply the strategy:

> out <-try(applyStrategy(strategy = stratMACROSS, portfolios=portfolio.st))
Error in if (is.null(names(tmp_val)) & ncol(tmp_val) == 1) names(tmp_val) <- indicator$label :
  argument is of length zero


I do not understand this error even if it seems fairly basic. :/ Attached is the full code along with my indicator function. I used the maCross.r template provided in the quantstrat package except for the part I have highlighted in the code. If any of you does have a solution to this error, please share!

R_code.txt

Thanks a lot for your help!

Markus Jr.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Quantstrat - Error while applying strategy

soren wilkening
Hi

I had a look. the main bug was that your 'randPos' function needs to return a vector instead of a scalar. See the code. Seems to run ok. no credit for elegance though :)

Soren

# =================================================================================================
# The code provided here is from the crossMA example in quantstrat except in the middle where said
# =================================================================================================
require(quantstrat)
try(rm("order_book.macross",pos=.strategy),silent=TRUE)
try(rm("account.macross","portfolio.macross",pos=.blotter),silent=TRUE)
try(rm("account.st","portfolio.st","stock.str","stratMACROSS","initDate","initEq",'start_t','end_t'), silent=TRUE)
stock.str='AAPL' # what are we trying it on
currency('USD')
stock(stock.str,currency='USD',multiplier=1)
initDate = '2007-12-31'
initEq = 3000
portfolio.st='macross'
account.st='macross'
initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
initAcct(account.st,portfolios=portfolio.st, initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)


# =================================================================================================
# Here is my own part of the code
# randPos is a function that provides an output -1, 0 or 1 for short, neutral or long respectively.
# Up signal if random indicator = 1
# Down signal if random indicator = -1
# Enter rules: long or short if respectively up or down signal
# Exit rules: exit long or exit short if respectively down or up signal
# =================================================================================================

# need to redefine this. indicators have to be vector-valued functions
nDays <- 20
probDn <- 1/2
probUp <- 1/2
# Random rebalancing
randPos <- function(x, nDays, probDn, probUp) {
.randPos <- function(i) {
    # random rebalancing (no rebalancing = 0, rebalancing = 1)
    rebal <- ifelse(runif(1) < 1/nDays, 1, 0)
    # random position (short = -1, long = 1, neutral = 0)
    a <- runif(1)
    if (a < probDn) {
        pos <- -1
    } else {
        pos <- ifelse(a > (1 - probUp), 1, 0)
    }
    # no rebalancing or neutral = 0, long = 1, short = -1
    return(rebal*pos)
}

return( xts( sapply(1:nrow(x), .randPos), order.by=index(x)) )
}


stratMACROSS<- strategy(portfolio.st)
stratMACROSS <- add.indicator(strategy = stratMACROSS, name = "randPos",
    arguments = list(x = quote(mktdata), nDays = nDays, probDn = probDn, probUp = probUp), label = "randInd")  #need to use quote(mktdata)

stratMACROSS <- add.signal(strategy = stratMACROSS, name = "sigThreshold",
    arguments = list(label = "randInd", column = "randInd", threshold = -1, relationship = "eq"), label = "sigDn")  #need to omit data=mktdata
stratMACROSS <- add.signal(strategy = stratMACROSS, name = "sigThreshold",
    arguments = list(label = "randInd", column = "randInd", threshold = 1, relationship = "eq"), label = "sigUp")   #need to omit data=mktdata
     
stratMACROSS <- add.rule(strategy = stratMACROSS, name = "ruleSignal",
    arguments = list(sigcol = "sigDn", sigval = TRUE, orderqty = -100, ordertype = "market", orderside = "short", TxnFees = -5), type = "enter")
stratMACROSS <- add.rule(strategy = stratMACROSS, name = "ruleSignal",
    arguments = list(sigcol = "sigUp", sigval = TRUE, orderqty = 100, ordertype = "market", orderside = "short", TxnFees = -5), type = "exit")
   
stratMACROSS <- add.rule(strategy = stratMACROSS, name = "ruleSignal",
    arguments = list(sigcol = "sigUp", sigval = TRUE, orderqty = 100, ordertype = "market", orderside = "long", TxnFees = -5), type = "enter")
stratMACROSS <- add.rule(strategy = stratMACROSS, name = "ruleSignal",
    arguments = list(sigcol = "sigDn", sigval = TRUE, orderqty = -100, ordertype = "market", orderside = "long", TxnFees = -5), type = "exit")
# =================================================================================================
# End of my part
# =================================================================================================

getSymbols(stock.str,from=initDate)
for (i in stock.str) {
  assign(i, adjustOHLC(get(i),use.Adjusted = TRUE))
}

start_t <-Sys.time()
out <-try(applyStrategy(strategy = stratMACROSS, portfolios=portfolio.st))
end_t <-Sys.time()
print(end_t-start_t)

start_t <-Sys.time()
updatePortf(Portfolio='macross',Dates=paste('::',as.Date(Sys.time()),sep=''))
end_t <-Sys.time()
print("trade blotter portfolio update:")
print(end_t-start_t)

chart.Posn(Portfolio='macross',Symbol=stock.str)

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Quantstrat - Error while applying strategy

varcovar
This post has NOT been accepted by the mailing list yet.
It works perfectly now. Thanks a lot, Soren!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Quantstrat - Error while applying strategy

Tim Meggs
In reply to this post by soren wilkening
Hi Soren,
Your example below also helped me, as I have been trying return a vector from a custom indicator function I have built.
Could you explain the use of the variable "i" that is given as an argument in the internal fucntion .randPos you define below? What part of the function provides "i"?  Is this somehow linked to the internal control flow of the sapply you invoke later?
Thank you in advance.
Tim
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Quantstrat - Error while applying strategy

soren wilkening
Hi

what the randPos() function does is apply the .randPos() function
"nrow(x)" times so the result is a vector of the same length as x. Of
that result, only the last value is used in the strategy, i think. All
the results are the same. so "i" is just a counter that the 'sapply'
function needs. It does not do anything else.

Looking at it agin now, I am thinking that this is a particularly ugly
way of doing this. maybe just use "rep" instead of "sapply".

see ?rep and ?sapply
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Quantstrat - Error while applying strategy

bhavna
This post was updated on .
Hi,

I am facing similar issue and tried the workaround suggested by Soren, but still does not seem to work.

My data is in below format with datetime as index since I am looking at 15 min bars:
> nseData[1,]
                                     Open   High     Low    Close
2011-07-26 09:15:00 5704 5708.8 5689.05  5693
> class(nseData)
[1] "xts" "zoo"

Attached is the custom indicator and strategy I am trying to run.
When I run the applyStrategy command
out<-try(applyStrategy(strategy=strat,portfolios=portfolio.st,mktdata=nseData))

R takes lot of time to run and gives error as:
Error in if (length(j) == 0 || (length(j) == 1 && j == 0)) { :
  missing value where TRUE/FALSE needed


Not sure what is wrong. Even though I am passing an xts object to the T3 custom indicator I see errors like below when I do not use the .function(i) method
(ncol(tmp_val) == 1) { : argument is of length zero

TestStrat.txt

I tried another thing, when I pass the data with only date as index of xts object the strategy runs fine even without the vectorising to the custom indicator.

> head(cldata)
                      open  high   low    close    vol       openint   unadclose
2009-12-01 89.43 91.12 89.09 90.45 570613 1209796     78.37
2009-12-02 89.98 90.67 88.30 88.68 593896 1203036     76.60
2009-12-03 88.59 89.58 87.62 88.54 650285 1216682     76.46
2009-12-04 87.91 89.98 86.93 87.55 695232 1225598     75.47
2009-12-07 87.88 88.18 85.78 86.01 708825 1263830     73.93
2009-12-08 86.08 86.47 84.59 84.70 891018 1217908     72.62

So the question is how do I pass the 15 min bar data as xts to my custom indicator and apply the strategy?

> head(nseData)
                                       Open     High        Low        Close
2011-07-26 09:15:00 5704.00 5708.80 5689.05 5693.0
2011-07-26 09:30:00 5693.00 5698.25 5686.00 5687.5
2011-07-26 09:45:00 5687.30 5689.00 5676.00 5687.0
2011-07-26 10:00:00 5687.00 5689.50 5683.00 5686.0
2011-07-26 10:15:00 5687.00 5687.75 5678.00 5685.2
2011-07-26 10:30:00 5685.05 5691.95 5680.90 5688.0

Please advice.

Thanks,
Bhavna
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Quantstrat - Error while applying strategy

braverock
>From glancing at your code, it seems that your first major problem is
that your indicator function is not vectorized, and should be.  

Indicators, as described in the documentation, should use vectorized
operations whenever possible.

You are calling your .T3 function once for every row of your data
(ouch), rather than taking advantage of vectorized operations to act on
every row of the market data at once.  Your sapply loop will be
expensive in both time and memory, and appears completely unnecessary.  

You can and should test your indicator function on your market data
outside of quantstrat before trying to include it in a strategy.

Given how your function is constructed, it's also unclear whether you
even need to call it twice, or whether you would also be better off just
giving it both your fast.ma and the slow.ma at the same time, and only
going over the data once.

In short, R is 'taking a lot of time to run' because your indicator
function needs to be rewritten to be efficient.

Regards,

   - Brian

On Fri, 2012-05-04 at 21:49 -0700, bhavna wrote:

> Hi,
>
> I am facing similar issue and tried the workaround suggested by Soren, but
> still does not seem to work.
>
> My data is in below format with datetime as index since I am looking at 15
> min bars:
> > nseData[1,]
>                                      Open   High     Low    Close
> 2011-07-26 09:15:00 5704 5708.8 5689.05  5693
> > class(nseData)
> [1] "xts" "zoo"
>
> Attached is the custom indicator and strategy I am trying to run.
> When I run the applyStrategy command
> out<-try(applyStrategy(strategy=strat,portfolios=portfolio.st,mktdata=nseData))
>
> R takes lot of time to run. Not sure what is wrong. Even though I am passing
> an xts object to the T3 custom indicator I see errors like below when I do
> not use the .function(i) method
> (ncol(tmp_val) == 1) { : argument is of length zero
>
> http://r.789695.n4.nabble.com/file/n4610537/TestStrat.txt TestStrat.txt
>
> Please advice.
>
> Thanks,
> Bhavna
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Quantstrat-Error-while-applying-strategy-tp3472438p4610537.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> [hidden email] 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.

--
Brian G. Peterson
http://braverock.com/brian/
Ph: 773-459-4973
IM: bgpbraverock

_______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Quantstrat - Error while applying strategy

soren wilkening
In reply to this post by bhavna
Dear Bhavna

I had quick look at your code. If you define your T3 function like
this (as Brian has already suggested), the whole thing runs fine (and
fast), as long as you also reduce your 'nseData' to a single column,
i.e. the Close. If you need all OHLC columns then you need to have a
closer look at what their names are and if you use those names
directly in another indicator/signal in your strategy. I remember there
was a tiny bug in quantstrat where the output of indicator or signal
functions would not get renamed as indicated in the 'label' argument.
But then again, maybe this has already been fixed.

nseData <- Cl(nseData)


`T3` <-
function(x, n=3) {

  # T3 indicator

  a <- 0.7
  alpha <- 2/(n+1)
  mavg1  <- EMA(x, n)
  mavg2  <- EMA( mavg1, n )
  mavg3  <- EMA( mavg2, n )
  mavg4  <- EMA( mavg3, n )
  mavg5  <- EMA( mavg4, n )
  mavg6  <- EMA( mavg5, n )
 
  res <-
  -(a*a*a)*mavg6+(3*a*a+3*a*a*a)*mavg5-(6*a*a+3*a+3*a*a*a)*mavg4+(1+3*a+a*a*a+3*a*a)*mavg3

  return(res)
}




Cheers

------------------------------------------------------------------
Soren Wilkening

http://censix.com

_______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Quantstrat - Error while applying strategy

bhavna
Thanks Soren and Brian!
I converted the data to xts correctly and that did the trick, am now using the indicator as mentioned by Brian and it works fine.

Thanks,
Bhavna
Loading...