Visualization of Strategy Back Testing

Avatar
Posted on Jul 21, 2015
Disclaimer: The statement and analysis in this website is provided as general information and for illustrative purpose only. This app does not intend to provide investment advice. The author may hold positions in stocks, currencies and industries discussed here. You understand and acknowledge that there is a very high degree of risk involved in trading securities and/or currencies. The author assumes no responsibility or liability for your trading and investment results.

Abstract

I present an interactive app to form strategies of trading by Bollinger Bands, to visualize the pros and cons of them and eventually to modify them by considering another popular technical indicator, MACD.

Introduction

It is well known that investors use technical indicator to predict the movement of stock price and make decision of orders. However, individual indicator provides little ability of predicting, experienced investors would take multiple indicators into account and make their decisions. Many assert that this combination significantly improve their return, but the process is more art than science and highly depends on investor's interpretation on the signals.

My idea is to apply machine learning process to do similar combination. Ideally, I would like to construct robust function generating transaction with input of multiple indicators. At this very early stage, I construct a very simple transaction generating function taking only Bollinger Bands and MACD into account. Usual parameters of these indicators can be tuned manually and result would be visualized by a back testing process.

Demonstration

The app I designed is used for Back Testing, which looks like:

Open R shiny App from a new window here!

Play with the App here:

 

Strategy-Back-Test

 

The first column allows user to select the particular asset and the date range they are interested in as well as the plot type they desire. We see from the red square that we are trading S&P 500 ETF from 2013-01-01 to 2015-07-16.

The second column allow us to initialize trading strategy with Bollinger Band. For the detail please click on Bollinger Bands toward Wikipedia. Basically Bollinger Band shows the range of fluctuation during certain "window" in the past. User can adjust the length of the window to the period he or she believe to best predict the fluctuation in the near future. The multiple of standard deviation is another important parameter in the Bollinger Band. The width of the band become more narrow if the smaller multiple is used. Basically this decides how aggressive user wants the strategy to be. To visualize the indicator, we select the parameter (40 days window and 1.6 times of the standard deviation for example) we want and check "Add Bollinger Bands" as the following:

BB2

Notice from the graph above I also check "Generate order" and Maximum 60 days of Holding. When submitted together with the Bollinger Band they result in the following plot in the app:

BBP

The red dot line bounding a shadow region is the Bollinger Band. The simple strategy generated here is buying whenever the green line (actual price) hits the bottom (buy cheap) and selling whenever the green line hits the top (sell expensive). User can see the return of the strategy right beneath the price graph----it's so easy to  make more than 40% in two and half years!!

This happened probably because we experienced a big bullish market during the time. Notice that even in such a good time, if we tune the parameters in Bollinger Bands poorly, the return can reduce significantly. But at least we still have positive return, don't we?

Well, not all things were good during the period. For example, crude oil. Let's consider the same strategy on another ETF that track the WTI crude oil future, USO. The following is the result:

BBUSO

This time we lost a lot!!!! If we look deeper in our return, we lost a lot from the second half of 2014 to the beginning of 2015. We all enjoyed the good price of gas in that period but for oil traders it was probably not as pleasant.

Here we have a good example to show how visualization help us to understand the flaw of our strategy. Since I put a maximum 60 days holding restriction, we actually sell the ETF once during the period of the greatest loss (red square above) because we have reached the max holding restriction. However, we bought the very next day because the price is still beneath the Bollinger Band. After all, Bollinger Band is based on the belief that whenever the price deviate the average, it bounces back. This is certainly not true for USO in that period.

This suggest we should modify our strategy a little bit. Maybe we can add stop rule in our strategy so that when things go wrong we can at least control the damage. User can trigger stop sell when the loss reach the threshold.

Following is the example of 2% stopping threshold:
stop

We see here because of the stopping rule we sell the holding soon when experiencing loss. However, since the price is still below the band, the buying signal triggered almost right after we sell. It seems we need something to stop Bollinger Band from being so active.

MACD is another indicator that shows the momentum of the price, users can adjust the parameter in the third column of this app (red square below). I implement a very simple function that suspend buying signal and encourage selling to stop loss if MACD suggest down trend (when MACD has more negative histogram). On the contrast, when MACD suggest up trend, the program will be more aggressive in buying and hesitate to sell. The result is as below:

macd_modi

It turns out that we manage to avoid huge amount of loss because MACD stop us from buying crazily in the dramatic drop of the oil price. Let's take a closer look in our plot. The shadow part in MACD is the histogram, which I used as the indicator to the trend. The drop of the price coincides the biggest continuous shadow below the time axis, so the trading was extremely careful in that period.

Conclusion: Draw Back and The Next

We can keep playing with these parameters and have good time feeling we can make fortune! However, it is in fact not as good as it looks like. I mentioned that tuning Bollinger Band has effect on the return, same is for MACD. I tuned the parameters so that the negative shadow region is comparable to the period of price drop, which obviously leads to a better result. This is cheating because there's no way to foresee how long it takes for price drop to slow down. One can argue that in this strategy MACD is meant to track the trend of relatively long period so it makes sense to tune it to longer time range. But I really don't see how we can decide the precise length in advance. Even we somehow obtain good parameters, the same strategy seriously reduced the profit from SPY. It could be asking too much for a strategy for all the assets; yet the fact is, this simple app did not tell us which strategy is better in which situation.

For now, if we complete this app with more technical indicators and strategy generating functions, this app can serve a great tool for investors who wish to plan or review their strategies. For most of amateur investors, we seem to make the same mistake over and over again. That is because our brain is not good with informations that we are not explicitly involved. We used to have whole bunch of numbers and chart that are, though fancy, totally irrelevant to us. Their meaning is vague and their effect looks random. Our behaviors and the situation we deliver them were not record in any information, so our memory about them fade away until next time we make the exactly same mistake. This visualization app in the contrast give us opportunity to see how our feeling about the market is turned into strategy, how these strategies succeeds or fails and how we can adjust them in what kind of situation--just like what I did on USO with MACD.

To move even further, it would definitely be a interesting project to figure out how to tune the parameters in different situations. This is, to some extent, equivalent to finding a way of combining indicators. Again, more indicators should be added into consideration and more robust generating function should be implemented so that we will have more related parameters to play with. However, what to feed to the learning algorithm should be the first problem we need to deal with.

About Author

Leave a Comment

No comments found.

View Posts by Categories


Our Recent Popular Posts


View Posts by Tags

#python #trainwithnycdsa 2019 airbnb Alex Baransky alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus API Application artist aws beautiful soup Best Bootcamp Best Data Science 2019 Best Data Science Bootcamp Best Data Science Bootcamp 2020 Best Ranked Big Data Book Launch Book-Signing bootcamp Bootcamp Alumni Bootcamp Prep Bundles California Cancer Research capstone Career Career Day citibike clustering Coding Course Demo Course Report D3.js data Data Analyst data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization Deep Learning Demo Day Discount dplyr employer networking feature engineering Finance Financial Data Science Flask gbm Get Hired ggplot2 googleVis Hadoop higgs boson Hiring hiring partner events Hiring Partners Industry Experts Instructor Blog Instructor Interview Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter lasso regression Lead Data Scienctist Lead Data Scientist leaflet linear regression Logistic Regression machine learning Maps matplotlib Medical Research Meet the team meetup Networking neural network Neural networks New Courses nlp NYC NYC Data Science nyc data science academy NYC Open Data NYCDSA NYCDSA Alumni Online Online Bootcamp Open Data painter pandas Part-time Portfolio Development prediction Prework Programming PwC python python machine learning python scrapy python web scraping python webscraping Python Workshop R R language R Programming R Shiny r studio R Visualization R Workshop R-bloggers random forest Ranking recommendation recommendation system regression Remote remote data science bootcamp Scrapy scrapy visualization seaborn Selenium sentiment analysis Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau team TensorFlow Testimonial tf-idf Top Data Science Bootcamp twitter visualization web scraping Weekend Course What to expect word cloud word2vec XGBoost yelp