Linear regression fit of stock prices

Discussion in 'Trading' started by mililani, Jun 8, 2009.

  1. mililani


    Hey folks,

    I'm somewhat new to technical analysis, but is there something in the standard bag of tricks that shows you if the stock price in a given time frame fits a line? Or, if it is trending upwards or downwards (the slope of the linear fitted line) ? And how closely the prices fit that trend (R squared) ? Basically, is there something that does a standard linear regression fit of stock prices in a certain time period, and show you how deviated the prices are around that line?

  2. Most standard charting packages let you draw a linear regression line through a groups of price points. If they support this they'll also let you place outer parallel lines at some distance, usually some factor of 1 standard deviation.

    If you want actual mathematical functions that will return slope/intercept data on LR lines, well that's more complicated. There are packages that support this but you have to be a programmer to some degree.
  3. While it is easy to perform a regression on a chart, your "typical" TA does not approach the problem with much statistical rigor. This is one reason you won't find such a function in too many TA off the shelf packages.

    You might want to look into "time series" and econometrics a bit, and the notion of fitting a model to a series.

    As you start to look into common TA, I think you'll find it is more of a subjective art (it is up to you to determine if that is useful; good luck).
  4. Excel can do these things for you.
  5. You can use excel to do linear regression for a series of prices. It will give you the R squared and the whole nine. If you are strickly looking for the trends you can use simple moving averages or EMAs to determine the overall trend. I have tried both at one time or another and did not find either overly usefully in generating a decent forcast.
  6. mililani


    Thanks for the response, guys. No, I need a systematic way of doing it vs. Excel. I've had this idea for quite some time, but I need to backtest the strategy to see how well it performs. At the very least, it requires me to do a linear regression fit on stock prices for a specified time period. This regression fit is done on stocks that meet a screening criteria. So, I need software that allows you to screen for certain stocks during the backtest, and then whenever a stock meets that criteria, the simulation runs a linear regression fit on the stock for the past several months and calculates the R squared value to determine how much prices deviate from the trend.

    I realize that some TA requires basic charting and bullshit looking diagrams. I require a much more rigorous approach. I can program. If anything, I just need a database of stock information for the past 20-30 years. I can create my own program to run simulations on the data. Does anyone know of any stock database that I can buy at a reasonable price, or might be free online?
  7. Most of the linear reg packages in TA softwares are standard ordinary least square regressions. Technically they are not correct since the standard error estimated will be off because you are fitting a standard OLS on time-series data which suffer from severe autocorrelation problems. You are much better off writing your own procedure in Matlab or some other stat programming language if you are looking for robust backtestable estimates.
  8. Occam


    Yahoo has data going back many years (how many, I can't remember) on many stocks; maybe Google Finance, too.
  9. I use SAS and it is very good for this type of analysis, but I don't think it is cheap and it is not super quick to learn. I hear R can do the same stuff and is free but I have not used it.
  10. NinjaTrader has the linear regression function in its technical analysis toolbox.

    BTW, it's surprisingly easy to use, you just move the mouse around, and it automatically draws the best fit line through the selected chart segment.

    The Ninja is a FREE tool for simulated trading and backtesting (you only have to pay if you want to use it as a platform for actual trading), a very good deal, I think.

    Why pay for something if you can get it for free.

    The only thing you may have to pay for is a quote provider, such as DTN IQ, but that's a separate service. I use DTN IQ for backtesting, because I need to run some stuff on one-minute bars.

    But you can use yahoo finance for free, if you are ok with using only closing prices.

    #10     Jun 9, 2009