Friday, September 25, 2015

Machine learning and commonly used trading indicators (part 2)

Portfolio trading comparison example - part 2

In my previous post, I produced two portfolio histories based on two different startegies. The first is based upon a machine learning algorithm, the second based on a commonly used trading indicator, the moving average. Being more specific, the machine learning model was based on a prediction of the next week close-to-open price ratio using its current and previous two week values. The other model was based on crossing the current weekly price with its 10 weeks moving average.

In this post, I show plots to compare time evolution of both portfolios gain by taking advantage of beautiful dygraph library features.

suppressPackageStartupMessages(library(dygraphs))
suppressPackageStartupMessages(library(xts))

portfolioTradingHistory <- read.csv("portfolioTradingHistory.csv", header=TRUE, stringsAsFactors = FALSE)
colnames(portfolioTradingHistory)[1] <- "day"
filt.col <- c("portfolio.Open", "portfolio.Close", "portfolio.Gain", "action", 
              "own.shares", "available.funds.Close")
portfolioTradingHistFilt <- subset(portfolioTradingHistory[,filt.col], action != "hold")

portfolioMaTradingHistory <- read.csv("portfolioMaTradingHistory.csv", header=TRUE, stringsAsFactors = FALSE)
colnames(portfolioMaTradingHistory)[1] <- "day"
portfolioMaTradingHistFilt <- subset(portfolioMaTradingHistory[,filt.col], action != "hold")

t <- as.POSIXct(portfolioTradingHistory$day, origin="1970-01-01", tz="US/Eastern") 
# machine learning portfolio trading history gain
ml <- xts(portfolioTradingHistory$portfolio.Gain, order.by=t)
# MA-10 portfolio trading history gain
ma10 <- xts(portfolioMaTradingHistory$portfolio.Gain, order.by=t)
p <- cbind(ml, ma10)
colnames(p) <- c("ml", "ma10")
#dygraph(p, main = "portfolios comparison") %>%
#  dySeries("ml", label = "M.L. portfolio", drawPoints = TRUE, pointSize = 3) %>%
#  dySeries("ma10", label = "MA-10 portfolio", drawPoints = TRUE, pointSize = 3) %>%
#  dyAxis("y", label = "portfolio gain percentage", valueRange = c(-20, 50))

alt

Such plot was produced by the above commented dygraph() call code which cannot be embedded straight in R markdown as the produced HTML page was exceeding this blog size limit. As a consequence, I could embed it only as png file, loosing the chance for iteractive plot mode as you get by running that commented code.

We can notice how the machine learning algorithm is performing worse of the MA-10 until July 18th, 2014. From that day, the machine learning trading strategy appears to outperform the MA-10 one until the end of the year 2014. At the beginning of 2015, machine learning strategy mislooked the market trend as issued a buy action which ended up with a loss affecting the advantage so far cumulated with respect to the other portfolio. At the end of the trading window, our two trading strategies ended with very similar portfolio gains.

Furhermore, I show boxplot and summaries to highlight variability aspects of portfolios time evolution. A qualitative evaluation highlight more risk propension for the machine learning algorithm with respect the MA-10 one.

boxplot(portfolioTradingHistory$portfolio.Gain, portfolioMaTradingHistory$portfolio.Gain, 
        col=c("lightgreen", "lightblue"))

summary(portfolioTradingHistory$portfolio.Gain)
##     Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
## -10.0900  -8.6000   0.4424   7.3490  18.4100  40.3500
summary(portfolioMaTradingHistory$portfolio.Gain)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -7.1080 -2.6490 -0.8194  2.0390  7.0970 29.3700

Following, a comparison of buy/sell transactions performed.

ml.transactions <- nrow(portfolioTradingHistFilt)/2
ma10.transactions <- nrow(portfolioMaTradingHistFilt)/2
cbind(ml.transactions, ma10.transactions)
##      ml.transactions ma10.transactions
## [1,]              12                 9

The number of transactions performed by trading strategies is another variable to inspect as tradingPortfolio() does not take into account the transaction costs.

As a final comment, I can say that there is more work to do for tuning the machine learning model in order to consistently beat the MA-10 trading indicator.