Showing posts with label statistics. Show all posts
Showing posts with label statistics. Show all posts

Sunday, 19 August 2018

SP500 vs Gold, using their correlation to make a trading strategy (Part 1)


This post is part of a new series in which I will show how to figure out if we can build a strategy using some assets’ correlation. Let me introduce the assets:


S&P500

Mini S&P500 future (continuous contract), daily, Source: TradingView
    Mini S&P500 future (continuous contract), daily, Source: TradingView

The Standard & Poor's 500 is one of the main American stock market index based on the market capitalizations of 500 large companies having common stock listed on the NYSE or NASDAQ. As you can see, this index hasn´t stopped raising since 2011. 

Gold

Comex Gold future (continuous contract), daily, Source: TradingView
    Comex Gold future (continuous contract), daily, Source: TradingView

The Comex Gold is one of the most important futures. You can trade it directly or you can use it to hedge your stock portfolio. Historically this hedge has been successful and has protected the portfolios versus big drawdowns. It´s recommended to have at least a small part of your investments in gold (even if it´s in an exchange-traded fund that tracks this metal)


I reviewed Gold futures and ETF´s last year: Gold Analysis

 


Spread between SP500 and Gold

Spread between SP500 and Gold futures, daily, Source: TradingView
    Spread between SP500 and Gold futures, daily, Source: TradingView

To simplify the calculation, I decided to make the spread as 1 E-mini S&P500 future minus 1 Comex Gold future. As you can see, the relationship was negative before 2013 because the gold price was higher than the S&P. Since then, this spread has raised almost like the US index. This explains that the different QE programs calmed down the uncertainty (so the investors started buying the S&P and started selling or reducing their gold portfolio)


Introduction to the study

Correlation series

For this part, I chose 4 correlation series (20, 60, 120 and 250 days) that represent different time frames. 


Correlation time frames table, own elaboration
      Correlation time frames table, own elaboration

The main reason for choosing this time frames is to make comparisons and to see if I can work out a strategy in the following posts. 



 S&P500 and Gold correlation series, own elaboration
     S&P500 and Gold correlation series, own elaboration

We can’t get any conclusion from this chart apart that the long-term correlation between S&P500 and gold is negative (the most part of the time).  One of the things I would like to study in the following days is if I can build a profitable system based on the correlation series divergences. For now, I can show the different charts with the asset prices and the correlation (The Y left axis represents the price of the assets and the Y right axis represents the correlation coefficient):


 S&P500, Gold, and  20 days correlation serie, own elaboration
     S&P500, Gold, and  20 days correlation serie, own elaboration

This chart doesn’t show any clear relationship. Another problem is that is a short-term correlation that generates a lot of noise in the signals and it´s difficult to know if it´s worth to check this correlation to trade the spread.



S&P500, Gold, and  60 days correlation serie, own elaboration
    S&P500, Gold, and  60 days correlation serie, own elaboration

The 60-day correlation is smoother than the previous one. I think that we can take advantage of the correlation every time that is above 0, however, a statistical study is required. 


 S&P500, Gold, and  120 and 250 days correlation series, own elaboration
  S&P500, Gold, and  120 and 250 days correlation series, own elaboration

As I said before, we can see that the most part of the time these correlations are below 0. Like in the previous chart we can take advantage of the correlation above 0. In addition, I would be interesting to study a trading system based on the 120 days correlations that trigger a trade every time is under -0.2. In terms of correlation’s divergence, we need to backtest it properly.

Sum up

I’ve chosen these assets because they are really important. The S&P500 reflects the US economy and the Comex Gold can be used as an investment or as a hedge vs the main index in a recession. Sadly this post is an introduction. I will analyze the systems proposed using advanced statistics and some backtests. As a reminder, the systems will be based on the correlation and its divergences. 



#trading #investing #correlation #ES #GC #SP500 #Gold #statistics

Sunday, 15 July 2018

Why I took a 5-day Moving Average instead of the 9-day Moving Average?


As I promised in the last post, I will review my decision of taking a smaller moving average. My purpose wasn’t to overfit the model, I was looking for a drawdown in which I feel comfortable. 

How did I start?


I had an idea about a day trading system. My objective was a good performance with adjusted risk. I’m a big fan of the stop loss. If you, as a trader or an investor, can avoid drawdowns bigger than 20-25%, you will be successful over the time.

Our example



The good point about the way I run the backtestings is that it’s easy to change some parameters and adapt to different indicators. Obviously, I ran the strategy with different moving averages in order to know what parameters generated a smooth performance curve avoiding big drawdowns. 

Max drawdown and final balance from the different MA, own elaboration
       Max drawdown and final balance from the different MA, own elaboration

As you can see the best profit to drawdown ratio was the system based on the 5-day Moving Average. This seems logical as it’s a daily trading system. The reason behind the bigger drawdown, in the MA3 system, is that the there is more noise in the entry signals and it´s not accurate enough with the tight stop loss. In the case of the MA9, MA15, and MA50, the signals are more accurate but the retracements and the close stop loss don´t allow the systems to perform as well as the ones with lower moving average. If you see the MA9 system's figures, you will understand that the profit is exactly the same as the MA5 but the drawdown is worse. 



Max drawdown  from the different MA systems tested, own elaboration
        Max drawdown  from the different MA systems tested, own elaboration

This chart is complementary to the table and the explanation above. Surprisingly, at least for me, the MA50 has a lower drawdown than the MA15. If we think about it, probably the best system would be between the 3 and the 9-day moving average. As I said before, I didn’t optimize the system. 



Performance of the different systems tested, own elaboration
         Performance of the different systems tested, own elaboration

This chart is more important than the ones shown above. Here you can see how the MA5 system has barely stayed under the initial balance invested. Even after its worst drawdown the system was up more than 5% while the other ones went under 10K (and the worst one, under 9K) The main reason is that a “small drawdown” is not as painful as a big one, in simple terms, capital preservation will assure you better financial future. Imagine that you could have avoided all mistakes in the stock market (or futures) or at least you could have limited your loses. I’m sure that today, you will be better off. The reason is the asymmetrical leverage.

(second part of this post: First lesson for an investor )

 Having said that, I would like to add that I´m not questioning your investments decisions. I´ve made so many mistakes in the past and some of the trades were good considering the expected value of the trade. 



 Final balance of the systems tested, own elaboration
          Final balance of the systems tested, own elaboration

To finish, I wanted to show you the chart of the final balance after 3.56 years. The best system has returned 56.6% which is a little bit more than 18% per year.

Final thoughts


I hope that you like this post. The main purpose was to show you how to analyze the systems and even extrapolate the basis and test different indicators or the same indicator at a different level. The stop loss can be painful but I wouldn’t place an order without knowing my stop and target. As I said the asymmetrical leverage is very important because recovering small losses is easy while recovering big drawdowns can be really difficult. You have seen this concept with the system reviewed. 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 #MovingAverage #Backtesting #Drawdown #Analysis #QuantitativeAnalysis

Saturday, 18 November 2017

How can we make a strategy profitable modifying a couple of things?

Nowadays trading is in vogue, even more, if we consider the new cryptocurrency trend. Basically, everyone wants to jump in. The trader lifestyle is desired by all the people. Sadly, trading is harder than what the social media shows. The industry is changing a lot. Concepts such as machine learning, artificial intelligence are taking importance in leading investment banks and hedge funds as they are heavily investing in it.

Why do the biggest companies invest in machine learning, artificial intelligence, and algorithms?


It´s very difficult to replace an experienced trader because he knows how to adapt the strategies in different economic cycles and conditions. Some hedge fund managers are hiring a lot of developers and programmers to create algos that emulate the behaviour of their best traders. This seems really expensive, at least in the first years, but I believe that in the long run will save money for the hedge fund. How can you emulate the trader behaviour? In my humble opinion, I would divide the strategies applied by the trader in little pieces and I will study the trader´s track record in order to study the conditions (price, type of order, macro events on that day, news) of the trades. Once I understand the reasons I will try to replicate its piece of strategy and I will code it. Once it’s coded and tested, I will assign a subaccount to use this strategy and I will do the same process for each strategy. To sum up, I will have a trading account made-up of subaccounts that run a specific strategy. We can say that the main account is the portfolio and the subaccounts are different traders or fund managers.
This process can take a lot of time and some parts can be difficult to replicate.

What aspects should we modify to make a simple strategy profitable?


The strategy is based on the EURUSD futures but I'm not going to explain how it works. The main purpose of this post is to show you how to modify a simple strategy to improve the profitability and reduce the risk. It only trades once a day if the conditions are met. This backtest shows the last 5 years. The initial portfolio was 50000 USD.

Plain strategy



This is the strategy without any modification. 

     Statistics of the strategy, own elaboration using RStudio

The mean is positive and it shows that the system will make on average 16.21 USD per day. Sadly is not that easy, because there are winning days and losing days. The best day it banked a 3250USD profit. On the other hand, the worst day shows a loss of 2440 USD. The Sharpe Ratio is very low. The returns’ distribution was a normal distribution around 0. The main problem is that there were trades that lost a big percentage of the portfolio. This is why I decided to limit the loses in the second strategy. Let’s see the maximum Drawdown.


     Max drawdown and track record of the strategy, own elaboration using RStudio

Any serious investor can’t tolerate this drawdown considering the size of the portfolio. I wouldn’t be confident to use this system after reviewing the track record. Basically, it goes sideways.

Strategy 2, limiting loses


In this case, I decided to limit the loss to 600USD per day. Let’s see if the system has improved or not.



     Statistics of the strategy, own elaboration using RStudio

In general terms, this system is worse than the first one. The system makes 15,19 USD per trade on average, which is  1 dollar less than in the first strategy. The worst loss has been limited but the distribution contains more days on the negative side. The days with big swings generated the most part of the loses. The return is only 19.96% in the backtesting.




     Profit and Loss from trades distribution, own elaboration using RStudio

The losing days are concentrated around the maximum loss allowed.


     Max drawdown and track record of the strategy 2, own elaboration using RStudio

The distribution is not appealing to me. The best thing is that the max drawdown is smaller than in the first strategy. This system is clearly limited by days with big ranges.

Strategy 3, looking for different entries


Once I limited the losses of the first system and I checked that it wasn’t working as I would like it, I decided to change my entries. Will this be the solution?


     Statistics of the strategy, own elaboration using RStudio

Modifying the entries improved a lot the system. Now the system mades 49.88 USD per day. The standard deviation is lower. It would have returned 81.4% in 5 years, around 17% per year. What a change!! The winning days' percentage has increased and the Sharpe Ratio is very good. You should think that the commissions are not included.


    Max drawdown and Track record of the system, own elaboration

This is the best point. Look at the line! Now the system is consistent and the maximum drawdown has decreased a lot.

Conclusion


Even if you have read or heard about a successful trading system, you shouldn’t trade it without testing it before. We have seen that with minor tweaks the strategy can improve a lot. I hope that this post will help you to understand the process. The sky is the limit, in this field, the creativity doesn’t have limits. If you are a professional trader this can help you to test ideas and become more confident. Another important point is that you shouldn’t invest in these strategies even when the statistics are good. You should test them with a paper trading account and compare that the behaviour is similar to the previous backtesting. This is crucial because there is the risk of overfitting. I hope you like.

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

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...