Thursday 9 November 2017

Why should we use R to backtest some strategies? Quantitative approach

We live in a technological era. Basically, we can have whatever we imagine. Walt Disney said once: “If you can dream it, you can do it”. What happens if we put together the technology and the investment world?


Algo Functionality or develop from scratch with a programming language


I know that there are a lot of trading platforms that offer their own easy language or built-in algo functionality, sadly, in my opinion, is not flexible. Let me explain in a better way, you can do a lot of things but mostly it’s focused on Technical Analysis.
Using programming languages allows you to apply whatever you have in mind as far as you can code it. However, it´s more difficult and learning takes time. There are a lot of books and online courses.  
I started with R a couple of years ago. It’s an open source programming language and software environment focused on statistics. I think is one of the easiest and it has similarities with Excel. There are a lot of specific packages that contain different functions and studies. It’s a powerful tool to backest some strategies.

     R Studio screenshot, own elaboration

Create your own systems


Let me sum up some of the advantages and disadvantages of developing a trading system in R.

Advantages

  • You can analyze and backtest large datasets
  • The statistical insights you get from the data can help you to build new systems.
  • It’s more flexible, you can base your decisions purely on the data or even support with some technical analysis.
  • You can optimize the different variables and see how it affects to the system
  • Once the system is live, the risk management won´t be discretionary and you will know the maximum risk you are taking.
  • Attaching  risk management systems and money management systems provide interesting scenarios to consider


Disadvantages

  • Takes time to learning about programming
  • I would recommend to have a good knowledge of trading or investing
  • You will find out that the most part of your ideas are not profitable
  • Programming some of the trading ideas is challenging
  • Linking with the Brokerage API can be difficult


Successful Hedge Funds and Market Makers

There are a lot of Hedge Funds that are known for their specialization on systematic trading using only quantitative models.  Renaissance  Technologies is well known in the sector and they started this way of trading a long time ago. In the recent years, more hedge funds are following these methods and some of the reasons are above. Developing and applying these systems are the hardest part.  Can we emulate this activity in our home? Well, in my humble opinion, we can try. First, we should now that our possibilities are reduced in comparison to a hedge fund or investment bank. These companies employ big teams of people, they can afford to invest money in the latest technology and they have been a long time in the business.

What is the process I follow?

First is the idea generation. Before this step, you should be familiar with the product and understand how it moves. It can be as simple as buying at 9:00 and selling after 5 minutes. You can complicate as much as you want but you should think that you need to code it later. Adding variables to the system will reduce the times that you trade and you will need a larger data sample to meet statistical significance.

Second, you need to download the data from your trading platform or data vendor. Remember to check if the data contains any error. Even if you know the product, I recommend analyzing from a statistical point of view. This can provide you better insights than the chart. The size of the sample should be big enough to meet the statistical significance

Third, code your strategy. Try to make the code as flexible as possible because you will need to optimize some variables in future tests. I would recommend focussing on the risk management and money management because they are key parts for the success of the system. Add ratios to measure the performance, the risk-reward, the biggest drawdown, the success ratio…

Four, applying the strategy to the data. If you are not happy with the ratios shown, try to optimize some variables.

The last step should be adapting your code to the brokerage API to execute the trades.

My little system


I’m not going to disclosure the strategy but it’s based on mean reversion. I chose the Euro-Bund (FGBL) for its liquidity and I believe that we can see significant moves in the near term. The system is designed to open and close positions on the same day. I do apologize for any error as the strategy is at an early stage. Let’s check how is performing from the beginning of the year.

The initial portfolio was set up as 20000 Euros.
    Statistics and ratios from the strategy, own elaboration using R Studio

Let me briefly comment these ratios. As you can see each trade generates 79.35 EUR gain on average, please consider 77 trades because the system doesn’t trade every day. The biggest gain was 910 EUR. The worst day it lost 620 EUR, which shouldn’t be right because I limited the losses to 250 EUR per day. After a while, I discover that it was due to an error in the data. The system has generated 6110 euros this year that considering the initial portfolio of 20000 euros brings a 30.55% return. The probability of a successful trade is 59.65%. The Sharpe Ratio is 2.38.




   Histogram of the closed trades, own elaboration

This is the distribution of the PnL generate by each trade. Sadly it’s concentrated around -250 euros and this is because some movements trigger the stops. 


   PnL Curve since the beginning of the year, own elaboration

I like this chart because it shows that in general terms the system is making money consistently. There are certain drawdowns that I would like to smooth if I decide to optimize some variables of the system.

Finally one of my favourites metrics, the maximum drawdown:


    Max Drawdown, own elaboration

The maximum drawdown is 2650 Euros which was the equivalent to around 10% of the portfolio at that time. It happened between the trades 51 and 62.
I think that the metrics are good, but discussing the performance is not the purpose of this post. You should focus on the process and how to get the advantage of that. Don’t think that every mean reversion system is profitable, I’m sure that if I change the risk parameters and I run the backtest again the system can show loses.

Conclusion


I hope you like it. If you like trading and coding, I recommend following this kind of approach at least for a second opinion. Some of the biggest hedge funds are investing in this kind of technology and they are trying to create systems that emulate the most experienced and successful traders. Thanks.

Have a good trading!




Disclaimer

I wrote this article myself, and it expresses my own opinions that shouldn't be used as a trading advice. Trading carries considerable risk due to the high leverage involved


#trading #quantitativeanalysis  #tradingstrategies #tradingsystems #Rstudio #riskmetrics #performance #FGBL


No comments:

Post a Comment

Thank you for participating in fxandfixedincometradring.blogspot.com

8th day small profit that helps me to keep going in the competition

After a successful week and most importantly from recovering almost $6k, I wanted to consolidate my positive results. My desire was to b...