Python Progress

A quick update on my Python coding progress.

I am proud to say that I am actually writing some code that is actually doing stuff in Python. Boy starting something new is time consuming, doing the most mundane tasks that one could do in Excel in a flash takes absolute hours to do in a new language.

I also constantly feel why I am doing this when I know how to code many of these functions in R.

After a couple of weeks of learning I have now accumulated some knowledge which has been further enhanced with the arrival of a few more Python textbooks last week. In summary I am fast falling in love with the Pandas data library in Python which is like learning a whole new language on its own. Pandas has one of the most comprehensive data manipulation functions a data scientist can dream of. I love it, and I am sure in the weeks and months to come if I stay with the learning I will be doing really cool things with the data I come across.

So far I haven’t come across a library like “Performance Analytics” in R, once I come into contact with this type of library in Python then I will feel complete. I believe there is a way to pass R functionality via a wrapper into a Python project. This may be something I will research later today, however my first prize is to stay completely Python native, so anyone with insights into a Performance Analytic type library in Python please let me know.

Drop and Gain Clustering

I decided to build on John Hussman’s clustering of large moves research. As demonstrated in a previous post we showed how large drops -3% seemed to cluster. This time I superimposed the gains to see if there was a similar pattern the gain behaviour and as you can clearly see there is. My takeaway is volatility begets volatility, but where is the start and the finish? (subject for another time)

title: "Drop and Gain Clustering"
author: "Michael Berman"
date: "Thursday, October 30, 2014"
output: html_document
#get the data of S&P500
getSymbols('SPY', from='1990-01-01')
#lets look at it from 1990 to 2015
spy <- SPY['1990/2015']
#our baseline, unfiltered results
ret <- ROC(Cl(spy)) 
#our comparision, filtered result
filter.d <- Lag(ifelse(ret < -0.02, 1, 0))
drops<- rollapply(filter.d==1,100,sum)
filter.g <- Lag(ifelse(ret > 0.02, 1, 0))
gain<- rollapply(filter.g==1,100,sum)
#two versions of plots - A
plot(gain, main = "Drop and Gain Clustering", sub = "sum of 2% movements over 100 prior days")
plot(drops, main = "Drop and Gain Clustering", labels = FALSE, col = "red")

# plots - B
plot(drops, main = "Drop and Gain Clustering", sub = "sum of 2% movements over 100 prior days", ylab ="drops")
plot(gain, main = "Drop and Gain Clustering", labels = FALSE, col = "red")
axis(side =4)
mtext("gains", side = 4)

Created by Pretty R at

I am actually not sure of which one is a better way to look at it.

Golden Cross Trading System in R code

I wanted to try and post my first piece of R code on the site. I am having a few problems with the HTML of the code so here goes a first try. I am rookie coder with R and any other language for that matter. What this code does is goes and downloads more than 60 years of S&P 500 daily data, it then observes when the gold cross takes place, i.e. 50 day moving average crosses the 200 day moving average. In this instance there is a long signal. When the 50 day cross the 200 day to the downside it is a signal to close out the long trade. The system does not enter short trades and is exceptional relative to the traditional buy and hold strategy.

    #get the data and fill out the MA
    getSymbols(‘SPY’, from=’1950-01-01′)
    SPY$ma200 <- SMA(Cl(SPY), 200)
    SPY$ma50 <- SMA(Cl(SPY), 50)
    #lets look at it from 2000 to 2015
    spy <- SPY['1950/2015']
    #our baseline, unfiltered results
    ret <- ROC(Cl(spy))
    #our comparision, filtered result
    ma_sig <- Lag(ifelse(SPY$ma50 > SPY$ma200, 1, 0))
    ma_ret <- ROC(Cl(spy)) * ma_sig
    golden<- cbind(ma_ret,ret)
    colnames(golden) = c(‘GoldCross’,’Buy&Hold’)
    #Plot to visually see the actual moving averages
    type = “line,
    name = “Moving Average : Golden Cross”,
    TA= c(addSMA(50, col = ‘yellow’), addSMA(200)))
    table.AnnualizedReturns(golden, Rf= 0.02/252)
    charts.PerformanceSummary(golden, Rf = 0.02, main=”Golden Cross”,geometric=FALSE)

    Created by Pretty R at