Milktrader

Iterating Until Convergence

Friday, April 15, 2011

Recursive Trading System in R

I have a trick knee. Normally, it works just fine. But if I stand on my head when its raining on Tuesdays and Thursdays and pinch my nose, it hurts. Not just a little. It hurts a lot. I went to the doctor and he told me not to stand on my head when it's raining on Tuesdays and Thursdays and pinch my nose. I left the office a little dejected, realizing I have limitations. When I got back to the lab, I confided my story to White Bumblebee. It didn't say anything, but I could sense some empathy. As if it were telling me that sometimes it has bad days too.

Algorithms sometimes need to just sit still, kinda like you and I. They're always sending signals, so it's not an easy task. One way to settle down a wild algorithm is to program in a sit-down-and-relax statement. The equivalent of a "quote" -- go flat instruction.

You can determine when to go flat based on current algorithm performance. Look at the equity curve as a stock and determine if it's in an uptrend or downtrend. If it's on a good run, take whatever signal is generated. Otherwise, ignore the signal.

I tried this out on several different equities and the results are mixed. Sometimes it helps, other times not so much.  The following chart is for IWM. Notice how it had less severe drawdowns with the filter and realized better returns.

EDITOR NOTE: These charts reflect the results of code that had an error in them. This error was brought to my attention by Rahul Savani in the comments section. I have changed the code, but not the following charts. 
Now a chart for the same system on TLT, a less-volatile ETF. Makes you wonder if standing on your head on when it's raining on Tuesdays and Thursdays and pinching your nose is really all that bad after all.




Here is the R code in about 20 lines. (code edited May 25, 2011 to remedy error caught by reader Rahul Savani)

require("quantmod")   
                                                           
getSymbols("TLT")   
                                       
TLT$fast  <- BBands(( TLT[,4]), n=10, sd=0.5)                                            
TLT$slow  <- BBands(( TLT[,4] ), n=30, sd=0.5)                   
TLT       <- na.omit(TLT)                                                                

signal    <- ifelse (TLT$mavg > TLT$up.1, 1,                                          
              ifelse(TLT$mavg < TLT$dn.1, -1, NA))                                                                                                           
signal    <- na.locf(signal, na.rm=TRUE)                                                
                                                                                 
returns   <- na.omit(dailyReturn(Cl(TLT))*Lag(signal))                               
                                                                                 
equity    <- cumprod(1+returns)                                                       
                                                                                 
equity_10 <- SMA(equity)                              
equity_20 <- SMA(equity, n=30)                        
equity_TA <- na.omit(merge(equity_10, equity_20))     

recursion <- na.omit(merge(signal, equity_TA))

SIGNAL    <- ifelse(recursion$equity_10 > recursion$equity_20, recursion$mavg, 0)
SIGNAL    <- na.omit(SIGNAL)

RETURNS   <- na.omit(dailyReturn(Cl(TLT))*Lag(SIGNAL))    
                                                         
EQUITY    <- cumprod(1+RETURNS)       

plot(equity, main="white bumblebee")
plot(EQUITY, main="with equity curve filter")

15 comments:

  1. Love all the work you are doing. Question for you, do you know if you can get options data in R?

    ReplyDelete
  2. I haven't found any yet. So far, I've queried CBOT to see if they might have something, but nothing yet. For my purposes, I see the current option delta as a Bayesian prior for probability and plan on pursuing that angle.

    ReplyDelete
  3. I have found some code that scrapes data from Yahoo but it hasnt been updated in awhile, so not too sure how well it works or if it does at all. I am trying to avoid actually paying for the data

    http://www.math.tu-berlin.de/~mkeller/index.php?target=rcode

    ReplyDelete
  4. Apparently in the quantmod package you can use the following command

    getOptionChain

    however it doesnt seem to work for options that dont have the same number of strikes

    ReplyDelete
  5. @Patrick, I'm messing around with QuantLib now. It's not a trivial install as it requires first that you install QuantLib (which requires boost) and then install.packages(c("Rcpp", "RQuantLib")) It took me about 2 hours in the middle of the night, and then something went wrong. Not sure what, but R wouldn't load at all. I did this and that and got R working again. And I got RQuantLib working, at least as far as I can see.

    This may be the correct solution. Get option price data from getOptionChain() and then use the RQuantLib functions to calculate the greeks. One advantage to doing it this way is that you know what equation your option pricing model is based on, since you need to call the function.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Just to warn you, it appears to be a little picky about certain tickers that only have one strike for some reason. Example below

    > getOptionChain("ACUR",Exp="2011-05")
    Error in calls[, 2] : incorrect number of dimensions

    > getOptionChain("ACUR")
    Error in calls[, 2] : incorrect number of dimensions

    http://finance.yahoo.com/q/op?s=ACUR+Options

    ReplyDelete
  8. @Patrick I'll put that issue on my list of things to bring up in Chicago this next weekend for R/Finance. Is it something you could fix, as in are you a programmer?

    ReplyDelete
  9. I have mucked around with it, but it does not appear to be an easy work around for the quantmod pkg.

    I have been trying a different route: webscraping it with RCurl. However, I am a novice at HTML and it's been rather frustrating. I posted my problems on Stack Overflow

    http://stackoverflow.com/questions/5774370/how-to-use-r-rcurl-xml-packages-to-scrape-options-data-from-yahoo

    ReplyDelete
  10. Should your trading results not be returns*Lag(signal) rather than Lag(returns * signal)?

    ReplyDelete
  11. @Rahul a very good catch good sir! I've changed the code to reflect this gross defect but haven't altered the blog post except for a note of caution.

    ReplyDelete
  12. @Milk Trader: thanks, nice blog.

    ReplyDelete
  13. Do you have much experience scraping data and storing it in R then passing it thru to a database? Still working on my options data scraper, but need a smart way of storing the data from day-to-day.

    ReplyDelete
  14. @Patrick still haven't made significant progress on storing data into SQL databases, but near-term I'm working on a Rails project that should shed some light on this mysterious technology.

    ReplyDelete
  15. You are doing amazing work! Patrik you have very good skills! Improve it and make more deeper!

    ReplyDelete