Gas price seasonality

Last spring I read “Quantitative Trading” by Ernest P. Chan. In his book, he suggested to buy gas futures contract at the end of February and sell it later, in March. Today, I decided to test this strategy by using R-language.
The most important thing for such investigation is data. For this purpose, I used this public data: www.eia.doe.gov
I made 2 test – for the first one, I used futures contract, which will be settled after 4 months and for the second one, I used gas spot price.
In the first plot, we can see monthly price returns scaled by months (1-12). It is clear, that mean of March’s returns are above 0. What is encouraging in this plot is that the ranges of returns are above 0 as well (meaning, that majority of March’s returns was above 0).
Anova test gives p-value below 0.01 – some months in the group has different mean (that supports seasonality idea). March’s t-test gives these values: t = 2.8064, df = 15, p-value = 0.01329.

Photobucket

I used gas spot prices to generate second plot. It is similar to the first one, except that March’s returns has longer tail. It is worth to note, that September stands as a positive month as well. The statistics for this test are not so strong, as it was in the first example.
P-value of Anova test is only 0.11 and March’s t-test is:
t = 1.3791, df = 15, p-value = 0.1881

Photobucket

R-language code to run these tests:

#------gas contract 4 -----
library(xts)
library(ggplot2)
library(quantmod)
 
tmp<-as.matrix(read.table(file='gas.contract.4.csv',sep=';'))
gas<-as.xts(as.double(tmp[,2]),order.by=as.POSIXct(tmp[,1]))
plot(gas)
gas.monthly<-to.monthly(gas)
gas.monthly.delta<-Delt(Cl(gas.monthly))
gas.monthly.delta[1]<-0
 
gas.factor<-as.double(format(index(gas.monthly.delta),'%m'))
tmp<-data.frame(as.double(gas.monthly.delta),as.numeric(gas.factor))
qplot(factor(as.numeric(gas.factor)),as.double(gas.monthly.delta),data=tmp,geom = "boxplot",ylab='Returns',xlab='Months')
 
anova(lm(as.double(gas.monthly.delta)~gas.factor))
t.test(((gas.monthly.delta[which(format(index(gas.monthly.delta),'%m')=='03')]))[,1])
 
#----gas contract spot -------
tmp<-as.matrix(read.table(file='gas.csv',sep=';'))
gas<-as.xts(as.double(tmp[,2]),order.by=as.POSIXct(tmp[,1]))
 
plot(gas)
colnames(gas)<-c('Close')
 
gas.monthly<-to.monthly(gas)
gas.monthly.delta<-Delt(Cl(gas.monthly))
gas.monthly.delta[1]<-0
 
gas.factor<-as.double(format(index(gas.monthly.delta),'%m'))
tmp<-data.frame(as.double(gas.monthly.delta),as.numeric(gas.factor))
 
qplot(factor(as.numeric(gas.factor)),as.double(gas.monthly.delta),data=tmp,geom = "boxplot",ylab='Returns',xlab='Months')
 
anova(lm(as.double(gas.monthly.delta)~gas.factor))
t.test(((gas.monthly.delta[which(format(index(gas.monthly.delta),'%m')=='03')]))[,1])

4 Comments »

  1. Mike Caron said,

    April 15, 2010 @ 21:51

    First off, these examples are great! I am just learning to use R and so am having lots of problems with data typing.

    I tried to copy your gas example only using my own file and get the following error when typing plot(gas):
    > plot(gas)
    Error in periodicity(x) : can not calculate periodicity of 1 observation

    The steps I took up to this point are:

    library(xts)
    library(ggplot2)
    library(quantmod)

    tmp<-read.csv("c:\\data\\ascii\\CLCData\\GC.LNG.csv",header=T)
    dates<-strptime(tmp$X.DATE.,format="%Y%m%d")
    tmp<-cbind(tmp,dates)
    tmp<-as.matrix(tmp)
    gas names(tmp)
    [1] “X.TICKER.” “X.NAME.” “X.PER.” “X.DATE.” “X.TIME.”
    [6] “X.OPEN.” “X.HIGH.” “X.LOW.” “X.CLOSE.” “X.VOL.”
    [11] “X.OPENINT.”

    This line produced:
    > oil str(gas)
    An ‘xts’ object from 1975-01-02 to 2010-04-12 containing:
    Data: num [1:17709, 1] 998 997 997 996 1000 …
    Indexed by objects of class: [POSIXt,POSIXct] TZ: EST5EDT
    Original class: ‘double’
    xts Attributes:
    NULL
    >

    Any thoughts would be appreciated. Thanks!

  2. Mike Caron said,

    April 15, 2010 @ 21:54

    I seemed to omitted a couple of steps, sorry

    library(xts)
    library(ggplot2)
    library(quantmod)

    tmp<-read.csv("c:\\data\\ascii\\CLCData\\GC.LNG.csv",header=T)
    dates<-strptime(tmp$X.DATE.,format="%Y%m%d")
    tmp<-cbind(tmp,dates)
    tmp<-as.matrix(tmp)
    gas<-as.xts(as.double(tmp[,9]),order.by=as.POSIXct(tmp[,12]))
    plot(gas)

  3. kafka said,

    April 16, 2010 @ 9:11

    Did you manage to fix your problem?
    In case, if you didn’t – check what do you have in tmp object after each step. That way you will spot the problem.

    Error in periodicity(x) : can not calculate periodicity of 1 observation – this means, that object has only one value and an array is expected.

  4. Mike Caron said,

    April 17, 2010 @ 2:11

    I just found my problem. I had some blank data in the data set. By using tmp<-na.omit(tmp), I was able to omit that null data. I also had my string data being converted to a factor, so I had to type the string data so I ended up with gas<-as.xts(as.double(as.character(tmp[,9])),order.by=as.POSIXct(tmp[,12])) . That now allows me to plot.

RSS feed for comments on this post · TrackBack URI

Leave a Comment