Take the 2-minute tour ×
Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. It's 100% free, no registration required.

I was thinking about writing my own backtester and I realize I have to make some assumptions. So I was hoping I could post what I am planning on doing and hopefully some of you can give me some ideas on how to make it better (I'm sure there is a lot that can be improved).

First of all, my strategy involves holding stocks for usually some days, I am not doing (probably any) intra-day trading.

So here is what I was thinking. First, I would buy some minute OHLC stock quotes covering the stocks I am interested in (thinking about buying some from pitrading.com, is their quality acceptable?). Then if the algorithm triggers a buy or sell at some bar, I would "execute" the order using the high or low of the very next bar (attempting to be as pessimistic as possible here). One thing I am curious about is bid/ask, so I was thinking about maybe adding/subtracting a few cents to take this into account when buying/selling. I would just see what these values have been recently (difference between bid/ask and quote for some recent data on these stocks and then just use these numbers as I wouldn't be backtesting that far back). I would assume that I can buy/sell all I want then at that price.

Lastly I would include the cost of commission in the trade. I would neglect any effect my trade would have on the market. Is there any rough guideline using volume to estimate how much you would have to buy/sell to have an effect?

I would also simulate stop-loss sell orders and they, too, would be executed at the next bar low after the price passed the threshold.

That's it, it will be pretty simple to implement. I am just hoping to make it conservative so it can give me some insight into how well my program works.

Any thoughts or criticisms about this program would be greatly appreciated. I am new at this and I am sure there are some details I am missing.

share|improve this question
    
Here you mean that you want to backtest your own, one single strategy right ? –  Vytautas Mar 29 '11 at 7:51
    
Yep. Just out of curiosity what issues would their be if this were not true? –  user667 Mar 29 '11 at 15:26
    
What happens if the signal is triggered during the last bar of the day? Do you assume you buy at the close or would you buy at the next (day) open? –  Terco Mar 30 '11 at 9:15
    
choose your technology carefully I'd say, and make sure to create different modules for the different steps of the backtesting process (data importing, strategy simulation, performance analysis), –  SRKX Apr 12 '11 at 7:24
    
have you decided what programming language you want to use? –  Milktrader Apr 12 '11 at 12:19
add comment

7 Answers

Having developed many custom backtesting programs in the past, I wish I would have just started out by purchasing a decent commercial backtester for a few hundred dollars. The cost of buying one already completed will save you hours of time on learning the nuances and problems that come with backtesting implementation. Once you are adept at the ins and outs and limitations of the software, you can move on to more flexible programming. At the very least, you have a production implementation (with thousands of professional man-hours of effort) to compare your own custom software results and debug issues that surface. The cost of buying an actual professional product to learn the ins and outs is trivial in the long run.

share|improve this answer
add comment

I recommend to check tradestation.com Their EasyLanguage allows to build and backtest much more advanced strategies. They also have pre-build indicators and data for many years. It is not cheap, about $100 a month but most of the time they have a special offers for first 3 or 6 month free and monthly fee is dropped if you reach a certain level of trading. Here is a brief video: http://www.tradestation.com/education/university/school-of-easylanguage/video-tutorials

share|improve this answer
add comment

It depends on what do you need. Basically, an efficient backtester allows you to study all the market microstructure. In other words, you have to simulate a scenario that gives you all the information of bid-ask prices, volumes, traded price, volume traded, etc.

Two years ago I developed my own market in Python. I used a Poisson process to simulate trades. I also analyse the supply and demand to define the aggressiveness of the volumes and the size of the spread.

share|improve this answer
add comment

Stop loss simulation with strictly OHLC is not possible since you need to assume the ordering of the high and low (which one came first)? You can make an assumption when there is a straight line thru the price (open at low, close at high for example), however, in general you will have days where it is not clear what low/high came first.

You also need to consider the logic for market vs. limit orders. If you use market orders you need some slippage assumptions. If you use limit orders there is a probability your order will not be filled.

Also, using OHLC may lead you to design a strategy that trades on the Open. However, the open price is extremely volatile and you may face steep slippage and inability to achieve the quoted open price.

share|improve this answer
    
For the stop loss issue, you should assume the worst case scenario. Ideally you should not work at a resolution where the stop loss would be so close that it would be a common occurrence that the order would matter. –  BlueTrin Jan 9 at 13:57
add comment

There are many details you need to take into account in order to do a proper backtest. Besides the correction regarding bar entry/exit prices mentioned earlier, you will also need to correct for the bid/ask spread, which may be wider than the high/low for some stocks at some times. Some other details are:

  • Splits
  • Dividends
  • Ticker changes
  • Warrants/rights issuances
  • Merger/acquisition activity

Be careful to include stocks that have been delisted in your backtest, and generally only use criteria that would have been known at the time to construct your sample (see here).

As for volume limits, a good proxy is to never be trading more than 5% of the typical volume that takes place in a given period. For example, if you intend to buy $1MM of some stock over, say, a half hour period, be sure that at least 20MM is typically traded during that half hour of the day. From your question, though, it sounds like you are thinking of a strategy that is executed minute-to-minute, so you are probably better off just looking at the order book and how many shares are typically available to trade. You can somewhat get a sense for that by looking at minute volume bars, and assume you will not be able to trade more than the 10th percentile of the distribution.

share|improve this answer
add comment

You can also enter/exit at the opening of the next bar after the trading signal is generated by the backtester. In this case hourly bars may be more appropriate for day trading.

share|improve this answer
add comment

For "maximum pessimism" you should calculate thus:

  • for longs - enter at the bar high and exit at the bar low on bar following signal bar

  • for shorts - enter at the bar low and exit at the bar high on bar following signal bar

I had previously heard this approach to back testing called the "torture test."

share|improve this answer
    
Oops this is what I meant to say, thanks for pointing this out. –  user667 Mar 29 '11 at 15:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.