Monday, February 15, 2016

Backtesting - part 1

Hello, welcome back to my blog about automated trading. Last week I have covered the topic about real time quotes. This week I start talking about back testing. This topic is long enough so I will split it into 2 parts. Today there will be an introduction and next week I will present simple standalone backtest unit.

For the source code, please download repository and update to branch 6-backtest-1, changeset 29fc2509e9a9.

Background

Without doubt backtesting is important part for automated trading although there are many inaccuracies and biases (there are already plenty of articles, just search for backtest). I am not going into these details. This post is about the implementation of back test framework. And I would like to mentione the following issues I had to go through.
  1. How to cook real time quotes
  2. How to prevent future prediction
  3. Searching according finviz criteria
  4. Speed
  5. What about market impact 
For some of them I have solution. For some not. If you have any better one, please let me know. Now let's go through them.

First issue was how to cook real time quotes. Obviously they have to come somehow from the historical quotes as these are the only one available. I sorted this out simply by translating the average price of historical quote to the bid and ask of real time quote. Limitation is that the maximum amount of real time quotes is same as the number of historical quotes. I just live with that.

Second issue was how to prevent future prediction. This is solved by implementation of HistoricalProvider which acts like a proxy. Then the request date is simply repaired as the test progresses. Here let me mention one point about TrainingDataProvider. There is a method called today. Algo implementations have to use is to get current date. This makes date operations within the backtest possible.

Third issue is tricky and I don't have perfect solution yet. This issue appears when Algo implementation relies on the data from Finviz page. In the real daily cycle the implementation gets up to date date every day. But this page doesn't reflect the backtest, so data might be incorrect. Probably daily crawling and storing would solve it, but this is too early. For the moment I live with the wrong data, nothing is perfect.

Fourth issue is speed. According my experience most time is spend by querying the remote services. Solution to this is caching. Now using the cheap cloud there is a need to find balance between available memory and speed. Please refer to the coding part.

And finally fifth issue is market impact. Every transaction has some cost. One part of the cost is broker comission. Another part is market impact. And there might be more. To simulate this I introduced transaction cost function which is associated with all transactions. Please refer to the coding part.

And what about you? Do you know about other issues? Do you have better solutions? Please let me know in the comments.

Coding

This chapter is just preparaion. So here are the prepared units. Next time I will put them into the framework. There is an implementation of VariableContext called InMemoryVariableContext. This is used to suply variable persistence for the backtest lifetime. Then there are 2 implementations of HistoricalProvider called CachedHistoricalProvider and EndDateLimitedHistoricalProvider. These are used as a guarantee for the future prediction problem and as a one part of speed optimization. And finally there is object called TransactionCostFunction obviously used for simulating transaction cost.

That's it for today. Next week I will finish this chapter. Thank you for reading!

No comments:

Post a Comment