The Average Investor's Blog

A software developer view on the markets

Archive for April, 2011

The Weekly Update

Posted by The Average Investor on Apr 30, 2011

All is good in the world markets. Last week most indexes surpassed their recent heights thus confirming the upwards trend.

Asset Symbol Position Date In Gain
US REIT VNQ Long 2010-07-23 23.81%
S&P 500 ^GSPC Long 2010-09-30 19.49%
Emerging Markets EEM Long 2011-03-25 5.62%
Nasdaq 100 ^NDX Long 2011-03-25 3.79%

One can’t help but notice the advance of anything in dollar terms. Most currencies, most socks, most commodities, etc. Right or wrong, I do see a trend – all currencies are losing their value (buying power for real stuff), the worst among the bad being the US dollar. So we have replaced deflation in nominal terms with inflation. Right now we see the inflation only in non-Fed watched goods, but I bet, it’s coming to the US (it is already roaring around the world).

Make no mistake, in real terms there is no difference – the large middle class who has mostly nothing (but debt) is again being financed by the people who did nothing wrong and lived within their means. So much about the great society we have build.

In real terms however the story is quite different. Ignoring the details I mentioned above, we have avoided the Great Depression, we have broken the neck of deflation which devastated the world in the 30s, we are back to business as usual. Big political success!

Only time will tell, but I promise to keep the political and fundamental rants on this blog to a minimum – this blog is all about technical trading and investing. Nothing else. I guess it’s the election bug that got me …

Posted in Uncategorized | Leave a Comment »

The Weekly Update

Posted by The Average Investor on Apr 27, 2011

It was a good week for the markets, significant gains across the board. Let’s see what the next Fed meeting (coming this week) brings.

Asset Symbol Position Date In Gain
US REIT VNQ Long 2010-07-23 20.12%
S&P 500 ^GSPC Long 2010-09-30 17.09%
Emerging Markets EEM Long 2011-03-25 6.00%
Nasdaq 100 ^NDX Long 2011-03-25 2.63%

Posted in Market Timing, Trades | Leave a Comment »

ARMA Models for Trading, Part II

Posted by The Average Investor on Apr 21, 2011

All posts in this series were combined into a single, extended tutorial and posted on my new blog.

We left the last post at the point of determining the best ARMA model. Before continuing the discussion, however, I would like to make a few points that might seem a bit questionable or unclear:

  • We model the daily returns instead of the prices. There are multiples reasons: this way financial series usually become stationary, we need some way to “normalize” a series, etc
  • We use the diff and log function to compute the daily returns instead of percentages. Not only this is a standard practice in statistics, but it also provides a damn good approximation

Now back to choosing the best fitting ARMA model. A well known statistic to measure the goodness of fit test is AIC (for Akaike Information Criteria). Once the fitting is done, the value of the aic statistics is accessible via:

xxArma = armaFit( xx ~ arma( 5, 1 ), data=xx )
xxArma@fit$aic

There are other statistics of course, which for instance penalize models with mode parameters to avoid over-parametrization, however, typically the results are quite similar.

To summarize, all we need is a loop to go through all parameter combinations we deem reasonable, for instance from 0 to 5, inclusive, both for the AR (the first component) and the MA (the second component), for each parameter pair fit the model, and finally pick the model with the lowest AIC or some other statistic. The full code for findBestArma is at the end of the post.

In the code below, note that sometimes armaFit fails to find a fit and returns an error, thus quitting the loop immediately. findBestArma handles this problem by using the tryCatch function to catch any error or warning and return a logical value (FALSE) instead of interrupting everything and exiting with an error. Thus we can distinguish an erroneous and normal function return just by checking the type of the result. A bit messy probably, but it works.

findBestArma = function( xx, minOrder=c(0,0), maxOrder=c(5,5), trace=FALSE )
{
   bestAic = 1e9 
   len = NROW( xx )
   for( p in minOrder[1]:maxOrder[1] ) for( q in minOrder[2]:maxOrder[2] )
   {   
      if( p == 0 && q == 0 ) 
      {   
         next
      }   

      formula = as.formula( paste( sep="", "xx ~ arma(", p, ",", q, ")" ) ) 

      fit = tryCatch( armaFit( formula, data=xx ),
                      error=function( err ) FALSE,
                      warning=function( warn ) FALSE )
      if( !is.logical( fit ) ) 
      {   
         fitAic = fit@fit$aic
         if( fitAic < bestAic )
         {   
            bestAic = fitAic
            bestFit = fit 
            bestModel = c( p, q ) 
         }   

         if( trace )
         {   
            ss = paste( sep="", "(", p, ",", q, "): AIC = ", fitAic )
            print( ss )
         }   
      }   
      else
      {   
         if( trace )
         {   
            ss = paste( sep="", "(", p, ",", q, "): None" )
            print( ss )
         }   
      }   
   }   

   if( bestAic < 1e9 )
   {   
      return( list( aic=bestAic, fit=bestFit, model=bestModel ) ) 
   }   

   return( FALSE )
}

Posted in R | Tagged: , , , | 3 Comments »

The Weekly Update

Posted by The Average Investor on Apr 18, 2011

Another uneventful week for the monitored indexes. The quarterly reporting started but the bulk of it is coming out this week. Still the chances of some big (in terms of market moves of course) surprises either way are quite slim.

Asset Symbol Position Date In Gain
S&P 500 ^GSPC Long 2010-09-30 15.64%
US REIT VNQ Long 2010-07-23 15.22%
Emerging Markets EEM Long 2011-03-25 5.70%
Nasdaq 100 ^NDX Long 2011-03-25 -0.38%

Let’s wrap up the todays post with a list of liquid instruments, ETFs, that can be used to trade the above indexes. Please don’t trade any of these unless you know what you are doing!

Asset Symbol Leverage Price Volume
S&P 500 SPY 132.17 171,144,000
S&P 500 SSO 2x 52.84 13,459,300
S&P 500 SDS -2x 21.09 26,469,900
Nasdaq 100 QQQ 56.65 65,613,600
Nasdaq 100 QLD 2x 87.48 4,201,440
Nasdaq 100 QID -2x 52.36 5,114,740
Nasdaq 100 TQQQ 3x 81.43 619,195
Nasdaq 100 SQQQ -3x 26.45 966,995
US REIT DRN 3x 68.00 503,060
US REIT DRV -3x 13.83 580,632
Emerging Markets EEV -2x 28.69 732,210

Posted in Market Timing, Trades | Leave a Comment »

ARMA Models for Trading, Part I

Posted by The Average Investor on Apr 14, 2011

All posts in this series were combined into a single, extended tutorial and posted on my new blog.

Lately I have been testing trading models based on methods from various fields: statistics, machine learning, wavelet analysis and others. And I have been doing all that in R! In this series, I will try to share some of these efforts starting with the well-known from statistics Autoregressive Moving Average Model (ARMA). There is a lot of written about these models, however, I strongly recommend Introductory Time Series with R, which I find is a perfect combination between light theoretical background and practical implementations in R.

In R, I am using the fArma package, which is a nice wrapper with extended functionality around the arima function from the stats package (used in the book). Here is a simple session of fitting an ARMA model to the S&P 500 daily returns:

library(quantmod)
library(fArma)

# Get S&P 500
getSymbols( "^GSPC", from="2000-01-01" )

# Compute the daily returns
GSPC.rets = diff(log(Cl(GSPC)))

# Use only the last two years of returns
GSPC.tail = as.ts( tail( GSPC.rets, 500 ) )

# Fit the model
GSPC.arma = armaFit( formula=~arma(2,2), data=GSPC.tail )

The first obstacle is to select the model parameters. In the case of ARMA, there are two parameters. In other words, there is an infinite number of choices: (0,1), (1,0), (1,1), (2,1), etc. How do we know what parameters to use?

A naive approach would be to back-test strategies with all different combinations over a period of time and pick the best. This is something I have dubbed “hyper system” (ie a system of systems) and can be applied to any combination of indicators and comparative function.

Fortunately there are more robust statistical methods to do that. More on that in the next post

Posted in R | Tagged: , , , | 6 Comments »

The (Delayed) Weekly Update

Posted by The Average Investor on Apr 13, 2011

There were no changes in the positions over the last two weeks so I decided to not interrupt my vacation to update the site. Here is the state of the index portfolio:

Asset Symbol Position Date In Gain
S&P 500 ^GSPC Long 2010-09-30 15.16%
US REIT VNQ Long 2010-07-23 15.10%
Nasdaq 100 ^NDX Long 2011-03-25 -0.78%
Emerging Markets EEM Long 2011-03-25 2.49%

Posted in Market Timing, Trades | Leave a Comment »

 
%d bloggers like this: