Sunday, April 26, 2015

Share Trading System Development - Back Testing

This is the subsequent post about process of share trading system development. Similarly I will use  volume spike strategy which I outlined in my previous post as an example.

Implementing your strategy as code(custom formula) in Amibroker is not complicated but how could we verify that our strategy is actually working and profitable? The answer is you have to test your strategy against historical data. There are many providers which you can get KLSE end of day (EOD) historical data from. You should make sure the data is adjusted for bonus, split, consolidate and special large dividend.

The process of testing your strategy against historical data is called back testing. From the testing you could fine tune your code and parameters to improve the performance of the system. This is called optimization. However, over optimization will cause your system to only perform well on the data which you have tested against with. This is because the data is static and you optimize your code to fit the data. This issue is called curve fitting. Most probably in live trading, your system will not perform well as the result from your back testing. The solve this issue I will split my data to in sample data and out of sample data. The concept is simple, you do the back testing optimization against one set of data (in sample data) and then verify the performance of your system by testing against another set of data (out of sample data). You have done the right optimization if the performance of your system is consistent for in sample and out of sample data. The process of optimizing your system against in sample data and verify its result against out of sample data is also known as walk forward analysis. Amibroker support optimization and walk forward analysis out of the box.

Now I will use my strategy to explain about the back testing process above so that you could have better idea. Assuming you have implemented the volume spike strategy as I outlined in previous post and you have 20 years historical EOD data imported to Amibroker. Let us refer back to the setup of volume spike strategy, in the step to identify volume spike, we have parameter n as how many time the spike volume is greater than average volume. Assuming we set this parameter to 1.5 initially. Before back testing, we split our historical EOD as below:

  • In sample - EOD from 1/1/1995 to 31/12/2004
  • Out of sample - EOD from 1/1/2005 to 31/12/2014 

Note that this is just a simplified example to explain the concept, in real back testing, we could split  EOD to multiple sets of in sample and out of sample data.

Now, we can start testing our system against in sample data. Let us assume we only focus on annual return for now as performance indicator of your trading system (I will explain later about what aspects we should look at to determine the performance of a system in my next post, apparently, annual return should not be the only consideration). After back testing, we found that the annual return is 15% which we are not happy with. We should do another back test against out of sample data as a benchmark too, assuming that the annual return which we get is consistent which is also around 15%.

Now we will try to optimize this parameter by trying different value, let us say when we set this value to 5 and we notice that the back test result has been improved to 25% which meets our expectation. Then, we will need to verify our optimization result against out of sample data. For instance, the annual profit from back test against out of sample data is 16%. Obviously, the result is far behind our expectation, hence we haven't identified the optimum parameter. We should go back to optimized the parameter again and repeat the step of optimizing against in sample data and verifying against out of sample data. Assuming finally we have identified parameter n of value 4 which gives a us 26.5% return from in sample data and 25% return from out of sample data. From this result it appears that the annual return from out of sample date has been significantly improved from the benchmark. We could conclude that we have found a optimum value for parameter n which most likely will work well too in live trading.    

The following is the summary process of back testing


     


Wednesday, April 8, 2015

Share Trading System Development - Volume Spike Strategy

Develop Share Trading System

At the end of my first post about systematic trading, I have posted the result of one of my trading system. I will re-post and explain about how to examine the result later. Finally, I will start writing about the strategy, high level development process and other details of the trading system which my audiences long awaiting for. Although I will touch some of the technical terms and development processes but this post (and generally this blog) is intended for any experienced traders without any programming experience who are interested to know more about system trading. So don't worry if you are not interested in developing your own system or you are not programmer, I will try to be concise and less emphasize on technical in this post. I believe it should make sense for any experience traders. However, if you are really interested in developing your own system, I don't think the information I provide here is enough for you to start. Please feel free to leave a comment or contact me so that I could provide more details and help you to start.

I call this strategy volume spike strategy. We know that share market is supply and demand market, more demand than supply will drive up the price.

Volume is a good indicator about demand of a share. In this strategy I will focus on volume increased which indicates that demand is pouring in to a share and also the share is getting notice by investors. Usually, a sudden volume increased also indicates that institutional investors are buying.

Ok, let me go straight to the point and show you the setup of this strategy. The setup and rules below have been implemented as custom formula in Amibroker except placing the buy/sell orders.

The Setup

  • Select shares which are in uptrend
    • short term moving average of close price (close) is greater than long term moving average of close price
    • Example: 10 days moving average of close is greater than 200 days moving average of close 
  • Identify volume spike
    • volume is greater than n times of moving average of volume - n is a parameter
    • spike volume is greater than m - m is a parameter
    • price is up when volume spike
  • After pullback(consolidation), generate buy signal
    • the following are the conditions of pullback
      • close is lower than previous close
      • close shouldn't be lower than short term moving average of close

Entry Rules

  • After receiving buy signal, on next day, check the stock price at around 10 minutes before market close, buy at market price if price is up from previous close, but only up for less than 10% (turn green) 


Exit Rules

  • Generate sell signal based on the following conditions
    • 10% trailing stop from highest close since buying OR
    • 10% initial stop loss from high of the buy day OR
    • Close price drops below short term moving average
  • Sell on next day open after receiving sell signal

In next post, I will explain about one of the most interesting and challenging process in trading system development - how to back test the strategy.

Tuesday, April 7, 2015

The life of a system trader

Before posting about the strategy of the result of system in my previous post, I will write a short post today to share what I do everyday in term of following my system, so you can have a glance what do you expect if you do the same.

The following are my daily routine:

  • Download KLSE EOD data from provider and import to Amibroker few hours after trading session ended, it takes me 2 minutes.
  • Run my system to scan for buy/sell signal, it takes me 2 minutes.  
  • If I receive buy/sell signals I will buy/sell on the next day at market open/close (depends on the strategy of my system, I might need to buy only if the stock up - turn green), it probably takes me 5 minutes. 
Too easy, isn't it? It only takes me less than 10 minutes/day. I don't need to stick to my computer monitor for the whole day to monitor the movement of the shares which I bought. I don't need to analyze the potential shares which I am going to buy. I don't even need to decide when the shares I bought are ripe to take profit or cut lost. All decision are made by my system :).      


What I need to remind myself is - since I am following a system I should follow it strictly and shouldn't override the decision of a system. So regardless of market condition, the price movement of the shares or the system is losing money (it is called system drawdown, I will touch about this concept in my next posts), I will still stick to the buy/sell signal of the system.  

Sunday, April 5, 2015

Systematic Trading (or Mechanical Trading) - Let computer do the hard work

What is Systematic Trading?

Systematic trading or system trading is also known as quant (quantitative) trading, mechanical trading, algorithmic trading or even auto trading. It utilizes computer system to analyse share market and generate buy and sell signal of stocks automatically . More advanced systems even make buy and sell orders without human intervention if the market provide interfaces for the system to integrate with, which to my knowledge KLSE doesn't support this.  

Traders who are good in technical analysis and charting utilize share charting computer software to analyse stock market extensively and make the trading decision. This is called discretionary trading because the buy and sell decision is based on the strategy, experience and knowledge of the trader. System trading take the power of computer to the next level, the strategies of enter/exit to/from a trade are converted to computer algorithm (system) , all of the analysis work and trading decision are made by the system.     

What are the advantages of systematic trading?

All of us in share trading know that scanning through all of potential shares, analyse each of them and finally make the trading decision manually is a time consuming and complex job. So why not let the machine (computer) do the hard work for us, and let us focus on our daily job, family or just enjoy our free time?  

We can't deny machines make less mistake and are less emotional than human. Trading is a highly pressure activity because of its high risk, even a very experience trader will be affected by emotion and make wrong decision working under pressure . Therefore, if we have a reliable system, we will just follow its decision and we know that the decision is based on our proven strategy which implemented as a system and has been tested statistically. I will explain how to test a system in my next blog.        

Why we still need to develop our own system?

As I have explained above, a system generates buy/sell signal. You might have doubt why we still need a system because of the following reasons:
  • some of advanced charting software include stock scanner which allows us to scan for potential stock to buy/sell based on predefined criteria, some of the software even allow us to customized our own criteria
  • buy/sell signals are freely available or with a small charges by stock brokerage firms, newspaper, social networking site, share masters or gurus  
The following are my justifications based on the sequence of the points above:
  • A complete system is not equal to a scanner. It is true that both of them can generate buy/sell signal. But a system is very flexible and highly customized software program based on your own strategy. You develop the system yourself and you have access to the code so it is not constraint by any limitation. Most importantly, the system is back tested with historical data and proven profitable historically (I will explain more about this in my next post). We can argue that we can also apply some proposed or well known strategies for example golden cross, MACD crosss, multi-year high and etc from some books or gurus to the ready made scanner and hope to make good profit. But this kind of strategies are usually too general enough and it will not work on its own based on my testing.    
  • Experienced traders know that we shouldn't follow buy/sell call blindly from any source without our own analysis. Otherwise, we will be easily lured by syndicate which try to manipulate the market. Of cause I believe that there are some genuine stock brokerage firms, masters/gurus which provide good buy/sell calls to their members. But even with a very good strategy, it will lost its edge if too many people are buying and selling at the same time. Hence in my opinion we should only trust buy/sell signals from a trusted provider only if there are limited number of members/subscribers especially in a short or medium term trading. But I guess this is not really under our control?

How to develop our own system?

After reading till here, you might thought that develop a system is a highly complicated task and it needs specialized skill. But the good news is it is not as complex as it sounds, you don't need to be a seasoned programmer like me to create a system. I believe any experienced traders with good logical thinking and mathematics are able to create their own systems. A system could be extremely simple with few lines of code, even a quite complex system usually will not have more than few hundred lines of code.  

A trading system is simple because we do not develop from scratch, we are utilizing advanced charting software like MetaStock, TradeStation, Amibroker or etc. So you can imagine that creating a system is not really like writing a program but it is more to embed your formulas to the charting software. In this blog, I will focus on creating system using Amibroker.  

More to come...

In next posts, I will write about my experience of writing share trading systems and my systematic share trading journey in Kuala Lumpur Stock Exchange (KLSE or BURSA). I think most people like to see the end result instead of the process. I will end this post by showing you the result of one of my trading system which I tested from year 1991-2014 with initial capital of RM 100K. I will explain more about this system, please stay tuned for my next post :)   

1. Statistic

Initial capital 100000.00
Ending capital 31429349.55
Net Profit 31329349.55
Net Profit % 31329.35 %
Exposure % 55.31 %
Net Risk Adjusted Return % 56647.86 %
Annual Return % 27.06 %
Risk Adjusted Return % 48.93 %
Total transaction costs 2275586.78


2. Portfolio Equity




3. Profit Table