[This article was first published on Milk Trader, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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.
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.
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(Lag(dailyReturn(Cl(TLT))*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(Lag(dailyReturn(Cl(TLT))*SIGNAL)) EQUITY <- cumprod(1+RETURNS) plot(equity, main="white bumblebee") plot(EQUITY, main="with equity curve filter")
To leave a comment for the author, please follow the link and comment on their blog: Milk Trader.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.