Thursday, August 11, 2011

Crude gold

Back in July 2008 some hedge funds decided to sell oil short and buy gold.

The strategy proved successful.

It was based on an assumption that oil and gold are closely correlated and 1 ounce of gold is equivalent to 10 barrels of oil.

Such relation held true till the beginning of 2008, when first the oil overshoot gold, and later felt down below the previous parity.



In July 2008, one ounce of gold was enough to buy just between 6 and 7 barrels of oil. In January 2009, more than 20.

Later, gold and oil reach a new equilibrium. The new gold-to-oil ratio stood at 13.7 (1100:80) barrels per ounce and 15.8 (1420:90) at the end of 2009 and 2010 respectively.

Most recently the prices diverged significantly again. As for August 10th, one ounce of gold can buy 22 barrels of oil (1796:82).



Gold is most probably entering the final phase of the bubble. It is hard to predict how far it can go during its ultimate exponential ascent. Meanwhile oil is falling in anticipation of economic slow down.

Clearly both assets are on different dynamic trajectories and the gap can extend even further. However, this divergence will most probably end. And the bigger it is, the stronger reversal can occur.

One leg of the strategy is easily implementable using SPDR Gold Trust ETF which very closely tracks gold futures.



For crude oil you can chose either United States Oil Fund ETF or United States 12 Month Oil Fund ETF. For crude oil futures term structure, it is better to chose the later.



Alternatively one can use futures.

Let's see what will happen...



Wednesday, August 10, 2011

Shorting Apple against the market

In June 2010 I suggested a pair trading experiment: Apple short vs Google long. It wouldn't have given positive results.

As for today, the short position on Apple would return -31%, while going long Google would give +25%. Hence the net would be -6%.

I'm going back to this idea, since Apple's market capitalization has exceeded Exxon's just yesterday:

Zero Hedge, 2011-08-09

I think I've made a crucial mistake in my assumptions of the above mentioned experiment. I thought Google would continue bringing innovative solutions that would instigate interest in the company from both users and investors. Unfortunately, except for the launch of Google+ (which outlook is still uncertain, taking into account its defensive nature against Facebook), Google did not bring any significant products to the market in the analyzed period.

Yes, the Google's recent financial results were pretty impressive. But Apple delivered both strong financial results AND hype-prone products. The launch of iPhone 5 is expected for September and this should support the Apple stock price. Especially relatively to the market.

Apple has significantly outperformed the market over the year:

Source: stooq.com

Meanwhile, Google's performance was mixed:

Source: stooq.com

I'm not sure whether Google+ will become success Google would like it to be. I do not know whether Google has any more aces in his sleeve to show over the next 12-24 months. However, there is a high probability that Apple's has overrun the market or is very close to it.

Hence I'd like to propose another experiment: shorting Apple against the market, i.e. Nasdaq futures. Probably the best moment would be on the day of the release of iPhone 5.

But let's check the strategy performance before that day:

Monday, August 8, 2011

S&P500 down 6,66%

S&P500 index has fallen 6,66% today.

It's 10,99% down from the beginning of the year.

As a result we have a nice waterfall formation on the chart:



Friday, August 5, 2011

Downloading market data from Stooq to R

Stooq provides multiple market data for free in the CSV format. I often use them in my analyses in R.

However there was a small problem with automatically downloading market data from Stooq.

The CSV files are generated dynamically, so there is no static URL available. The name of the instrument for downloading is passed in cookie.

Therefore, I've written a code that circumvents this limitation:


# http://www.rhinocerus.net/forum/lang-php/662887-how-read-download-attachment-uri.html - inspiration :)


library(RCurl)


getStooqData <- function(asset_code,static_cookie=TRUE) {

data_tmp <- tempfile() # "data.csv"
cookie_tmp <- "cookie.txt"

u1 <- paste("http://stooq.com/q/d/?s=",asset_code,sep="")
u2 <- paste("http://stooq.com/q/d/l/?s=",asset_code,"&i=d",sep="")

if (!static_cookie) {



h <- c(paste("GET ",u1," HTTP/1.0",sep=""),
Accept="image/gif",Accept="image/x-xbitmap",Accept="image/jpeg",Accept="mage/pjpeg",Accept="application/x-shockwave-flash",Accept="application/vnd.ms-excel",Accept="application/msword",Accept="*/*",
'Accept-Language'="pl, en-us;q=0.7",'User-Agent'="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)",'Proxy-Connection'="Keep-Alive")


u1Opts <- curlOptions(header=TRUE,httpheader=h,cookiejar=cookie_tmp)
curlPerform(url=u1,.opts=u1Opts,verbose=TRUE)

h <- c(paste("GET",u2,"HTTP/1.0"),
Accept="image/gif",Accept="image/x-xbitmap",Accept="image/jpeg",Accept="mage/pjpeg",Accept="application/x-shockwave-flash",Accept="application/vnd.ms-excel",Accept="application/msword",Accept="*/*",
'Accept-Language'="pl, en-us;q=0.7",'User-Agent'="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)",'Proxy-Connection'="Keep-Alive")

u2Opts <- curlOptions(header=TRUE,httpheader=h,cookiefile=cookie_tmp)

}

else {


h <- c(paste("GET",u2,"HTTP/1.0"),
Accept="image/gif",Accept="image/x-xbitmap",Accept="image/jpeg",Accept="mage/pjpeg",Accept="application/x-shockwave-flash",Accept="application/vnd.ms-excel",Accept="application/msword",Accept="*/*",
'Accept-Language'="pl, en-us;q=0.7",'User-Agent'="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)",'Proxy-Connection'="Keep-Alive",Cookie=paste("cookie_uu=p;cookie_user=%3F0001dllg000011500d1300%7C",asset_code,sep=""))

u2Opts <- curlOptions(header=TRUE,httpheader=h)

}


reader <- basicTextGatherer()

w <- getURLContent(url=u2,.opts=u2Opts)


write(w,file=data_tmp)

stooq_data <- read.csv(data_tmp)

stooq_data
}


stooq_data <- getStooqData("es.f",static_cookie=TRUE)


In most cases, the function should work with static_cookie set to TRUE. However, a possibility that the cookie will change exists, so I've added some additional code that actually downloads a cookie before downloading the data.

Tuesday, August 2, 2011

Some improvements with MD2 and S&P500

Seems, the MD2 trading strategy can SOMETIMES work with S&P500, too:

Fig. MD2 strategy (2 variants) for S&P500, 1000 sessions, no commission
blue line - market
black line - strategy
important assumption: perfect asset divisibility

The results as measured by returns are still unstable, but after some modifications, they improve :)

Results for the first variant:

market   =  1345.02   -6.143497 %
strategy =  2041.307   42.44396 %


Results for the second variant:

market   =  1345.02   -6.143497 %
strategy =  2301.505   60.60075 %

Still, both the lack of stability and returns are not satisfactory.

Monday, August 1, 2011

No bugs so far

So far I was unable to identify any serious bugs in the code of previously mentioned strategy.

The strategy seems to work (i.e is able to beat the market) for longer horizons (>500 sessions) for EURUSD and USDJPY.

It does not work for SP500 (UPDATE 2011-08-02: seems it sometimes does...)

Fig. MD2 strategy for USDJPY, 500 sessions
blue line - market
black line - strategy

What's even more interesting, it seems there is not short term relation between the signal used and daily fluctuations of the assets:

Fig. Relationship between MD2 signal and daily changes, EURUSD

Currently I'm working on improving the strategy. I'm also going to test it on high-frequency data.