Wednesday, September 1, 2010


OPTIMIZATION AND MONTE CARLO SIMULATION OF TRADING SYSTEMS




In our previous article about using Monte Carlo simulation for testing trading systems we concluded that the MA Cross 5-25 trading system should not be traded in real time in its current state. The question now is how we can improve this sytem to become more profitable. Last time we used a very basic model for testing our trading system (Fig.1).




Figure 1. Simple system test used in our previous article about testing trading systems.

 
We will now make the model slightly more complex by introducing optimization (Fig.2).



Figure 2. Flow chart for trading system test including optimization of parameters.


Optimization
First of all what is optimization? Starting with an initial trading model where buying when MA5 crosses above MA25 and selling when MA5 crosses down below MA25 resulted in a list of trades with relatively good profits, but also loads of wipsaws. Even so maybe the crossing of MA5 and MA25 is not the best parameters to use when doing our trades. What if crossing of MA8 and MA18 produces the highest return for our trading system? To find out about this we use a method called Optimization.

What we do is keep our basic trading idea where buying and selling when MA crosses each other and then optimize for number of days to be used for MA look back period. Programming, optimization and backtesting of the system is all done in Amibroker.

Because we have few parameters to optimize in this system we chose to use standard optimization (Amibroker provides various methods for optimization where the method you chose depends largely upon how many parameters you have to optimize). The trading system is optimized using the same data series and timeframe as in our previous article.


Backtest
The result for our optimization shows that using moving averages of 1 and 19 give us the optimum results for this dataset and timeframe (Fig. 3). If we do a backtest using the new MA values from the optimization it gives us the result of a whooping 746512.57 NOK or a percentage gain of 746.51%(optimized on net profit which is probably not the best thing to do)! Not bad for 9 years of trading (Fig. 4).



Figure 3. 3D view of optimization result. Already when looking at this image we see that the trading system is no good. What we seek is a gently sloping "dome" where we then use the values that the peak represent.


What we also need to mention is that the actual buy trades in this test is done on the open the day after we get the actual trading signal (typical end of day data trading). The time of selling is also important to the results that we can get from our analysis. In this example we sell on the average price on the day we get a sell signal. How realistic this is can be discussed but the main purpose with this article is to get a general idea of how we can test trading systems and not necessarily the actual numerical results from the tests.

 

Statistics

 


Figure 4. Backtest results of MA crossover trading system using optimized variables.

 
Not surprisingly this produce a better result than we got from the backtesting of the initial MA Cross 5-25 trading system (seen in our first article about using Monte Carlo for Backtesting trading systems). This looks very promising but we need to be aware that the result might be a consequence of curve-fitting.

Curve-fitting is adjusting your parameters so that the trading system performs the best over the historical data used in the optimization- and backtesting- period. In other words what you basically do is to find the parameters for your trading system that would give you the best return over the time frame you are looking at. But why should we be careful using this method? Well the answer is that optimization and backtesting alone will never tell you how robust your trading system is.

If we would take the chance of trading an optimized- only trading system we would go broke sooner or later as the method give us no indications of how well we could expect the trading system to perform in the future. Some might argue with this and say “hey I have been trading like this and have made loads of money”. To this I must say that it is due to almost pure randomness where you probably will be caught up with the “reality” sooner or later (not entirely true but almost).

Monte Carlo Simulation
Like we did in our previous article about testing trading systems we will now do a Monte Carlo simulation of this new trading system. We do this to get a clearer picture of how robust the trading system is and what we might expect from it in the future. As before the Monte Carlo simulation is done using a software called “Zen Monte Carlo Simulator”. The result from the MCS is seen in figures 5 and 6.




Figure 5. Input data and results from Monte Carlo simulation.


From the Monte Carlo simulation we see a clear improvement in result by optimizing the moving average look back days. Minimum profit pr year has gone from -37789 NOK to 17271 NOK and average profit pr year has increased to 106673 NOK. However we would still need a nice account to be able to stay in the market if we are maximum “unlucky” as seen from the max draw down of -91917 NOK. This would make us need a trading account of (margin 100000 NOK) max drawdown -91917 NOK) 191917 NOK to win a profit of 17271 (worst case) in a single year. This is way better than our previous trading system but still it is quite risky.




Figure 6. Graphical display of Monte Carlo simulation.

 

Conclusion
When analyzing the Monte Carlo test results we see a nice improvement but we are still somewhat concerned about the robustness of this trading system. Making things even more complicated, if we were using the optimized parameters in our trading system trading another stock, or do a bulk optimization we would expect the result to further support our conclusion.

So what can we do to make this system more reliable and robust? Beside modify the system itself we can pull out another tool we have in our toolbox – Walk Forward Optimization. This will be the topic in our next article.

Please notice that stocktradersbulletin.com is not responsible for profit or loss any reader of this article must have using this trading model. Also this model is for educational purpose only so take this article as an example of how you can test trading models only and do not pay too much attention to the actual numeric results from this testing. 

 



comments (0)


Monday, June 8, 2009


TESTING TRADING SYSTEM USING MONTE CARLO SIMULATIONS




Last year we had an article about building a trading system. We designed a simple moving average crossover system where we did some basic backtesting. We will now start a series of articles covering Monte Carlo testing of trading systems to help us further support our trading system testing. Today we will take a closer look at the trading system we designed earlier and do a basic Monte Carlo analysis to see if our decision about not trading the system in real time was correct or not. Later we might also look at how we could improve the system further by looking av walk forward analysis and optimization. The software we use for the Monte Carlo analysis is "Zen Monte Carlo Simulator V5.01e" made by Volker Butzlaff.


Fig.1. OSEBX and equity curve along with buy- and sell- arrows. A green buy arrow occurs when there is a 5 day- and a 25 day- moving average crossover and vice verse when there is a red sell arrow.

Here we can see the OSEBX chart and an equity curve of how the system performs with the buy and sell signals. Starting capital for this single backtest was 100000 NOK. From the chart we can immediately see that the system performs quite well this is partly because the test data is mostly trending. Where the market is moving sideways we see that the system performs poorer. But again it looks not too bad.


                          Fig.2. Backtesting results of the MA Crossover trading system.

The backtesting results that you can see in the figure shows that 34 trades was executed where 16 were winners while 18 were losers. Average profit for the winners were 27292.32 NOK while average loss for the losers were -12456.37 NOK. Max drawdown was -129035.42  NOK (there should ring a bell already here about this trading system). The backtesting data is then fed into "Zen Monte Carlo" for further analysis (Fig.3).



Fig.3. Input data window for Zen Monte Carlo Simulator.


This gives us a trade ratio (TR) of 47% (47% winners), Payoff ratio (PR) of 2.19 (119% higher average win than loss) and finally a Profitability index (PX) of 1.95 (higher number the better and good indication when comparing scenarios and different trading systems).The other numbers put in here is dealing with units, the simulation period and number of simulations to be run (our test period will be run 100000 times. Running the simulation we get our results (Fig.4).



Fig.4. Monte Carlo simulation results.


So what do this tell us? Minimum profit for a year is -37789 NOK, maximum profit for a year is 98492 NOK while average profit for a year is 30388 NOK (remember that we start out with an equity of 100000 NOK). This might not look too bad at a first glance but we also need to take a closer look at the risk of the system. Minimum drawdown for a year is 27292 NOK, maximum drawdown is -264522 NOK(!!!!) and average drawdown is -16185 NOK. We can already say that this trading system is way too risky to trade. If we are really unlucky we would need a trading account of (margin (100000 NOK) + max drawdown (264522 NOK)) 364522 NOK to lose possible -37789 NOK (worst case) in a single year. In our opinion this is a very good reason not to trade this system. This is also confirmed by the ACR and WCR ratios. The results from our simulation can also be viewed graphically (Fig.5).



           Fig.5. Graphical display of Monte Carlo simulation.


How do the results from the Monte Carlo simulation coincide with the backtesting results?
Average yearly profit from backtesting showed a result of (212462.46 / 7 (net profit/years backtested) 30351.77 NOK compared to the Monte Carlo simulated profit of 30388 NOK. The biggest difference between the backtesting and the Monte Carlo simulation do we see by looking at the max drawdown. Bactesting suggested a max system drawdown of -129035.42 NOK while the Monte Carlo simulation showed max drawdown of -264522 NOK (Fig.4 & Fig.6).



                                  Fig.6. Drawdown results from backtesting.


Later we will see if we can improve the system by optimizing the parameters in use and possibly also modify the trading system. We will also look into how the system perform under different market settings by running Monte Carlo simulations on synthetic simulated data. If you have any comments, suggestions or questions please contact us at (contact@stocktradersbulletin.com). We are happy to receive suggestions for improving the trading system.

If you would like to check out the Monte Carlo simulation software used you can find it here:
http://www.zentrader.de

Happy trading!

SB


comments (0)


Tuesday, May 19, 2009


S&P500 THE PAST AND THE PRESENT




We here at Stocktradersbulletin.com has taken a closer look at the S&P 500 to see if we can find any indications of what could come the next years. We build this analysis on daily data dating back to 1970 up until now. By looking at the market in such a big scale easily overlooked signals in our typical day- bar display can be identified. Therefore let us now take a closer look at some of the immediate things we see. Important support and resistance levels are easily identified. The 380 level played a major role throughout whole of 1991 (please note that the use of the word "level" in this article do not mean the spesific number but more a range around that specific number). The 630 level was important part of 1996 but now the more interesting levels come into play. In 1997 the 806 level was broken and must be seen on as very important. In 2002-2003 this level was about to be tested again. S&P 500 ricochet of the level and this was the beginning of our last big rising wave that ended more or less in 2007. The interesting thing is that the level was "active" again end of last year - beginning this year. The level failed for a short period of time but the resent positive market development we have seen S&P 500 is now well above 806 again. Before going any further with this level we need to look at the higher levels also. If we see S&P 500 break out from the 982 level it must be regarded as very positive. 982 could be the first major test for S&P 500 to see if the market will rise of fall again in the time to come. 1200 is not that important level at the moment so we jump straight to the 1550 level. 1550 was first tested in 2000, in 2007 the level was tested again. As we see both times it failed and there was a pullback to 806. This brings us back to the important 806 level. We clearly see a double top formation developed. As we all know a double top is a reversal formation where the distance from 806 to 1550 (744) is the potential for the pullback. This is of course just the general idea about double tops and we should be careful to use that as hard fact. Another possibility is that we see S&P 500 rising above 982, reaching 1550, pulling back to 806 forming a triple top. These formations are also relatively common but for now it is too early to say if this is very realistic or not. If we choose to be optimistic we can see that today's situation looks quite similar to what we could see end of 2002 meaning that the market is just about to start on a new rising wave.





RSI
Looking at the RSI chart we see that the low values seen the last few months has not been seen many times since 1970. 2002 and 1974 with 1974 showing the lowest value is what we can find. What is also showing well is divergences between RSI and S&P 500. E.g. we see an RSI top in 1995 bottoming in 2002 while S&P 500 is still rising topping early 2000 before turning downwards. RSI do not give us many indications of where we are heading right now but what we can say is that the market has been/ is  what we call "oversold". The RSI values are not down at 30 (typical value indicating oversold) this is mainly due to the use of 200 day RSI and not the typical 14 day RSI setting. Therefore do not pay too much attention to the actual RSI value seen here but do pay more attention to the historical RSI tops and bottoms seen in the chart. If we agree that the market is now oversold then we could be facing a new positive development and if we choose we could agree that RSI is about to leave the low for now.  

ADX
Looking at +DI and -DI we can identify some interesting patterns. When +Di and -DI are "dancing" the market are moving more or less healthy. What we seen today is that ADX is quite high while +Di and -DI are separated quite a bit. +Di and -DI seems to be moving towards each other again meaning we are moving towards a more healthy market but it could still take some time before we are on safe ground.

Smaller scale
If we take a closer look at the movement seen from last top up until now we also find various important support and resistance levels in addition to those talked about earlier. The main extra ones are 881, 1121 and 1381. 881 has already been broken but where the first real test will be at 982. If 982 is broken we could see a much more healthy market development the time to come but for now what we can say is that it still looks quite risky but where we also see clear improvements.






To sum up what can be seen on a larger scale is that the market looks more positive than i a long time. RSI is indicating an oversold market but we need to remember that a market can remain oversold for some time before it rises again. On this scale though it seems that the RSI lows are present at relatively short time intervals. +DI and -DI is indicating that the market is improving but the danger is not yet over. The risk is still quite high. If the 982 level is broken we could probably say that we are on a much safer ground.

comments (0)


Saturday, October 4, 2008


BUILDING A TRADING SYSTEM




Have you ever wanted to make your own trading system? If so there are certain "rules" that you should follow when doing so. Here at http://stocktradersbulletin.com we will take a closer look at trading systems, how to make them and how to do proper testing of them. We will also take a look at various trading systems and see if they live up to expectations. Later we will also go in more detail about specific technical indicators. If you have any requests or suggestions let us know by contacting us. But first let us take a look at the fundamental parts of building a trading system.

Building a trading system
T
here is much to be said about technical analysis, what indicators to use and how to use them. In this article we will look at some indicators and how they can be combined to form a system that we can base our buy and sell actions upon.

Indicators can be divided into several categories like momentum, volume or trend as the most commonly used ones. The indicators we will focus on and use here are all from one of these categories.

If we take a look at www.stocktradersbulletin.com the analysis carried out there are usually based upon three (four) charts/indicators,

  • Price

  • (Volume, included in the price chart)

  • ADX

  • RSI


Before we take this any further we will now look at these charts/indicators individually.

Price
T
he price chart can be displayed in many ways with candlesticks and line chart probably being the most common ways. To complicate it further we can choose to plot the data logarithmic or linear. What you choose comes down to personal preferences but I would recommend using a logarithmic candlestick way of displaying the bars. The price chart basically tells us how the stock price is moving whether rising, falling or moving sideways. Candlesticks are also a nice way of displaying the data as you can recognize chart patterns. But we will not cover that in this article.

Price chart using candlesticks.


Volume
T
here are many volume indicators available for us to use but here we will look at the actual volume i.e. how many stocks have been traded throughout a day. Generally we speak of volume as a positive signal when the stock price is rising and volume is increasing, and a negative signal when the stock price is falling while volume is increasing. We can often see an increase in volume when the stock price is reaching support and resistance levels. We also often see a slowdown in volume as a price trend is coming to an end. If a stock has been in a nice upward moving trend with a general increasing volume and then all of a sudden the volume starts to decline this usually signals that the trend is coming to an end.

From this chart we clearly see that the volume is generally increasing while the stock price is falling. This is a negative signal and further fall in price can be expected.


ADX
D
irectional Movement indicator is a trend indicator developed by Wells Wilder. The indicator is usually plotted as three components

  • ADX

  • +DI

  • -DI

Basically what this indicator tells us is if a stock is trending or trading (moving sideways). The ADX line (blue line in the chart below) gives us the value of the indicator and as a general rule of thumb says that a reading of 25 and above indicates that the stock is trending, while a value lower than 25 indicates that the stock is trading. If you have a look at the chart you also see two more lines. The green line +DI and red line –DI tells us if the trend is negative or positive. When +DI is above –DI and ADX is showing values above 25 the stock is in a positive trend. When –DI is above +DI and ADX are showing values above 25 the stock is in a negative trend.


RSI
R
elative Strength Index is a momentum indicator developed by Wells Wilder.
This indicator is what we call an oscillator where the values oscillate between 0 and 100 but usually between 15 and 85. RSI measures the stocks own strength based upon previous
readings. It is most common to let RSI be based upon the last 14 day’s readings. As RSI is an oscillator we can use it as a measure to see if the stock is oversold or overbought. This means that if the stock has experienced a selling pressure lately RSI values falls leaving the stock to enter the oversold area. Oversold levels are usually defined as RSI values lower than 30 while overbought is usually defined as RSI values over 70. Some might also say that when the value crosses the 50 level upwards this is a good signal indicating strengthening in the stock.


Putting it all together
It is when we combine the information we get from the different indicators we really get the most out of technical analysis.

Divergence
When we recognize divergence between different indicators or indicators and price it is usually a strong signal that we should pay attention to. From the set of indicators we have in our setup here the most common divergence to look for is between price movement and RSI. If we see a falling trend in price but a rising trend for RSI values over the same time interval we have detected a divergence. A divergence like this could signal that the falling price trend is coming to an end and the stock price could start rising again. If the price trend is upward while the RSI trend is falling this usually signals a coming fall in price.

Here we see a divergence between price trend and
RSI trend. As expected it turned out that the falling
price trend was coming to an end and a rise in stock
price occured.


Trend or Trading
When you use oscillators like RSI you should also use an indicator telling you if the stock price is trading (moving sideways) or trending. Here we use ADX as our “Trend or Trading” telling indicator. When ADX tells us the stock is trading the actual RSI value becomes more reliable and valid. Oscillator indicators like RSI perform better when a stock is in a trading range. When we use RSI, its value is 20 and ADX tells us the stock is in a trading range we have much more confidence to say that the stock is now oversold and we could get a rise in price soon. If on the
other hand a stock is in a strong upward trend and RSI value is 80, this do not necessarily mean that the stock is overbought. When a stock is trending the RSI values become “exaggerated” in the direction of the trend. As you can see from this it is very important to combine indicators and not just analyze a stock based upon one indicator.


Trendlines, support and resistance
As in this article you have probably heard the words trendline, support, resistance and trend channel when someone talks about technical analysis. Basically this is not more than lines drawn at the charts. These lines are used as aid in predicting where future movement for indicators and price will be. There are many ways in drawing these lines but we will not focus on that in this article. The easiest way is just to go ahead and try for your self. The more you do it the better you get at it. Here you see an examples of how you could draw some trendlines (green and brown lines), trend channels (red lines) and support- (green line) and resistance- (brown) lines.


A few words on money management
Money management is a large subject in its self but before we do any trading we need to have at least some idée about money management. Money management is like trading with a security guard telling you when to get out of the shop when they close. If you do not get out in time you can be in great trouble. The most important thing you need to remember when it comes to money management is STOP LOSS. Before you even buy a stock decide what your stop loss level is i.e. when to sell the stock. This will define your risk level and can vary from person to person. But never risk more money than you can afford to loose.

There are many ways of how and where to set your stop loss limit depending upon personality and money available. Trend lines, support and resistance levels are all good methods of where to set your stop loss. Indicators can also be of great help. Experiment and find your way of doing it but do not skip it – Stop loss is very important.

Final note
W
hat is described in this article is just one way of using technical analysis.

I strongly suggest that you play around yourself and find a set of indicators that you feel comfortable with. Just remember a few points when you do so

  • Select a set of 3 – 5 indicators for your setup

  • Use indicators that tells the “story” differently from each other i.e. choose 1 – 2 indicators from the different indicator categories

  • Learn those indicators inn and out, the more you know your setup the better understanding you have for how to make money from it

  • Make your setup personal, one person’s setup might not be the correct one for another person as this varies due to risk, personal behavior etc.

For many more analysis of actual stocks please go to www.stocktradersbulletin.com


Disclaimer
The author of this article hereby disclaim all responsibility of any profit or loss people might have based upon this article.

www.stocktradersbulletin.com

comments (0)



1 - 4 of 4

RSS  


Articles


Stocktradersbulletin take a closer look at trading strategies, trading systems, the market in general and other relevant topics



TAGS




RECENT POSTS



Optimization and Monte Carlo Simulation of Trading Systems 


Testing trading system using Monte Carlo simulations 


S&P500 the past and the present 


Building a trading system 




ARCHIVE



september 2010


june 2009


may 2009


october 2008





TOPICS



TRADING SYSTEMS (3)


TRADING STRATEGIES (0)


MARKET COMMENTS (0)


OTHER (0)


MARKET OVERVIEW (1)



admin*









Today's chess puzzle